-
Notifications
You must be signed in to change notification settings - Fork 2
/
quad_prebaked_shadow.html
537 lines (425 loc) · 18.5 KB
/
quad_prebaked_shadow.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
<!-- vim: sw=4 ts=4 expandtab smartindent ft=javascript
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>WebGL Demo</title>
<style> document, body { margin: 0px; padding: 0px; overflow: hidden; } </style>
</head>
<body>
<canvas id="inspectorGraphCanvas"></canvas>
<script>
function mat4_create() {
let out = new Float32Array(16);
out[0] = 1;
out[5] = 1;
out[10] = 1;
out[15] = 1;
return out;
}
function mat4_ortho(out, left, right, bottom, top, near, far) {
let lr = 1 / (left - right);
let bt = 1 / (bottom - top);
let nf = 1 / (near - far);
out[0] = -2 * lr;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = -2 * bt;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[10] = 2 * nf;
out[11] = 0;
out[12] = (left + right) * lr;
out[13] = (top + bottom) * bt;
out[14] = (far + near) * nf;
out[15] = 1;
return out;
}
const canvas = document.getElementById('inspectorGraphCanvas');
const gl = canvas.getContext('webgl2', {antialias: true});
if (!gl) { alert('Failed to initialize WebGL'); }
(window.onresize = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
/* account for e.g. high-dpi retina macbook screens */
if (window.devicePixelRatio > 1) {
canvas.style.width = `${canvas.width}px`;
canvas.style.height = `${canvas.height}px`;
canvas.width *= window.devicePixelRatio;
canvas.height *= window.devicePixelRatio;
}
})();
const READOUT_SIZE_X = 315;
const READOUT_SIZE_Y = 150;
class PopupRenderer {
constructor(gl) {
const geo_vbuf_pos = gl.createBuffer();
const geo_vbuf_color = gl.createBuffer();
const geo_ibuf = gl.createBuffer();
const img_vbuf_pos = this.img_vbuf_pos = gl.createBuffer();
const img_vbuf_uv = this.img_vbuf_uv = gl.createBuffer();
const img_ibuf = this.img_ibuf = gl.createBuffer();
/* compile shaders */
let shaders;
{
const vsImg = `
attribute vec3 a_pos;
attribute vec2 a_uv;
uniform mat4 u_matrix;
varying vec2 v_uv;
void main() {
gl_Position = u_matrix * vec4(a_pos.xyz, 1);
v_uv = a_uv;
}`;
const fsImg = `
precision mediump float;
varying vec2 v_uv;
uniform sampler2D u_texture;
void main() {
gl_FragColor = texture2D(u_texture, v_uv);
}`;
const vsGeo = `
attribute vec3 a_pos;
attribute vec4 a_color;
uniform mat4 u_matrix;
uniform vec4 u_color_mask;
varying vec4 v_color;
void main() {
gl_Position = u_matrix * vec4(a_pos.xyz, 1);
v_color = a_color * u_color_mask;
}`;
const fsGeo = `
precision mediump float;
varying vec4 v_color;
void main() {
gl_FragColor = v_color;
}`;
const vsFullscreenBlur = `#version 300 es
precision mediump float;
out vec2 v_texcoord;
void main(void) {
float x = float((gl_VertexID & 1) << 2);
float y = float((gl_VertexID & 2) << 1);
v_texcoord.x = x * 0.5;
v_texcoord.y = y * 0.5;
gl_Position = vec4(x - 1.0, y - 1.0, 0, 1);
}
`;
const fsFullscreenBlur = `#version 300 es
precision mediump float;
uniform sampler2D u_texture;
uniform vec2 u_direction;
in vec2 v_texcoord;
out vec4 frag_color;
void main(void) {
vec2 one_pixel = u_direction*(vec2(1) / vec2(textureSize(u_texture, 0)));
frag_color = texture(u_texture, v_texcoord) * 0.2270270270;
frag_color += texture(u_texture, v_texcoord + one_pixel ) * 0.1945945946;
frag_color += texture(u_texture, v_texcoord - one_pixel ) * 0.1945945946;
frag_color += texture(u_texture, v_texcoord + one_pixel*2.0) * 0.1216216216;
frag_color += texture(u_texture, v_texcoord - one_pixel*2.0) * 0.1216216216;
frag_color += texture(u_texture, v_texcoord + one_pixel*3.0) * 0.0540540541;
frag_color += texture(u_texture, v_texcoord - one_pixel*3.0) * 0.0540540541;
frag_color += texture(u_texture, v_texcoord + one_pixel*4.0) * 0.0162162162;
frag_color += texture(u_texture, v_texcoord - one_pixel*4.0) * 0.0162162162;
}
`;
function createProgram(gl, vertexSource, fragmentSource) {
function createShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw new Error(gl.getShaderInfoLog(shader));
}
return shader;
}
const program = gl.createProgram();
const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexSource);
const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentSource);
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
throw new Error(gl.getProgramInfoLog(program));
}
const wrapper = {program};
const numAttributes = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
for (let i = 0; i < numAttributes; i++) {
const attribute = gl.getActiveAttrib(program, i);
wrapper[attribute.name] = gl.getAttribLocation(program, attribute.name);
}
const numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
for (let i = 0; i < numUniforms; i++) {
const uniform = gl.getActiveUniform(program, i);
wrapper[uniform.name] = gl.getUniformLocation(program, uniform.name);
}
return wrapper;
}
shaders = this.shaders = {
img: createProgram(gl, vsImg, fsImg),
geo: createProgram(gl, vsGeo, fsGeo),
fullscreenBlur: createProgram(gl, vsFullscreenBlur, fsFullscreenBlur),
};
}
let prebaked;
{
/* more robust approach documented here https://stackoverflow.com/a/55976760 */
const canvas_width = READOUT_SIZE_X*window.devicePixelRatio;
const canvas_height = READOUT_SIZE_Y*window.devicePixelRatio;
const pad = 40;
const world_width = READOUT_SIZE_X;
const world_height = READOUT_SIZE_Y;
gl.viewport(
0,
0,
canvas_width,
canvas_height
);
const pMatrix = mat4_ortho(mat4_create(), -pad, world_width+pad, -pad, world_height+pad, 1, -1);
const render_targets = [
{ tex: gl.createTexture(), fb: gl.createFramebuffer() },
{ tex: gl.createTexture(), fb: gl.createFramebuffer() },
];
for (const rt of render_targets) {
gl.bindTexture(gl.TEXTURE_2D, rt.tex);
// Upload the image into the texture.
gl.texImage2D(
/* target */ gl.TEXTURE_2D,
/* level */ 0,
/* internalformat */ gl.RGBA,
/* width */ canvas_width ,
/* height */ canvas_height,
/* border, */ 0,
/* format, */ gl.RGBA,
/* type, */ gl.UNSIGNED_BYTE,
/* data */ 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);
gl.bindFramebuffer(gl.FRAMEBUFFER, rt.fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, rt.tex, 0);
}
/* prebaked rt is bound */
{
const geo_idx = [];
const geo_pos = [];
const geo_color = [];
function drawBox( min_x, min_y, max_x, max_y, color ) {
geo_color.push(
color[0], color[1], color[2], color[3],
color[0], color[1], color[2], color[3],
color[0], color[1], color[2], color[3],
color[0], color[1], color[2], color[3]
)
const vbuf_i = geo_pos.length / 3;
geo_pos.push(max_x, min_y, 0.1);
geo_pos.push(min_x, min_y, 0.1);
geo_pos.push(min_x, max_y, 0.1);
geo_pos.push(max_x, max_y, 0.1);
geo_idx.push(vbuf_i + 0, vbuf_i + 1, vbuf_i + 2, vbuf_i + 2, vbuf_i + 3, vbuf_i + 0);
}
function drawTriangle(
a_x, a_y,
b_x, b_y,
c_x, c_y,
color
) {
geo_color.push(
color[0], color[1], color[2], color[3],
color[0], color[1], color[2], color[3],
color[0], color[1], color[2], color[3]
)
const vbuf_i = geo_pos.length / 3;
geo_pos.push(a_x, a_y, 0.1);
geo_pos.push(b_x, b_y, 0.1);
geo_pos.push(c_x, c_y, 0.1);
geo_idx.push(vbuf_i + 0, vbuf_i + 1, vbuf_i + 2);
}
const ARROW_HEIGHT = 40
drawBox(
0, 0,
READOUT_SIZE_X, READOUT_SIZE_Y - ARROW_HEIGHT,
[100, 80, 60, 255]
);
drawTriangle(
READOUT_SIZE_X*0.5 , READOUT_SIZE_Y,
READOUT_SIZE_X*0.5 - 30, READOUT_SIZE_Y - ARROW_HEIGHT,
READOUT_SIZE_X*0.5 + 30, READOUT_SIZE_Y - ARROW_HEIGHT,
[100, 80, 60, 255]
)
/* set up premultiplied alpha */
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.BLEND);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
/* clear all */
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
/* shadow geo pass (see color mask) */
{
gl.useProgram(shaders.geo.program);
gl.enableVertexAttribArray(shaders.geo.a_pos);
gl.enableVertexAttribArray(shaders.geo.a_color);
gl.uniformMatrix4fv(shaders.geo.u_matrix, false, pMatrix);
// gl.uniform4fv(shaders.geo.u_color_mask, [30, 30, 30, 255].map(x => x/255));
gl.uniform4fv(shaders.geo.u_color_mask, [0, 0, 0, 0.15]);
/* upload/bind geometry */
{
gl.bindBuffer(gl.ARRAY_BUFFER, geo_vbuf_pos);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(geo_pos), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.geo.a_pos, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, geo_vbuf_color);
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(geo_color), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.geo.a_color, 4, gl.UNSIGNED_BYTE, true, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, geo_ibuf);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(geo_idx), gl.STATIC_DRAW);
}
gl.drawElements(gl.TRIANGLES, geo_idx.length, gl.UNSIGNED_SHORT, 0);
}
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.BLEND);
/* blur time */
{
const passes = [
[render_targets[1].fb, render_targets[0].tex, [ 0, 10]],
[render_targets[0].fb, render_targets[1].tex, [ 9, 0]],
[render_targets[1].fb, render_targets[0].tex, [ 0, 8]],
[render_targets[0].fb, render_targets[1].tex, [ 7, 0]],
[render_targets[1].fb, render_targets[0].tex, [ 0, 6]],
[render_targets[0].fb, render_targets[1].tex, [ 5, 0]],
[render_targets[1].fb, render_targets[0].tex, [ 0, 4]],
[render_targets[0].fb, render_targets[1].tex, [ 3, 0]],
[render_targets[1].fb, render_targets[0].tex, [ 0, 2]],
[render_targets[0].fb, render_targets[1].tex, [ 1, 0]]
];
for (const [dst_fb, src_tex, dir] of passes) {
gl.bindFramebuffer(gl.FRAMEBUFFER, dst_fb);
gl.useProgram(shaders.fullscreenBlur.program);
gl.uniform2fv(shaders.fullscreenBlur.u_direction, [dir[0]*2, dir[1]*2]);
/* bind tex */
{
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, src_tex);
gl.uniform1i(shaders.fullscreenBlur.u_texture, 0);
}
gl.drawArrays(gl.TRIANGLE_FAN, 0, 3);
}
}
/* non-shadow thing time */
{
gl.useProgram(shaders.geo.program);
gl.enableVertexAttribArray(shaders.geo.a_pos);
gl.enableVertexAttribArray(shaders.geo.a_color);
gl.uniformMatrix4fv(shaders.geo.u_matrix, false, pMatrix);
gl.uniform4fv(shaders.geo.u_color_mask, [1, 1, 1, 1]);
/* upload/bind geometry */
{
gl.bindBuffer(gl.ARRAY_BUFFER, geo_vbuf_pos);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(geo_pos), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.geo.a_pos, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, geo_vbuf_color);
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(geo_color), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.geo.a_color, 4, gl.UNSIGNED_BYTE, true, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, geo_ibuf);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(geo_idx), gl.STATIC_DRAW);
}
gl.drawElements(gl.TRIANGLES, geo_idx.length, gl.UNSIGNED_SHORT, 0);
}
}
gl.deleteFramebuffer(render_targets[0].fb);
gl.deleteFramebuffer(render_targets[1].fb);
gl.deleteTexture(render_targets[1].tex);
this.prebaked = prebaked = render_targets[0].tex;
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
this.img_idx = [];
this.img_pos = [];
this.img_uv = [];
}
drawBox(
min_x, min_y,
max_x, max_y,
min_u, min_v,
max_u, max_v
) {
const vbuf_i = this.img_pos.length / 3;
this.img_pos.push(max_x, min_y, 0.1);
this.img_pos.push(min_x, min_y, 0.1);
this.img_pos.push(min_x, max_y, 0.1);
this.img_pos.push(max_x, max_y, 0.1);
this.img_uv .push(max_u, min_v);
this.img_uv .push(min_u, min_v);
this.img_uv .push(min_u, max_v);
this.img_uv .push(max_u, max_v);
this.img_idx.push(vbuf_i + 0, vbuf_i + 1, vbuf_i + 2, vbuf_i + 2, vbuf_i + 3, vbuf_i + 0);
}
render(gl) {
const { shaders } = this;
gl.viewport(
0,
0,
canvas.width,
canvas.height
);
/* set up premultiplied alpha */
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.BLEND);
/* clear all */
gl.clearColor(0.2, 0.2, 0.2, 1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
/* geo pass */
{
gl.useProgram(shaders.img.program);
gl.enableVertexAttribArray(shaders.img.a_pos);
gl.enableVertexAttribArray(shaders.img.a_uv);
const pMatrix = mat4_ortho(mat4_create(), 0, gl.canvas.width, gl.canvas.height, 0, 1, -1);
gl.uniformMatrix4fv(shaders.img.u_matrix, false, pMatrix);
{
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, this.prebaked);
gl.uniform1i(shaders.img.u_texture, 0);
}
/* upload/bind geometry */
{
gl.bindBuffer(gl.ARRAY_BUFFER, this.img_vbuf_pos);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.img_pos), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.img.a_pos, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, this.img_vbuf_uv);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.img_uv), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.img.a_uv, 2, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.img_ibuf);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this.img_idx), gl.STATIC_DRAW);
}
gl.drawElements(gl.TRIANGLES, this.img_idx.length, gl.UNSIGNED_SHORT, 0);
}
/* prepare for next frame */
this.img_idx = [];
this.img_pos = [];
this.img_uv = [];
}
}
const pr = new PopupRenderer(gl, canvas);
requestAnimationFrame(function frame(timestamp) {
requestAnimationFrame(frame);
for (let _i = 0; _i < 5; _i++) {
const i = _i;// 5 - 1 - _i;
pr.drawBox(
i*50 + 300,
i*50 + 300,
i*50 + 300+READOUT_SIZE_X,
i*50 + 300+READOUT_SIZE_Y,
0, 0, 1, 1
);
}
pr.render(gl);
})
</script>
</body>
</html>