-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathindex.html
1246 lines (1002 loc) · 40 KB
/
index.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
<html>
<head>
<title>Chinese Painting Tile Based Deferred Shading</title>
<meta charset ="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1"> <!-- Use Chrome Frame in IE -->
</head>
<style type="text/css">
* { -webkit-user-select: none; -moz-user-select: none; cursor: default; }
body { font: 13px/18px Arial, sans-serif; background: black; overflow: hidden; }
a { color: inherit; cursor: pointer; }
img { display: none; }
ul { padding: 0 0 0 20px; }
h1 { font: bold italic 30px/30px Arial; text-align: center; }
h2 { font: bold italic 17px/17px Arial; padding-top: 10px; }
small { display: block; font-size: 11px; line-height: 15px; }
canvas { position: absolute; top: 0; left: 0;}
#help { color: white; position: absolute; top: 0; right: 0; bottom: 0; width: 20%; padding-right: 8px; overflow: auto; padding-bottom: 30px;/*background-color: black;*/}
#help .slider{width: 90%; padding-left:10px;padding-top:5px;}
#range{font-size:18px;font:Arial, sans-serif;}
#info { position: absolute; top: 750px; right: 0; bottom: 0; width: 300px; padding-right: 30px; overflow: auto; }
#loading {position:absolute; left:30%; top:50%; right:0; color:white; font-size: 30px;}
</style>
<body>
<script src="//cdnjs.cloudflare.com/ajax/libs/mobile-detect/1.3.5/mobile-detect.min.js"></script>
<div id="message" style="position:absolute;top:100px"></div> <!-- Pixel offset to avoid FPS counter -->
<canvas id="canvas" style="border: none; width: 100%; height: 100%" tabindex="1"></canvas>
<div id = "loading">
<p id="loading-text" style="color: white">Initializing the awesome......</p>
</div>
<div id = "help">
<h1><span id="title-webgl2"></span> - Tile Based Deferred Shading</h1>
<p> Developed by <br> Sijie Tian <a href = "https://twitter.com/sijietian">twitter</a> <a href = "https://github.com/tiansijie">github</a> <br> Yuqin Shao <a href = "https://twitter.com/YuqinShao
">twitter</a> <a href = "https://github.com/yuqinshao">github</a> </p>
<p>
This demo includes <span id ="helplightnum"></span> dynamic lights and <span id ="trianglenum">66450 </span>triangles. <br>
You are uisng <b><span id="is-webgl2"></span><span id="helpmode">Deferred Shading</span></b>.
</p>
<p>
This demo requires a decent graphics card and up-to-date drivers. If you can't run the demo, you can still see it on <a href = "https://vimeo.com/83616115">Vimeo</a>
</p>
<p>
<h2>Instructions:</h2>
<p>
<li>Modify number of lights in the scene:<span id = "range"></span> lights</li>
<input id="light-number-range" class = "slider" type="range" min="0" max="1000" step="5" width = "200px" onchange="showValue(this.value)"/>
<li>Drag mouse to view around the scene</li>
<li>w,s,a,d to move the camera</li>
<li>Press 1 to display depth texutre</li>
<li>Press 2 to display normal texture</li>
<li>Press 3 to display position texutre</li>
<li>Press 4 to display color texture</li>
<li>Press 5 to display ambient texture</li>
<li>Press 6 to display tile based deferred shading</li>
<li>Press 7 to display non tile based deferred shading</li>
<li>Press 8 to display NPR with tile based deferred shading</li>
<li>Press 9 to display debug view for tile based</li>
<li>Press 0 to display scissor test visualization</li>
<li>Press f to switch between deferred shading and forward shading</li>
</p>
<h2> Source Code: </h2>
<a href = "https://github.com/tiansijie/Tile_Based_WebGL_DeferredShader">Code</a>
</div>
<script>
var md = new MobileDetect(window.navigator.userAgent);
if (md.mobile()) {
window.lightNum = 60;
}
else {
window.lightNum = 240;
}
document.getElementById("range").innerHTML = window.lightNum;
document.getElementById("helplightnum").innerHTML = window.lightNum;
document.getElementById("light-number-range").value = window.lightNum;
</script>
<script id="pass_vs" type="x-shader/x-vertex">
precision highp float;
uniform mat4 u_Model;
uniform mat4 u_View;
uniform mat4 u_Persp;
uniform mat4 u_InvTrans;
attribute vec3 Position;
attribute vec3 Normal;
attribute vec2 Texcoord;
varying vec3 fs_Normal;
varying vec4 fs_Position;
varying vec2 fs_Texcoord;
varying float fs_Depth;
void main(void) {
fs_Normal = ((u_InvTrans*vec4(Normal,0.0)).xyz);
vec4 world = u_Model * vec4(Position, 1.0);
vec4 camera = u_View * world;
fs_Position = camera;
gl_Position = u_Persp * camera;
fs_Texcoord = Texcoord;
fs_Depth = ((gl_Position.z / gl_Position.w));
}
</script>
<script id="forward_fs" type="x-shader/x-fragment">
precision highp float;
uniform vec3 u_Color;
uniform vec3 light_pos;
uniform vec3 light_color;
uniform float light_radius;
uniform int drawmode;
uniform vec4 u_Light;
//in
varying vec3 fs_Normal;
varying vec4 fs_Position;
varying vec2 fs_Texcoord;
varying float fs_Depth;
uniform sampler2D u_Texutre;
void main(void)
{
vec3 color = texture2D(u_Texutre, fs_Texcoord).xyz;
vec3 position = fs_Position.xyz;
vec3 normal = normalize(fs_Normal);
if(drawmode == 1){
if(distance(light_pos, position) < light_radius){
float dist = distance(light_pos, position);
float lightR = light_radius;
float attenuation = dist/(1.0 - (dist/lightR) * (dist/lightR));
attenuation = attenuation / lightR + 1.0;
attenuation = 1.0 / (attenuation * attenuation);
float diffuse = abs(dot(normal, normalize(light_pos)));
gl_FragColor = vec4(diffuse * light_color * color * attenuation, 1.0);
}
else
gl_FragColor = vec4(vec3(0.0),1.0);
}
else
{
vec3 light = u_Light.xyz;
float strength = u_Light.w;
float ambient = 0.09;
float diffuse = abs(dot(normalize(light),(normal)));
gl_FragColor = vec4(color*(strength*diffuse + ambient),1.0);
}
}
</script>
<script id="pass_fs" type="x-shader/x-fragment">
#extension GL_EXT_draw_buffers : require
precision highp float;
uniform vec3 u_Color;
//in
varying vec3 fs_Normal;
varying vec4 fs_Position;
varying vec2 fs_Texcoord;
varying float fs_Depth;
uniform sampler2D u_Texutre;
void main(void)
{
//normal, position, depth, color
gl_FragData[0] = vec4(vec3(fs_Depth), 1.0);
gl_FragData[1] = vec4(normalize(fs_Normal.xyz), 1.0);
gl_FragData[2] = fs_Position;
gl_FragData[3] = vec4(texture2D(u_Texutre, fs_Texcoord).xyz, 1.0);
}
</script>
<script id="webgl2_pass_vs" type="x-shader/x-vertex">#version 300 es
precision highp float;
uniform mat4 u_Model;
uniform mat4 u_View;
uniform mat4 u_Persp;
uniform mat4 u_InvTrans;
in vec3 Position;
in vec3 Normal;
in vec2 Texcoord;
out vec3 fs_Normal;
out vec4 fs_Position;
out vec2 fs_Texcoord;
out float fs_Depth;
void main(void) {
fs_Normal = ((u_InvTrans*vec4(Normal,0.0)).xyz);
vec4 world = u_Model * vec4(Position, 1.0);
vec4 camera = u_View * world;
fs_Position = camera;
gl_Position = u_Persp * camera;
fs_Texcoord = Texcoord;
fs_Depth = ((gl_Position.z / gl_Position.w));
}
</script>
<script id="webgl2_pass_fs" type="x-shader/x-fragment">#version 300 es
precision highp float;
uniform vec3 u_Color;
//in
in vec3 fs_Normal;
in vec4 fs_Position;
in vec2 fs_Texcoord;
in float fs_Depth;
uniform sampler2D u_Texutre;
out vec4 fragColor[4];
void main(void)
{
//normal, position, depth, color
fragColor[0] = vec4(vec3(fs_Depth), 1.0);
fragColor[1] = vec4(normalize(fs_Normal.xyz), 1.0);
fragColor[2] = fs_Position;
fragColor[3] = vec4(texture(u_Texutre, fs_Texcoord).xyz, 1.0);
}
</script>
<script type="x-shader/x-fragment" id = "withoutdrawbufferfs">
precision mediump float;
uniform int u_DrawMode;
varying vec3 fs_Normal;
varying vec4 fs_Position;
varying vec2 fs_Texcoord;
varying float fs_Depth;
uniform sampler2D u_Texutre;
void main(void) {
if(u_DrawMode == 0)
{
gl_FragColor = vec4(normalize(fs_Normal),0.0);
return;
}
else if(u_DrawMode == 1)
{
//diaplay color
gl_FragColor = vec4(texture2D(u_Texutre, fs_Texcoord).xyz, 1.0);
return;
}
else if(u_DrawMode == 2)
{
//display screen space position
gl_FragColor = vec4(fs_Position.xyz,1.0);
return;
}
}
</script>
<script id="webgl2_shade_vs" type="x-shader/x-vertex">#version 300 es
precision highp float;
in vec3 Position;
in vec2 Texcoord;
out vec2 fs_Texcoord;
void main() {
fs_Texcoord = Texcoord * 0.5 + vec2(0.5);
gl_Position = vec4(Position,1.0);
}
</script>
<script id="shade_vs" type="x-shader/x-vertex">
precision highp float;
attribute vec3 Position;
attribute vec2 Texcoord;
varying vec2 fs_Texcoord;
void main() {
fs_Texcoord = Texcoord * 0.5 + vec2(0.5);
gl_Position = vec4(Position,1.0);
}
</script>
<script id="light_fs" type="x-shader/x-fragment">
precision highp float;
#define DISPLAY_DEPTH 0
#define DISPLAY_NORMAL 1
#define DISPLAY_POSITION 2
#define DISPLAY_COLOR 3
#define DISPLAY_TOTAL 4
#define DISPLAY_LIGHTS 5
#define DISPLAY_NONTILE_LIGHTS 6
#define DISPLAY_INK 7
#define DISPLAY_DEBUGTILE 8
#define MAXLIGHTNUM 1000
uniform sampler2D u_Depthtex;
uniform sampler2D u_Normaltex;
uniform sampler2D u_Positiontex;
uniform sampler2D u_Colortex;
//for light
uniform sampler2D u_LightGridtex;
uniform sampler2D u_LightIndextex;
uniform sampler2D u_LightPositiontex;
uniform sampler2D u_LightColorRadiustex;
uniform int u_LightNum;
uniform int u_DisplayType;
uniform float u_Far;
uniform float u_Near;
uniform float u_Width;
uniform float u_Height;
uniform int u_MaxTileLightNum;
uniform int u_TileSize;
uniform int u_LightIndexImageSize;
uniform float u_FloatLightIndexSize;
uniform float u_WidthTile;
uniform float u_HeightTile;
varying vec2 fs_Texcoord;
float linearizeDepth(float exp_depth, float near, float far) {
return (2.0 * near) / (far + near - exp_depth * (far - near));
}
void main(void)
{
vec3 normal = texture2D(u_Normaltex, fs_Texcoord).xyz;
vec3 position = texture2D(u_Positiontex, fs_Texcoord).xyz;
vec3 color = texture2D(u_Colortex, fs_Texcoord).xyz;
if(u_DisplayType == DISPLAY_LIGHTS || u_DisplayType == DISPLAY_INK || u_DisplayType == DISPLAY_DEBUGTILE){
//get the grid data index
vec2 gridIndex = vec2(((fs_Texcoord.x*u_Width) / float(u_TileSize)) / (u_WidthTile), ((fs_Texcoord.y*u_Height) / float(u_TileSize)) / (u_HeightTile));
//x for offset, y for count
vec3 gridInfo = texture2D(u_LightGridtex, gridIndex).xyz;
int offset = int(gridInfo.x);
int count = int(gridInfo.y);
int offset2 = int(gridInfo.z);
vec3 lightColor = vec3(0.0);
int lightCount = 0;
if(count > 0){
for(int i = 0; i < MAXLIGHTNUM; i++){
if(i < count){
int lightId;
float temp = float(offset + i);
int addNext = 0;
if(temp >= u_FloatLightIndexSize){
temp -= u_FloatLightIndexSize;
addNext++;
}
//precision problem
if(temp == u_FloatLightIndexSize)
temp = 0.0;
float lightSize = u_FloatLightIndexSize-1.0;
vec2 dataIndex = vec2(
temp / lightSize,
float(offset2 + addNext) / lightSize
);
lightId = int((texture2D(u_LightIndextex, dataIndex).x));
vec3 lightPos = texture2D(u_LightPositiontex, vec2(float(lightId)/float(u_LightNum-1))).xyz;
vec4 lightColorRadius = texture2D(u_LightColorRadiustex, vec2(float(lightId)/float(u_LightNum-1))).xyzw;
if(distance(lightPos, position) < lightColorRadius.w){
float diffuse = abs(dot(normal, normalize(lightPos - position)));
float dist = distance(lightPos, position);
float lightR = lightColorRadius.w;
float attenuation = dist/(1.0 - (dist/lightR) * (dist/lightR));
attenuation = attenuation / lightR + 1.0;
attenuation = 1.0 / (attenuation * attenuation);
if(u_DisplayType == DISPLAY_INK){
vec3 quaColor = vec3(0.0);
if(diffuse<0.4){
quaColor = vec3(0.4);
}
else if(diffuse < 0.6){
quaColor = vec3(0.6);
}
else if(diffuse < 0.9){
quaColor = vec3(0.78);
}
else if(diffuse < 1.0){
quaColor = vec3(0.89);
}
else
{
quaColor = vec3(1.0,0.0,1.0);
}
lightColor += quaColor * attenuation * lightColorRadius.xyz * diffuse * color; //* ((lightColorRadius.w - distance(lightPos, position)) / lightColorRadius.w);// * lightColorRadius.xyz;
}
else
{
lightColor += diffuse * lightColorRadius.xyz * attenuation* color;// * ((lightColorRadius.w - distance(lightPos, position)) / lightColorRadius.w);
}
lightCount ++;
}
}
else
break;
}
if(u_DisplayType == DISPLAY_DEBUGTILE){
lightColor = vec3(float(count) / float(u_MaxTileLightNum)) ;
}
else
lightColor = mix(vec3(0.0), vec3(1.0), lightColor);
lightColor = clamp(lightColor, vec3(0.0), vec3(1.0));
}
gl_FragColor = vec4(lightColor, 1.0);
}
else
gl_FragColor = vec4(vec3(0.0), 1.0);
}
</script>
<script id="webgl2_light_fs" type="x-shader/x-fragment">#version 300 es
precision highp float;
#define DISPLAY_DEPTH 0
#define DISPLAY_NORMAL 1
#define DISPLAY_POSITION 2
#define DISPLAY_COLOR 3
#define DISPLAY_TOTAL 4
#define DISPLAY_LIGHTS 5
#define DISPLAY_NONTILE_LIGHTS 6
#define DISPLAY_INK 7
#define DISPLAY_DEBUGTILE 8
#define MAXLIGHTNUM 1000
uniform sampler2D u_Depthtex;
uniform sampler2D u_Normaltex;
uniform sampler2D u_Positiontex;
uniform sampler2D u_Colortex;
//for light
uniform sampler2D u_LightGridtex;
uniform sampler2D u_LightIndextex;
uniform sampler2D u_LightPositiontex;
uniform sampler2D u_LightColorRadiustex;
uniform int u_LightNum;
uniform int u_DisplayType;
uniform float u_Far;
uniform float u_Near;
uniform float u_Width;
uniform float u_Height;
uniform int u_MaxTileLightNum;
uniform int u_TileSize;
uniform int u_LightIndexImageSize;
uniform float u_FloatLightIndexSize;
uniform float u_WidthTile;
uniform float u_HeightTile;
in vec2 fs_Texcoord;
out vec4 fragmentColor;
float linearizeDepth(float exp_depth, float near, float far) {
return (2.0 * near) / (far + near - exp_depth * (far - near));
}
void main(void)
{
vec3 normal = texture(u_Normaltex, fs_Texcoord).xyz;
vec3 position = texture(u_Positiontex, fs_Texcoord).xyz;
vec3 color = texture(u_Colortex, fs_Texcoord).xyz;
if(u_DisplayType == DISPLAY_LIGHTS || u_DisplayType == DISPLAY_INK || u_DisplayType == DISPLAY_DEBUGTILE){
//get the grid data index
vec2 gridIndex = vec2(((fs_Texcoord.x*u_Width) / float(u_TileSize)) / (u_WidthTile), ((fs_Texcoord.y*u_Height) / float(u_TileSize)) / (u_HeightTile));
//x for offset, y for count
vec3 gridInfo = texture(u_LightGridtex, gridIndex).xyz;
int offset = int(gridInfo.x);
int count = int(gridInfo.y);
int offset2 = int(gridInfo.z);
vec3 lightColor = vec3(0.0);
int lightCount = 0;
if(count > 0){
for(int i = 0; i < MAXLIGHTNUM; i++){
if(i < count){
int lightId;
float temp = float(offset + i);
int addNext = 0;
if(temp >= u_FloatLightIndexSize){
temp -= u_FloatLightIndexSize;
addNext++;
}
//precision problem
if(temp == u_FloatLightIndexSize)
temp = 0.0;
float lightSize = u_FloatLightIndexSize-1.0;
vec2 dataIndex = vec2(
temp / lightSize,
float(offset2 + addNext) / lightSize
);
lightId = int((texture(u_LightIndextex, dataIndex).x));
vec3 lightPos = texture(u_LightPositiontex, vec2(float(lightId)/float(u_LightNum-1))).xyz;
vec4 lightColorRadius = texture(u_LightColorRadiustex, vec2(float(lightId)/float(u_LightNum-1))).xyzw;
if(distance(lightPos, position) < lightColorRadius.w){
float diffuse = abs(dot(normal, normalize(lightPos - position)));
float dist = distance(lightPos, position);
float lightR = lightColorRadius.w;
float attenuation = dist/(1.0 - (dist/lightR) * (dist/lightR));
attenuation = attenuation / lightR + 1.0;
attenuation = 1.0 / (attenuation * attenuation);
if(u_DisplayType == DISPLAY_INK){
vec3 quaColor = vec3(0.0);
if(diffuse<0.4){
quaColor = vec3(0.4);
}
else if(diffuse < 0.6){
quaColor = vec3(0.6);
}
else if(diffuse < 0.9){
quaColor = vec3(0.78);
}
else if(diffuse < 1.0){
quaColor = vec3(0.89);
}
else
{
quaColor = vec3(1.0,0.0,1.0);
}
lightColor += quaColor * attenuation * lightColorRadius.xyz * diffuse * color; //* ((lightColorRadius.w - distance(lightPos, position)) / lightColorRadius.w);// * lightColorRadius.xyz;
}
else
{
lightColor += diffuse * lightColorRadius.xyz * attenuation* color;// * ((lightColorRadius.w - distance(lightPos, position)) / lightColorRadius.w);
}
lightCount ++;
}
}
else
break;
}
if(u_DisplayType == DISPLAY_DEBUGTILE){
lightColor = vec3(float(count) / float(u_MaxTileLightNum)) ;
}
else
lightColor = mix(vec3(0.0), vec3(1.0), lightColor);
lightColor = clamp(lightColor, vec3(0.0), vec3(1.0));
}
fragmentColor = vec4(lightColor, 1.0);
}
else
fragmentColor = vec4(vec3(0.0), 1.0);
}
</script>
<script id="webgl2_nontilelight_fs" type="x-shader/x-fragment">#version 300 es
precision highp float;
#define DISPLAY_DEPTH 0
#define DISPLAY_NORMAL 1
#define DISPLAY_POSITION 2
#define DISPLAY_COLOR 3
#define DISPLAY_TOTAL 4
#define DISPLAY_TILE_LIGHTS 5
#define DISPLAY_NONTILE_LIGHTS 6
#define DISPLAY_SCISSOR -1
uniform int u_DisplayType;
uniform sampler2D u_Depthtex;
uniform sampler2D u_Normaltex;
uniform sampler2D u_Positiontex;
uniform sampler2D u_Colortex;
uniform vec4 u_Light;
uniform vec3 u_LightColor;
in vec2 fs_Texcoord;
out vec4 fragmentColor;
void main()
{
vec3 normal = texture(u_Normaltex, fs_Texcoord).xyz;
vec3 position = texture(u_Positiontex, fs_Texcoord).xyz;
vec3 color = texture(u_Colortex, fs_Texcoord).xyz;
vec3 light = u_Light.xyz;
float lightRadius = u_Light.w;
if(u_DisplayType == DISPLAY_NONTILE_LIGHTS){
if(distance(light, position) < lightRadius){
float dist = distance(light, position);
float lightR = lightRadius;
float attenuation = dist/(1.0 - (dist/lightR) * (dist/lightR));
attenuation = attenuation / lightR + 1.0;
attenuation = 1.0 / (attenuation * attenuation);
float diffuse = abs(dot(normal, normalize(light - position)));
fragmentColor = vec4(diffuse * u_LightColor * color * attenuation, 1.0);
}
else
fragmentColor = vec4(0.0,0.0,0.0,1.0);
}
else if(u_DisplayType == DISPLAY_SCISSOR){
float dis = distance(light, position) * 2.0;
fragmentColor = vec4(1.0/dis,0.0,0.0, 1.0);
}
else
fragmentColor = vec4(0.0,0.0,0.0, 1.0);
}
</script>
<script id="nontilelight_fs" type="x-shader/x-fragment">
precision highp float;
#define DISPLAY_DEPTH 0
#define DISPLAY_NORMAL 1
#define DISPLAY_POSITION 2
#define DISPLAY_COLOR 3
#define DISPLAY_TOTAL 4
#define DISPLAY_TILE_LIGHTS 5
#define DISPLAY_NONTILE_LIGHTS 6
#define DISPLAY_SCISSOR -1
uniform int u_DisplayType;
uniform sampler2D u_Depthtex;
uniform sampler2D u_Normaltex;
uniform sampler2D u_Positiontex;
uniform sampler2D u_Colortex;
uniform vec4 u_Light;
uniform vec3 u_LightColor;
varying vec2 fs_Texcoord;
void main()
{
vec3 normal = texture2D(u_Normaltex, fs_Texcoord).xyz;
vec3 position = texture2D(u_Positiontex, fs_Texcoord).xyz;
vec3 color = texture2D(u_Colortex, fs_Texcoord).xyz;
vec3 light = u_Light.xyz;
float lightRadius = u_Light.w;
if(u_DisplayType == DISPLAY_NONTILE_LIGHTS){
if(distance(light, position) < lightRadius){
float dist = distance(light, position);
float lightR = lightRadius;
float attenuation = dist/(1.0 - (dist/lightR) * (dist/lightR));
attenuation = attenuation / lightR + 1.0;
attenuation = 1.0 / (attenuation * attenuation);
float diffuse = abs(dot(normal, normalize(light - position)));
gl_FragColor = vec4(diffuse * u_LightColor * color * attenuation, 1.0);
}
else
gl_FragColor = vec4(0.0,0.0,0.0,1.0);
}
else if(u_DisplayType == DISPLAY_SCISSOR){
float dis = distance(light, position) * 2.0;
gl_FragColor = vec4(1.0/dis,0.0,0.0, 1.0);
}
else
gl_FragColor = vec4(0.0,0.0,0.0, 1.0);
}
</script>
<script id="diagnostic_fs" type="x-shader/x-fragment">
precision highp float;
#define DISPLAY_DEPTH 0
#define DISPLAY_NORMAL 1
#define DISPLAY_POSITION 2
#define DISPLAY_COLOR 3
#define DISPLAY_TOTAL 4
#define DISPLAY_LIGHTS 5
uniform sampler2D u_Depthtex;
uniform sampler2D u_Normaltex;
uniform sampler2D u_Positiontex;
uniform sampler2D u_Colortex;
uniform int u_DisplayType;
uniform float u_Far;
uniform float u_Near;
uniform float u_Width;
uniform float u_Height;
uniform vec4 u_Light;
varying vec2 fs_Texcoord;
float linearizeDepth(float exp_depth, float near, float far) {
return (2.0 * near) / (far + near - exp_depth * (far - near));
}
void main(void)
{
vec3 depth = texture2D(u_Depthtex, fs_Texcoord).xyz;
float exp_depth = texture2D(u_Depthtex, fs_Texcoord).r;
float lin_depth = linearizeDepth(exp_depth,u_Near,u_Far);
vec3 normal = normalize(texture2D(u_Normaltex, fs_Texcoord).xyz);
vec3 position = texture2D(u_Positiontex, fs_Texcoord).xyz;
vec3 color = texture2D(u_Colortex, fs_Texcoord).xyz;
vec3 light = u_Light.xyz;//.position.xyz;
//u_Light.xyz;
float diffuse = abs(dot(normal, normalize(light - position)));
if(u_DisplayType == DISPLAY_DEPTH)
gl_FragColor = vec4(vec3(lin_depth) * 30.0, 1.0);
else if(u_DisplayType == DISPLAY_NORMAL)
gl_FragColor = vec4(abs(normal), 1.0);
else if(u_DisplayType == DISPLAY_POSITION)
gl_FragColor = vec4(abs(position)/u_Far * 120.0, 1.0);
else if(u_DisplayType == DISPLAY_COLOR){
gl_FragColor = vec4(color, 1.0);
}
else if(u_DisplayType == DISPLAY_TOTAL)
{
gl_FragColor = vec4(diffuse * color , 1.0);
}
else
gl_FragColor = vec4(vec3(0.0), 1.0);
}
</script>
<script id="ambient_fs" type="x-shader/x-fragment">
precision highp float;
uniform sampler2D u_Depthtex;
uniform sampler2D u_Normaltex;
uniform sampler2D u_Positiontex;
uniform sampler2D u_Colortex;
uniform int u_DisplayType;
uniform vec4 u_Light;
varying vec2 fs_Texcoord;
void main(void)
{
vec3 depth = texture2D(u_Depthtex, fs_Texcoord).xyz;
vec3 normal = normalize(texture2D(u_Normaltex, fs_Texcoord).xyz);
vec3 position = texture2D(u_Positiontex, fs_Texcoord).xyz;
vec3 color = texture2D(u_Colortex, fs_Texcoord).xyz;
vec3 light = u_Light.xyz;
float strength = u_Light.w;
float ambient = 0.09;
float diffuse = abs(dot(normalize(light),(normal)));
gl_FragColor = vec4(color*(strength*diffuse + ambient),1.0);
}
</script>
<script id="post_vs" type="x-shader/x-vertex">
precision highp float;
attribute vec3 Position;
attribute vec2 Texcoord;
varying vec2 fs_Texcoord;
void main() {
fs_Texcoord = Texcoord * 0.5 + vec2(0.5);
gl_Position = vec4(Position,1.0);
}
</script>
<script id="post_fs" type="x-shader/x-fragment">
precision highp float;
#define s2(a,b) temp = a; a= min(a,b); b = max(temp,b);
#define mn3(a, b, c) s2(a, b); s2(a, c);
#define mx3(a, b, c) s2(b, c); s2(a, c);
#define mnmx3(a, b, c) mx3(a, b, c); s2(a, b); // 3 exchanges
#define mnmx4(a, b, c, d) s2(a, b); s2(c, d); s2(a, c); s2(b, d); // 4 exchanges
#define mnmx5(a, b, c, d, e) s2(a, b); s2(c, d); mn3(a, c, e); mx3(b, d, e); // 6 exchanges
#define mnmx6(a, b, c, d, e, f) s2(a, d); s2(b, e); s2(c, f); mn3(a, b, c); mx3(d, e, f); // 7 exchanges
#define DISPLAY_DEPTH 0
#define DISPLAY_NORMAL 1
#define DISPLAY_POSITION 2
#define DISPLAY_COLOR 3
#define DISPLAY_TOTAL 4
#define DISPLAY_LIGHTS 5
#define DISPLAY_NONTILE_LIGHTS 6
#define DISPLAY_INK 7
uniform sampler2D u_Posttex;
uniform sampler2D u_StrokeBlurtex;
uniform int u_DisplayType;
uniform float u_Width;
uniform float u_Height;
varying vec2 fs_Texcoord;
varying vec4 out_Color;
vec3 MedianFilter(sampler2D sampler)
{
vec2 imageSize = vec2(float(u_Width),float(u_Height));
vec2 tex_inv_size = 1.0 / imageSize;
vec3 v[9];
for(int dx = -1; dx <=1; ++dx)
{
for(int dy = -1; dy <=1; ++dy)
{
vec2 offset = vec2(float(dx),float(dy));
v[(dx+1)*3 + (dy+1)] = texture2D(sampler,fs_Texcoord+offset * tex_inv_size).rgb;
}
}
vec3 temp;
// Starting with a subset of size 6, remove the min and max each time
mnmx6(v[0], v[1], v[2], v[3], v[4], v[5]);
mnmx5(v[1], v[2], v[3], v[4], v[6]);
mnmx4(v[2], v[3], v[4], v[7]);
mnmx3(v[3], v[4], v[8]);
return v[4];
}
vec3 colorEnhance(float min,float max,vec3 color)
{
if(color.r == 0.0)
{
return vec3(0.97,0.94,0.85);
}
if(color.r > max)
{
color = vec3(1.0);
}
else if(color.r <= max && color.r >= min)
{
color = vec3((color.r - min) / (max - min));
}
else if(color.r < min)
{
color = vec3(0.0);
}
return color;
}
void main(void)
{
if(u_DisplayType == DISPLAY_INK){
vec3 spattercolor = MedianFilter(u_Posttex);
spattercolor = colorEnhance(0.2,0.98,spattercolor);
vec3 strokecolor = texture2D(u_StrokeBlurtex,fs_Texcoord).rgb;
if(strokecolor.x != 1.0 || strokecolor.y != 1.0 || strokecolor.z != 1.0)
{
gl_FragColor = vec4(min(spattercolor,strokecolor),1.0);
}
else
{
gl_FragColor = vec4(spattercolor,1.0);
}
}
else
{
vec3 out_Color = texture2D(u_Posttex, fs_Texcoord).xyz;
gl_FragColor = vec4(out_Color, 1.0);
}
}
</script>
<script type="x-shader/x-fragment" id = "spatterfs">
precision mediump float;
#define DISPLAY_DEPTH 0
#define DISPLAY_NORMAL 1
#define DISPLAY_POSITION 2
#define DISPLAY_COLOR 3
#define DISPLAY_TOTAL 4
#define DISPLAY_LIGHTS 5
#define DISPLAY_NONTILE_LIGHTS 6
#define DISPLAY_INK 7
uniform int u_DisplayType;
uniform sampler2D u_QuatColorSampler;
uniform int u_Width;
uniform int u_Height;
float rand(vec2 n,float radius)
{
return radius * fract(sin(dot(n.xy, vec2(12.9898, 78.233)))*43758.5453);
}
varying vec2 fs_Texcoord;
float radius = 0.01;
void main(void)
{
if(u_DisplayType == DISPLAY_INK){
vec2 pixelIndex = vec2(fs_Texcoord.x * (float(u_Width) - 1.0),fs_Texcoord.y * (float(u_Height) - 1.0));
float randnum = rand(fs_Texcoord,radius);
pixelIndex += vec2(randnum,0.0);
vec2 newTexcoord = vec2(pixelIndex.x/(float(u_Width)-1.0),pixelIndex.y / (float(u_Height)-1.0));
float randnum2 = rand(newTexcoord,radius);
pixelIndex += vec2(0,randnum2);
newTexcoord = vec2(pixelIndex.x/(float(u_Width)-1.0),pixelIndex.y / (float(u_Height)-1.0));
vec4 newColorSample = texture2D(u_QuatColorSampler,newTexcoord);
newColorSample.w = 1.0;
gl_FragColor = newColorSample;
}
else
{
vec3 out_Color = texture2D(u_QuatColorSampler, fs_Texcoord).rgb;
gl_FragColor = vec4(out_Color, 1.0);
}
return;
}
</script>
<script type="x-shader/x-vertex" id = "siledgevs">
uniform mat4 u_Model;
uniform mat4 u_View;
uniform mat4 u_Persp;
uniform mat4 u_Inverse;
attribute vec3 Position;
varying vec4 vPosition;