-
Notifications
You must be signed in to change notification settings - Fork 2
/
scroll_thick.html
2371 lines (1976 loc) · 77.3 KB
/
scroll_thick.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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!-- vim: sw=4 ts=4 expandtab smartindent ft=javascript
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📜</text></svg>">
<title>Scroll WebGL Demo 0.1</title>
<style> document, body { margin: 0px; padding: 0px; overflow: hidden; } </style>
</head>
<body>
<canvas id="glcanvas"></canvas>
<script>
const canvas = document.getElementById('glcanvas');
const gl = canvas.getContext('webgl2', {antialias: true});
const gl_anisotropy =
gl.getExtension("EXT_texture_filter_anisotropic") ||
gl.getExtension("MOZ_EXT_texture_filter_anisotropic") ||
gl.getExtension("WEBKIT_EXT_texture_filter_anisotropic");
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
if (!gl) { alert('Failed to initialize WebGL'); }
(window.onresize = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
/* account for e.g. high-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;
}
gl.viewport(
0,
0,
canvas.width,
canvas.height
);
})();
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;
}
let shaders;
/* compile shaders */
{
const paint_vs =
'attribute vec3 a_pos;' +
'attribute vec2 a_uv;' +
'varying vec3 v_pos;' +
'void main() {' +
' v_pos = a_pos;' +
' gl_Position = vec4(a_uv*2.0 - 1.0, 0.0, 1.0);' +
'}';
const paint_fs =
'precision mediump float;' +
'varying vec3 v_pos;' +
'uniform mat4 u_matrix;' +
'uniform vec4 u_color;' +
'uniform vec2 u_stroke_from;' +
'uniform vec2 u_stroke_to;' +
'uniform vec2 u_aspect_ratio;' +
'uniform sampler2D u_tex_depth;' +
/* thanks iq! https://iquilezles.org/articles/distfunctions2d/ */
'float sd_segment( in vec2 p, in vec2 a, in vec2 b ) {' +
' vec2 pa = p-a, ba = b-a;' +
' float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );' +
' return length( pa - ba*h );' +
'}' +
'float decode_float_rgba(vec4 rgba) {' +
' return dot(rgba, vec4(1.0, 1.0/255.0, 1.0/65025.0, 1.0/16581375.0));' +
'}' +
'void main() {' +
' vec4 full_pos = u_matrix * vec4(v_pos.xyz, 1.0);' +
' vec2 screen_pos = full_pos.xy / full_pos.w;' +
'' +
' float occluding_depth = decode_float_rgba(texture2D(u_tex_depth, (screen_pos * 0.5) + 0.5));' +
' float frag_depth = (full_pos.z / full_pos.w * 0.5) + 0.5;' +
'' +
' const float bias = 0.00001;' +
' float occlusion = step(frag_depth - bias, occluding_depth);' +
'' +
' vec2 ar = u_aspect_ratio;' +
' float dist = sd_segment(screen_pos*ar, u_stroke_from*ar, u_stroke_to*ar);' +
' const float brush_size = 0.18;' +
' gl_FragColor = smoothstep(0.023*brush_size, 0.02*brush_size, dist) * u_color * occlusion;' +
// ' gl_FragColor = occlusion * u_color;' +
'}';
const vs_geo = `
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.0);
v_uv = a_uv;
}`;
const fs_geo = `
precision mediump float;
uniform sampler2D u_texture;
uniform float u_color_mask;
uniform float u_alpha_mask;
varying vec2 v_uv;
void main() {
gl_FragColor = texture2D(u_texture, v_uv);
gl_FragColor.a *= u_alpha_mask;
if (gl_FragColor.a <= 0.0) discard;
gl_FragColor.xyz *= u_color_mask;
}`;
const vs_grid = `#version 300 es
in vec3 a_pos;
in vec2 a_uv;
uniform mat4 u_matrix;
out vec2 v_uv;
void main() {
gl_Position = u_matrix * vec4(a_pos.xyz, 1);
v_uv = (a_uv - 0.5)*abs(a_pos.xy*10.0);
}`;
const fs_grid = `#version 300 es
precision mediump float;
in vec2 v_uv;
/* https://iquilezles.org/articles/filterableprocedurals/ */
const float N = 30.0; // grid ratio
float gridTextureGradBox(in vec2 p, in vec2 ddx, in vec2 ddy) {
// filter kernel
vec2 w = max(abs(ddx), abs(ddy)) + 0.01;
// analytic (box) filtering
vec2 a = p + 0.5*w;
vec2 b = p - 0.5*w;
vec2 i = (floor(a)+min(fract(a)*N,1.0)-
floor(b)-min(fract(b)*N,1.0))/(N*w);
//pattern
return (1.0-i.x)*(1.0-i.y);
}
out vec4 frag_color;
void main() {
// vec2 d = 1.0 - step(0.01, fract(v_uv*10.0));
// gl_FragColor = vec4(max(d.x, d.y));
vec2 uv = (v_uv - 0.5) + 0.5/N;
float grid = gridTextureGradBox(uv, dFdx(uv), dFdy(uv));
frag_color = vec4(1.0 - grid);
frag_color *= 0.5;
frag_color *= smoothstep(0.0, 0.8, 1.0 - length(v_uv)/9.5);
}`;
const depth_vs =
'precision mediump float;' +
'uniform mat4 u_matrix;' +
'attribute vec4 a_pos;' +
'varying vec4 v_pos;' +
'void main() {' +
' gl_Position = v_pos = u_matrix * a_pos;' +
'}';
const depth_fs =
'precision mediump float;' +
'varying vec4 v_pos;' +
'vec4 encode_float_rgba(float v) {' +
' vec4 enc = vec4(1.0, 255.0, 65025.0, 16581375.0) * v;' +
' enc = fract(enc);' +
' enc -= enc.yzww * vec4(1.0/255.0,1.0/255.0,1.0/255.0,0.0);' +
' return enc;' +
'}' +
'void main() {' +
' float depth = v_pos.z / v_pos.w;' +
' gl_FragColor = encode_float_rgba(depth * 0.5 + 0.5);' +
'}';
shaders = {
geo: createProgram(gl, vs_geo , fs_geo ),
grid: createProgram(gl, vs_grid , fs_grid ),
paint: createProgram(gl, paint_vs, paint_fs),
depth: createProgram(gl, depth_vs, depth_fs),
}
}
let emoji_atlas = gl.createTexture();
const ATLAS_SIZE = 2; /* in emoji */
const EMOJI_SIZE = 256;
{
const canvas = document.createElement("canvas");
canvas.width = canvas.height = ATLAS_SIZE * EMOJI_SIZE;
const ctx = canvas.getContext("2d");
{
const atlas = ['✒️', '🌈', '🤚', '📜'];
const PAD = EMOJI_SIZE*0.2;
ctx.font = (EMOJI_SIZE - PAD*2) + 'px sans-serif';
let i = 0;
for (const char of atlas) {
const x = ( i % ATLAS_SIZE ) * EMOJI_SIZE;
const y = (Math.floor(i / ATLAS_SIZE)) * EMOJI_SIZE;
ctx.fillText(
char,
x + PAD,
y + EMOJI_SIZE - PAD
);
i++;
}
}
gl.bindTexture(gl.TEXTURE_2D, emoji_atlas);
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
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 */ canvas
);
if (1) {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
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 shadowed = shadowForTexture(
emoji_atlas,
canvas.width,
canvas.height,
0
);
gl.disableVertexAttribArray(1);
gl.deleteTexture(emoji_atlas);
emoji_atlas = shadowed;
}
gl.bindTexture(gl.TEXTURE_2D, emoji_atlas);
gl.generateMipmap(gl.TEXTURE_2D);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_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);
/* if you're going to use this multiple times in a single program,
* consider modifying it to not immediately delete all the shaders.
*
* or just take the shader compilation hit -- i don't care what you do.
*
* note, if you don't care about supporting Safari, you can generate shadows entirely in canvas
* https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/filter
*
*/
function shadowForTexture(texture, texture_width, texture_height, texture_pad) {
/* compile shaders */
let shaders;
{
const vs_img = `
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 fs_img = `
precision mediump float;
varying vec2 v_uv;
uniform sampler2D u_texture;
uniform vec4 u_mask;
void main() {
gl_FragColor = texture2D(u_texture, v_uv);
/* somehow alpha got overmultiplied, freaky af */
// gl_FragColor.xyz /= gl_FragColor.w;
gl_FragColor.rgb *= u_mask.rgb;
gl_FragColor *= u_mask.a;
}`;
const vs_fullscreen_blur = `#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 fs_fullscreen_blur = `#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;
}
`;
shaders = {
img: createProgram(gl, vs_img , fs_img ),
fullscreen_blur: createProgram(gl, vs_fullscreen_blur, fs_fullscreen_blur),
};
}
let prebaked;
{
/* some suggestions on AA/multisampling https://stackoverflow.com/a/55976760 */
const canvas_width = texture_width + texture_pad*2;
const canvas_height = texture_height + texture_pad*2;
gl.viewport(
0,
0,
canvas_width,
canvas_height
);
/* this way we can just draw the image from (0, 0)..(width,height)
* and it will have even padding on all sides */
const pMatrix = mat4_ortho(
mat4_create(),
-texture_pad,
texture_width +texture_pad,
-texture_pad,
texture_height+texture_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 img_vbuf_pos = gl.createBuffer();
const img_vbuf_uv = gl.createBuffer();
const img_ibuf = gl.createBuffer();
{
const img_idx = [];
const img_pos = [];
const img_uv = [];
{
const min_x = 0;
const min_y = 0;
const max_x = texture_width;
const max_y = texture_height;
img_uv.push(
1, 0,
0, 0,
0, 1,
1, 1
);
const vbuf_i = img_pos.length / 3;
img_pos.push(max_x, min_y, 0.1);
img_pos.push(min_x, min_y, 0.1);
img_pos.push(min_x, max_y, 0.1);
img_pos.push(max_x, max_y, 0.1);
img_idx.push(vbuf_i + 0, vbuf_i + 1, vbuf_i + 2, vbuf_i + 2, vbuf_i + 3, vbuf_i + 0);
}
/* set up premultiplied alpha */
gl.blendFunc(gl.ONE, 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 img pass (see color mask) */
function imgPass(u_mask_r, u_mask_g, u_mask_b, u_alpha_mask) {
gl.useProgram(shaders.img.program);
gl.enableVertexAttribArray(shaders.img.a_pos);
gl.enableVertexAttribArray(shaders.img.a_uv);
/* upload/bind atlas */
{
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.uniform1i(shaders.img.u_texture, 0);
}
gl.uniformMatrix4fv(shaders.img.u_matrix, false, pMatrix);
gl.uniform4f(shaders.img.u_mask, u_mask_r, u_mask_g, u_mask_b, u_alpha_mask);
/* upload/bind geometry */
{
gl.bindBuffer(gl.ARRAY_BUFFER, img_vbuf_pos);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(img_pos), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.img.a_pos, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, img_vbuf_uv);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(img_uv), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.img.a_uv, 2, gl.FLOAT, true, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, img_ibuf);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(img_idx), gl.STATIC_DRAW);
}
gl.drawElements(gl.TRIANGLES, img_idx.length, gl.UNSIGNED_SHORT, 0);
}
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.BLEND);
imgPass(0.20, 0.20, 0.20, 0.20);
/* 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.fullscreen_blur.program);
gl.uniform2fv(shaders.fullscreen_blur.u_direction, [dir[0]*2, dir[1]*2]);
/* bind tex */
{
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, src_tex);
gl.uniform1i(shaders.fullscreen_blur.u_texture, 0);
}
gl.drawArrays(gl.TRIANGLE_FAN, 0, 3);
}
}
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.BLEND);
imgPass(1, 1, 1, 1);
}
gl.deleteBuffer(img_vbuf_pos);
gl.deleteBuffer(img_vbuf_uv );
gl.deleteBuffer(img_ibuf );
gl.deleteProgram(shaders.img.program);
gl.deleteShader(shaders.img.vs);
gl.deleteShader(shaders.img.fs);
gl.deleteProgram(shaders.fullscreen_blur.program);
gl.deleteShader(shaders.fullscreen_blur.vs);
gl.deleteShader(shaders.fullscreen_blur.fs);
gl.deleteFramebuffer(render_targets[0].fb);
gl.deleteFramebuffer(render_targets[1].fb);
gl.deleteTexture(render_targets[1].tex);
prebaked = render_targets[0].tex;
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
return prebaked;
}
}
let tex_skydome = gl.createTexture();
(async () => {
const img = new Image();
img.src = 'castle_zavelstein_cellar.jpg';
await new Promise(res => img.onload = res);
const canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext("2d").drawImage(img, 0, 0);
gl.bindTexture(gl.TEXTURE_2D, tex_skydome);
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 */ canvas
);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
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);
})();
let tex_quill = gl.createTexture();
(async () => {
const img = new Image();
img.src = 'tex_quill.png';
await new Promise(res => img.onload = res);
const canvas = document.createElement("canvas");
canvas.width = img.width * 1.2;
canvas.height = img.height * 1.2;
const ctx = canvas.getContext("2d");
ctx.save();
ctx.translate(canvas.width*0.5, 0);
ctx.rotate(Math.PI/2 * 0.5);
ctx.drawImage(img, 0, 0);
ctx.restore();
gl.bindTexture(gl.TEXTURE_2D, tex_quill);
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 */ canvas
);
gl.generateMipmap(gl.TEXTURE_2D);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_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);
if (gl_anisotropy) {
const max = gl.getParameter(gl_anisotropy.MAX_TEXTURE_MAX_ANISOTROPY_EXT);
gl.texParameterf(gl.TEXTURE_2D, gl_anisotropy.TEXTURE_MAX_ANISOTROPY_EXT, max);
}
})();
const SCROLL_WIDTH = 2000;
const SCROLL_LENGTH_WORLDSPACE = 7.198429;
const tex_scroll_fb = gl.createFramebuffer();
const tex_scroll = gl.createTexture();
let tex_scroll_loaded = false;
(async () => {
const font = new FontFace("quill", "url(https://fonts.gstatic.com/s/medievalsharp/v26/EvOJzAlL3oU5AQl2mP5KdgptMqhwMinUPDg.woff2) format('woff2')")
await font.load();
document.fonts.add(font);
document.body.classList.add("fonts-loaded");
const img = new Image();
img.src = 'tex_scroll.jpg';
await new Promise(res => img.onload = res);
const canvas = document.createElement("canvas");
/* scroll is 2 units wide in worldspace so this gives us an aspect ratio. */
canvas.width = SCROLL_WIDTH;
canvas.height = SCROLL_WIDTH * (SCROLL_LENGTH_WORLDSPACE / 2.0);
const ctx = canvas.getContext("2d");
for (let i = 0, y = 0; y < canvas.height; y += img.height, i++) {
ctx.save();
ctx.translate(img.width*0.5, img.height*0.5 + y);
if (i%2) ctx.rotate(Math.PI);
else ctx.scale(-1, 1);
ctx.drawImage(img, -img.width*0.5, -img.height*0.5);
ctx.restore();
}
const ITERATIONS = 20;
for (let i = 0; i < ITERATIONS; i++) {
ctx.save();
if (i == (ITERATIONS - 1)) {
ctx.globalAlpha = 0.7;
ctx.fillStyle = "rgb(15% 9% 7%)";
} else {
const shake = 15;
ctx.translate((0.5 - Math.random())*shake,
(0.5 - Math.random())*shake);
ctx.globalAlpha = 0.07*Math.random();
ctx.fillStyle = "rgb(35% 10% 7%)";
}
const LINE_COUNT = 30;
const lines = [
1.0, '',
1.0, '',
1.5, 'SIGN OVER YOUR',
1.5, ' FIRST BORN',
0.8, ' by order of imperial decree',
1.0, '',
2.0, ' or else!',
1.0, '',
0.8, ''
];
lines.push(1.0, '', 1.0, '', 1.0, '', ...lines);
let y = 7/30;
for (let i = 0; i < lines.length; i += 2) {
const size = lines[i + 0];
const text = lines[i + 1];
ctx.font = (size * SCROLL_WIDTH * 0.08) + "px quill";
y += 1/30;
ctx.fillText(text, 50, y * canvas.height);
}
ctx.restore();
}
gl.bindTexture(gl.TEXTURE_2D, tex_scroll);
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 */ canvas
);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
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);
tex_scroll_loaded = true;
gl.bindFramebuffer(gl.FRAMEBUFFER, tex_scroll_fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex_scroll, 0);
})();
/* we need a depth texture so we can sample it when painting,
* to determine if the fragment should be occluded */
let depth_color = gl.createTexture();
let depth_depth = gl.createTexture();
let depth_fb = gl.createFramebuffer();
const DEPTH_TEX_SIZE = 1 << 9;
{
{
gl.bindTexture(gl.TEXTURE_2D, depth_color);
gl.texImage2D(
/* target */ gl.TEXTURE_2D,
/* level */ 0,
/* internalformat */ gl.RGBA,
/* width */ DEPTH_TEX_SIZE,
/* height */ DEPTH_TEX_SIZE,
/* border, */ 0,
/* format, */ gl.RGBA,
/* type, */ gl.UNSIGNED_BYTE,
/* data */ null
);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
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, depth_fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, depth_color, 0);
}
{
gl.bindTexture(gl.TEXTURE_2D, depth_depth);
// define size and format of level 0
gl.texImage2D(
gl.TEXTURE_2D,
/* level, */ 0,
/* internalFormat, */ gl.DEPTH_COMPONENT24,
/* width, */ DEPTH_TEX_SIZE,
/* height, */ DEPTH_TEX_SIZE,
/* border, */ 0,
/* format, */ gl.DEPTH_COMPONENT,
/* type, */ gl.UNSIGNED_INT,
/* data */ null
);
// set the filtering so we don't need mips
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
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);
/* bind depth tex to our framebuffer */
gl.bindFramebuffer(gl.FRAMEBUFFER, depth_fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, depth_depth, 0);
}
}
const buf = {
scroll_v_pos: gl.createBuffer(),
scroll_v_uv: gl.createBuffer(),
scroll_i: gl.createBuffer(),
quill_v_pos: gl.createBuffer(),
quill_v_uv: gl.createBuffer(),
quill_i: gl.createBuffer(),
emoji_v_pos: gl.createBuffer(),
emoji_v_uv: gl.createBuffer(),
emoji_i: gl.createBuffer(),
skydome_v_pos: gl.createBuffer(),
skydome_v_uv: gl.createBuffer(),
skydome_i: gl.createBuffer(),
skydome_i_count: 0,
grid_v_pos: gl.createBuffer(),
grid_v_uv: gl.createBuffer(),
grid_i: gl.createBuffer(),
grid_i_count: 0,
};
/* generate grid geometry */
{
const grid_idx = [];
const grid_pos = [];
const grid_uv = [];
{
const z = -1.5;
const corners = [
-999, -999, z, -999, 999, z, 999, 999, z, 999, -999, z, /* Bottom face */
];
for (let corner_i = 0; corner_i < corners.length; corner_i += 3)
corners[corner_i+2] -= 0.025;
for (let corner_i = 0; corner_i < corners.length; corner_i += 12) {
grid_uv.push(
255, 0,
0, 0,
0, 255,
255, 255,
);
const vbuf_i = grid_pos.length / 3;
grid_pos.push(corners[corner_i + 0], corners[corner_i + 1], corners[corner_i + 2]);
grid_pos.push(corners[corner_i + 3], corners[corner_i + 4], corners[corner_i + 5]);
grid_pos.push(corners[corner_i + 6], corners[corner_i + 7], corners[corner_i + 8]);
grid_pos.push(corners[corner_i + 9], corners[corner_i + 10], corners[corner_i + 11]);
grid_idx.push(
vbuf_i + 0,
vbuf_i + 1,
vbuf_i + 2,
vbuf_i + 2,
vbuf_i + 3,
vbuf_i + 0
);
}
}
{
gl.bindBuffer(gl.ARRAY_BUFFER, buf.grid_v_pos);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(grid_pos), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, buf.grid_v_uv);
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(grid_uv), gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buf.grid_i);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(grid_idx), gl.STATIC_DRAW);
}
buf.grid_i_count = grid_idx.length;
}
/* generate skydome geometry */
{
const geo_idx = [];
const geo_pos = [];
const geo_uv = [];
{
const WIDTH_SEGMENTS = 1 << 6;
const HEIGHT_SEGMENTS = 1 << 5;
const RADIUS = 100;
const grid = [];
let index = 0;
for (let iy = 0; iy <= HEIGHT_SEGMENTS; iy++) {
const v = iy / HEIGHT_SEGMENTS;
/* poles */
let u_offset = 0;
if (iy == 0 ) u_offset = 0.5 / WIDTH_SEGMENTS;
if (iy == HEIGHT_SEGMENTS) u_offset = - 0.5 / WIDTH_SEGMENTS;
for (let ix = 0; ix <= WIDTH_SEGMENTS; ix++) {
const u = ix / WIDTH_SEGMENTS;
geo_uv.push(u + u_offset, 1 - v);
geo_pos.push(
RADIUS * -Math.cos(u * Math.PI*2) * Math.sin(v * Math.PI),
RADIUS * Math.sin(u * Math.PI*2) * Math.sin(v * Math.PI),
RADIUS * -Math.cos(v * Math.PI)
);
grid.push(index++);
}
}
for (let iy = 0; iy < HEIGHT_SEGMENTS; iy++) {
for (let ix = 0; ix < WIDTH_SEGMENTS; ix++) {
const a = grid[(iy )*(WIDTH_SEGMENTS + 1) + ix + 1];
const b = grid[(iy )*(WIDTH_SEGMENTS + 1) + ix ];
const c = grid[(iy + 1)*(WIDTH_SEGMENTS + 1) + ix ];
const d = grid[(iy + 1)*(WIDTH_SEGMENTS + 1) + ix + 1];
if (iy !== 0 ) geo_idx.push(a, b, d);
if (iy !== HEIGHT_SEGMENTS - 1) geo_idx.push(b, c, d);
}
}
}
/* upload to GPU */
{
gl.bindBuffer(gl.ARRAY_BUFFER, buf.skydome_v_pos);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(geo_pos), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, buf.skydome_v_uv);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(geo_uv), gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buf.skydome_i);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(geo_idx), gl.STATIC_DRAW);
}
buf.skydome_i_count = geo_idx.length;
}
const INPUT_MODE_GRAB_SCROLL = 0;
const INPUT_MODE_PEN = 1;
const INPUT_MODE_PEN_RAINBOW = 2;
const INPUT_MODE_COUNT = 3;
const save = {
quill: [0, 0],
quill_inertia: [0, 0],
input_mode_button_y_offsets: Array.from({ length: INPUT_MODE_COUNT }, _ => 0),
scroll_prog: 0.9,
}
let input = {
pitch: Math.PI*0.30,
yaw: -Math.PI*0.15,
dampedEvent: { button: 0, movementX: 0, movementY: 0 },
hand_grabbing_parchment: false,
cam_pivot_x: 0,
cam_pivot_y: 0,
cam_pivot_z: -0.15,
zoom: 4,
scroll: 0,