-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeferred-rendering.html
More file actions
1863 lines (1578 loc) · 71.4 KB
/
Copy pathdeferred-rendering.html
File metadata and controls
1863 lines (1578 loc) · 71.4 KB
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
<html>
<head>
<title>Instanced Rendering - WebGL</title>
<style>
body {
background-color: black;
margin: 0;
padding: 0;
position: relative;
}
#main {
display: flex;
height: 100%;
justify-content: center;
align-items: center;
}
canvas {
height: 100%;
}
</style>
</head>
<body>
<section id="main">
<canvas id="canvas" height="1024" width="1024"></canvas>
</section>
</body>
<script src="lib/dat.gui.js"></script>
<script src="lib/gl-matrix.js"></script>
<script src="assets/scifi-box.obj.js"></script>
<script>
/*
*
* https://learnopengl.com/PBR/Theory
* https://learnopengl.com/Advanced-Lighting/Deferred-Shading
* https://learnopengl.com/Advanced-Lighting/Normal-Mapping
* https://www.geeks3d.com/20130122/normal-mapping-without-precomputed-tangent-space-vectors/
* https://en.m.wikipedia.org/wiki/Physically_based_rendering
* http://www.paulallenrenton.com/individual-projects/webgl-deferred-renderer
* http://renderwonk.com/publications/s2010-shading-course/
* https://gist.github.com/galek/53557375251e1a942dfa
* https://github.com/penumbra23/PBR-Shaders/blob/master/PBR-Shaders/Assets/Shaders/PBRShader.shader
*
*/
const { glMatrix } = window;
const Z_NEAR = 0.1;
const Z_FAR = 1000.0;
const TEXTURE_NONE = -1;
const TEXTURE_DIFFUSE = 0;
const TEXTURE_NORMAL = 1;
const TEXTURE_METALLIC = 2;
const TEXTURE_ROUGHNESS = 3;
const TEXTURE_POLKA_DOT = 4;
const TEXTURE_CHECKERBOARD = 5;
const TEXTURE_POLKA_DOT_STRIPES = 6;
const TEXTURE_STRIPES_VERTICAL = 7;
const TEXTURE_STRIPES_HORIZONTAL = 8;
const TEXTURE_OPTIONS = [
{ name: 'No Texture', enum: TEXTURE_NONE },
{ name: 'Diffuse', enum: TEXTURE_DIFFUSE },
{ name: 'Normal', enum: TEXTURE_NORMAL },
{ name: 'Metallic', enum: TEXTURE_METALLIC },
{ name: 'Roughness', enum: TEXTURE_ROUGHNESS },
{ name: 'Polka Dots', enum: TEXTURE_POLKA_DOT },
{ name: 'Checkerboard', enum: TEXTURE_CHECKERBOARD },
{ name: 'Polka Dot Stripes', enum: TEXTURE_POLKA_DOT_STRIPES },
{ name: 'Stripes (Vertical)', enum: TEXTURE_STRIPES_VERTICAL },
{ name: 'Stripes (Horizontal)', enum: TEXTURE_STRIPES_HORIZONTAL },
];
const OUTPUT_COLOUR = 0;
const OUTPUT_POSITION = 1
const OUTPUT_UV = 2;
const OUTPUT_NORMAL = 3;
const OUTPUT_DEPTH = 4;
const OUTPUT_SHADED_1 = 5;
const OUTPUT_SHADED_2 = 8;
const OUTPUT_DIFFUSE_COLOUR = 6;
const OUTPUT_PBR = 7;
const OUTPUT_OPTIONS = [
{ name: 'Shaded Phong', enum: OUTPUT_SHADED_1 },
{ name: 'Shaded PBR', enum: OUTPUT_SHADED_2 },
{ name: 'Colours', enum: OUTPUT_COLOUR },
{ name: 'Diffuse Colours', enum: OUTPUT_DIFFUSE_COLOUR },
{ name: 'Positions', enum: OUTPUT_POSITION },
{ name: 'UVs', enum: OUTPUT_UV },
{ name: 'Normals', enum: OUTPUT_NORMAL },
{ name: 'PBR', enum: OUTPUT_PBR },
{ name: 'Depth', enum: OUTPUT_DEPTH },
];
const FILTER_NONE = -1;
const FILTER_GAUSSIAN = 10;
const FILTER_SHARPEN = 11;
const FILTER_BOX = 12;
const FILTER_EMBOSS = 13;
const FILTER_SOBEL = 14;
const FILTER_OPTIONS = [
{ name: 'No Filter', enum: FILTER_NONE },
{ name: 'Gaussian', enum: FILTER_GAUSSIAN },
{ name: 'Sharpen', enum: FILTER_SHARPEN },
{ name: 'Box', enum: FILTER_BOX },
{ name: 'Emboss', enum: FILTER_EMBOSS },
{ name: 'Sobel', enum: FILTER_SOBEL },
];
const EFFECT_NONE = -1;
const EFFECT_PIXEL_GRID = 100;
const EFFECT_OUTLINE = 101;
const EFFECT_OPTIONS = [
{ name: 'No Effect', enum: EFFECT_NONE },
{ name: 'Pixel Grid', enum: EFFECT_PIXEL_GRID },
{ name: 'Outline', enum: EFFECT_OUTLINE },
];
const pbrSnippet = `
#define PI 3.1415926
#define COOK
#define COOK_GGX
// constant light position, only one light source for testing (treated as point light)
// const vec3 light_pos = vec3(-2, 3, -2);
// handy value clamping to 0 - 1 range
float saturate(in float value)
{
return clamp(value, 0.0, 1.0);
}
// phong (lambertian) diffuse term
float phong_diffuse()
{
return (1.0 / PI);
}
// compute fresnel specular factor for given base specular and product
// product could be NdV or VdH depending on used technique
vec3 fresnel_factor(in vec3 f0, in float product)
{
return mix(f0, vec3(1.0), pow(1.01 - product, 5.0));
}
// following functions are copies of UE4
// for computing cook-torrance specular lighting terms
float D_blinn(in float roughness, in float NdH)
{
float m = roughness * roughness;
float m2 = m * m;
float n = 2.0 / m2 - 2.0;
return (n + 2.0) / (2.0 * PI) * pow(NdH, n);
}
float D_beckmann(in float roughness, in float NdH)
{
float m = roughness * roughness;
float m2 = m * m;
float NdH2 = NdH * NdH;
return exp((NdH2 - 1.0) / (m2 * NdH2)) / (PI * m2 * NdH2 * NdH2);
}
float D_GGX(in float roughness, in float NdH)
{
float m = roughness * roughness;
float m2 = m * m;
float d = (NdH * m2 - NdH) * NdH + 1.0;
return m2 / (PI * d * d);
}
float G_schlick(in float roughness, in float NdV, in float NdL)
{
float k = roughness * roughness * 0.5;
float V = NdV * (1.0 - k) + k;
float L = NdL * (1.0 - k) + k;
return 0.25 / (V * L);
}
// simple phong specular calculation with normalization
vec3 phong_specular(in vec3 V, in vec3 L, in vec3 N, in vec3 specular, in float roughness)
{
vec3 R = reflect(-L, N);
float spec = max(0.0, dot(V, R));
float k = 1.999 / (roughness * roughness);
return min(1.0, 3.0 * 0.0398 * k) * pow(spec, min(10000.0, k)) * specular;
}
// simple blinn specular calculation with normalization
vec3 blinn_specular(in float NdH, in vec3 specular, in float roughness)
{
float k = 1.999 / (roughness * roughness);
return min(1.0, 3.0 * 0.0398 * k) * pow(NdH, min(10000.0, k)) * specular;
}
// cook-torrance specular calculation
vec3 cooktorrance_specular(in float NdL, in float NdV, in float NdH, in vec3 specular, in float roughness, in float rimLighting)
{
#ifdef COOK_BLINN
float D = D_blinn(roughness, NdH);
#endif
#ifdef COOK_BECKMANN
float D = D_beckmann(roughness, NdH);
#endif
#ifdef COOK_GGX
float D = D_GGX(roughness, NdH);
#endif
float G = G_schlick(roughness, NdV, NdL);
float rim = mix(1.0 - roughness * rimLighting * 0.9, 1.0, NdV);
return (1.0 / rim) * specular * G * D;
}
vec3 lighting_pbr(vec3 base_colour, vec3 position, vec3 normal, vec2 pbr, vec3 light_pos) {
// Incoming light_pos is model space.
// light attenuation
float A = 20.0 / dot(light_pos - position, light_pos - position);
// L, V, H vectors
vec3 L = normalize(light_pos - position);
vec3 V = normalize(u_camera_position - position);
vec3 H = normalize(L + V);
vec3 nn = normalize(normal);
// I default to normal map is used, but that's something we get back upstream
vec3 N = nn;
vec3 base = base_colour;
// // roughness
// #if USE_ROUGHNESS_MAP
// float roughness = texture2D(spec, texcoord).y * material.y;
float roughness = pbr.g;
float metallic = pbr.r;
// mix between metal and non-metal material, for non-metal
// constant base specular factor of 0.04 grey is used
vec3 specular = mix(vec3(0.04), base, metallic);
// // diffuse IBL term
// // I know that my IBL cubemap has diffuse pre-integrated value in 10th MIP level
// // actually level selection should be tweakable or from separate diffuse cubemap
// mat3x3 tnrm = transpose(normal_matrix);
// vec3 envdiff = textureCubeLod(envd, tnrm * N, 10).xyz;
// // specular IBL term
// // 11 magic number is total MIP levels in cubemap, this is simplest way for picking
// // MIP level from roughness value (but it's not correct, however it looks fine)
// vec3 refl = tnrm * reflect(-V, N);
// vec3 envspec = textureCubeLod(
// envd, refl, max(roughness * 11.0, textureQueryLod(envd, refl).y)
// ).xyz;
// compute material reflectance
float NdL = max(0.0, dot(N, L));
float NdV = max(0.001, dot(N, V));
float NdH = max(0.001, dot(N, H));
float HdV = max(0.001, dot(H, V));
float LdV = max(0.001, dot(L, V));
// fresnel term is common for any, except phong
// so it will be calcuated inside ifdefs
#ifdef PHONG
// specular reflectance with PHONG
vec3 specfresnel = fresnel_factor(specular, NdV);
vec3 specref = phong_specular(V, L, N, specfresnel, roughness);
#endif
#ifdef BLINN
// specular reflectance with BLINN
vec3 specfresnel = fresnel_factor(specular, HdV);
vec3 specref = blinn_specular(NdH, specfresnel, roughness);
#endif
#ifdef COOK
// specular reflectance with COOK-TORRANCE
vec3 specfresnel = fresnel_factor(specular, HdV);
// Hasn't been calculated yet.
// float rimLighting = pbr.w;
float rimLighting = 1.;
vec3 specref = cooktorrance_specular(NdL, NdV, NdH, specfresnel, roughness, rimLighting);
#endif
specref *= vec3(NdL);
// diffuse is common for any model
vec3 diffref = (vec3(1.0) - specfresnel) * phong_diffuse() * NdL;
// compute lighting
vec3 reflected_light = vec3(0.0);
vec3 diffuse_light = vec3(0.15); // initial value == constant ambient light
// point light
// vec3 light_color = vec3(1.0) * A;
// Remove attenuation
vec3 light_color = vec3(1.0);
reflected_light += specref * light_color;
diffuse_light += diffref * light_color;
// IBL lighting
// vec2 brdf = texture2D(iblbrdf, vec2(roughness, 1.0 - NdV)).xy;
// vec3 iblspec = min(vec3(0.99), fresnel_factor(specular, NdV) * brdf.x + brdf.y);
// reflected_light += iblspec * envspec;
// diffuse_light += envdiff * (1.0 / PI);
// Can bring out the blue outline, but it's too much like the diffuse colour
// and has no highlight impact.
// diffuse_light += 0.9 * (1.0 / PI);
// final result
vec3 result = diffuse_light * mix(base, vec3(0.0), metallic) + reflected_light;
return result;
}
vec3 lighting_pbr_all_lights(vec3 base_colour, vec3 position, vec3 normal, vec2 pbr) {
vec3 colour = vec3(0.);
for (int i = 0; i < LIGHTS.length(); i++) {
Light light = LIGHTS[i];
// Need to light position so we negate it.
colour += lighting_pbr(base_colour, position, normal, pbr, -light.position);
}
return colour;
}
`
// Good combos:
// Shaded + Sobel + (Checkerboard | Polka Dots | Diffuse) etc.
// plus applying different scale.
const SHADERS = {
phong: {
vertex: `#version 300 es
precision highp float;
uniform mat4 u_model_matrix;
uniform mat4 u_view_matrix;
uniform mat4 u_projection_matrix;
uniform float u_time;
uniform bool u_animate_camera;
uniform bool u_animate_y;
uniform int u_instance_total;
in vec3 a_position;
in vec2 a_uv;
in vec3 a_normal;
in vec3 a_instance_translation;
in vec3 a_instance_rotation;
out vec4 v_position;
out vec4 v_uv;
out vec4 v_normal;
void rotX(inout mat4 r, float angle) {
r[1][1] = cos(angle);
r[1][2] = sin(angle);
r[2][1] = -sin(angle);
r[2][2] = cos(angle);
}
void rotY(inout mat4 r, float angle) {
r[0][0] = cos(angle);
r[0][2] = sin(angle);
r[2][0] = -sin(angle);
r[2][2] = cos(angle);
}
void rotZ(inout mat4 r, float angle) {
r[0][0] = cos(angle);
r[0][1] = sin(angle);
r[1][0] = -sin(angle);
r[1][1] = cos(angle);
}
void main(void) {
// This but for R4:
// https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
mat4 rotationX = mat4(1.);
mat4 rotationY = mat4(1.);
mat4 rotationZ = mat4(1.);
rotX(rotationX, a_instance_rotation.x);
rotY(rotationY, a_instance_rotation.y);
rotZ(rotationZ, a_instance_rotation.z);
// Row-major
mat4 translation = mat4(1.);
translation[3].xyz = a_instance_translation;
// Awesome way to bump things up and down. Because it depends on
// instance id, it is consistent across all position values for
// the instance. 1 is added for instance id 0, which would just result in
// a permanent sin of 0.
if (u_animate_y) {
translation[3][1] += sin(u_time * float(gl_InstanceID + 1)) + cos(u_time * float(u_instance_total - gl_InstanceID + 1));
}
mat4 rotation = rotationZ * rotationY * rotationX;
mat4 MTR = translation * rotation * u_model_matrix;
// We reuse this quite a bit so do it once:
if (u_animate_camera) {
// Disable x, z to orbit around origin.
rotX(rotationX, u_time);
rotY(rotationY, u_time);
rotZ(rotationZ, u_time);
mat4 camera_rotation = rotationZ * rotationY * rotationX;
// Let scaling from the model matrix happen first.
MTR = camera_rotation * MTR;
}
v_position = MTR * vec4(a_position, 1.);
v_uv = vec4(a_uv, 0., 0.);
v_normal = MTR * vec4(a_normal, 0.);
gl_Position = u_projection_matrix * u_view_matrix * MTR * vec4(a_position, 1.);
}
`,
fragment: `#version 300 es
precision highp float;
uniform sampler2D u_texture_diffuse;
uniform sampler2D u_texture_normal;
uniform sampler2D u_texture_metallic;
uniform sampler2D u_texture_roughness;
uniform mat4 u_view_matrix;
uniform int u_use_texture;
uniform vec3 u_colour;
uniform float u_opacity;
in vec4 v_position;
in vec4 v_uv;
in vec4 v_normal;
layout(location = 0) out vec4 position;
layout(location = 1) out vec4 uv;
layout(location = 2) out vec4 normal;
layout(location = 3) out vec4 diffuse;
layout(location = 4) out vec4 pbr;
vec3 polka_dots(vec2 point) {
// mx and my are now some value between 0 and size.
float size = 0.2;
float half_size = size * 0.5;
vec2 modPoint = mod(point, size);
// Shift to centre of the quadrant, since it's been divided into 4
vec2 centre = vec2(half_size);
float d = distance(centre, modPoint);
// controls how big the circles will be
float radius = half_size * 0.5;
if (radius < d) {
return vec3(1.);
}
// Smoothes the intersection between the polkadot and the returned background
// because we slowly blendly between white and red as we apporach the radius
// boundary.
float stepped = 1. - smoothstep(0.2, 0.3, abs(radius - d) / radius);
return vec3(1., stepped, stepped);
}
vec3 striped_polka_dots(vec2 point) {
// mx and my are now some value between 0 and size.
float size = 0.2;
float half_size = size * 0.5;
vec2 modPoint = mod(point, size);
// Shift to centre of the quadrant, since it's been divided into 4
vec2 centre = vec2(half_size);
float d = distance(centre, modPoint);
// controls how big the circles will be
float radius = half_size * 0.5;
// controls the stripe staggering.
// we take double the size because the circle occupies this entire row.
// if we're above half the doubled size, we're technically on another row.
float colour = mod(point.y, size * 2.0) > size ? 1. : 0.;
if (radius < d) {
return vec3(1. - colour);
}
return vec3(colour);
}
vec3 checkerboard(vec2 point) {
// mx and my are now some value between 0 and size.
float size = 0.2;
float half_size = size * 0.5;
float mx = mod(point.x, size);
float my = mod(point.y, size);
// tracking even/odd rows lets us stagger the colour like a proper checkerboard
// without doing this, we get stripes.
// even rows
// if it's > half_size, choose an even row, otherwise use an odd row.
float colour = my > half_size ? 1. : 0.;
if (mx > half_size) {
return vec3(1. - colour);
}
return vec3(colour);
}
vec3 stripes(vec2 point, int direction) {
// 0 for horizontal, 1 for vertical.
// mx and my are now some value between 0 and size.
float size = 0.05;
float half_size = size * 0.5;
float mx = mod(point.x, size);
float my = mod(point.y, size);
// tracking even/odd rows lets us stagger the colour like a proper checkerboard
// without doing this, we get stripes.
// even rows
// if it's > half_size, choose an even row, otherwise use an odd row.
if ((mx > half_size && direction == 0) || (my > half_size && direction == 1)) {
return vec3(1.);
}
return vec3(0.);
}
vec3 get_texture(vec3 base_colour, vec2 v_world_uv) {
// Just scale the point up if we want more repetition from the texture.
float scale = 5.;
// Use this to 'scroll' the texture over the shape, since it will keep
// using rotated values to sample from the boxmap.
vec2 uv = v_world_uv * scale;
vec3 colour = base_colour;
switch (u_use_texture) {
case ${TEXTURE_DIFFUSE}: colour = texture(u_texture_diffuse, v_world_uv).xyz; break;
case ${TEXTURE_NORMAL}: colour = texture(u_texture_normal, v_world_uv).xyz; break;
case ${TEXTURE_METALLIC}: colour = texture(u_texture_metallic, v_world_uv).xyz; break;
case ${TEXTURE_ROUGHNESS}: colour = texture(u_texture_roughness, v_world_uv).xyz; break;
case ${TEXTURE_POLKA_DOT}: colour = polka_dots(uv); break;
case ${TEXTURE_CHECKERBOARD}: colour = checkerboard(uv); break;
case ${TEXTURE_POLKA_DOT_STRIPES}: colour = striped_polka_dots(uv); break;
case ${TEXTURE_STRIPES_VERTICAL}: colour = stripes(uv, 1); break;
case ${TEXTURE_STRIPES_HORIZONTAL}: colour = stripes(uv, 0); break;
}
return colour;
}
// https://www.geeks3d.com/20130122/normal-mapping-without-precomputed-tangent-space-vectors/
// http://www.thetenthplanet.de/archives/1180
mat3 cotangent_frame(vec3 N, vec3 p, vec2 uv)
{
// get edge vectors of the pixel triangle
vec3 dp1 = dFdx( p );
vec3 dp2 = dFdy( p );
vec2 duv1 = dFdx( uv );
vec2 duv2 = dFdy( uv );
// solve the linear system
vec3 dp2perp = cross( dp2, N );
vec3 dp1perp = cross( N, dp1 );
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
// construct a scale-invariant frame
float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) );
return mat3( T * invmax, B * invmax, N );
}
// https://www.geeks3d.com/20130122/normal-mapping-without-precomputed-tangent-space-vectors/
vec3 perturb_normal( vec3 N, vec3 V, vec2 texcoord )
{
// assume N, the interpolated vertex normal and
// V, the view vector (vertex to eye)
vec3 map = texture(u_texture_normal, texcoord ).xyz;
// Normals in the map were mapped from [0, 1], but that encodes values ranging from [-1, 1].
// As a result, we transform it back form [0, 1] to [-1, 1] here.
map = map * 2. - 1.;
mat3 TBN = cotangent_frame(N, -V, texcoord);
// I believe this puts the normal map from its tangent space into the world space,
// since the input normals and the input viewer-to-vertex were both all done in
// world space.
return normalize(TBN * map);
}
void main (void) {
position = v_position;
uv = v_uv;
// If we don't want to use the normal map.
// normal = vec4(normalize(v_normal.xyz), 0.);
vec3 vertex_to_eye = u_view_matrix[3].xyz - position.xyz;
vec3 normal_from_normal_map = perturb_normal(normalize(v_normal.xyz), vertex_to_eye, uv.xy);
normal = vec4(normal_from_normal_map, 1.);
diffuse = vec4(get_texture(u_colour, v_uv.xy), u_opacity);
// They're both greyscale, so we only need one channel each.
pbr.r = texture(u_texture_metallic, v_uv.xy).r;
pbr.g = texture(u_texture_roughness, v_uv.xy).r;
// Placeholder for storing other things in the pbr texture.
pbr.ba = vec2(0.);
// If we turn deferred rendering off, we see the 0th buffer or the default
// colour attachment.
// Debug uvs
// position = v_uv
// Debug normals
// position = vec4(abs(normal.xyz), 1.);
// Debug diffuse
// position = diffuse;
}
`,
uniforms: [
'u_texture_diffuse',
'u_texture_normal',
'u_texture_metallic',
'u_texture_roughness',
'u_model_matrix',
'u_view_matrix',
'u_projection_matrix',
'u_colour',
'u_opacity',
'u_use_texture',
'u_time',
'u_animate_camera',
'u_animate_y',
'u_instance_total',
],
attributes: [
'a_position',
'a_uv',
'a_normal',
'a_instance_translation',
'a_instance_rotation',
],
},
quad: {
vertex: `#version 300 es
precision highp float;
in vec2 a_position;
void main(void) {
gl_Position = vec4(a_position, 0., 1.);
}
`,
fragment: `#version 300 es
precision highp float;
uniform sampler2D u_buffer_position;
uniform sampler2D u_buffer_uv;
uniform sampler2D u_buffer_normal;
uniform sampler2D u_buffer_diffuse;
uniform sampler2D u_buffer_pbr;
uniform sampler2D u_buffer_depth;
uniform vec3 u_camera_position;
uniform vec3 u_colour;
uniform int u_use_output;
uniform int u_use_filter;
uniform int u_use_effect;
out vec4 colour;
struct Light {
vec3 position;
float intensity;
};
Light LIGHTS[] = Light[](
// TODO
// coords are inverted for some reason.
// top centre,
Light(vec3(-5, -20., 0.), 0.85),
// bottom right front
Light(vec3(0., 0., -20.), 0.1)
// front right or left
// Light(vec3(-10., 0., 10), 0.7)
);
vec4 colour_at(ivec2 frag_coord, int use_input);
${pbrSnippet}
vec3 lighting_phong(vec3 base_colour, vec3 position, vec3 normal) {
// We use frag_coord and not uv here because uv is only good for sampling in textures.
// We need the full size frag_coord to sample from the buffers we have.
// TODO
// https://gist.github.com/galek/53557375251e1a942dfa
vec3 colour = vec3(0.);
vec3 camera_position = u_camera_position;
for (int i = 0; i < LIGHTS.length(); i++) {
Light light = LIGHTS[i];
vec3 light_to_vertex = normalize(position - light.position);
// https://www.cs.toronto.edu/~jacobson/phong-demo/
// Lambert's cosine law
float lambertian = max(dot(normal, light_to_vertex), 0.0);
float specular = 0.0;
float shininess_value = 2048.;
if (lambertian > 0.0) {
// Reflected light vector
vec3 reflection = reflect(-light_to_vertex, normal);
// Vector to viewer
vec3 vector_to_camera = normalize(camera_position - position);
// Compute the specular term
float specular_angle = max(dot(reflection, vector_to_camera), 0.0);
specular = pow(specular_angle, shininess_value);
}
float Kd = light.intensity;
float Ka = 0.05;
float Ks = light.intensity;
// Metallic is in the r channel.
vec3 working_colour = base_colour;
vec3 ambient_colour = working_colour.xyz;
vec3 specular_colour = base_colour;
colour += vec3(
Ka * ambient_colour +
Kd * lambertian * working_colour +
Ks * specular * specular_colour
);
}
return colour;
}
float luminance(vec3 colour) {
return 0.2126 * colour.r + 0.7152 * colour.g + 0.0722 * colour.b;
}
vec3 apply_filter(vec3 base_colour, ivec2 frag_coord, int use_input) {
// Sample surrounding texels.
ivec2 offset = ivec2(1, 1);
ivec2 offset_frag_coords[9] = ivec2[9](
// top left
frag_coord - offset,
// top
ivec2(frag_coord.x, frag_coord.y - offset.y),
// top right
ivec2(frag_coord.x + offset.x, frag_coord.y - offset.y),
// left
ivec2(frag_coord.x - offset.x, frag_coord.y),
// centre
frag_coord,
// right
ivec2(frag_coord.x + offset.x, frag_coord.y),
// bottom left
ivec2(frag_coord.x - offset.x, frag_coord.y + offset.y),
// bottom
ivec2(frag_coord.x, frag_coord.y + offset.y),
// bottom right
frag_coord + offset
);
float convolution_matrix[9];
if (${FILTER_GAUSSIAN} == u_use_filter) {
convolution_matrix = float[9](
0.0625, 0.125, 0.0625,
0.125, 0.25, 0.125,
0.0625, 0.125, 0.0625
);
} else if (${FILTER_SHARPEN} == u_use_filter) {
convolution_matrix = float[9](
0.0, -1.0, 0.0,
-1.0, 5.0, -1.0,
0.0, -1.0, 0.0
);
} else if (${FILTER_BOX} == u_use_filter) {
convolution_matrix = float[9](
0.11111, 0.11111, 0.11111,
0.11111, 0.11111, 0.11111,
0.11111, 0.11111, 0.11111
);
} else if (${FILTER_EMBOSS} == u_use_filter) {
convolution_matrix = float[9](
-2.0, -1.0, 0.0,
-1.0, 1.0, 1.0,
0.0, 1.0, 2.0
);
}
vec3 colour = vec3(0.0, 0.0, 0.0);
for (int i = 0; i < convolution_matrix.length(); i++) {
ivec2 next_frag_coord = offset_frag_coords[i];
colour += colour_at(next_frag_coord, use_input).rgb * convolution_matrix[i];
}
return colour;
}
vec3 apply_filter_sobel(vec3 base_colour, ivec2 frag_coord, int use_input) {
// Sample surrounding texels.
ivec2 offset = ivec2(1, 1);
// // "Seeing doubles".
// offset.x = 10;
// offset.y = 0;
ivec2 offset_frag_coords[9] = ivec2[9](
// top left
frag_coord - offset,
// top
ivec2(frag_coord.x, frag_coord.y - offset.y),
// top right
ivec2(frag_coord.x + offset.x, frag_coord.y - offset.y),
// left
ivec2(frag_coord.x - offset.x, frag_coord.y),
// centre
frag_coord,
// right
ivec2(frag_coord.x + offset.x, frag_coord.y),
// bottom left
ivec2(frag_coord.x - offset.x, frag_coord.y + offset.y),
// bottom
ivec2(frag_coord.x, frag_coord.y + offset.y),
// bottom right
frag_coord + offset
);
float convolution_matrix_x[9] = float[9](
-1.0, 0.0, 1.0,
-2.0, 0.0, 2.0,
-1.0, 0.0, 1.0
);
float convolution_matrix_y[9] = float[9](
-1.0, -2.0, -1.0,
0.0, 0.0, 0.0,
1.0, 2.0, 1.0
);
vec3 colour = vec3(0.0, 0.0, 0.0);
vec2 gradient = vec2(0.);
for (int i = 0; i < offset_frag_coords.length(); i++) {
// Sample surrounding data to get lighting in the region.
ivec2 next_frag_coord = offset_frag_coords[i];
vec3 next_colour = colour_at(next_frag_coord, use_input).rgb;
float lum = luminance(next_colour);
gradient += (vec2(convolution_matrix_x[i], convolution_matrix_y[i]) * lum);
}
float mag = length(gradient);
return vec3(mag);
}
vec4 apply_effect_pixel_grid(ivec2 frag_coord, int use_input) {
// https://github.com/sketchpunk/FunWithWebGL2/blob/de4f8e919056196b422cd6cd406cdd0ccf279f48/lesson_061/fungi/shaders/deferred/DeferredRender.txt
float pixel_size = 7.0;
float x_mod = mod(float(frag_coord.x), pixel_size);
float y_mod = mod(float(frag_coord.y), pixel_size);
if (x_mod == 0.0 || y_mod == 0.0) {
return vec4(0.0, 0.0, 0.0, 0.3);
} else {
ivec2 next_frag_coord = frag_coord;
next_frag_coord.x -= int( x_mod );
next_frag_coord.y -= int( y_mod );
// Need to fetch with the modded value.
return colour_at(next_frag_coord, use_input);
}
}
float linearize_depth(float z) {
// https://github.com/sketchpunk/FunWithWebGL2/blob/de4f8e919056196b422cd6cd406cdd0ccf279f48/lesson_061/fungi/shaders/deferred/DeferredRender.txt#L49
float n = float(${Z_NEAR}); // camera z near
float f = float(${Z_FAR}); // camera z far
return (2.0 * n) / (f + n - z * (f - n));
}
vec4 colour_at(ivec2 frag_coord, int use_output) {
// Everything should get the right colour from here, not try to
// determine it on its own using a frag_coord.
vec3 position = texelFetch(u_buffer_position, frag_coord, 0).rgb;
vec2 uv = texelFetch(u_buffer_uv, frag_coord, 0).xy;
// Invert the normal direction to get bounce back IIRC. Only do this for the normal map.
// vec3 normal = -texelFetch(u_buffer_normal, frag_coord, 0).rgb;
vec3 normal = texelFetch(u_buffer_normal, frag_coord, 0).rgb;
vec4 diffuse = texelFetch(u_buffer_diffuse, frag_coord, 0).rgba;
// Depth is in the red channel.
float depth = texelFetch(u_buffer_depth, frag_coord, 0).r;
depth = linearize_depth(depth);
// Metallic in r, roughness in g. b and a are reserved (0 now).
vec2 pbr = texelFetch(u_buffer_pbr, frag_coord, 0).rg;
vec4 colour = vec4(1.);
// Compositing
// colour += diffuse * 0.05;
// Gradient?
// float grad = length(step(0.05, cross(dFdx(position), dFdy(position)) * 10.));
// colour.rgb = vec3(grad);
// colour = texelFetch(u_buffer_normal, frag_coord, 0);
switch (use_output) {
// Cut at diffuse edge since it's surrounded by black. This makes it
// entirely white inside (so duotone)
case ${OUTPUT_COLOUR}: colour = step(0.000000001, diffuse); break;
case ${OUTPUT_DIFFUSE_COLOUR}: colour = diffuse; break;
case ${OUTPUT_POSITION}: colour.rgb = position; break;
case ${OUTPUT_UV}: colour.rgb = vec3(uv, 1.); break;
// Undo the inversion for debugging.
case ${OUTPUT_NORMAL}: colour.rgb = abs(normal); break;
case ${OUTPUT_PBR}: colour.rgb = vec3(pbr.rg, 0.); break;
case ${OUTPUT_SHADED_1}: colour.rgb = lighting_phong(diffuse.xyz, position, normalize(normal)); break;
case ${OUTPUT_SHADED_2}: colour.rgb = lighting_pbr_all_lights(diffuse.xyz, position, normalize(normal), pbr); break;
case ${OUTPUT_DEPTH}: colour.rgb = vec3(depth); break;
}
return colour;
}
void main(void) {
ivec2 frag_coord = ivec2(gl_FragCoord.xy);
colour = colour_at(frag_coord, u_use_output);
// Effects then filters. This order is arbitrary might be nice to switch on demand.
// TODO
// They don't play nicely together because the pixel grid impacts the frag_coord
// but then the filters reuse the non-modded frag coord so it removes the effect.
switch (u_use_effect) {
case ${EFFECT_PIXEL_GRID}:
colour = apply_effect_pixel_grid(frag_coord, u_use_output); break;
case ${EFFECT_OUTLINE}:
// Draw a sobel around the colour-only output.
colour.rgb += apply_filter_sobel(colour.rgb, frag_coord, ${OUTPUT_COLOUR}); break;
}
switch (u_use_filter) {
case ${FILTER_BOX}:
case ${FILTER_EMBOSS}:
case ${FILTER_GAUSSIAN}:
case ${FILTER_SHARPEN}:
colour.rgb = apply_filter(colour.rgb, frag_coord, u_use_output); break;
case ${FILTER_SOBEL}:
colour.rgb = apply_filter_sobel(colour.rgb, frag_coord, u_use_output); break;
}
}
`,
uniforms: [
'u_buffer_position',
'u_buffer_uv',
'u_buffer_normal',
'u_buffer_diffuse',
'u_buffer_pbr',
'u_buffer_depth',
'u_camera_position',
'u_colour',
'u_use_output',
'u_use_effect',
'u_use_filter',
],
attributes: [
'a_position',
],
}
};
async function loadImage(imageData) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = reject;
img.src = imageData;
});
}
class Shader {
constructor(gl, vertCode, fragCode) {
this.program = null;
this.setupShader(gl, vertCode, fragCode);