-
Notifications
You must be signed in to change notification settings - Fork 0
/
gallery.html
645 lines (462 loc) · 19.5 KB
/
gallery.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
<html>
<title>Gallery</title>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, height=device-height, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
margin: 0 auto;
font-family: Arial, Helvetica, sans-serif;
color: #444;
overflow: hidden;
width: auto;
height: auto;
}
canvas {
border: 0;
width: 100%;
height: 100%;
display: block;
position: absolute;
}
.progress-bar {
height: 24px;
width: 30%;
border: 1px solid black;
padding: 2px;
margin: 0 auto;
top: 45%;
left: 35%;
z-index: 9;
position: absolute;
text-align: center;
line-height: 60px;
}
.progress-fill {
background-color: gray;
height: 100%;
}
</style>
<body>
<!--<div>temporary mobile console area:<div id="tmpConsole"></div>-->
<div class="progress-bar">
<div class="progress-fill" style="width: 50%;"></div>
loading...
</div>
<canvas id="canvas"></canvas>
<script id="vertex-shader-3d" type="x-shader/x-vertex">
uniform mat4 u_worldViewProjection;
attribute vec4 a_position;
attribute vec2 a_texcoord;
varying vec2 v_texCoord;
//varying float v_fogDepth;
void main() {
v_texCoord = a_texcoord;
gl_Position = u_worldViewProjection * a_position;
//v_fogDepth = -(u_worldViewProjection * a_position).z;
}
</script>
<script id="fragment-shader-3d" type="x-shader/x-fragment">
precision highp float;
varying vec2 v_texCoord;
uniform sampler2D u_diffuse;
uniform float u_opacity;
//uniform vec4 u_fogColor;
//uniform float u_fogAmount;
//varying float v_fogDepth;
void main() {
vec4 diffuseColor = texture2D(u_diffuse, v_texCoord);
gl_FragColor = vec4(diffuseColor.xyz, u_opacity);
//gl_FragColor = vec4(1.0, 0.8, 0.0, 1.0);
//float fogAmount = smoothstep(2.1, 20.0, v_fogDepth);
//gl_FragColor = mix(vec4(1.0, 0.8, 0.0, 1.0), vec4(0.0, 0.0, 1.0, 1.0), fogAmount);
}
</script>
<script id="frame-vertex-shader-3d" type="x-shader/x-vertex">
uniform mat4 u_worldViewProjection;
attribute vec4 a_position;
void main() {
gl_Position = u_worldViewProjection * a_position;
}
</script>
<script id="frame-fragment-shader-3d" type="x-shader/x-fragment">
precision highp float;
void main() {
gl_FragColor = vec4(.2, 0.0, 0.0, 1.);
}
</script>
<script src="./webgl-utils.js"></script>
<script src="https://webglfundamentals.org/webgl/resources/m4.js"></script>
<script>
"use strict";
function main() {
//const tmpConsole = document.getElementById("tmpConsole");
let isMouseDown = false;
const loader = document.getElementsByClassName("progress-bar")[0];
//互动区
const up = [0, 1, 0];
let aspect = window.innerWidth / window.innerHeight;
let id = -1, viewMatrix = [];
let cameraPosition = [0, 0, 265] /* -80*/, target = [0, 0, -1000];
//let cameraPosition=[-55*aspect,5*aspect,-130], target=[-50,0,-1000];
//let newCamPosStart = [0, 0, 10], newTargetPosStart=[100,0,-1000];
//类似打牌效果:
let newCamPosStart = [0, 0, 260], newTargetPosStart = [0, 0, -1000];
let camMov = [0, 0, 0], targetMov = [0, 0, 0];
let tweenCount = 20;
let isTweenReady = false, isTweening = false;
//gl初始化
var canvas = document.querySelector("#canvas");
var gl = canvas.getContext("webgl");
if (!gl) {
return;
}
//贴图区
const picNum = 38;
let loaded = 0;
let isLoaded = false;
let textures = [];
let objects = [];
function loadImage(url, callback) {
var image = new Image();
image.src = url;
image.onload = callback;
return image;
}
var images = [];
function loadImages(urls, callback) {
//var images = [];
var imagesToLoad = urls.length;
// Called each time an image finished
// loading.
var onImageLoad = function () {
--imagesToLoad;
// If all the images are loaded call the callback.
if (imagesToLoad === 0) {
callback(images);
loader.style.display = 'none';
}
};
for (var ii = 0; ii < imagesToLoad; ++ii) {
var image = loadImage(urls[ii], onImageLoad);
images.push(image);
}
}
gl.enable(gl.DEPTH_TEST);
const targetTexture = gl.createTexture();
/* 加入depth buffer(1/3) */
const depthBuffer = gl.createRenderbuffer();
const fb = gl.createFramebuffer();
const d = 0.1, picW = 5, picH = 8, frameW = 7, frameH = 10.5;
var arrays2 = {
position: {
numComponents: 3, data: [-frameW - d, frameH + d, 0, -frameW - d, frameH - d, 0, frameW - d, frameH - d, 0, frameW - d, frameH + d, 0,
-frameW - d, -frameH - d, 0, -frameW + d, -frameH - d, 0, -frameW + d, frameH - d, 0,
frameW + d, -frameH - d, 0, frameW + d, -frameH + d, 0, -frameW + d, -frameH + d, 0,
frameW - d, -frameH + d, 0, frameW + d, frameH + d, 0,
// -picW,-picH,0, picW,-picH,0, -picW,picH,0, picW,picH,0
],
},
texcoord: {
numComponents: 2, data: [0, 1, 1, 1, 0, 0, 1, 0,
0, 1, 1, 1, 0, 0, 1, 0,
0, 1, 1, 1, 0, 0, 1, 0,
0, 1, 1, 1, 0, 0, 1, 0,
// 0, 1, 1, 1, 0, 0, 1, 0
],
},
indices: {
numComponents: 3, data: [0, 1, 2, 0, 2, 3,
1, 4, 6, 4, 5, 6,
5, 7, 9, 7, 8, 9,
10, 8, 11, 11, 3, 10,
// 0+12, 1+12, 2+12, 1+12, 2+12, 3+12
],
},
};
var arrays = {
position: { numComponents: 3, data: [-picW, -picH, 0, picW, -picH, 0, -picW, picH, 0, picW, picH, 0], },
texcoord: { numComponents: 2, data: [0, 1, 1, 1, 0, 0, 1, 0], },
indices: { numComponents: 3, data: [0, 1, 2, 1, 2, 3], },
};
var bufferInfo = webglUtils.createBufferInfoFromArrays(gl, arrays);
var bufferInfo2 = webglUtils.createBufferInfoFromArrays(gl, arrays2);
var programInfo = webglUtils.createProgramInfo(gl, ["vertex-shader-3d", "fragment-shader-3d"]);
var frameProgramInfo = webglUtils.createProgramInfo(gl, ["frame-vertex-shader-3d", "frame-fragment-shader-3d"]);
function degToRad(d) {
return d * Math.PI / 180;
}
var fieldOfViewRadians = degToRad(60);
var uniformsThatAreComputedForEachObject = {
u_worldViewProjection: m4.identity(),
};
var pickUniforms = {
u_worldViewProjection: m4.identity(),
};
//fog区
//var fogColorLocation = gl.getUniformLocation(programInfo.program, "u_fogColor");
//var fogAmountLocation = gl.getUniformLocation(programInfo.program, "u_fogAmount");
var opacityLocation = gl.getUniformLocation(programInfo.program, "u_opacity");
function drawScene() {
resizeCanvasToDisplaySize(canvas);
aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
//需要一个viewMatrix
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
// Tell WebGL how to convert from clip space to pixels
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
//gl.clearColor(1.0,1.0,1.0,1.0);
// Clear the canvas AND the depth buffer.
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
var projectionMatrix = m4.perspective(fieldOfViewRadians, aspect, 1, 2000);
var viewProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);
gl.useProgram(programInfo.program);
// Setup all the needed buffers and attributes.
webglUtils.setBuffersAndAttributes(gl, programInfo, bufferInfo);
//webglUtils.setBuffersAndAttributes(gl, programInfo, bufferInfo2);
gl.enable(gl.BLEND);
//gl.blendFunc(gl.SRC_COLOR, gl.DST_COLOR);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
//gl.blendFunc(gl.ONE, gl.DST_COLOR);
//gl.blendFunc(gl.SRC_ALPHA, gl.DST_ALPHA);
//gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
objects.forEach(function (object) {
var worldMatrix = m4.xRotation(0);
worldMatrix = m4.translate(worldMatrix, -object.xTranslation, object.yTranslation, object.zTranslation);
worldMatrix = m4.yRotate(worldMatrix, object.yRotation);
// Multiply the matrices.
m4.multiply(viewProjectionMatrix, worldMatrix, uniformsThatAreComputedForEachObject.u_worldViewProjection);
// Set the uniforms we just computed
webglUtils.setUniforms(programInfo, uniformsThatAreComputedForEachObject);
// Set the uniforms that are specific to the this object.
webglUtils.setUniforms(programInfo, object.materialUniforms);
if (cameraPosition[0] === 0) {
const opacity = (Math.sin((Math.abs(cameraPosition[2]) - 5) / 10 * Math.PI) + 2) / 2;
gl.uniform1f(opacityLocation, opacity);
}
else {
const opacity = 0.45 + 2 / Math.abs(cameraPosition[0]);
gl.uniform1f(opacityLocation, opacity);
console.log(cameraPosition[0], target[0]);
}
// Draw the geometry.
//gl.drawElements(gl.TRIANGLES, bufferInfo2.numElements, gl.UNSIGNED_SHORT, 0);
gl.drawElements(gl.TRIANGLES, bufferInfo.numElements, gl.UNSIGNED_SHORT, 0);
});
//console.log('drawScene');
}
function drawFrames() {
resizeCanvasToDisplaySize(canvas);
aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
//需要一个viewMatrix
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
// Tell WebGL how to convert from clip space to pixels
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
//gl.clearColor(1.0,1.0,1.0,1.0);
// Clear the canvas AND the depth buffer.
//gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
var projectionMatrix = m4.perspective(fieldOfViewRadians, aspect, 1, 2000);
var viewProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);
gl.useProgram(frameProgramInfo.program);
// Setup all the needed buffers and attributes.
//webglUtils.setBuffersAndAttributes(gl, frameProgramInfo, bufferInfo);
webglUtils.setBuffersAndAttributes(gl, programInfo, bufferInfo2);
gl.enable(gl.BLEND);
//gl.blendFunc(gl.SRC_COLOR, gl.DST_COLOR);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
//gl.blendFunc(gl.ONE, gl.DST_COLOR);
//gl.blendFunc(gl.SRC_ALPHA, gl.DST_ALPHA);
//gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
objects.forEach(function (object) {
var worldMatrix = m4.xRotation(0);
worldMatrix = m4.translate(worldMatrix, object.xTranslation, object.yTranslation, object.zTranslation);
worldMatrix = m4.yRotate(worldMatrix, object.yRotation);
// Multiply the matrices.
m4.multiply(viewProjectionMatrix, worldMatrix, uniformsThatAreComputedForEachObject.u_worldViewProjection);
// Set the uniforms we just computed
webglUtils.setUniforms(frameProgramInfo, uniformsThatAreComputedForEachObject);
// Set the uniforms that are specific to the this object.
webglUtils.setUniforms(frameProgramInfo, object.materialUniforms);
//gl.uniform1f(opacityLocation, opacity);
//console.log(cameraPosition[2], opacity);
// Draw the geometry.
//gl.drawElements(gl.TRIANGLES, bufferInfo2.numElements, gl.UNSIGNED_SHORT, 0);
gl.drawElements(gl.TRIANGLES, bufferInfo2.numElements, gl.UNSIGNED_SHORT, 0);
});
//console.log('drawScene');
}
function camPos() {
if (id < 0) {
var cameraMatrix = m4.lookAt(cameraPosition, target, up, m4.identity());
viewMatrix = m4.inverse(cameraMatrix);
} /* else if (id==0){
cameraPosition = [0,0,0];
target = [0,0,-1000];
}*/
else {
/*
cameraPosition = [(1*id-15)*aspect, id*aspect, -20*id-20];
target = [(1*id+5)*aspect, id*aspect, -1000];
*/
cameraPosition = [0, 0, -28];
target = [0, 0, -1000];
}
}
function resizeCanvasToDisplaySize(canvas, multiplier) {
multiplier = multiplier || 2;
const width = canvas.clientWidth * multiplier | 0;
const height = canvas.clientHeight * multiplier | 0;
if (canvas.width !== width || canvas.height !== height) {
canvas.width = width;
canvas.height = height;
aspect = width / height;
return true;
}
return false;
//drawScene();
}
let imgList = [];
function generateImgList() {
for (let i = 0; i < picNum; i++) {
imgList.push("./pic-v1/" + (i + 1) + ".jpg");
}
}
function init() {
camPos();
generateImgList();
loadImages(imgList, () => {
for (var ii = 0; ii < picNum; ++ii) {
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
// Set the parameters so we can render any size image.
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
// Upload the image into the texture.
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, images[ii]);
// add the texture to the array of textures.
textures.push(texture);
//console.log(textures);
}
for (var ii = 0; ii < picNum; ++ii) {
objects.push({
yRotation: 0,
xTranslation: 0,//ii-10,
yTranslation: 0,
zTranslation: -500 + ii * 20,
materialUniforms: {
u_diffuse: textures[ii],
//u_opacity: 0.5,
},
pickIDUniform: {
u_id: [(ii + 1) / 255, 0, 0, 1],
}
});
}
render();
});
}
init();
let x0 = 0, x1 = 0, y0 = 0, y1 = 0;
function onMouseDown(e) {
isMouseDown = true;
x0 = e.clientX;
y0 = e.clientY;
canvas.addEventListener('mousemove', onMouseMove, false);
}
function onMouseMove(e) {
//console.log(e.clientX, e.clientY);
if (isMouseDown) {
x1 = e.clientX;
y1 = e.clientY;
const deltaX = x1 - x0;
const deltaY = y1 - y0;
//if(deltaX > 0.5 || deltaY >0.5){
cameraPosition[0] -= deltaX / 25;
//cameraPosition[1]+=deltaY/25;
target[0] += deltaX;
//target[1]-=deltaY;
if ((cameraPosition[2]) % 20 < 18) {
cameraPosition[2] += 0.5;
}
var cameraMatrix = m4.lookAt(cameraPosition, target, up, m4.identity());
viewMatrix = m4.inverse(cameraMatrix);
render();
x0 = x1;
y0 = y1;
}
}
function onMouseUp() {
isMouseDown = false;
canvas.removeEventListener('mousemove', onMouseMove, false);
}
function onMouseWheel(e) {
//console.log(e.deltaY);
if (Math.abs(cameraPosition[0]) < 5) {
if (cameraPosition[0] > 0) {
cameraPosition[0] -= 0.1;
cameraPosition[0] = Math.max(cameraPosition[0], 0)
} else {
cameraPosition[0] += 0.1;
cameraPosition[0] = Math.min(cameraPosition[0], 0)
}
target[0] = 0;//如何更加smooth
}
cameraPosition[2] += e.deltaY / 15;
// console.log(cameraPosition[2]);
//target=[0,0,-1000];
cameraPosition[2] = Math.max(-480, cameraPosition[2]);
cameraPosition[2] = Math.min(278, cameraPosition[2]);
var cameraMatrix = m4.lookAt(cameraPosition, target, up, m4.identity());
viewMatrix = m4.inverse(cameraMatrix);
render();
}
function render() {
drawScene();
drawFrames();
}
//canvas.addEventListener('click', pick, false);
function touchstart(e) {
//tmpConsole.innerText = 'touchstart';
x0 = e.pageX;
y0 = e.pageY;
window.addEventListener('touchmove', touchmove, false);
window.addEventListener('touchend', touchend, false);
}
function touchmove(e) {
x1 = e.pageX;
y1 = e.pageY;
//tmpConsole.innerText = 'touchmove';
const deltaX = x1 - x0;
const deltaY = y1 - y0;
cameraPosition[0] -= deltaX / 25;
target[0] += deltaX;
cameraPosition[2] += deltaY / 15;
cameraPosition[2] = Math.max(-480, cameraPosition[2]);
cameraPosition[2] = Math.min(278, cameraPosition[2]);
var cameraMatrix = m4.lookAt(cameraPosition, target, up, m4.identity());
viewMatrix = m4.inverse(cameraMatrix);
render();
x0 = x1;
y0 = y1;
}
function touchend() {
x1 = 0;
x0 = 0;
y1 = 0;
y0 = 0;
window.removeEventListener('touchmove', touchmove, false);
window.removeEventListener('touchend', touchend, false);
}
window.addEventListener('resize', render, false);
canvas.addEventListener('mousedown', onMouseDown, false);
canvas.addEventListener('mouseup', onMouseUp, false);
//window.addEventListener('load', init, false);
canvas.addEventListener('wheel', onMouseWheel, { passive: false });
window.addEventListener('touchstart', touchstart, false);
}
main();
document.addEventListener('gesturestart', function (event) { event.preventDefault() }, false);
</script>
</body>
</html>