-
Notifications
You must be signed in to change notification settings - Fork 0
/
bug-playground.html
447 lines (321 loc) · 14.5 KB
/
bug-playground.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
<html>
<title>My Projects</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: #777;
overflow: hidden;
width: auto;
height: auto;
}
canvas {
border: 0;
width: 100%;
height: 100%;
display: block;
position: absolute;
z-index: 1;
}
#title-area {
text-align: center;
padding: 40px 0;
z-index: 9;
position: absolute;
width: 100%;
}
</style>
<body>
<div id="title-area">
<h4>Calm Down</h4>
<p>And make bugs a playground 😉</p>
<p style="font-size: 12px;">( currently only responsive on desktop )</p>
</div>
<canvas id="canvas"></canvas>
<script id="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="fragment-shader-3d" type="x-shader/x-fragment">
precision highp float;
uniform vec4 u_colorMult;
void main() {
gl_FragColor = u_colorMult;//vec4(0.05, 0.3,0.8, 0.55);
}
</script>
<script id="pick-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="pick-fragment-shader-3d" type="x-shader/x-fragment">
precision mediump float;
uniform vec4 u_id;
void main() {
gl_FragColor = u_id;
//gl_FragColor = vec4(1.0, 0.8, 0.0, 1.0);
}
</script>
<script src="./webgl-utils.js"></script>
<script src="./m4.js"></script>
<script>
"use strict";
//canvas & context
const canvas = document.querySelector("#canvas");
const gl = canvas.getContext("webgl");
let aspect = window.innerWidth / window.innerHeight;
//projection & view
const fieldOfViewRadians = degToRad(60);
const up = [0, 1, 0];
let cameraPosition = [150, 60, 150], targetPosition = [0, 0, 0];
let cameraMatrix = m4.lookAt(cameraPosition, targetPosition, up, m4.identity());
let viewMatrix = m4.inverse(cameraMatrix);
//objects
const width = 15.6, height = 10;
const rowNum = 5, columnNum = 5;
let gapW = 25, gapH = 25;
let objects = [];
const uniformsThatAreComputedForEachObject = {
u_worldViewProjection: m4.identity(),
};
let objectPicked = 1;
//create frame buffer
const targetTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, targetTexture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
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);
const depthBuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
function setFramebufferAttachmentSizes(width, height) {
gl.bindTexture(gl.TEXTURE_2D, targetTexture);
const level = 0;
const internalFormat = gl.RGBA;
const border = 0;
const format = gl.RGBA;
const type = gl.UNSIGNED_BYTE;
const data = null;
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat,
width, height, border,
format, type, data);
//gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.canvas.width * 2, gl.canvas.height * 2, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
}
const fb = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fb); //这条放出来以后,图形就没了,但也分离了渲染层和pick层
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, targetTexture, 0);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
let pickUniforms = {
u_worldViewProjection: m4.identity(),
u_world: m4.identity(),
};
function main() {
if (!gl) {
return;
}
const arrays = {
position: { numComponents: 3, data: [-width / 2, -height / 2, 0, width / 2, -height / 2, 0, -width / 2, height / 2, 0, width / 2, height / 2, 0], },
indices: { numComponents: 3, data: [0, 1, 2, 1, 2, 3], },
};
const bufferInfo = webglUtils.createBufferInfoFromArrays(gl, arrays);
const programInfo = webglUtils.createProgramInfo(gl, ["vertex-shader-3d", "fragment-shader-3d"]);
const pickProgramInfo = webglUtils.createProgramInfo(gl, ["pick-vertex-shader-3d", "pick-fragment-shader-3d"]);
function drawScene() {
resizeCanvasToDisplaySize(canvas);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.clearColor(1, 1, 1, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
const projectionMatrix = m4.perspective(fieldOfViewRadians, aspect, 1, 2000);
const viewProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);
gl.useProgram(programInfo.program);
// Setup all the needed buffers and attributes.
webglUtils.setBuffersAndAttributes(gl, programInfo, bufferInfo);
gl.disable(gl.DEPTH_TEST);
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
objects.forEach(function (object) {
let 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);
webglUtils.setUniforms(programInfo, object.colorUniforms);
// Draw the geometry.
gl.drawElements(gl.TRIANGLES, bufferInfo.numElements, gl.UNSIGNED_SHORT, 0);
});
shrink();
//console.log('drawscene');
}
function renderPicking(e) {
// setup frame buffer
gl.bindTexture(gl.TEXTURE_2D, targetTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.canvas.width * 2, gl.canvas.height * 2, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
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);
// add depth buffer(2/3)
gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, gl.canvas.width * 2, gl.canvas.height * 2);
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, targetTexture, 0);
// add depth buffer(3/3)
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
gl.bindTexture(gl.TEXTURE_2D, targetTexture);
// mouse event to frustum matrix
const near = 1, far = 2000;
const top = Math.tan(degToRad(60) * 0.5) * near;
const bottom = -top;
const left = aspect * bottom;
const right = aspect * top;
const width = Math.abs(right - left);
const height = Math.abs(top - bottom);
const rect = canvas.getBoundingClientRect();
const pixelX = (e.clientX - rect.left) * gl.canvas.width / gl.canvas.clientWidth;
const pixelY = gl.canvas.height - (e.clientY - rect.top) * gl.canvas.height / gl.canvas.clientHeight - 1;
const subLeft = left + pixelX * width / gl.canvas.width;
const subBottom = bottom + pixelY * height / gl.canvas.height;
const subWidth = width / gl.canvas.width;
const subHeight = height / gl.canvas.height;
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
gl.viewport(0, 0, 1, 1);
// Clear the canvas AND the depth buffer.
// gl.clear(gl.COLOR_BUFFER_BIT); //| gl.DEPTH_BUFFER_BIT
var projectionMatrix = m4.frustum(subLeft, subLeft + subWidth, subBottom, subBottom + subHeight, near, far);
var viewProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);
gl.useProgram(pickProgramInfo.program);
webglUtils.setBuffersAndAttributes(gl, pickProgramInfo, bufferInfo);
webglUtils.setUniforms(pickProgramInfo, pickUniforms);
// Draw objects
objects.forEach(function (object) {
let 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(pickProgramInfo, uniformsThatAreComputedForEachObject);
webglUtils.setUniforms(pickProgramInfo, object.pickIDUniform);
// Draw the geometry.
gl.drawElements(gl.TRIANGLES, bufferInfo.numElements, gl.UNSIGNED_SHORT, 0);
});
const pixels = new Uint8Array(4);
gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
if (pixels[3] > 0) {
objectPicked = (pixels[0] - 1) * rowNum + pixels[1];
objects[(objectPicked - 1) * 4].colorUniforms.u_colorMult = [0.5, 0.3, 0.8, 0.55];
objects[(objectPicked - 1) * 4 + 1].colorUniforms.u_colorMult = [0.5, 0.3, 0.8, 0.55];
objects[(objectPicked - 1) * 4 + 2].colorUniforms.u_colorMult = [0.5, 0.3, 0.8, 0.55];
objects[(objectPicked - 1) * 4 + 3].colorUniforms.u_colorMult = [0.5, 0.3, 0.8, 0.55];
objects[(objectPicked - 1) * 4].yTranslation += 5;
objects[(objectPicked - 1) * 4 + 1].yTranslation += 5;
objects[(objectPicked - 1) * 4 + 2].yTranslation += 5;
objects[(objectPicked - 1) * 4 + 3].yTranslation += 5;
} else {
objects[(objectPicked - 1) * 4].colorUniforms.u_colorMult = [0.05, 0.3, 0.8, 0.55];
objects[(objectPicked - 1) * 4 + 1].colorUniforms.u_colorMult = [0.05, 0.3, 0.8, 0.55];
objects[(objectPicked - 1) * 4 + 2].colorUniforms.u_colorMult = [0.05, 0.3, 0.8, 0.55];
objects[(objectPicked - 1) * 4 + 3].colorUniforms.u_colorMult = [0.05, 0.3, 0.8, 0.55];
}
drawScene();
}
function drawObjects() {
for (var i = 0; i < rowNum; i++) {
for (var j = 0; j < columnNum; j++) {
objects.push({
yRotation: 0,
xTranslation: - (width + gapW) * (i - rowNum / 2) - width / 2 - gapW / 2,
yTranslation: 0,
zTranslation: (j - columnNum / 2) * (width + gapH) + gapH / 2,
pickIDUniform: {
u_id: [(i + 1) / 255, (j + 1) / 255, 0, 1],
},
colorUniforms: {
u_colorMult: [0.05, 0.3, 0.8, 0.55],
}
});
objects.push({
yRotation: 0,
xTranslation: - (width + gapW) * (i - rowNum / 2) - width / 2 - gapW / 2,
yTranslation: 0,
zTranslation: (j - columnNum / 2) * (width + gapH) + width + gapH / 2,
pickIDUniform: {
u_id: [(i + 1) / 255, (j + 1) / 255, 0, 1],
},
colorUniforms: {
u_colorMult: [0.05, 0.3, 0.8, 0.55],
}
});
objects.push({
yRotation: -Math.PI / 2,
xTranslation: -(width + gapW) * (i - rowNum / 2) - width - gapW / 2,
yTranslation: 0,
zTranslation: (j - columnNum / 2) * (width + gapH) + width / 2 + gapH / 2,
pickIDUniform: {
u_id: [(i + 1) / 255, (j + 1) / 255, 0, 1],
},
colorUniforms: {
u_colorMult: [0.05, 0.3, 0.8, 0.55],
}
});
objects.push({
yRotation: Math.PI / 2,
xTranslation: -(width + gapW) * (i - rowNum / 2) - gapW / 2,
yTranslation: 0,
zTranslation: (j - columnNum / 2) * (width + gapH) + width / 2 + gapH / 2,
pickIDUniform: {
u_id: [(i + 1) / 255, (j + 1) / 255, 0, 1],
},
colorUniforms: {
u_colorMult: [0.05, 0.3, 0.8, 0.55],
}
});
}
}
//console.log(objects);
}
function onMouseDown() {
objects.forEach(function (object) {
object.yTranslation = 0;
object.colorUniforms.u_colorMult = [0.05, 0.3, 0.8, 0.55];
});
drawScene();
}
function shrink() {
window.addEventListener('resize', drawScene, false);
window.addEventListener('mousemove', renderPicking, false);
window.addEventListener('mousedown', onMouseDown, false);
}
drawObjects();
drawScene();
}
function degToRad(d) {
return d * Math.PI / 180;
}
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;
}
main();
</script>
</body>
</html>