Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/libs/ShaderExtras.js
4626 views
1
/**
2
* @author alteredq / http://alteredqualia.com/
3
* @author zz85 / http://www.lab4games.net/zz85/blog
4
* @author davidedc / http://www.sketchpatch.net/
5
*
6
* ShaderExtras currently contains:
7
*
8
* screen
9
* convolution
10
* film
11
* bokeh
12
* sepia
13
* dotscreen
14
* vignette
15
* bleachbypass
16
* basic
17
* dofmipmap
18
* focus
19
* triangleBlur
20
* horizontalBlur + verticalBlur
21
* horizontalTiltShift + verticalTiltShift
22
* blend
23
* fxaa
24
* luminosity
25
* colorCorrection
26
* normalmap
27
* ssao
28
* colorify
29
* unpackDepthRGBA
30
*/
31
32
THREE.ShaderExtras = {
33
34
/* -------------------------------------------------------------------------
35
// Full-screen textured quad shader
36
------------------------------------------------------------------------- */
37
38
'screen': {
39
40
uniforms: {
41
42
tDiffuse: { type: "t", value: 0, texture: null },
43
opacity: { type: "f", value: 1.0 }
44
45
},
46
47
vertexShader: [
48
49
"varying vec2 vUv;",
50
51
"void main() {",
52
53
"vUv = uv;",
54
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
55
56
"}"
57
58
].join("\n"),
59
60
fragmentShader: [
61
62
"uniform float opacity;",
63
64
"uniform sampler2D tDiffuse;",
65
66
"varying vec2 vUv;",
67
68
"void main() {",
69
70
"vec4 texel = texture2D( tDiffuse, vUv );",
71
"gl_FragColor = opacity * texel;",
72
73
"}"
74
75
].join("\n")
76
77
},
78
79
/* ------------------------------------------------------------------------
80
// Convolution shader
81
// - ported from o3d sample to WebGL / GLSL
82
// http://o3d.googlecode.com/svn/trunk/samples/convolution.html
83
------------------------------------------------------------------------ */
84
85
'convolution': {
86
87
uniforms: {
88
89
"tDiffuse" : { type: "t", value: 0, texture: null },
90
"uImageIncrement" : { type: "v2", value: new THREE.Vector2( 0.001953125, 0.0 ) },
91
"cKernel" : { type: "fv1", value: [] }
92
93
},
94
95
vertexShader: [
96
97
//"#define KERNEL_SIZE 25.0",
98
99
"uniform vec2 uImageIncrement;",
100
101
"varying vec2 vUv;",
102
103
"void main() {",
104
105
"vUv = uv - ( ( KERNEL_SIZE - 1.0 ) / 2.0 ) * uImageIncrement;",
106
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
107
108
"}"
109
110
].join("\n"),
111
112
fragmentShader: [
113
114
//"#define KERNEL_SIZE 25",
115
"uniform float cKernel[ KERNEL_SIZE ];",
116
117
"uniform sampler2D tDiffuse;",
118
"uniform vec2 uImageIncrement;",
119
120
"varying vec2 vUv;",
121
122
"void main() {",
123
124
"vec2 imageCoord = vUv;",
125
"vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );",
126
127
"for( int i = 0; i < KERNEL_SIZE; i ++ ) {",
128
129
"sum += texture2D( tDiffuse, imageCoord ) * cKernel[ i ];",
130
"imageCoord += uImageIncrement;",
131
132
"}",
133
134
"gl_FragColor = sum;",
135
136
"}"
137
138
139
].join("\n")
140
141
},
142
143
/* -------------------------------------------------------------------------
144
145
// Film grain & scanlines shader
146
147
// - ported from HLSL to WebGL / GLSL
148
// http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html
149
150
// Screen Space Static Postprocessor
151
//
152
// Produces an analogue noise overlay similar to a film grain / TV static
153
//
154
// Original implementation and noise algorithm
155
// Pat 'Hawthorne' Shearon
156
//
157
// Optimized scanlines + noise version with intensity scaling
158
// Georg 'Leviathan' Steinrohder
159
160
// This version is provided under a Creative Commons Attribution 3.0 License
161
// http://creativecommons.org/licenses/by/3.0/
162
------------------------------------------------------------------------- */
163
164
'film': {
165
166
uniforms: {
167
168
tDiffuse: { type: "t", value: 0, texture: null },
169
time: { type: "f", value: 0.0 },
170
nIntensity: { type: "f", value: 0.5 },
171
sIntensity: { type: "f", value: 0.05 },
172
sCount: { type: "f", value: 4096 },
173
grayscale: { type: "i", value: 1 }
174
175
},
176
177
vertexShader: [
178
179
"varying vec2 vUv;",
180
181
"void main() {",
182
183
"vUv = uv;",
184
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
185
186
"}"
187
188
].join("\n"),
189
190
fragmentShader: [
191
192
// control parameter
193
"uniform float time;",
194
195
"uniform bool grayscale;",
196
197
// noise effect intensity value (0 = no effect, 1 = full effect)
198
"uniform float nIntensity;",
199
200
// scanlines effect intensity value (0 = no effect, 1 = full effect)
201
"uniform float sIntensity;",
202
203
// scanlines effect count value (0 = no effect, 4096 = full effect)
204
"uniform float sCount;",
205
206
"uniform sampler2D tDiffuse;",
207
208
"varying vec2 vUv;",
209
210
"void main() {",
211
212
// sample the source
213
"vec4 cTextureScreen = texture2D( tDiffuse, vUv );",
214
215
// make some noise
216
"float x = vUv.x * vUv.y * time * 1000.0;",
217
"x = mod( x, 13.0 ) * mod( x, 123.0 );",
218
"float dx = mod( x, 0.01 );",
219
220
// add noise
221
"vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx * 100.0, 0.0, 1.0 );",
222
223
// get us a sine and cosine
224
"vec2 sc = vec2( sin( vUv.y * sCount ), cos( vUv.y * sCount ) );",
225
226
// add scanlines
227
"cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity;",
228
229
// interpolate between source and result by intensity
230
"cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );",
231
232
// convert to grayscale if desired
233
"if( grayscale ) {",
234
235
"cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );",
236
237
"}",
238
239
"gl_FragColor = vec4( cResult, cTextureScreen.a );",
240
241
"}"
242
243
].join("\n")
244
245
},
246
247
248
/* -------------------------------------------------------------------------
249
// Depth-of-field shader with bokeh
250
// ported from GLSL shader by Martins Upitis
251
// http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html
252
------------------------------------------------------------------------- */
253
254
'bokeh' : {
255
256
uniforms: { tColor: { type: "t", value: 0, texture: null },
257
tDepth: { type: "t", value: 1, texture: null },
258
focus: { type: "f", value: 1.0 },
259
aspect: { type: "f", value: 1.0 },
260
aperture: { type: "f", value: 0.025 },
261
maxblur: { type: "f", value: 1.0 }
262
},
263
264
vertexShader: [
265
266
"varying vec2 vUv;",
267
268
"void main() {",
269
270
"vUv = uv;",
271
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
272
273
"}"
274
275
].join("\n"),
276
277
fragmentShader: [
278
279
"varying vec2 vUv;",
280
281
"uniform sampler2D tColor;",
282
"uniform sampler2D tDepth;",
283
284
"uniform float maxblur;", // max blur amount
285
"uniform float aperture;", // aperture - bigger values for shallower depth of field
286
287
"uniform float focus;",
288
"uniform float aspect;",
289
290
"void main() {",
291
292
"vec2 aspectcorrect = vec2( 1.0, aspect );",
293
294
"vec4 depth1 = texture2D( tDepth, vUv );",
295
296
"float factor = depth1.x - focus;",
297
298
"vec2 dofblur = vec2 ( clamp( factor * aperture, -maxblur, maxblur ) );",
299
300
"vec2 dofblur9 = dofblur * 0.9;",
301
"vec2 dofblur7 = dofblur * 0.7;",
302
"vec2 dofblur4 = dofblur * 0.4;",
303
304
"vec4 col = vec4( 0.0 );",
305
306
"col += texture2D( tColor, vUv.xy );",
307
"col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur );",
308
"col += texture2D( tColor, vUv.xy + ( vec2( 0.15, 0.37 ) * aspectcorrect ) * dofblur );",
309
"col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur );",
310
"col += texture2D( tColor, vUv.xy + ( vec2( -0.37, 0.15 ) * aspectcorrect ) * dofblur );",
311
"col += texture2D( tColor, vUv.xy + ( vec2( 0.40, 0.0 ) * aspectcorrect ) * dofblur );",
312
"col += texture2D( tColor, vUv.xy + ( vec2( 0.37, -0.15 ) * aspectcorrect ) * dofblur );",
313
"col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur );",
314
"col += texture2D( tColor, vUv.xy + ( vec2( -0.15, -0.37 ) * aspectcorrect ) * dofblur );",
315
"col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur );",
316
"col += texture2D( tColor, vUv.xy + ( vec2( -0.15, 0.37 ) * aspectcorrect ) * dofblur );",
317
"col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur );",
318
"col += texture2D( tColor, vUv.xy + ( vec2( 0.37, 0.15 ) * aspectcorrect ) * dofblur );",
319
"col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur );",
320
"col += texture2D( tColor, vUv.xy + ( vec2( -0.37, -0.15 ) * aspectcorrect ) * dofblur );",
321
"col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur );",
322
"col += texture2D( tColor, vUv.xy + ( vec2( 0.15, -0.37 ) * aspectcorrect ) * dofblur );",
323
324
"col += texture2D( tColor, vUv.xy + ( vec2( 0.15, 0.37 ) * aspectcorrect ) * dofblur9 );",
325
"col += texture2D( tColor, vUv.xy + ( vec2( -0.37, 0.15 ) * aspectcorrect ) * dofblur9 );",
326
"col += texture2D( tColor, vUv.xy + ( vec2( 0.37, -0.15 ) * aspectcorrect ) * dofblur9 );",
327
"col += texture2D( tColor, vUv.xy + ( vec2( -0.15, -0.37 ) * aspectcorrect ) * dofblur9 );",
328
"col += texture2D( tColor, vUv.xy + ( vec2( -0.15, 0.37 ) * aspectcorrect ) * dofblur9 );",
329
"col += texture2D( tColor, vUv.xy + ( vec2( 0.37, 0.15 ) * aspectcorrect ) * dofblur9 );",
330
"col += texture2D( tColor, vUv.xy + ( vec2( -0.37, -0.15 ) * aspectcorrect ) * dofblur9 );",
331
"col += texture2D( tColor, vUv.xy + ( vec2( 0.15, -0.37 ) * aspectcorrect ) * dofblur9 );",
332
333
"col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur7 );",
334
"col += texture2D( tColor, vUv.xy + ( vec2( 0.40, 0.0 ) * aspectcorrect ) * dofblur7 );",
335
"col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur7 );",
336
"col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur7 );",
337
"col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur7 );",
338
"col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur7 );",
339
"col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur7 );",
340
"col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur7 );",
341
342
"col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur4 );",
343
"col += texture2D( tColor, vUv.xy + ( vec2( 0.4, 0.0 ) * aspectcorrect ) * dofblur4 );",
344
"col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur4 );",
345
"col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur4 );",
346
"col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur4 );",
347
"col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur4 );",
348
"col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur4 );",
349
"col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur4 );",
350
351
"gl_FragColor = col / 41.0;",
352
"gl_FragColor.a = 1.0;",
353
354
"}"
355
356
].join("\n")
357
358
},
359
360
/* -------------------------------------------------------------------------
361
// Depth-of-field shader using mipmaps
362
// - from Matt Handley @applmak
363
// - requires power-of-2 sized render target with enabled mipmaps
364
------------------------------------------------------------------------- */
365
366
'dofmipmap': {
367
368
uniforms: {
369
370
tColor: { type: "t", value: 0, texture: null },
371
tDepth: { type: "t", value: 1, texture: null },
372
focus: { type: "f", value: 1.0 },
373
maxblur: { type: "f", value: 1.0 }
374
375
},
376
377
vertexShader: [
378
379
"varying vec2 vUv;",
380
381
"void main() {",
382
383
"vUv = uv;",
384
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
385
386
"}"
387
388
].join("\n"),
389
390
fragmentShader: [
391
392
"uniform float focus;",
393
"uniform float maxblur;",
394
395
"uniform sampler2D tColor;",
396
"uniform sampler2D tDepth;",
397
398
"varying vec2 vUv;",
399
400
"void main() {",
401
402
"vec4 depth = texture2D( tDepth, vUv );",
403
404
"float factor = depth.x - focus;",
405
406
"vec4 col = texture2D( tColor, vUv, 2.0 * maxblur * abs( focus - depth.x ) );",
407
408
"gl_FragColor = col;",
409
"gl_FragColor.a = 1.0;",
410
411
"}"
412
413
].join("\n")
414
415
},
416
417
/* -------------------------------------------------------------------------
418
// Sepia tone shader
419
// - based on glfx.js sepia shader
420
// https://github.com/evanw/glfx.js
421
------------------------------------------------------------------------- */
422
423
'sepia': {
424
425
uniforms: {
426
427
tDiffuse: { type: "t", value: 0, texture: null },
428
amount: { type: "f", value: 1.0 }
429
430
},
431
432
vertexShader: [
433
434
"varying vec2 vUv;",
435
436
"void main() {",
437
438
"vUv = uv;",
439
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
440
441
"}"
442
443
].join("\n"),
444
445
fragmentShader: [
446
447
"uniform float amount;",
448
449
"uniform sampler2D tDiffuse;",
450
451
"varying vec2 vUv;",
452
453
"void main() {",
454
455
"vec4 color = texture2D( tDiffuse, vUv );",
456
"vec3 c = color.rgb;",
457
458
"color.r = dot( c, vec3( 1.0 - 0.607 * amount, 0.769 * amount, 0.189 * amount ) );",
459
"color.g = dot( c, vec3( 0.349 * amount, 1.0 - 0.314 * amount, 0.168 * amount ) );",
460
"color.b = dot( c, vec3( 0.272 * amount, 0.534 * amount, 1.0 - 0.869 * amount ) );",
461
462
"gl_FragColor = vec4( min( vec3( 1.0 ), color.rgb ), color.a );",
463
464
"}"
465
466
].join("\n")
467
468
},
469
470
/* -------------------------------------------------------------------------
471
// Dot screen shader
472
// - based on glfx.js sepia shader
473
// https://github.com/evanw/glfx.js
474
------------------------------------------------------------------------- */
475
476
'dotscreen': {
477
478
uniforms: {
479
480
tDiffuse: { type: "t", value: 0, texture: null },
481
tSize: { type: "v2", value: new THREE.Vector2( 256, 256 ) },
482
center: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },
483
angle: { type: "f", value: 1.57 },
484
scale: { type: "f", value: 1.0 }
485
486
},
487
488
vertexShader: [
489
490
"varying vec2 vUv;",
491
492
"void main() {",
493
494
"vUv = uv;",
495
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
496
497
"}"
498
499
].join("\n"),
500
501
fragmentShader: [
502
503
"uniform vec2 center;",
504
"uniform float angle;",
505
"uniform float scale;",
506
"uniform vec2 tSize;",
507
508
"uniform sampler2D tDiffuse;",
509
510
"varying vec2 vUv;",
511
512
"float pattern() {",
513
514
"float s = sin( angle ), c = cos( angle );",
515
516
"vec2 tex = vUv * tSize - center;",
517
"vec2 point = vec2( c * tex.x - s * tex.y, s * tex.x + c * tex.y ) * scale;",
518
519
"return ( sin( point.x ) * sin( point.y ) ) * 4.0;",
520
521
"}",
522
523
"void main() {",
524
525
"vec4 color = texture2D( tDiffuse, vUv );",
526
527
"float average = ( color.r + color.g + color.b ) / 3.0;",
528
529
"gl_FragColor = vec4( vec3( average * 10.0 - 5.0 + pattern() ), color.a );",
530
531
"}"
532
533
].join("\n")
534
535
},
536
537
/* ------------------------------------------------------------------------------------------------
538
// Vignette shader
539
// - based on PaintEffect postprocess from ro.me
540
// http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
541
------------------------------------------------------------------------------------------------ */
542
543
'vignette': {
544
545
uniforms: {
546
547
tDiffuse: { type: "t", value: 0, texture: null },
548
offset: { type: "f", value: 1.0 },
549
darkness: { type: "f", value: 1.0 }
550
551
},
552
553
vertexShader: [
554
555
"varying vec2 vUv;",
556
557
"void main() {",
558
559
"vUv = uv;",
560
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
561
562
"}"
563
564
].join("\n"),
565
566
fragmentShader: [
567
568
"uniform float offset;",
569
"uniform float darkness;",
570
571
"uniform sampler2D tDiffuse;",
572
573
"varying vec2 vUv;",
574
575
"void main() {",
576
577
// Eskil's vignette
578
579
"vec4 texel = texture2D( tDiffuse, vUv );",
580
"vec2 uv = ( vUv - vec2( 0.5 ) ) * vec2( offset );",
581
"gl_FragColor = vec4( mix( texel.rgb, vec3( 1.0 - darkness ), dot( uv, uv ) ), texel.a );",
582
583
/*
584
// alternative version from glfx.js
585
// this one makes more "dusty" look (as opposed to "burned")
586
587
"vec4 color = texture2D( tDiffuse, vUv );",
588
"float dist = distance( vUv, vec2( 0.5 ) );",
589
"color.rgb *= smoothstep( 0.8, offset * 0.799, dist *( darkness + offset ) );",
590
"gl_FragColor = color;",
591
*/
592
593
"}"
594
595
].join("\n")
596
597
},
598
599
/* -------------------------------------------------------------------------
600
// Bleach bypass shader [http://en.wikipedia.org/wiki/Bleach_bypass]
601
// - based on Nvidia example
602
// http://developer.download.nvidia.com/shaderlibrary/webpages/shader_library.html#post_bleach_bypass
603
------------------------------------------------------------------------- */
604
605
'bleachbypass': {
606
607
uniforms: {
608
609
tDiffuse: { type: "t", value: 0, texture: null },
610
opacity: { type: "f", value: 1.0 }
611
612
},
613
614
vertexShader: [
615
616
"varying vec2 vUv;",
617
618
"void main() {",
619
620
"vUv = uv;",
621
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
622
623
"}"
624
625
].join("\n"),
626
627
fragmentShader: [
628
629
"uniform float opacity;",
630
631
"uniform sampler2D tDiffuse;",
632
633
"varying vec2 vUv;",
634
635
"void main() {",
636
637
"vec4 base = texture2D( tDiffuse, vUv );",
638
639
"vec3 lumCoeff = vec3( 0.25, 0.65, 0.1 );",
640
"float lum = dot( lumCoeff, base.rgb );",
641
"vec3 blend = vec3( lum );",
642
643
"float L = min( 1.0, max( 0.0, 10.0 * ( lum - 0.45 ) ) );",
644
645
"vec3 result1 = 2.0 * base.rgb * blend;",
646
"vec3 result2 = 1.0 - 2.0 * ( 1.0 - blend ) * ( 1.0 - base.rgb );",
647
648
"vec3 newColor = mix( result1, result2, L );",
649
650
"float A2 = opacity * base.a;",
651
"vec3 mixRGB = A2 * newColor.rgb;",
652
"mixRGB += ( ( 1.0 - A2 ) * base.rgb );",
653
654
"gl_FragColor = vec4( mixRGB, base.a );",
655
656
"}"
657
658
].join("\n")
659
660
},
661
662
/* --------------------------------------------------------------------------------------------------
663
// Focus shader
664
// - based on PaintEffect postprocess from ro.me
665
// http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
666
-------------------------------------------------------------------------------------------------- */
667
668
'focus': {
669
670
uniforms : {
671
672
"tDiffuse": { type: "t", value: 0, texture: null },
673
"screenWidth": { type: "f", value: 1024 },
674
"screenHeight": { type: "f", value: 1024 },
675
"sampleDistance": { type: "f", value: 0.94 },
676
"waveFactor": { type: "f", value: 0.00125 }
677
678
},
679
680
vertexShader: [
681
682
"varying vec2 vUv;",
683
684
"void main() {",
685
686
"vUv = uv;",
687
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
688
689
"}"
690
691
].join("\n"),
692
693
fragmentShader: [
694
695
"uniform float screenWidth;",
696
"uniform float screenHeight;",
697
"uniform float sampleDistance;",
698
"uniform float waveFactor;",
699
700
"uniform sampler2D tDiffuse;",
701
702
"varying vec2 vUv;",
703
704
"void main() {",
705
706
"vec4 color, org, tmp, add;",
707
"float sample_dist, f;",
708
"vec2 vin;",
709
"vec2 uv = vUv;",
710
711
"add = color = org = texture2D( tDiffuse, uv );",
712
713
"vin = ( uv - vec2( 0.5 ) ) * vec2( 1.4 );",
714
"sample_dist = dot( vin, vin ) * 2.0;",
715
716
"f = ( waveFactor * 100.0 + sample_dist ) * sampleDistance * 4.0;",
717
718
"vec2 sampleSize = vec2( 1.0 / screenWidth, 1.0 / screenHeight ) * vec2( f );",
719
720
"add += tmp = texture2D( tDiffuse, uv + vec2( 0.111964, 0.993712 ) * sampleSize );",
721
"if( tmp.b < color.b ) color = tmp;",
722
723
"add += tmp = texture2D( tDiffuse, uv + vec2( 0.846724, 0.532032 ) * sampleSize );",
724
"if( tmp.b < color.b ) color = tmp;",
725
726
"add += tmp = texture2D( tDiffuse, uv + vec2( 0.943883, -0.330279 ) * sampleSize );",
727
"if( tmp.b < color.b ) color = tmp;",
728
729
"add += tmp = texture2D( tDiffuse, uv + vec2( 0.330279, -0.943883 ) * sampleSize );",
730
"if( tmp.b < color.b ) color = tmp;",
731
732
"add += tmp = texture2D( tDiffuse, uv + vec2( -0.532032, -0.846724 ) * sampleSize );",
733
"if( tmp.b < color.b ) color = tmp;",
734
735
"add += tmp = texture2D( tDiffuse, uv + vec2( -0.993712, -0.111964 ) * sampleSize );",
736
"if( tmp.b < color.b ) color = tmp;",
737
738
"add += tmp = texture2D( tDiffuse, uv + vec2( -0.707107, 0.707107 ) * sampleSize );",
739
"if( tmp.b < color.b ) color = tmp;",
740
741
"color = color * vec4( 2.0 ) - ( add / vec4( 8.0 ) );",
742
"color = color + ( add / vec4( 8.0 ) - color ) * ( vec4( 1.0 ) - vec4( sample_dist * 0.5 ) );",
743
744
"gl_FragColor = vec4( color.rgb * color.rgb * vec3( 0.95 ) + color.rgb, 1.0 );",
745
746
"}"
747
748
749
].join("\n")
750
},
751
752
/* -------------------------------------------------------------------------
753
// Triangle blur shader
754
// - based on glfx.js triangle blur shader
755
// https://github.com/evanw/glfx.js
756
757
// A basic blur filter, which convolves the image with a
758
// pyramid filter. The pyramid filter is separable and is applied as two
759
// perpendicular triangle filters.
760
------------------------------------------------------------------------- */
761
762
'triangleBlur': {
763
764
765
uniforms : {
766
767
"texture": { type: "t", value: 0, texture: null },
768
"delta": { type: "v2", value:new THREE.Vector2( 1, 1 ) }
769
770
},
771
772
vertexShader: [
773
774
"varying vec2 vUv;",
775
776
"void main() {",
777
778
"vUv = uv;",
779
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
780
781
"}"
782
783
].join("\n"),
784
785
fragmentShader: [
786
787
"#define ITERATIONS 10.0",
788
789
"uniform sampler2D texture;",
790
"uniform vec2 delta;",
791
792
"varying vec2 vUv;",
793
794
"float random( vec3 scale, float seed ) {",
795
796
// use the fragment position for a different seed per-pixel
797
798
"return fract( sin( dot( gl_FragCoord.xyz + seed, scale ) ) * 43758.5453 + seed );",
799
800
"}",
801
802
"void main() {",
803
804
"vec4 color = vec4( 0.0 );",
805
806
"float total = 0.0;",
807
808
// randomize the lookup values to hide the fixed number of samples
809
810
"float offset = random( vec3( 12.9898, 78.233, 151.7182 ), 0.0 );",
811
812
"for ( float t = -ITERATIONS; t <= ITERATIONS; t ++ ) {",
813
814
"float percent = ( t + offset - 0.5 ) / ITERATIONS;",
815
"float weight = 1.0 - abs( percent );",
816
817
"color += texture2D( texture, vUv + delta * percent ) * weight;",
818
"total += weight;",
819
820
"}",
821
822
"gl_FragColor = color / total;",
823
824
"}"
825
826
].join("\n")
827
828
},
829
830
/* -------------------------------------------------------------------------
831
// Simple test shader
832
------------------------------------------------------------------------- */
833
834
'basic': {
835
836
uniforms: {},
837
838
vertexShader: [
839
840
"void main() {",
841
842
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
843
844
"}"
845
846
].join("\n"),
847
848
fragmentShader: [
849
850
"void main() {",
851
852
"gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );",
853
854
"}"
855
856
].join("\n")
857
858
},
859
860
/* --------------------------------------------------------------------------------------------------
861
// Two pass Gaussian blur filter (horizontal and vertical blur shaders)
862
// - described in http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/
863
// and used in http://www.cake23.de/traveling-wavefronts-lit-up.html
864
//
865
// - 9 samples per pass
866
// - standard deviation 2.7
867
// - "h" and "v" parameters should be set to "1 / width" and "1 / height"
868
-------------------------------------------------------------------------------------------------- */
869
870
'horizontalBlur': {
871
872
uniforms: {
873
874
"tDiffuse": { type: "t", value: 0, texture: null },
875
"h": { type: "f", value: 1.0 / 512.0 }
876
877
},
878
879
vertexShader: [
880
881
"varying vec2 vUv;",
882
883
"void main() {",
884
885
"vUv = uv;",
886
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
887
888
"}"
889
890
].join("\n"),
891
892
fragmentShader: [
893
894
"uniform sampler2D tDiffuse;",
895
"uniform float h;",
896
897
"varying vec2 vUv;",
898
899
"void main() {",
900
901
"vec4 sum = vec4( 0.0 );",
902
903
"sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * h, vUv.y ) ) * 0.051;",
904
"sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * h, vUv.y ) ) * 0.0918;",
905
"sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * h, vUv.y ) ) * 0.12245;",
906
"sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * h, vUv.y ) ) * 0.1531;",
907
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
908
"sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * h, vUv.y ) ) * 0.1531;",
909
"sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * h, vUv.y ) ) * 0.12245;",
910
"sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * h, vUv.y ) ) * 0.0918;",
911
"sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * h, vUv.y ) ) * 0.051;",
912
913
"gl_FragColor = sum;",
914
915
"}"
916
917
918
].join("\n")
919
920
},
921
922
'verticalBlur': {
923
924
uniforms: {
925
926
"tDiffuse": { type: "t", value: 0, texture: null },
927
"v": { type: "f", value: 1.0 / 512.0 }
928
929
},
930
931
vertexShader: [
932
933
"varying vec2 vUv;",
934
935
"void main() {",
936
937
"vUv = uv;",
938
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
939
940
"}"
941
942
].join("\n"),
943
944
fragmentShader: [
945
946
"uniform sampler2D tDiffuse;",
947
"uniform float v;",
948
949
"varying vec2 vUv;",
950
951
"void main() {",
952
953
"vec4 sum = vec4( 0.0 );",
954
955
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 4.0 * v ) ) * 0.051;",
956
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 3.0 * v ) ) * 0.0918;",
957
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 2.0 * v ) ) * 0.12245;",
958
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 1.0 * v ) ) * 0.1531;",
959
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
960
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 1.0 * v ) ) * 0.1531;",
961
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 2.0 * v ) ) * 0.12245;",
962
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 3.0 * v ) ) * 0.0918;",
963
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 4.0 * v ) ) * 0.051;",
964
965
"gl_FragColor = sum;",
966
967
"}"
968
969
970
].join("\n")
971
972
},
973
974
/* --------------------------------------------------------------------------------------------------
975
// Simple fake tilt-shift effect, modulating two pass Gaussian blur (see above) by vertical position
976
//
977
// - 9 samples per pass
978
// - standard deviation 2.7
979
// - "h" and "v" parameters should be set to "1 / width" and "1 / height"
980
// - "r" parameter control where "focused" horizontal line lies
981
-------------------------------------------------------------------------------------------------- */
982
983
'horizontalTiltShift': {
984
985
uniforms: {
986
987
"tDiffuse": { type: "t", value: 0, texture: null },
988
"h": { type: "f", value: 1.0 / 512.0 },
989
"r": { type: "f", value: 0.35 }
990
991
},
992
993
vertexShader: [
994
995
"varying vec2 vUv;",
996
997
"void main() {",
998
999
"vUv = uv;",
1000
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
1001
1002
"}"
1003
1004
].join("\n"),
1005
1006
fragmentShader: [
1007
1008
"uniform sampler2D tDiffuse;",
1009
"uniform float h;",
1010
"uniform float r;",
1011
1012
"varying vec2 vUv;",
1013
1014
"void main() {",
1015
1016
"vec4 sum = vec4( 0.0 );",
1017
1018
"float hh = h * abs( r - vUv.y );",
1019
1020
"sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * hh, vUv.y ) ) * 0.051;",
1021
"sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * hh, vUv.y ) ) * 0.0918;",
1022
"sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * hh, vUv.y ) ) * 0.12245;",
1023
"sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * hh, vUv.y ) ) * 0.1531;",
1024
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
1025
"sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * hh, vUv.y ) ) * 0.1531;",
1026
"sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * hh, vUv.y ) ) * 0.12245;",
1027
"sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * hh, vUv.y ) ) * 0.0918;",
1028
"sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * hh, vUv.y ) ) * 0.051;",
1029
1030
"gl_FragColor = sum;",
1031
1032
"}"
1033
1034
1035
].join("\n")
1036
1037
},
1038
1039
'verticalTiltShift': {
1040
1041
uniforms: {
1042
1043
"tDiffuse": { type: "t", value: 0, texture: null },
1044
"v": { type: "f", value: 1.0 / 512.0 },
1045
"r": { type: "f", value: 0.35 }
1046
1047
},
1048
1049
vertexShader: [
1050
1051
"varying vec2 vUv;",
1052
1053
"void main() {",
1054
1055
"vUv = uv;",
1056
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
1057
1058
"}"
1059
1060
].join("\n"),
1061
1062
fragmentShader: [
1063
1064
"uniform sampler2D tDiffuse;",
1065
"uniform float v;",
1066
"uniform float r;",
1067
1068
"varying vec2 vUv;",
1069
1070
"void main() {",
1071
1072
"vec4 sum = vec4( 0.0 );",
1073
1074
"float vv = v * abs( r - vUv.y );",
1075
1076
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 4.0 * vv ) ) * 0.051;",
1077
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 3.0 * vv ) ) * 0.0918;",
1078
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 2.0 * vv ) ) * 0.12245;",
1079
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 1.0 * vv ) ) * 0.1531;",
1080
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
1081
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 1.0 * vv ) ) * 0.1531;",
1082
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 2.0 * vv ) ) * 0.12245;",
1083
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 3.0 * vv ) ) * 0.0918;",
1084
"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 4.0 * vv ) ) * 0.051;",
1085
1086
"gl_FragColor = sum;",
1087
1088
"}"
1089
1090
1091
].join("\n")
1092
1093
},
1094
1095
/* -------------------------------------------------------------------------
1096
// Blend two textures
1097
------------------------------------------------------------------------- */
1098
1099
'blend': {
1100
1101
uniforms: {
1102
1103
tDiffuse1: { type: "t", value: 0, texture: null },
1104
tDiffuse2: { type: "t", value: 1, texture: null },
1105
mixRatio: { type: "f", value: 0.5 },
1106
opacity: { type: "f", value: 1.0 }
1107
1108
},
1109
1110
vertexShader: [
1111
1112
"varying vec2 vUv;",
1113
1114
"void main() {",
1115
1116
"vUv = uv;",
1117
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
1118
1119
"}"
1120
1121
].join("\n"),
1122
1123
fragmentShader: [
1124
1125
"uniform float opacity;",
1126
"uniform float mixRatio;",
1127
1128
"uniform sampler2D tDiffuse1;",
1129
"uniform sampler2D tDiffuse2;",
1130
1131
"varying vec2 vUv;",
1132
1133
"void main() {",
1134
1135
"vec4 texel1 = texture2D( tDiffuse1, vUv );",
1136
"vec4 texel2 = texture2D( tDiffuse2, vUv );",
1137
"gl_FragColor = opacity * mix( texel1, texel2, mixRatio );",
1138
1139
"}"
1140
1141
].join("\n")
1142
1143
},
1144
1145
/* -------------------------------------------------------------------------
1146
// NVIDIA FXAA by Timothy Lottes
1147
// http://timothylottes.blogspot.com/2011/06/fxaa3-source-released.html
1148
// - WebGL port by @supereggbert
1149
// http://www.glge.org/demos/fxaa/
1150
------------------------------------------------------------------------- */
1151
1152
'fxaa': {
1153
1154
uniforms: {
1155
1156
"tDiffuse": { type: "t", value: 0, texture: null },
1157
"resolution": { type: "v2", value: new THREE.Vector2( 1 / 1024, 1 / 512 ) }
1158
1159
},
1160
1161
vertexShader: [
1162
1163
"varying vec2 vUv;",
1164
1165
"void main() {",
1166
1167
"vUv = uv;",
1168
1169
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
1170
1171
"}"
1172
1173
].join("\n"),
1174
1175
fragmentShader: [
1176
1177
"uniform sampler2D tDiffuse;",
1178
"uniform vec2 resolution;",
1179
1180
"varying vec2 vUv;",
1181
1182
"#define FXAA_REDUCE_MIN (1.0/128.0)",
1183
"#define FXAA_REDUCE_MUL (1.0/8.0)",
1184
"#define FXAA_SPAN_MAX 8.0",
1185
1186
"void main() {",
1187
1188
"vec3 rgbNW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, -1.0 ) ) * resolution ).xyz;",
1189
"vec3 rgbNE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, -1.0 ) ) * resolution ).xyz;",
1190
"vec3 rgbSW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, 1.0 ) ) * resolution ).xyz;",
1191
"vec3 rgbSE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, 1.0 ) ) * resolution ).xyz;",
1192
"vec4 rgbaM = texture2D( tDiffuse, gl_FragCoord.xy * resolution );",
1193
"vec3 rgbM = rgbaM.xyz;",
1194
"float opacity = rgbaM.w;",
1195
1196
"vec3 luma = vec3( 0.299, 0.587, 0.114 );",
1197
1198
"float lumaNW = dot( rgbNW, luma );",
1199
"float lumaNE = dot( rgbNE, luma );",
1200
"float lumaSW = dot( rgbSW, luma );",
1201
"float lumaSE = dot( rgbSE, luma );",
1202
"float lumaM = dot( rgbM, luma );",
1203
"float lumaMin = min( lumaM, min( min( lumaNW, lumaNE ), min( lumaSW, lumaSE ) ) );",
1204
"float lumaMax = max( lumaM, max( max( lumaNW, lumaNE) , max( lumaSW, lumaSE ) ) );",
1205
1206
"vec2 dir;",
1207
"dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));",
1208
"dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));",
1209
1210
"float dirReduce = max( ( lumaNW + lumaNE + lumaSW + lumaSE ) * ( 0.25 * FXAA_REDUCE_MUL ), FXAA_REDUCE_MIN );",
1211
1212
"float rcpDirMin = 1.0 / ( min( abs( dir.x ), abs( dir.y ) ) + dirReduce );",
1213
"dir = min( vec2( FXAA_SPAN_MAX, FXAA_SPAN_MAX),",
1214
"max( vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),",
1215
"dir * rcpDirMin)) * resolution;",
1216
1217
"vec3 rgbA = 0.5 * (",
1218
"texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 1.0 / 3.0 - 0.5 ) ).xyz +",
1219
"texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 2.0 / 3.0 - 0.5 ) ).xyz );",
1220
1221
"vec3 rgbB = rgbA * 0.5 + 0.25 * (",
1222
"texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * -0.5 ).xyz +",
1223
"texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * 0.5 ).xyz );",
1224
1225
"float lumaB = dot( rgbB, luma );",
1226
1227
"if ( ( lumaB < lumaMin ) || ( lumaB > lumaMax ) ) {",
1228
1229
"gl_FragColor = vec4( rgbA, opacity );",
1230
1231
"} else {",
1232
1233
"gl_FragColor = vec4( rgbB, opacity );",
1234
1235
"}",
1236
1237
"}"
1238
1239
].join("\n")
1240
1241
},
1242
1243
/* -------------------------------------------------------------------------
1244
// Luminosity
1245
// http://en.wikipedia.org/wiki/Luminosity
1246
------------------------------------------------------------------------- */
1247
1248
'luminosity': {
1249
1250
uniforms: {
1251
1252
"tDiffuse": { type: "t", value: 0, texture: null }
1253
1254
},
1255
1256
vertexShader: [
1257
1258
"varying vec2 vUv;",
1259
1260
"void main() {",
1261
1262
"vUv = uv;",
1263
1264
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
1265
1266
"}"
1267
1268
].join("\n"),
1269
1270
fragmentShader: [
1271
1272
"uniform sampler2D tDiffuse;",
1273
1274
"varying vec2 vUv;",
1275
1276
"void main() {",
1277
1278
"vec4 texel = texture2D( tDiffuse, vUv );",
1279
1280
"vec3 luma = vec3( 0.299, 0.587, 0.114 );",
1281
1282
"float v = dot( texel.xyz, luma );",
1283
1284
"gl_FragColor = vec4( v, v, v, texel.w );",
1285
1286
"}"
1287
1288
].join("\n")
1289
1290
},
1291
1292
/* -------------------------------------------------------------------------
1293
// Color correction
1294
------------------------------------------------------------------------- */
1295
1296
'colorCorrection': {
1297
1298
uniforms: {
1299
1300
"tDiffuse" : { type: "t", value: 0, texture: null },
1301
"powRGB" : { type: "v3", value: new THREE.Vector3( 2, 2, 2 ) },
1302
"mulRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
1303
1304
},
1305
1306
vertexShader: [
1307
1308
"varying vec2 vUv;",
1309
1310
"void main() {",
1311
1312
"vUv = uv;",
1313
1314
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
1315
1316
"}"
1317
1318
].join("\n"),
1319
1320
fragmentShader: [
1321
1322
"uniform sampler2D tDiffuse;",
1323
"uniform vec3 powRGB;",
1324
"uniform vec3 mulRGB;",
1325
1326
"varying vec2 vUv;",
1327
1328
"void main() {",
1329
1330
"gl_FragColor = texture2D( tDiffuse, vUv );",
1331
"gl_FragColor.rgb = mulRGB * pow( gl_FragColor.rgb, powRGB );",
1332
1333
"}"
1334
1335
].join("\n")
1336
1337
},
1338
1339
/* -------------------------------------------------------------------------
1340
// Normal map shader
1341
// - compute normals from heightmap
1342
------------------------------------------------------------------------- */
1343
1344
'normalmap': {
1345
1346
uniforms: {
1347
1348
"heightMap" : { type: "t", value: 0, texture: null },
1349
"resolution": { type: "v2", value: new THREE.Vector2( 512, 512 ) },
1350
"scale" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
1351
"height" : { type: "f", value: 0.05 }
1352
1353
},
1354
1355
vertexShader: [
1356
1357
"varying vec2 vUv;",
1358
1359
"void main() {",
1360
1361
"vUv = uv;",
1362
1363
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
1364
1365
"}"
1366
1367
].join("\n"),
1368
1369
fragmentShader: [
1370
1371
"uniform float height;",
1372
"uniform vec2 resolution;",
1373
"uniform sampler2D heightMap;",
1374
1375
"varying vec2 vUv;",
1376
1377
"void main() {",
1378
1379
"float val = texture2D( heightMap, vUv ).x;",
1380
1381
"float valU = texture2D( heightMap, vUv + vec2( 1.0 / resolution.x, 0.0 ) ).x;",
1382
"float valV = texture2D( heightMap, vUv + vec2( 0.0, 1.0 / resolution.y ) ).x;",
1383
1384
"gl_FragColor = vec4( ( 0.5 * normalize( vec3( val - valU, val - valV, height ) ) + 0.5 ), 1.0 );",
1385
1386
"}"
1387
1388
].join("\n")
1389
1390
},
1391
1392
/* -------------------------------------------------------------------------
1393
// Screen-space ambient occlusion shader
1394
// - ported from
1395
// SSAO GLSL shader v1.2
1396
// assembled by Martins Upitis (martinsh) (http://devlog-martinsh.blogspot.com)
1397
// original technique is made by ArKano22 (http://www.gamedev.net/topic/550699-ssao-no-halo-artifacts/)
1398
// - modifications
1399
// - modified to use RGBA packed depth texture (use clear color 1,1,1,1 for depth pass)
1400
// - made fog more compatible with three.js linear fog
1401
// - refactoring and optimizations
1402
------------------------------------------------------------------------- */
1403
1404
'ssao': {
1405
1406
uniforms: {
1407
1408
"tDiffuse": { type: "t", value: 0, texture: null },
1409
"tDepth": { type: "t", value: 1, texture: null },
1410
"size": { type: "v2", value: new THREE.Vector2( 512, 512 ) },
1411
"cameraNear": { type: "f", value: 1 },
1412
"cameraFar": { type: "f", value: 100 },
1413
"fogNear": { type: "f", value: 5 },
1414
"fogFar": { type: "f", value: 100 },
1415
"fogEnabled": { type: "i", value: 0 },
1416
"onlyAO": { type: "i", value: 0 },
1417
"aoClamp": { type: "f", value: 0.3 },
1418
"lumInfluence": { type: "f", value: 0.9 }
1419
1420
},
1421
1422
vertexShader: [
1423
1424
"varying vec2 vUv;",
1425
1426
"void main() {",
1427
1428
"vUv = uv;",
1429
1430
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
1431
1432
"}"
1433
1434
].join("\n"),
1435
1436
fragmentShader: [
1437
1438
"uniform float cameraNear;",
1439
"uniform float cameraFar;",
1440
1441
"uniform float fogNear;",
1442
"uniform float fogFar;",
1443
1444
"uniform bool fogEnabled;", // attenuate AO with linear fog
1445
"uniform bool onlyAO;", // use only ambient occlusion pass?
1446
1447
"uniform vec2 size;", // texture width, height
1448
"uniform float aoClamp;", // depth clamp - reduces haloing at screen edges
1449
1450
"uniform float lumInfluence;", // how much luminance affects occlusion
1451
1452
"uniform sampler2D tDiffuse;",
1453
"uniform sampler2D tDepth;",
1454
1455
"varying vec2 vUv;",
1456
1457
//"#define PI 3.14159265",
1458
"#define DL 2.399963229728653", // PI * ( 3.0 - sqrt( 5.0 ) )
1459
"#define EULER 2.718281828459045",
1460
1461
// helpers
1462
1463
"float width = size.x;", // texture width
1464
"float height = size.y;", // texture height
1465
1466
"float cameraFarPlusNear = cameraFar + cameraNear;",
1467
"float cameraFarMinusNear = cameraFar - cameraNear;",
1468
"float cameraCoef = 2.0 * cameraNear;",
1469
1470
// user variables
1471
1472
"const int samples = 8;", // ao sample count
1473
"const float radius = 5.0;", // ao radius
1474
1475
"const bool useNoise = true;", // use noise instead of pattern for sample dithering
1476
"const float noiseAmount = 0.0003;", // dithering amount
1477
1478
"const float diffArea = 0.4;", // self-shadowing reduction
1479
"const float gDisplace = 0.4;", // gauss bell center
1480
1481
"const vec3 onlyAOColor = vec3( 1.0, 0.7, 0.5 );",
1482
//"const vec3 onlyAOColor = vec3( 1.0, 1.0, 1.0 );",
1483
1484
1485
// RGBA depth
1486
1487
"float unpackDepth( const in vec4 rgba_depth ) {",
1488
1489
"const vec4 bit_shift = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );",
1490
"float depth = dot( rgba_depth, bit_shift );",
1491
"return depth;",
1492
1493
"}",
1494
1495
// generating noise / pattern texture for dithering
1496
1497
"vec2 rand( const vec2 coord ) {",
1498
1499
"vec2 noise;",
1500
1501
"if ( useNoise ) {",
1502
1503
"float nx = dot ( coord, vec2( 12.9898, 78.233 ) );",
1504
"float ny = dot ( coord, vec2( 12.9898, 78.233 ) * 2.0 );",
1505
1506
"noise = clamp( fract ( 43758.5453 * sin( vec2( nx, ny ) ) ), 0.0, 1.0 );",
1507
1508
"} else {",
1509
1510
"float ff = fract( 1.0 - coord.s * ( width / 2.0 ) );",
1511
"float gg = fract( coord.t * ( height / 2.0 ) );",
1512
1513
"noise = vec2( 0.25, 0.75 ) * vec2( ff ) + vec2( 0.75, 0.25 ) * gg;",
1514
1515
"}",
1516
1517
"return ( noise * 2.0 - 1.0 ) * noiseAmount;",
1518
1519
"}",
1520
1521
"float doFog() {",
1522
1523
"float zdepth = unpackDepth( texture2D( tDepth, vUv ) );",
1524
"float depth = -cameraFar * cameraNear / ( zdepth * cameraFarMinusNear - cameraFar );",
1525
1526
"return smoothstep( fogNear, fogFar, depth );",
1527
1528
"}",
1529
1530
"float readDepth( const in vec2 coord ) {",
1531
1532
//"return ( 2.0 * cameraNear ) / ( cameraFar + cameraNear - unpackDepth( texture2D( tDepth, coord ) ) * ( cameraFar - cameraNear ) );",
1533
"return cameraCoef / ( cameraFarPlusNear - unpackDepth( texture2D( tDepth, coord ) ) * cameraFarMinusNear );",
1534
1535
1536
"}",
1537
1538
"float compareDepths( const in float depth1, const in float depth2, inout int far ) {",
1539
1540
"float garea = 2.0;", // gauss bell width
1541
"float diff = ( depth1 - depth2 ) * 100.0;", // depth difference (0-100)
1542
1543
// reduce left bell width to avoid self-shadowing
1544
1545
"if ( diff < gDisplace ) {",
1546
1547
"garea = diffArea;",
1548
1549
"} else {",
1550
1551
"far = 1;",
1552
1553
"}",
1554
1555
"float dd = diff - gDisplace;",
1556
"float gauss = pow( EULER, -2.0 * dd * dd / ( garea * garea ) );",
1557
"return gauss;",
1558
1559
"}",
1560
1561
"float calcAO( float depth, float dw, float dh ) {",
1562
1563
"float dd = radius - depth * radius;",
1564
"vec2 vv = vec2( dw, dh );",
1565
1566
"vec2 coord1 = vUv + dd * vv;",
1567
"vec2 coord2 = vUv - dd * vv;",
1568
1569
"float temp1 = 0.0;",
1570
"float temp2 = 0.0;",
1571
1572
"int far = 0;",
1573
"temp1 = compareDepths( depth, readDepth( coord1 ), far );",
1574
1575
// DEPTH EXTRAPOLATION
1576
1577
"if ( far > 0 ) {",
1578
1579
"temp2 = compareDepths( readDepth( coord2 ), depth, far );",
1580
"temp1 += ( 1.0 - temp1 ) * temp2;",
1581
1582
"}",
1583
1584
"return temp1;",
1585
1586
"}",
1587
1588
"void main() {",
1589
1590
"vec2 noise = rand( vUv );",
1591
"float depth = readDepth( vUv );",
1592
1593
"float tt = clamp( depth, aoClamp, 1.0 );",
1594
1595
"float w = ( 1.0 / width ) / tt + ( noise.x * ( 1.0 - noise.x ) );",
1596
"float h = ( 1.0 / height ) / tt + ( noise.y * ( 1.0 - noise.y ) );",
1597
1598
"float pw;",
1599
"float ph;",
1600
1601
"float ao;",
1602
1603
"float dz = 1.0 / float( samples );",
1604
"float z = 1.0 - dz / 2.0;",
1605
"float l = 0.0;",
1606
1607
"for ( int i = 0; i <= samples; i ++ ) {",
1608
1609
"float r = sqrt( 1.0 - z );",
1610
1611
"pw = cos( l ) * r;",
1612
"ph = sin( l ) * r;",
1613
"ao += calcAO( depth, pw * w, ph * h );",
1614
"z = z - dz;",
1615
"l = l + DL;",
1616
1617
"}",
1618
1619
"ao /= float( samples );",
1620
"ao = 1.0 - ao;",
1621
1622
"if ( fogEnabled ) {",
1623
1624
"ao = mix( ao, 1.0, doFog() );",
1625
1626
"}",
1627
1628
"vec3 color = texture2D( tDiffuse, vUv ).rgb;",
1629
1630
"vec3 lumcoeff = vec3( 0.299, 0.587, 0.114 );",
1631
"float lum = dot( color.rgb, lumcoeff );",
1632
"vec3 luminance = vec3( lum );",
1633
1634
"vec3 final = vec3( color * mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // mix( color * ao, white, luminance )
1635
1636
"if ( onlyAO ) {",
1637
1638
"final = onlyAOColor * vec3( mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // ambient occlusion only
1639
1640
"}",
1641
1642
"gl_FragColor = vec4( final, 1.0 );",
1643
1644
"}"
1645
1646
].join("\n")
1647
1648
},
1649
1650
/* -------------------------------------------------------------------------
1651
// Colorify shader
1652
------------------------------------------------------------------------- */
1653
1654
'colorify': {
1655
1656
uniforms: {
1657
1658
tDiffuse: { type: "t", value: 0, texture: null },
1659
color: { type: "c", value: new THREE.Color( 0xffffff ) }
1660
1661
},
1662
1663
vertexShader: [
1664
1665
"varying vec2 vUv;",
1666
1667
"void main() {",
1668
1669
"vUv = uv;",
1670
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
1671
1672
"}"
1673
1674
].join("\n"),
1675
1676
fragmentShader: [
1677
1678
"uniform vec3 color;",
1679
"uniform sampler2D tDiffuse;",
1680
1681
"varying vec2 vUv;",
1682
1683
"void main() {",
1684
1685
"vec4 texel = texture2D( tDiffuse, vUv );",
1686
1687
"vec3 luma = vec3( 0.299, 0.587, 0.114 );",
1688
"float v = dot( texel.xyz, luma );",
1689
1690
"gl_FragColor = vec4( v * color, texel.w );",
1691
1692
"}"
1693
1694
].join("\n")
1695
1696
},
1697
1698
/* -------------------------------------------------------------------------
1699
// Unpack RGBA depth shader
1700
// - show RGBA encoded depth as monochrome color
1701
------------------------------------------------------------------------- */
1702
1703
'unpackDepthRGBA': {
1704
1705
uniforms: {
1706
1707
tDiffuse: { type: "t", value: 0, texture: null },
1708
opacity: { type: "f", value: 1.0 }
1709
1710
},
1711
1712
vertexShader: [
1713
1714
"varying vec2 vUv;",
1715
1716
"void main() {",
1717
1718
"vUv = uv;",
1719
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
1720
1721
"}"
1722
1723
].join("\n"),
1724
1725
fragmentShader: [
1726
1727
"uniform float opacity;",
1728
1729
"uniform sampler2D tDiffuse;",
1730
1731
"varying vec2 vUv;",
1732
1733
// RGBA depth
1734
1735
"float unpackDepth( const in vec4 rgba_depth ) {",
1736
1737
"const vec4 bit_shift = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );",
1738
"float depth = dot( rgba_depth, bit_shift );",
1739
"return depth;",
1740
1741
"}",
1742
1743
"void main() {",
1744
1745
"float depth = 1.0 - unpackDepth( texture2D( tDiffuse, vUv ) );",
1746
"gl_FragColor = opacity * vec4( vec3( depth ), 1.0 );",
1747
1748
"}"
1749
1750
].join("\n")
1751
1752
},
1753
1754
// METHODS
1755
1756
buildKernel: function( sigma ) {
1757
1758
// We lop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.
1759
1760
function gauss( x, sigma ) {
1761
1762
return Math.exp( - ( x * x ) / ( 2.0 * sigma * sigma ) );
1763
1764
}
1765
1766
var i, values, sum, halfWidth, kMaxKernelSize = 25, kernelSize = 2 * Math.ceil( sigma * 3.0 ) + 1;
1767
1768
if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;
1769
halfWidth = ( kernelSize - 1 ) * 0.5;
1770
1771
values = new Array( kernelSize );
1772
sum = 0.0;
1773
for ( i = 0; i < kernelSize; ++i ) {
1774
1775
values[ i ] = gauss( i - halfWidth, sigma );
1776
sum += values[ i ];
1777
1778
}
1779
1780
// normalize the kernel
1781
1782
for ( i = 0; i < kernelSize; ++i ) values[ i ] /= sum;
1783
1784
return values;
1785
1786
}
1787
1788
};
1789
1790