forked from google/santa-tracker-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostprocessing.js
More file actions
10856 lines (7673 loc) · 326 KB
/
Copy pathpostprocessing.js
File metadata and controls
10856 lines (7673 loc) · 326 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
/**
* postprocessing v6.9.0 build Sun Dec 08 2019
* https://github.com/vanruesc/postprocessing
* Copyright 2019 Raoul van Rüschen, Zlib
*/
import { ShaderMaterial, Uniform, Vector2, PerspectiveCamera, Scene, OrthographicCamera, Mesh, BufferGeometry, BufferAttribute, WebGLRenderTarget, LinearFilter, RGBFormat, Color, MeshDepthMaterial, RGBADepthPacking, MeshNormalMaterial, DepthTexture, DepthStencilFormat, UnsignedInt248Type, RGBAFormat, RepeatWrapping, NearestFilter, DataTexture, Vector3, Matrix4, Vector4, MeshBasicMaterial, Texture, LinearMipmapLinearFilter, LinearMipMapLinearFilter, Box2 } from '../build/three.module.js';
/**
* The Disposable contract.
*
* Implemented by objects that can free internal resources.
*
* @interface
*/
class Disposable {
/**
* Frees internal resources.
*/
dispose() {}
}
/**
* The initializable contract.
*
* Implemented by objects that can be initialized.
*
* @interface
*/
class Initializable {
/**
* Performs initialization tasks.
*
* @param {WebGLRenderer} renderer - A renderer.
* @param {Boolean} alpha - Whether the renderer uses the alpha channel.
*/
initialize(renderer, alpha) {}
}
var fragmentShader = "uniform sampler2D previousLuminanceBuffer;uniform sampler2D currentLuminanceBuffer;uniform float minLuminance;uniform float deltaTime;uniform float tau;varying vec2 vUv;void main(){float previousLuminance=texture2D(previousLuminanceBuffer,vUv,MIP_LEVEL_1X1).r;float currentLuminance=texture2D(currentLuminanceBuffer,vUv,MIP_LEVEL_1X1).r;previousLuminance=max(minLuminance,previousLuminance);currentLuminance=max(minLuminance,currentLuminance);float adaptedLum=previousLuminance+(currentLuminance-previousLuminance)*(1.0-exp(-deltaTime*tau));gl_FragColor.r=adaptedLum;}";
var vertexShader = "varying vec2 vUv;void main(){vUv=position.xy*0.5+0.5;gl_Position=vec4(position.xy,1.0,1.0);}";
/**
* An adaptive luminance shader material.
*/
class AdaptiveLuminanceMaterial extends ShaderMaterial {
/**
* Constructs a new adaptive luminance material.
*/
constructor() {
super({
type: "AdaptiveLuminanceMaterial",
defines: {
MIP_LEVEL_1X1: "0.0"
},
uniforms: {
previousLuminanceBuffer: new Uniform(null),
currentLuminanceBuffer: new Uniform(null),
minLuminance: new Uniform(0.01),
deltaTime: new Uniform(0.0),
tau: new Uniform(1.0)
},
fragmentShader,
vertexShader,
depthWrite: false,
depthTest: false
});
}
}
var fragmentShader$1 = "uniform sampler2D inputBuffer;varying vec2 vUv;varying vec2 vUv0;varying vec2 vUv1;varying vec2 vUv2;varying vec2 vUv3;varying vec2 vUv4;varying vec2 vUv5;void main(){const vec2 threshold=vec2(EDGE_THRESHOLD);vec4 delta;vec3 c=texture2D(inputBuffer,vUv).rgb;vec3 cLeft=texture2D(inputBuffer,vUv0).rgb;vec3 t=abs(c-cLeft);delta.x=max(max(t.r,t.g),t.b);vec3 cTop=texture2D(inputBuffer,vUv1).rgb;t=abs(c-cTop);delta.y=max(max(t.r,t.g),t.b);vec2 edges=step(threshold,delta.xy);if(dot(edges,vec2(1.0))==0.0){discard;}vec3 cRight=texture2D(inputBuffer,vUv2).rgb;t=abs(c-cRight);delta.z=max(max(t.r,t.g),t.b);vec3 cBottom=texture2D(inputBuffer,vUv3).rgb;t=abs(c-cBottom);delta.w=max(max(t.r,t.g),t.b);float maxDelta=max(max(max(delta.x,delta.y),delta.z),delta.w);vec3 cLeftLeft=texture2D(inputBuffer,vUv4).rgb;t=abs(c-cLeftLeft);delta.z=max(max(t.r,t.g),t.b);vec3 cTopTop=texture2D(inputBuffer,vUv5).rgb;t=abs(c-cTopTop);delta.w=max(max(t.r,t.g),t.b);maxDelta=max(max(maxDelta,delta.z),delta.w);edges*=step(maxDelta,LOCAL_CONTRAST_ADAPTATION_FACTOR*delta.xy);gl_FragColor=vec4(edges,0.0,1.0);}";
var vertexShader$1 = "uniform vec2 texelSize;varying vec2 vUv;varying vec2 vUv0;varying vec2 vUv1;varying vec2 vUv2;varying vec2 vUv3;varying vec2 vUv4;varying vec2 vUv5;void main(){vUv=position.xy*0.5+0.5;vUv0=vUv+texelSize*vec2(-1.0,0.0);vUv1=vUv+texelSize*vec2(0.0,-1.0);vUv2=vUv+texelSize*vec2(1.0,0.0);vUv3=vUv+texelSize*vec2(0.0,1.0);vUv4=vUv+texelSize*vec2(-2.0,0.0);vUv5=vUv+texelSize*vec2(0.0,-2.0);gl_Position=vec4(position.xy,1.0,1.0);}";
/**
* A material that detects edges in a color texture.
*
* Mainly used for Subpixel Morphological Antialiasing.
*/
class ColorEdgesMaterial extends ShaderMaterial {
/**
* Constructs a new color edges material.
*
* @param {Vector2} [texelSize] - The absolute screen texel size.
*/
constructor(texelSize = new Vector2()) {
super({
type: "ColorEdgesMaterial",
defines: {
LOCAL_CONTRAST_ADAPTATION_FACTOR: "2.0",
EDGE_THRESHOLD: "0.1"
},
uniforms: {
inputBuffer: new Uniform(null),
texelSize: new Uniform(texelSize)
},
fragmentShader: fragmentShader$1,
vertexShader: vertexShader$1,
depthWrite: false,
depthTest: false
});
}
/**
* Sets the local contrast adaptation factor.
*
* If there is a neighbor edge that has _factor_ times bigger contrast than
* the current edge, the edge will be discarded.
*
* This allows to eliminate spurious crossing edges and is based on the fact
* that if there is too much contrast in a direction, the perceptual contrast
* in the other neighbors will be hidden.
*
* @param {Number} factor - The local contrast adaptation factor. Default is 2.0.
*/
setLocalContrastAdaptationFactor(factor) {
this.defines.LOCAL_CONTRAST_ADAPTATION_FACTOR = factor.toFixed("2");
this.needsUpdate = true;
}
/**
* Sets the edge detection sensitivity.
*
* A lower value results in more edges being detected at the expense of
* performance.
*
* 0.1 is a reasonable value, and allows to catch most visible edges.
* 0.05 is a rather overkill value, that allows to catch 'em all.
*
* If temporal supersampling is used, 0.2 could be a reasonable value, as low
* contrast edges are properly filtered by just 2x.
*
* @param {Number} threshold - The edge detection sensitivity. Range: [0.05, 0.5].
*/
setEdgeDetectionThreshold(threshold) {
threshold = Math.min(Math.max(threshold, 0.05), 0.5);
this.defines.EDGE_THRESHOLD = threshold.toFixed("2");
this.needsUpdate = true;
}
}
var fragmentShader$2 = "#include <common>\n#include <dithering_pars_fragment>\nuniform sampler2D inputBuffer;varying vec2 vUv0;varying vec2 vUv1;varying vec2 vUv2;varying vec2 vUv3;void main(){vec4 sum=texture2D(inputBuffer,vUv0);sum+=texture2D(inputBuffer,vUv1);sum+=texture2D(inputBuffer,vUv2);sum+=texture2D(inputBuffer,vUv3);gl_FragColor=sum*0.25;\n#include <dithering_fragment>\n}";
var vertexShader$2 = "uniform vec2 texelSize;uniform vec2 halfTexelSize;uniform float kernel;uniform float scale;/*Packing multiple texture coordinates into one varying and using a swizzle toextract them in the fragment shader still causes a dependent texture read.*/varying vec2 vUv0;varying vec2 vUv1;varying vec2 vUv2;varying vec2 vUv3;void main(){vec2 uv=position.xy*0.5+0.5;vec2 dUv=(texelSize*vec2(kernel)+halfTexelSize)*scale;vUv0=vec2(uv.x-dUv.x,uv.y+dUv.y);vUv1=vec2(uv.x+dUv.x,uv.y+dUv.y);vUv2=vec2(uv.x+dUv.x,uv.y-dUv.y);vUv3=vec2(uv.x-dUv.x,uv.y-dUv.y);gl_Position=vec4(position.xy,1.0,1.0);}";
/**
* An optimised convolution shader material.
*
* This material supports dithering.
*
* Based on the GDC2003 Presentation by Masaki Kawase, Bunkasha Games:
* Frame Buffer Postprocessing Effects in DOUBLE-S.T.E.A.L (Wreckless)
* and an article by Filip Strugar, Intel:
* An investigation of fast real-time GPU-based image blur algorithms
*
* Further modified according to Apple's
* [Best Practices for Shaders](https://goo.gl/lmRoM5).
*/
class ConvolutionMaterial extends ShaderMaterial {
/**
* Constructs a new convolution material.
*
* @param {Vector2} [texelSize] - The absolute screen texel size.
*/
constructor(texelSize = new Vector2()) {
super({
type: "ConvolutionMaterial",
uniforms: {
inputBuffer: new Uniform(null),
texelSize: new Uniform(new Vector2()),
halfTexelSize: new Uniform(new Vector2()),
kernel: new Uniform(0.0),
scale: new Uniform(1.0)
},
fragmentShader: fragmentShader$2,
vertexShader: vertexShader$2,
depthWrite: false,
depthTest: false
});
this.setTexelSize(texelSize.x, texelSize.y);
/**
* The current kernel size.
*
* @type {KernelSize}
*/
this.kernelSize = KernelSize.LARGE;
}
/**
* Returns the kernel.
*
* @return {Float32Array} The kernel.
*/
getKernel() {
return kernelPresets[this.kernelSize];
}
/**
* Sets the texel size.
*
* @param {Number} x - The texel width.
* @param {Number} y - The texel height.
*/
setTexelSize(x, y) {
this.uniforms.texelSize.value.set(x, y);
this.uniforms.halfTexelSize.value.set(x, y).multiplyScalar(0.5);
}
}
/**
* The Kawase blur kernel presets.
*
* @type {Float32Array[]}
* @private
*/
const kernelPresets = [
new Float32Array([0.0, 0.0]),
new Float32Array([0.0, 1.0, 1.0]),
new Float32Array([0.0, 1.0, 1.0, 2.0]),
new Float32Array([0.0, 1.0, 2.0, 2.0, 3.0]),
new Float32Array([0.0, 1.0, 2.0, 3.0, 4.0, 4.0, 5.0]),
new Float32Array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 7.0, 8.0, 9.0, 10.0])
];
/**
* A kernel size enumeration.
*
* @type {Object}
* @property {Number} VERY_SMALL - A very small kernel that matches a 7x7 Gauss blur kernel.
* @property {Number} SMALL - A small kernel that matches a 15x15 Gauss blur kernel.
* @property {Number} MEDIUM - A medium sized kernel that matches a 23x23 Gauss blur kernel.
* @property {Number} LARGE - A large kernel that matches a 35x35 Gauss blur kernel.
* @property {Number} VERY_LARGE - A very large kernel that matches a 63x63 Gauss blur kernel.
* @property {Number} HUGE - A huge kernel that matches a 127x127 Gauss blur kernel.
*/
const KernelSize = {
VERY_SMALL: 0,
SMALL: 1,
MEDIUM: 2,
LARGE: 3,
VERY_LARGE: 4,
HUGE: 5
};
var fragmentShader$3 = "uniform sampler2D inputBuffer;uniform float opacity;varying vec2 vUv;void main(){vec4 texel=texture2D(inputBuffer,vUv);gl_FragColor=opacity*texel;}";
/**
* A simple copy shader material.
*/
class CopyMaterial extends ShaderMaterial {
/**
* Constructs a new copy material.
*/
constructor() {
super({
type: "CopyMaterial",
uniforms: {
inputBuffer: new Uniform(null),
opacity: new Uniform(1.0)
},
fragmentShader: fragmentShader$3,
vertexShader,
depthWrite: false,
depthTest: false
});
}
}
var fragmentShader$4 = "#include <packing>\n#include <clipping_planes_pars_fragment>\nuniform sampler2D depthBuffer;uniform float cameraNear;uniform float cameraFar;varying float vViewZ;varying vec4 vProjTexCoord;void main(){\n#include <clipping_planes_fragment>\nvec2 projTexCoord=(vProjTexCoord.xy/vProjTexCoord.w)*0.5+0.5;projTexCoord=clamp(projTexCoord,0.002,0.998);float fragCoordZ=unpackRGBAToDepth(texture2D(depthBuffer,projTexCoord));\n#ifdef PERSPECTIVE_CAMERA\nfloat viewZ=perspectiveDepthToViewZ(fragCoordZ,cameraNear,cameraFar);\n#else\nfloat viewZ=orthographicDepthToViewZ(fragCoordZ,cameraNear,cameraFar);\n#endif\nfloat depthTest=(-vViewZ>-viewZ)? 1.0 : 0.0;gl_FragColor.rg=vec2(0.0,depthTest);}";
var vertexShader$3 = "#include <common>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvarying float vViewZ;varying vec4 vProjTexCoord;void main(){\n#include <skinbase_vertex>\n#include <begin_vertex>\n#include <morphtarget_vertex>\n#include <skinning_vertex>\n#include <project_vertex>\nvViewZ=mvPosition.z;vProjTexCoord=gl_Position;\n#include <clipping_planes_vertex>\n}";
/**
* A depth comparison shader material.
*/
class DepthComparisonMaterial extends ShaderMaterial {
/**
* Constructs a new depth comparison material.
*
* @param {Texture} [depthTexture=null] - A depth texture.
* @param {PerspectiveCamera} [camera] - A camera.
*/
constructor(depthTexture = null, camera) {
super({
type: "DepthComparisonMaterial",
uniforms: {
depthBuffer: new Uniform(depthTexture),
cameraNear: new Uniform(0.3),
cameraFar: new Uniform(1000)
},
fragmentShader: fragmentShader$4,
vertexShader: vertexShader$3,
depthWrite: false,
depthTest: false,
morphTargets: true,
skinning: true
});
this.adoptCameraSettings(camera);
}
/**
* Adopts the settings of the given camera.
*
* @param {Camera} [camera=null] - A camera.
*/
adoptCameraSettings(camera = null) {
if(camera !== null) {
this.uniforms.cameraNear.value = camera.near;
this.uniforms.cameraFar.value = camera.far;
if(camera instanceof PerspectiveCamera) {
this.defines.PERSPECTIVE_CAMERA = "1";
} else {
delete this.defines.PERSPECTIVE_CAMERA;
}
}
}
}
var fragmentShader$5 = "#include <common>\n#include <packing>\nuniform sampler2D depthBuffer0;uniform sampler2D depthBuffer1;uniform sampler2D inputBuffer;varying vec2 vUv;void main(){\n#if DEPTH_PACKING_0 == 3201\nfloat d0=unpackRGBAToDepth(texture2D(depthBuffer0,vUv));\n#else\nfloat d0=texture2D(depthBuffer0,vUv).r;\n#endif\n#if DEPTH_PACKING_1 == 3201\nfloat d1=unpackRGBAToDepth(texture2D(depthBuffer1,vUv));\n#else\nfloat d1=texture2D(depthBuffer1,vUv).r;\n#endif\nif(d0<d1){discard;}gl_FragColor=texture2D(inputBuffer,vUv);}";
/**
* A depth mask shader material.
*
* This material masks a color buffer by comparing two depth textures.
*/
class DepthMaskMaterial extends ShaderMaterial {
/**
* Constructs a new depth mask material.
*/
constructor() {
super({
type: "DepthMaskMaterial",
defines: {
DEPTH_PACKING_0: "0",
DEPTH_PACKING_1: "0"
},
uniforms: {
depthBuffer0: new Uniform(null),
depthBuffer1: new Uniform(null),
inputBuffer: new Uniform(null)
},
fragmentShader: fragmentShader$5,
vertexShader,
depthWrite: false,
depthTest: false
});
}
}
var fragmentTemplate = "#include <common>\n#include <packing>\n#include <dithering_pars_fragment>\nuniform sampler2D inputBuffer;uniform sampler2D depthBuffer;uniform vec2 resolution;uniform vec2 texelSize;uniform float cameraNear;uniform float cameraFar;uniform float aspect;uniform float time;varying vec2 vUv;float readDepth(const in vec2 uv){\n#if DEPTH_PACKING == 3201\nreturn unpackRGBAToDepth(texture2D(depthBuffer,uv));\n#else\nreturn texture2D(depthBuffer,uv).r;\n#endif\n}float getViewZ(const in float depth){\n#ifdef PERSPECTIVE_CAMERA\nreturn perspectiveDepthToViewZ(depth,cameraNear,cameraFar);\n#else\nreturn orthographicDepthToViewZ(depth,cameraNear,cameraFar);\n#endif\n}FRAGMENT_HEADvoid main(){FRAGMENT_MAIN_UVvec4 color0=texture2D(inputBuffer,UV);vec4 color1=vec4(0.0);FRAGMENT_MAIN_IMAGEgl_FragColor=color0;\n#include <dithering_fragment>\n}";
var vertexTemplate = "uniform vec2 resolution;uniform vec2 texelSize;uniform float cameraNear;uniform float cameraFar;uniform float aspect;uniform float time;varying vec2 vUv;VERTEX_HEADvoid main(){vUv=position.xy*0.5+0.5;VERTEX_MAIN_SUPPORTgl_Position=vec4(position.xy,1.0,1.0);}";
/**
* An effect material for compound shaders.
*
* This material supports dithering.
*
* @implements {Resizable}
*/
class EffectMaterial extends ShaderMaterial {
/**
* Constructs a new effect material.
*
* @param {Map<String, String>} [shaderParts=null] - A collection of shader snippets. See {@link Section}.
* @param {Map<String, String>} [defines=null] - A collection of preprocessor macro definitions.
* @param {Map<String, Uniform>} [uniforms=null] - A collection of uniforms.
* @param {Camera} [camera=null] - A camera.
* @param {Boolean} [dithering=false] - Whether dithering should be enabled.
*/
constructor(shaderParts = null, defines = null, uniforms = null, camera = null, dithering = false) {
super({
type: "EffectMaterial",
defines: {
DEPTH_PACKING: "0"
},
uniforms: {
inputBuffer: new Uniform(null),
depthBuffer: new Uniform(null),
resolution: new Uniform(new Vector2()),
texelSize: new Uniform(new Vector2()),
cameraNear: new Uniform(0.3),
cameraFar: new Uniform(1000.0),
aspect: new Uniform(1.0),
time: new Uniform(0.0)
},
depthWrite: false,
depthTest: false,
dithering
});
if(shaderParts !== null) {
this.setShaderParts(shaderParts);
}
if(defines !== null) {
this.setDefines(defines);
}
if(uniforms !== null) {
this.setUniforms(uniforms);
}
this.adoptCameraSettings(camera);
}
/**
* The current depth packing.
*
* @type {Number}
*/
get depthPacking() {
return Number.parseInt(this.defines.DEPTH_PACKING);
}
/**
* Sets the depth packing.
*
* Use `BasicDepthPacking` or `RGBADepthPacking` if your depth texture
* contains packed depth.
*
* You'll need to call {@link EffectPass#recompile} after changing this value.
*
* @type {Number}
*/
set depthPacking(value) {
this.defines.DEPTH_PACKING = value.toFixed(0);
}
/**
* Sets the shader parts.
*
* @param {Map<String, String>} shaderParts - A collection of shader snippets. See {@link Section}.
* @return {EffectMaterial} This material.
*/
setShaderParts(shaderParts) {
this.fragmentShader = fragmentTemplate.replace(Section.FRAGMENT_HEAD, shaderParts.get(Section.FRAGMENT_HEAD))
.replace(Section.FRAGMENT_MAIN_UV, shaderParts.get(Section.FRAGMENT_MAIN_UV))
.replace(Section.FRAGMENT_MAIN_IMAGE, shaderParts.get(Section.FRAGMENT_MAIN_IMAGE));
this.vertexShader = vertexTemplate.replace(Section.VERTEX_HEAD, shaderParts.get(Section.VERTEX_HEAD))
.replace(Section.VERTEX_MAIN_SUPPORT, shaderParts.get(Section.VERTEX_MAIN_SUPPORT));
this.needsUpdate = true;
return this;
}
/**
* Sets the shader macros.
*
* @param {Map<String, String>} defines - A collection of preprocessor macro definitions.
* @return {EffectMaterial} This material.
*/
setDefines(defines) {
for(const entry of defines.entries()) {
this.defines[entry[0]] = entry[1];
}
this.needsUpdate = true;
return this;
}
/**
* Sets the shader uniforms.
*
* @param {Map<String, Uniform>} uniforms - A collection of uniforms.
* @return {EffectMaterial} This material.
*/
setUniforms(uniforms) {
for(const entry of uniforms.entries()) {
this.uniforms[entry[0]] = entry[1];
}
this.needsUpdate = true;
return this;
}
/**
* Adopts the settings of the given camera.
*
* @param {Camera} [camera=null] - A camera.
*/
adoptCameraSettings(camera = null) {
if(camera !== null) {
this.uniforms.cameraNear.value = camera.near;
this.uniforms.cameraFar.value = camera.far;
if(camera instanceof PerspectiveCamera) {
this.defines.PERSPECTIVE_CAMERA = "1";
} else {
delete this.defines.PERSPECTIVE_CAMERA;
}
this.needsUpdate = true;
}
}
/**
* Sets the resolution.
*
* @param {Number} width - The width.
* @param {Number} height - The height.
*/
setSize(width, height) {
width = Math.max(width, 1.0);
height = Math.max(height, 1.0);
this.uniforms.resolution.value.set(width, height);
this.uniforms.texelSize.value.set(1.0 / width, 1.0 / height);
this.uniforms.aspect.value = width / height;
}
}
/**
* An enumeration of shader code placeholders used by the {@link EffectPass}.
*
* @type {Object}
* @property {String} FRAGMENT_HEAD - A placeholder for function and variable declarations inside the fragment shader.
* @property {String} FRAGMENT_MAIN_UV - A placeholder for UV transformations inside the fragment shader.
* @property {String} FRAGMENT_MAIN_IMAGE - A placeholder for color calculations inside the fragment shader.
* @property {String} VERTEX_HEAD - A placeholder for function and variable declarations inside the vertex shader.
* @property {String} VERTEX_MAIN_SUPPORT - A placeholder for supporting calculations inside the vertex shader.
*/
const Section = {
FRAGMENT_HEAD: "FRAGMENT_HEAD",
FRAGMENT_MAIN_UV: "FRAGMENT_MAIN_UV",
FRAGMENT_MAIN_IMAGE: "FRAGMENT_MAIN_IMAGE",
VERTEX_HEAD: "VERTEX_HEAD",
VERTEX_MAIN_SUPPORT: "VERTEX_MAIN_SUPPORT"
};
var fragmentShader$6 = "#include <common>\n#include <dithering_pars_fragment>\nuniform sampler2D inputBuffer;uniform vec2 lightPosition;uniform float exposure;uniform float decay;uniform float density;uniform float weight;uniform float clampMax;varying vec2 vUv;void main(){vec2 coord=vUv;vec2 delta=coord-lightPosition;delta*=1.0/SAMPLES_FLOAT*density;float illuminationDecay=1.0;vec4 texel;vec4 color=vec4(0.0);/*Estimate the probability of occlusion at each pixel by summing samplesalong a ray to the light position.*/for(int i=0;i<SAMPLES_INT;++i){coord-=delta;texel=texture2D(inputBuffer,coord);texel*=illuminationDecay*weight;color+=texel;illuminationDecay*=decay;}gl_FragColor=clamp(color*exposure,0.0,clampMax);\n#include <dithering_fragment>\n}";
/**
* A crepuscular rays shader material.
*
* This material supports dithering.
*
* References:
*
* Thibaut Despoulain, 2012:
* [(WebGL) Volumetric Light Approximation in Three.js](
* http://bkcore.com/blog/3d/webgl-three-js-volumetric-light-godrays.html)
*
* Nvidia, GPU Gems 3, 2008:
* [Chapter 13. Volumetric Light Scattering as a Post-Process](
* https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch13.html)
*/
class GodRaysMaterial extends ShaderMaterial {
/**
* Constructs a new god rays material.
*
* @param {Vector2} lightPosition - The light position in screen space.
*/
constructor(lightPosition) {
super({
type: "GodRaysMaterial",
defines: {
SAMPLES_INT: "60",
SAMPLES_FLOAT: "60.0"
},
uniforms: {
inputBuffer: new Uniform(null),
lightPosition: new Uniform(lightPosition),
density: new Uniform(1.0),
decay: new Uniform(1.0),
weight: new Uniform(1.0),
exposure: new Uniform(1.0),
clampMax: new Uniform(1.0)
},
fragmentShader: fragmentShader$6,
vertexShader,
depthWrite: false,
depthTest: false
});
}
/**
* The amount of samples per pixel.
*
* @type {Number}
*/
get samples() {
return Number.parseInt(this.defines.SAMPLES_INT);
}
/**
* Sets the amount of samples per pixel.
*
* @type {Number}
*/
set samples(value) {
value = Math.floor(value);
this.defines.SAMPLES_INT = value.toFixed(0);
this.defines.SAMPLES_FLOAT = value.toFixed(1);
this.needsUpdate = true;
}
}
var fragmentShader$7 = "#include <common>\nuniform sampler2D inputBuffer;\n#ifdef RANGE\nuniform vec2 range;\n#elif defined(THRESHOLD)\nuniform float threshold;uniform float smoothing;\n#endif\nvarying vec2 vUv;void main(){vec4 texel=texture2D(inputBuffer,vUv);float l=linearToRelativeLuminance(texel.rgb);\n#ifdef RANGE\nfloat low=step(range.x,l);float high=step(l,range.y);l*=low*high;\n#elif defined(THRESHOLD)\nl=smoothstep(threshold,threshold+smoothing,l);\n#endif\n#ifdef COLOR\ngl_FragColor=vec4(texel.rgb*l,l);\n#else\ngl_FragColor=vec4(l);\n#endif\n}";
/**
* A luminance shader material.
*
* This shader produces a greyscale luminance map that describes the absolute
* amount of light emitted by a scene. It can also be configured to output
* colours that are scaled with their respective luminance value. Additionally,
* a range may be provided to mask out undesired texels.
*
* The alpha channel always contains the luminance value.
*
* On luminance coefficients:
* http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html#RTFToC9
*
* Coefficients for different colour spaces:
* https://hsto.org/getpro/habr/post_images/2ab/69d/084/2ab69d084f9a597e032624bcd74d57a7.png
*
* Luminance range reference:
* https://cycling74.com/2007/05/23/your-first-shader/#.Vty9FfkrL4Z
*/
class LuminanceMaterial extends ShaderMaterial {
/**
* Constructs a new luminance material.
*
* @param {Boolean} [colorOutput=false] - Defines whether the shader should output colors scaled with their luminance value.
* @param {Vector2} [luminanceRange] - If provided, the shader will mask out texels that aren't in the specified luminance range.
*/
constructor(colorOutput = false, luminanceRange = null) {
const useRange = (luminanceRange !== null);
super({
type: "LuminanceMaterial",
uniforms: {
inputBuffer: new Uniform(null),
threshold: new Uniform(0.0),
smoothing: new Uniform(1.0),
range: new Uniform(useRange ? luminanceRange : new Vector2())
},
fragmentShader: fragmentShader$7,
vertexShader,
depthWrite: false,
depthTest: false
});
this.colorOutput = colorOutput;
this.useThreshold = true;
this.useRange = useRange;
}
/**
* The luminance threshold.
*
* @type {Number}
*/
get threshold() {
return this.uniforms.threshold.value;
}
/**
* Sets the luminance threshold.
*
* @type {Number}
*/
set threshold(value) {
this.uniforms.threshold.value = value;
}
/**
* The luminance threshold smoothing.
*
* @type {Number}
*/
get smoothing() {
return this.uniforms.smoothing.value;
}
/**
* Sets the luminance threshold smoothing.
*
* @type {Number}
*/
set smoothing(value) {
this.uniforms.smoothing.value = value;
}
/**
* Indicates whether the luminance threshold is enabled.
*
* @type {Boolean}
*/
get useThreshold() {
return (this.defines.THRESHOLD !== undefined);
}
/**
* Enables or disables the luminance threshold.
*
* @type {Boolean}
*/
set useThreshold(value) {
value ? (this.defines.THRESHOLD = "1") : (delete this.defines.THRESHOLD);
this.needsUpdate = true;
}
/**
* Indicates whether color output is enabled.
*
* @type {Boolean}
*/
get colorOutput() {
return (this.defines.COLOR !== undefined);
}
/**
* Enables or disables color output.
*
* @type {Boolean}
*/
set colorOutput(value) {
value ? (this.defines.COLOR = "1") : (delete this.defines.COLOR);
this.needsUpdate = true;
}
/**
* Enables or disables color output.
*
* @deprecated Use colorOutput instead.
* @param {Boolean} enabled - Whether color output should be enabled.
*/
setColorOutputEnabled(enabled) {
enabled ? (this.defines.COLOR = "1") : (delete this.defines.COLOR);
this.needsUpdate = true;
}
/**
* Indicates whether luminance masking is enabled.
*
* @type {Boolean}
*/
get useRange() {
return (this.defines.RANGE !== undefined);
}
/**