source: other-projects/playing-in-the-street/summer-2013/trunk/Playing-in-the-Street-WPF/Content/Web/mrdoob-three.js-4862f5f/src/renderers/shaders/ShaderChunk.js@ 28897

Last change on this file since 28897 was 28897, checked in by davidb, 10 years ago

GUI front-end to server base plus web page content

File size: 40.9 KB
Line 
1/**
2 * Shader chunks for WebLG Shader library
3 *
4 * @author alteredq / http://alteredqualia.com/
5 * @author mrdoob / http://mrdoob.com/
6 * @author mikael emtinger / http://gomo.se/
7 */
8
9THREE.ShaderChunk = {
10
11 // FOG
12
13 fog_pars_fragment: [
14
15 "#ifdef USE_FOG",
16
17 "uniform vec3 fogColor;",
18
19 "#ifdef FOG_EXP2",
20
21 "uniform float fogDensity;",
22
23 "#else",
24
25 "uniform float fogNear;",
26 "uniform float fogFar;",
27
28 "#endif",
29
30 "#endif"
31
32 ].join("\n"),
33
34 fog_fragment: [
35
36 "#ifdef USE_FOG",
37
38 "float depth = gl_FragCoord.z / gl_FragCoord.w;",
39
40 "#ifdef FOG_EXP2",
41
42 "const float LOG2 = 1.442695;",
43 "float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );",
44 "fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );",
45
46 "#else",
47
48 "float fogFactor = smoothstep( fogNear, fogFar, depth );",
49
50 "#endif",
51
52 "gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );",
53
54 "#endif"
55
56 ].join("\n"),
57
58 // ENVIRONMENT MAP
59
60 envmap_pars_fragment: [
61
62 "#ifdef USE_ENVMAP",
63
64 "uniform float reflectivity;",
65 "uniform samplerCube envMap;",
66 "uniform float flipEnvMap;",
67 "uniform int combine;",
68
69 "#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP )",
70
71 "uniform bool useRefract;",
72 "uniform float refractionRatio;",
73
74 "#else",
75
76 "varying vec3 vReflect;",
77
78 "#endif",
79
80 "#endif"
81
82 ].join("\n"),
83
84 envmap_fragment: [
85
86 "#ifdef USE_ENVMAP",
87
88 "vec3 reflectVec;",
89
90 "#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP )",
91
92 "vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );",
93
94 "if ( useRefract ) {",
95
96 "reflectVec = refract( cameraToVertex, normal, refractionRatio );",
97
98 "} else { ",
99
100 "reflectVec = reflect( cameraToVertex, normal );",
101
102 "}",
103
104 "#else",
105
106 "reflectVec = vReflect;",
107
108 "#endif",
109
110 "#ifdef DOUBLE_SIDED",
111
112 "float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
113 "vec4 cubeColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );",
114
115 "#else",
116
117 "vec4 cubeColor = textureCube( envMap, vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );",
118
119 "#endif",
120
121 "#ifdef GAMMA_INPUT",
122
123 "cubeColor.xyz *= cubeColor.xyz;",
124
125 "#endif",
126
127 "if ( combine == 1 ) {",
128
129 "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularStrength * reflectivity );",
130
131 "} else if ( combine == 2 ) {",
132
133 "gl_FragColor.xyz += cubeColor.xyz * specularStrength * reflectivity;",
134
135 "} else {",
136
137 "gl_FragColor.xyz = mix( gl_FragColor.xyz, gl_FragColor.xyz * cubeColor.xyz, specularStrength * reflectivity );",
138
139 "}",
140
141 "#endif"
142
143 ].join("\n"),
144
145 envmap_pars_vertex: [
146
147 "#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP )",
148
149 "varying vec3 vReflect;",
150
151 "uniform float refractionRatio;",
152 "uniform bool useRefract;",
153
154 "#endif"
155
156 ].join("\n"),
157
158 worldpos_vertex : [
159
160 "#if defined( USE_ENVMAP ) || defined( PHONG ) || defined( LAMBERT ) || defined ( USE_SHADOWMAP )",
161
162 "#ifdef USE_SKINNING",
163
164 "vec4 worldPosition = modelMatrix * skinned;",
165
166 "#endif",
167
168 "#if defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )",
169
170 "vec4 worldPosition = modelMatrix * vec4( morphed, 1.0 );",
171
172 "#endif",
173
174 "#if ! defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )",
175
176 "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
177
178 "#endif",
179
180 "#endif"
181
182 ].join("\n"),
183
184 envmap_vertex : [
185
186 "#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP )",
187
188 "vec3 worldNormal = mat3( modelMatrix[ 0 ].xyz, modelMatrix[ 1 ].xyz, modelMatrix[ 2 ].xyz ) * objectNormal;",
189 "worldNormal = normalize( worldNormal );",
190
191 "vec3 cameraToVertex = normalize( worldPosition.xyz - cameraPosition );",
192
193 "if ( useRefract ) {",
194
195 "vReflect = refract( cameraToVertex, worldNormal, refractionRatio );",
196
197 "} else {",
198
199 "vReflect = reflect( cameraToVertex, worldNormal );",
200
201 "}",
202
203 "#endif"
204
205 ].join("\n"),
206
207 // COLOR MAP (particles)
208
209 map_particle_pars_fragment: [
210
211 "#ifdef USE_MAP",
212
213 "uniform sampler2D map;",
214
215 "#endif"
216
217 ].join("\n"),
218
219
220 map_particle_fragment: [
221
222 "#ifdef USE_MAP",
223
224 "gl_FragColor = gl_FragColor * texture2D( map, vec2( gl_PointCoord.x, 1.0 - gl_PointCoord.y ) );",
225
226 "#endif"
227
228 ].join("\n"),
229
230 // COLOR MAP (triangles)
231
232 map_pars_vertex: [
233
234 "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )",
235
236 "varying vec2 vUv;",
237 "uniform vec4 offsetRepeat;",
238
239 "#endif"
240
241 ].join("\n"),
242
243 map_pars_fragment: [
244
245 "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )",
246
247 "varying vec2 vUv;",
248
249 "#endif",
250
251 "#ifdef USE_MAP",
252
253 "uniform sampler2D map;",
254
255 "#endif"
256
257 ].join("\n"),
258
259 map_vertex: [
260
261 "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )",
262
263 "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
264
265 "#endif"
266
267 ].join("\n"),
268
269 map_fragment: [
270
271 "#ifdef USE_MAP",
272
273 "vec4 texelColor = texture2D( map, vUv );",
274
275 "#ifdef GAMMA_INPUT",
276
277 "texelColor.xyz *= texelColor.xyz;",
278
279 "#endif",
280
281 "gl_FragColor = gl_FragColor * texelColor;",
282
283 "#endif"
284
285 ].join("\n"),
286
287 // LIGHT MAP
288
289 lightmap_pars_fragment: [
290
291 "#ifdef USE_LIGHTMAP",
292
293 "varying vec2 vUv2;",
294 "uniform sampler2D lightMap;",
295
296 "#endif"
297
298 ].join("\n"),
299
300 lightmap_pars_vertex: [
301
302 "#ifdef USE_LIGHTMAP",
303
304 "varying vec2 vUv2;",
305
306 "#endif"
307
308 ].join("\n"),
309
310 lightmap_fragment: [
311
312 "#ifdef USE_LIGHTMAP",
313
314 "gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );",
315
316 "#endif"
317
318 ].join("\n"),
319
320 lightmap_vertex: [
321
322 "#ifdef USE_LIGHTMAP",
323
324 "vUv2 = uv2;",
325
326 "#endif"
327
328 ].join("\n"),
329
330 // BUMP MAP
331
332 bumpmap_pars_fragment: [
333
334 "#ifdef USE_BUMPMAP",
335
336 "uniform sampler2D bumpMap;",
337 "uniform float bumpScale;",
338
339 // Derivative maps - bump mapping unparametrized surfaces by Morten Mikkelsen
340 // http://mmikkelsen3d.blogspot.sk/2011/07/derivative-maps.html
341
342 // Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)
343
344 "vec2 dHdxy_fwd() {",
345
346 "vec2 dSTdx = dFdx( vUv );",
347 "vec2 dSTdy = dFdy( vUv );",
348
349 "float Hll = bumpScale * texture2D( bumpMap, vUv ).x;",
350 "float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;",
351 "float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;",
352
353 "return vec2( dBx, dBy );",
354
355 "}",
356
357 "vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {",
358
359 "vec3 vSigmaX = dFdx( surf_pos );",
360 "vec3 vSigmaY = dFdy( surf_pos );",
361 "vec3 vN = surf_norm;", // normalized
362
363 "vec3 R1 = cross( vSigmaY, vN );",
364 "vec3 R2 = cross( vN, vSigmaX );",
365
366 "float fDet = dot( vSigmaX, R1 );",
367
368 "vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );",
369 "return normalize( abs( fDet ) * surf_norm - vGrad );",
370
371 "}",
372
373 "#endif"
374
375 ].join("\n"),
376
377 // NORMAL MAP
378
379 normalmap_pars_fragment: [
380
381 "#ifdef USE_NORMALMAP",
382
383 "uniform sampler2D normalMap;",
384 "uniform vec2 normalScale;",
385
386 // Per-Pixel Tangent Space Normal Mapping
387 // http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html
388
389 "vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm ) {",
390
391 "vec3 q0 = dFdx( eye_pos.xyz );",
392 "vec3 q1 = dFdy( eye_pos.xyz );",
393 "vec2 st0 = dFdx( vUv.st );",
394 "vec2 st1 = dFdy( vUv.st );",
395
396 "vec3 S = normalize( q0 * st1.t - q1 * st0.t );",
397 "vec3 T = normalize( -q0 * st1.s + q1 * st0.s );",
398 "vec3 N = normalize( surf_norm );",
399
400 "vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;",
401 "mapN.xy = normalScale * mapN.xy;",
402 "mat3 tsn = mat3( S, T, N );",
403 "return normalize( tsn * mapN );",
404
405 "}",
406
407 "#endif"
408
409 ].join("\n"),
410
411 // SPECULAR MAP
412
413 specularmap_pars_fragment: [
414
415 "#ifdef USE_SPECULARMAP",
416
417 "uniform sampler2D specularMap;",
418
419 "#endif"
420
421 ].join("\n"),
422
423 specularmap_fragment: [
424
425 "float specularStrength;",
426
427 "#ifdef USE_SPECULARMAP",
428
429 "vec4 texelSpecular = texture2D( specularMap, vUv );",
430 "specularStrength = texelSpecular.r;",
431
432 "#else",
433
434 "specularStrength = 1.0;",
435
436 "#endif"
437
438 ].join("\n"),
439
440 // LIGHTS LAMBERT
441
442 lights_lambert_pars_vertex: [
443
444 "uniform vec3 ambient;",
445 "uniform vec3 diffuse;",
446 "uniform vec3 emissive;",
447
448 "uniform vec3 ambientLightColor;",
449
450 "#if MAX_DIR_LIGHTS > 0",
451
452 "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
453 "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
454
455 "#endif",
456
457 "#if MAX_HEMI_LIGHTS > 0",
458
459 "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
460 "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
461 "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
462
463 "#endif",
464
465 "#if MAX_POINT_LIGHTS > 0",
466
467 "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
468 "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
469 "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
470
471 "#endif",
472
473 "#if MAX_SPOT_LIGHTS > 0",
474
475 "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
476 "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
477 "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
478 "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
479 "uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];",
480 "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
481
482 "#endif",
483
484 "#ifdef WRAP_AROUND",
485
486 "uniform vec3 wrapRGB;",
487
488 "#endif"
489
490 ].join("\n"),
491
492 lights_lambert_vertex: [
493
494 "vLightFront = vec3( 0.0 );",
495
496 "#ifdef DOUBLE_SIDED",
497
498 "vLightBack = vec3( 0.0 );",
499
500 "#endif",
501
502 "transformedNormal = normalize( transformedNormal );",
503
504 "#if MAX_DIR_LIGHTS > 0",
505
506 "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
507
508 "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
509 "vec3 dirVector = normalize( lDirection.xyz );",
510
511 "float dotProduct = dot( transformedNormal, dirVector );",
512 "vec3 directionalLightWeighting = vec3( max( dotProduct, 0.0 ) );",
513
514 "#ifdef DOUBLE_SIDED",
515
516 "vec3 directionalLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
517
518 "#ifdef WRAP_AROUND",
519
520 "vec3 directionalLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
521
522 "#endif",
523
524 "#endif",
525
526 "#ifdef WRAP_AROUND",
527
528 "vec3 directionalLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
529 "directionalLightWeighting = mix( directionalLightWeighting, directionalLightWeightingHalf, wrapRGB );",
530
531 "#ifdef DOUBLE_SIDED",
532
533 "directionalLightWeightingBack = mix( directionalLightWeightingBack, directionalLightWeightingHalfBack, wrapRGB );",
534
535 "#endif",
536
537 "#endif",
538
539 "vLightFront += directionalLightColor[ i ] * directionalLightWeighting;",
540
541 "#ifdef DOUBLE_SIDED",
542
543 "vLightBack += directionalLightColor[ i ] * directionalLightWeightingBack;",
544
545 "#endif",
546
547 "}",
548
549 "#endif",
550
551 "#if MAX_POINT_LIGHTS > 0",
552
553 "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
554
555 "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
556 "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
557
558 "float lDistance = 1.0;",
559 "if ( pointLightDistance[ i ] > 0.0 )",
560 "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
561
562 "lVector = normalize( lVector );",
563 "float dotProduct = dot( transformedNormal, lVector );",
564
565 "vec3 pointLightWeighting = vec3( max( dotProduct, 0.0 ) );",
566
567 "#ifdef DOUBLE_SIDED",
568
569 "vec3 pointLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
570
571 "#ifdef WRAP_AROUND",
572
573 "vec3 pointLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
574
575 "#endif",
576
577 "#endif",
578
579 "#ifdef WRAP_AROUND",
580
581 "vec3 pointLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
582 "pointLightWeighting = mix( pointLightWeighting, pointLightWeightingHalf, wrapRGB );",
583
584 "#ifdef DOUBLE_SIDED",
585
586 "pointLightWeightingBack = mix( pointLightWeightingBack, pointLightWeightingHalfBack, wrapRGB );",
587
588 "#endif",
589
590 "#endif",
591
592 "vLightFront += pointLightColor[ i ] * pointLightWeighting * lDistance;",
593
594 "#ifdef DOUBLE_SIDED",
595
596 "vLightBack += pointLightColor[ i ] * pointLightWeightingBack * lDistance;",
597
598 "#endif",
599
600 "}",
601
602 "#endif",
603
604 "#if MAX_SPOT_LIGHTS > 0",
605
606 "for( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
607
608 "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
609 "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
610
611 "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - worldPosition.xyz ) );",
612
613 "if ( spotEffect > spotLightAngleCos[ i ] ) {",
614
615 "spotEffect = max( pow( spotEffect, spotLightExponent[ i ] ), 0.0 );",
616
617 "float lDistance = 1.0;",
618 "if ( spotLightDistance[ i ] > 0.0 )",
619 "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );",
620
621 "lVector = normalize( lVector );",
622
623 "float dotProduct = dot( transformedNormal, lVector );",
624 "vec3 spotLightWeighting = vec3( max( dotProduct, 0.0 ) );",
625
626 "#ifdef DOUBLE_SIDED",
627
628 "vec3 spotLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
629
630 "#ifdef WRAP_AROUND",
631
632 "vec3 spotLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
633
634 "#endif",
635
636 "#endif",
637
638 "#ifdef WRAP_AROUND",
639
640 "vec3 spotLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
641 "spotLightWeighting = mix( spotLightWeighting, spotLightWeightingHalf, wrapRGB );",
642
643 "#ifdef DOUBLE_SIDED",
644
645 "spotLightWeightingBack = mix( spotLightWeightingBack, spotLightWeightingHalfBack, wrapRGB );",
646
647 "#endif",
648
649 "#endif",
650
651 "vLightFront += spotLightColor[ i ] * spotLightWeighting * lDistance * spotEffect;",
652
653 "#ifdef DOUBLE_SIDED",
654
655 "vLightBack += spotLightColor[ i ] * spotLightWeightingBack * lDistance * spotEffect;",
656
657 "#endif",
658
659 "}",
660
661 "}",
662
663 "#endif",
664
665 "#if MAX_HEMI_LIGHTS > 0",
666
667 "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
668
669 "vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
670 "vec3 lVector = normalize( lDirection.xyz );",
671
672 "float dotProduct = dot( transformedNormal, lVector );",
673
674 "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
675 "float hemiDiffuseWeightBack = -0.5 * dotProduct + 0.5;",
676
677 "vLightFront += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
678
679 "#ifdef DOUBLE_SIDED",
680
681 "vLightBack += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeightBack );",
682
683 "#endif",
684
685 "}",
686
687 "#endif",
688
689 "vLightFront = vLightFront * diffuse + ambient * ambientLightColor + emissive;",
690
691 "#ifdef DOUBLE_SIDED",
692
693 "vLightBack = vLightBack * diffuse + ambient * ambientLightColor + emissive;",
694
695 "#endif"
696
697 ].join("\n"),
698
699 // LIGHTS PHONG
700
701 lights_phong_pars_vertex: [
702
703 "#ifndef PHONG_PER_PIXEL",
704
705 "#if MAX_POINT_LIGHTS > 0",
706
707 "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
708 "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
709
710 "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
711
712 "#endif",
713
714 "#if MAX_SPOT_LIGHTS > 0",
715
716 "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
717 "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
718
719 "varying vec4 vSpotLight[ MAX_SPOT_LIGHTS ];",
720
721 "#endif",
722
723 "#endif",
724
725 "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP )",
726
727 "varying vec3 vWorldPosition;",
728
729 "#endif"
730
731 ].join("\n"),
732
733
734 lights_phong_vertex: [
735
736 "#ifndef PHONG_PER_PIXEL",
737
738 "#if MAX_POINT_LIGHTS > 0",
739
740 "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
741
742 "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
743 "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
744
745 "float lDistance = 1.0;",
746 "if ( pointLightDistance[ i ] > 0.0 )",
747 "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
748
749 "vPointLight[ i ] = vec4( lVector, lDistance );",
750
751 "}",
752
753 "#endif",
754
755 "#if MAX_SPOT_LIGHTS > 0",
756
757 "for( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
758
759 "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
760 "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
761
762 "float lDistance = 1.0;",
763 "if ( spotLightDistance[ i ] > 0.0 )",
764 "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );",
765
766 "vSpotLight[ i ] = vec4( lVector, lDistance );",
767
768 "}",
769
770 "#endif",
771
772 "#endif",
773
774 "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP )",
775
776 "vWorldPosition = worldPosition.xyz;",
777
778 "#endif"
779
780 ].join("\n"),
781
782 lights_phong_pars_fragment: [
783
784 "uniform vec3 ambientLightColor;",
785
786 "#if MAX_DIR_LIGHTS > 0",
787
788 "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
789 "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
790
791 "#endif",
792
793 "#if MAX_HEMI_LIGHTS > 0",
794
795 "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
796 "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
797 "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
798
799 "#endif",
800
801 "#if MAX_POINT_LIGHTS > 0",
802
803 "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
804
805 "#ifdef PHONG_PER_PIXEL",
806
807 "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
808 "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
809
810 "#else",
811
812 "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
813
814 "#endif",
815
816 "#endif",
817
818 "#if MAX_SPOT_LIGHTS > 0",
819
820 "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
821 "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
822 "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
823 "uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];",
824 "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
825
826 "#ifdef PHONG_PER_PIXEL",
827
828 "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
829
830 "#else",
831
832 "varying vec4 vSpotLight[ MAX_SPOT_LIGHTS ];",
833
834 "#endif",
835
836 "#endif",
837
838 "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP )",
839
840 "varying vec3 vWorldPosition;",
841
842 "#endif",
843
844 "#ifdef WRAP_AROUND",
845
846 "uniform vec3 wrapRGB;",
847
848 "#endif",
849
850 "varying vec3 vViewPosition;",
851 "varying vec3 vNormal;"
852
853 ].join("\n"),
854
855 lights_phong_fragment: [
856
857 "vec3 normal = normalize( vNormal );",
858 "vec3 viewPosition = normalize( vViewPosition );",
859
860 "#ifdef DOUBLE_SIDED",
861
862 "normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
863
864 "#endif",
865
866 "#ifdef USE_NORMALMAP",
867
868 "normal = perturbNormal2Arb( -vViewPosition, normal );",
869
870 "#elif defined( USE_BUMPMAP )",
871
872 "normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );",
873
874 "#endif",
875
876 "#if MAX_POINT_LIGHTS > 0",
877
878 "vec3 pointDiffuse = vec3( 0.0 );",
879 "vec3 pointSpecular = vec3( 0.0 );",
880
881 "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
882
883 "#ifdef PHONG_PER_PIXEL",
884
885 "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
886 "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
887
888 "float lDistance = 1.0;",
889 "if ( pointLightDistance[ i ] > 0.0 )",
890 "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
891
892 "lVector = normalize( lVector );",
893
894 "#else",
895
896 "vec3 lVector = normalize( vPointLight[ i ].xyz );",
897 "float lDistance = vPointLight[ i ].w;",
898
899 "#endif",
900
901 // diffuse
902
903 "float dotProduct = dot( normal, lVector );",
904
905 "#ifdef WRAP_AROUND",
906
907 "float pointDiffuseWeightFull = max( dotProduct, 0.0 );",
908 "float pointDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
909
910 "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
911
912 "#else",
913
914 "float pointDiffuseWeight = max( dotProduct, 0.0 );",
915
916 "#endif",
917
918 "pointDiffuse += diffuse * pointLightColor[ i ] * pointDiffuseWeight * lDistance;",
919
920 // specular
921
922 "vec3 pointHalfVector = normalize( lVector + viewPosition );",
923 "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
924 "float pointSpecularWeight = specularStrength * max( pow( pointDotNormalHalf, shininess ), 0.0 );",
925
926 // 2.0 => 2.0001 is hack to work around ANGLE bug
927
928 "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
929
930 "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, pointHalfVector ), 5.0 );",
931 "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance * specularNormalization;",
932
933 "}",
934
935 "#endif",
936
937 "#if MAX_SPOT_LIGHTS > 0",
938
939 "vec3 spotDiffuse = vec3( 0.0 );",
940 "vec3 spotSpecular = vec3( 0.0 );",
941
942 "for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
943
944 "#ifdef PHONG_PER_PIXEL",
945
946 "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
947 "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
948
949 "float lDistance = 1.0;",
950 "if ( spotLightDistance[ i ] > 0.0 )",
951 "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );",
952
953 "lVector = normalize( lVector );",
954
955 "#else",
956
957 "vec3 lVector = normalize( vSpotLight[ i ].xyz );",
958 "float lDistance = vSpotLight[ i ].w;",
959
960 "#endif",
961
962 "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );",
963
964 "if ( spotEffect > spotLightAngleCos[ i ] ) {",
965
966 "spotEffect = max( pow( spotEffect, spotLightExponent[ i ] ), 0.0 );",
967
968 // diffuse
969
970 "float dotProduct = dot( normal, lVector );",
971
972 "#ifdef WRAP_AROUND",
973
974 "float spotDiffuseWeightFull = max( dotProduct, 0.0 );",
975 "float spotDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
976
977 "vec3 spotDiffuseWeight = mix( vec3 ( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );",
978
979 "#else",
980
981 "float spotDiffuseWeight = max( dotProduct, 0.0 );",
982
983 "#endif",
984
985 "spotDiffuse += diffuse * spotLightColor[ i ] * spotDiffuseWeight * lDistance * spotEffect;",
986
987 // specular
988
989 "vec3 spotHalfVector = normalize( lVector + viewPosition );",
990 "float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );",
991 "float spotSpecularWeight = specularStrength * max( pow( spotDotNormalHalf, shininess ), 0.0 );",
992
993 // 2.0 => 2.0001 is hack to work around ANGLE bug
994
995 "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
996
997 "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, spotHalfVector ), 5.0 );",
998 "spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * lDistance * specularNormalization * spotEffect;",
999
1000 "}",
1001
1002 "}",
1003
1004 "#endif",
1005
1006 "#if MAX_DIR_LIGHTS > 0",
1007
1008 "vec3 dirDiffuse = vec3( 0.0 );",
1009 "vec3 dirSpecular = vec3( 0.0 );" ,
1010
1011 "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
1012
1013 "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
1014 "vec3 dirVector = normalize( lDirection.xyz );",
1015
1016 // diffuse
1017
1018 "float dotProduct = dot( normal, dirVector );",
1019
1020 "#ifdef WRAP_AROUND",
1021
1022 "float dirDiffuseWeightFull = max( dotProduct, 0.0 );",
1023 "float dirDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
1024
1025 "vec3 dirDiffuseWeight = mix( vec3( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), wrapRGB );",
1026
1027 "#else",
1028
1029 "float dirDiffuseWeight = max( dotProduct, 0.0 );",
1030
1031 "#endif",
1032
1033 "dirDiffuse += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
1034
1035 // specular
1036
1037 "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
1038 "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
1039 "float dirSpecularWeight = specularStrength * max( pow( dirDotNormalHalf, shininess ), 0.0 );",
1040
1041 /*
1042 // fresnel term from skin shader
1043 "const float F0 = 0.128;",
1044
1045 "float base = 1.0 - dot( viewPosition, dirHalfVector );",
1046 "float exponential = pow( base, 5.0 );",
1047
1048 "float fresnel = exponential + F0 * ( 1.0 - exponential );",
1049 */
1050
1051 /*
1052 // fresnel term from fresnel shader
1053 "const float mFresnelBias = 0.08;",
1054 "const float mFresnelScale = 0.3;",
1055 "const float mFresnelPower = 5.0;",
1056
1057 "float fresnel = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( -viewPosition ), normal ), mFresnelPower );",
1058 */
1059
1060 // 2.0 => 2.0001 is hack to work around ANGLE bug
1061
1062 "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
1063
1064 //"dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;",
1065
1066 "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
1067 "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
1068
1069
1070 "}",
1071
1072 "#endif",
1073
1074 "#if MAX_HEMI_LIGHTS > 0",
1075
1076 "vec3 hemiDiffuse = vec3( 0.0 );",
1077 "vec3 hemiSpecular = vec3( 0.0 );" ,
1078
1079 "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
1080
1081 "vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
1082 "vec3 lVector = normalize( lDirection.xyz );",
1083
1084 // diffuse
1085
1086 "float dotProduct = dot( normal, lVector );",
1087 "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
1088
1089 "vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
1090
1091 "hemiDiffuse += diffuse * hemiColor;",
1092
1093 // specular (sky light)
1094
1095 "vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
1096 "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
1097 "float hemiSpecularWeightSky = specularStrength * max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );",
1098
1099 // specular (ground light)
1100
1101 "vec3 lVectorGround = -lVector;",
1102
1103 "vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
1104 "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
1105 "float hemiSpecularWeightGround = specularStrength * max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );",
1106
1107 "float dotProductGround = dot( normal, lVectorGround );",
1108
1109 // 2.0 => 2.0001 is hack to work around ANGLE bug
1110
1111 "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
1112
1113 "vec3 schlickSky = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, hemiHalfVectorSky ), 5.0 );",
1114 "vec3 schlickGround = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );",
1115 "hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
1116
1117 "}",
1118
1119 "#endif",
1120
1121 "vec3 totalDiffuse = vec3( 0.0 );",
1122 "vec3 totalSpecular = vec3( 0.0 );",
1123
1124 "#if MAX_DIR_LIGHTS > 0",
1125
1126 "totalDiffuse += dirDiffuse;",
1127 "totalSpecular += dirSpecular;",
1128
1129 "#endif",
1130
1131 "#if MAX_HEMI_LIGHTS > 0",
1132
1133 "totalDiffuse += hemiDiffuse;",
1134 "totalSpecular += hemiSpecular;",
1135
1136 "#endif",
1137
1138 "#if MAX_POINT_LIGHTS > 0",
1139
1140 "totalDiffuse += pointDiffuse;",
1141 "totalSpecular += pointSpecular;",
1142
1143 "#endif",
1144
1145 "#if MAX_SPOT_LIGHTS > 0",
1146
1147 "totalDiffuse += spotDiffuse;",
1148 "totalSpecular += spotSpecular;",
1149
1150 "#endif",
1151
1152 "#ifdef METAL",
1153
1154 "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient + totalSpecular );",
1155
1156 "#else",
1157
1158 "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient ) + totalSpecular;",
1159
1160 "#endif"
1161
1162 ].join("\n"),
1163
1164 // VERTEX COLORS
1165
1166 color_pars_fragment: [
1167
1168 "#ifdef USE_COLOR",
1169
1170 "varying vec3 vColor;",
1171
1172 "#endif"
1173
1174 ].join("\n"),
1175
1176
1177 color_fragment: [
1178
1179 "#ifdef USE_COLOR",
1180
1181 "gl_FragColor = gl_FragColor * vec4( vColor, 1.0 );",
1182
1183 "#endif"
1184
1185 ].join("\n"),
1186
1187 color_pars_vertex: [
1188
1189 "#ifdef USE_COLOR",
1190
1191 "varying vec3 vColor;",
1192
1193 "#endif"
1194
1195 ].join("\n"),
1196
1197
1198 color_vertex: [
1199
1200 "#ifdef USE_COLOR",
1201
1202 "#ifdef GAMMA_INPUT",
1203
1204 "vColor = color * color;",
1205
1206 "#else",
1207
1208 "vColor = color;",
1209
1210 "#endif",
1211
1212 "#endif"
1213
1214 ].join("\n"),
1215
1216 // SKINNING
1217
1218 skinning_pars_vertex: [
1219
1220 "#ifdef USE_SKINNING",
1221
1222 "#ifdef BONE_TEXTURE",
1223
1224 "uniform sampler2D boneTexture;",
1225 "uniform int boneTextureWidth;",
1226 "uniform int boneTextureHeight;",
1227
1228 "mat4 getBoneMatrix( const in float i ) {",
1229
1230 "float j = i * 4.0;",
1231 "float x = mod( j, float( boneTextureWidth ) );",
1232 "float y = floor( j / float( boneTextureWidth ) );",
1233
1234 "float dx = 1.0 / float( boneTextureWidth );",
1235 "float dy = 1.0 / float( boneTextureHeight );",
1236
1237 "y = dy * ( y + 0.5 );",
1238
1239 "vec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) );",
1240 "vec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) );",
1241 "vec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) );",
1242 "vec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) );",
1243
1244 "mat4 bone = mat4( v1, v2, v3, v4 );",
1245
1246 "return bone;",
1247
1248 "}",
1249
1250 "#else",
1251
1252 "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
1253
1254 "mat4 getBoneMatrix( const in float i ) {",
1255
1256 "mat4 bone = boneGlobalMatrices[ int(i) ];",
1257 "return bone;",
1258
1259 "}",
1260
1261 "#endif",
1262
1263 "#endif"
1264
1265 ].join("\n"),
1266
1267 skinbase_vertex: [
1268
1269 "#ifdef USE_SKINNING",
1270
1271 "mat4 boneMatX = getBoneMatrix( skinIndex.x );",
1272 "mat4 boneMatY = getBoneMatrix( skinIndex.y );",
1273
1274 "#endif"
1275
1276 ].join("\n"),
1277
1278 skinning_vertex: [
1279
1280 "#ifdef USE_SKINNING",
1281
1282 "#ifdef USE_MORPHTARGETS",
1283
1284 "vec4 skinVertex = vec4( morphed, 1.0 );",
1285
1286 "#else",
1287
1288 "vec4 skinVertex = vec4( position, 1.0 );",
1289
1290 "#endif",
1291
1292 "vec4 skinned = boneMatX * skinVertex * skinWeight.x;",
1293 "skinned += boneMatY * skinVertex * skinWeight.y;",
1294
1295 "#endif"
1296
1297 ].join("\n"),
1298
1299 // MORPHING
1300
1301 morphtarget_pars_vertex: [
1302
1303 "#ifdef USE_MORPHTARGETS",
1304
1305 "#ifndef USE_MORPHNORMALS",
1306
1307 "uniform float morphTargetInfluences[ 8 ];",
1308
1309 "#else",
1310
1311 "uniform float morphTargetInfluences[ 4 ];",
1312
1313 "#endif",
1314
1315 "#endif"
1316
1317 ].join("\n"),
1318
1319 morphtarget_vertex: [
1320
1321 "#ifdef USE_MORPHTARGETS",
1322
1323 "vec3 morphed = vec3( 0.0 );",
1324 "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
1325 "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
1326 "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
1327 "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
1328
1329 "#ifndef USE_MORPHNORMALS",
1330
1331 "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
1332 "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
1333 "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
1334 "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
1335
1336 "#endif",
1337
1338 "morphed += position;",
1339
1340 "#endif"
1341
1342 ].join("\n"),
1343
1344 default_vertex : [
1345
1346 "vec4 mvPosition;",
1347
1348 "#ifdef USE_SKINNING",
1349
1350 "mvPosition = modelViewMatrix * skinned;",
1351
1352 "#endif",
1353
1354 "#if !defined( USE_SKINNING ) && defined( USE_MORPHTARGETS )",
1355
1356 "mvPosition = modelViewMatrix * vec4( morphed, 1.0 );",
1357
1358 "#endif",
1359
1360 "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHTARGETS )",
1361
1362 "mvPosition = modelViewMatrix * vec4( position, 1.0 );",
1363
1364 "#endif",
1365
1366 "gl_Position = projectionMatrix * mvPosition;"
1367
1368 ].join("\n"),
1369
1370 morphnormal_vertex: [
1371
1372 "#ifdef USE_MORPHNORMALS",
1373
1374 "vec3 morphedNormal = vec3( 0.0 );",
1375
1376 "morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];",
1377 "morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];",
1378 "morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];",
1379 "morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];",
1380
1381 "morphedNormal += normal;",
1382
1383 "#endif"
1384
1385 ].join("\n"),
1386
1387 skinnormal_vertex: [
1388
1389 "#ifdef USE_SKINNING",
1390
1391 "mat4 skinMatrix = skinWeight.x * boneMatX;",
1392 "skinMatrix += skinWeight.y * boneMatY;",
1393
1394 "#ifdef USE_MORPHNORMALS",
1395
1396 "vec4 skinnedNormal = skinMatrix * vec4( morphedNormal, 0.0 );",
1397
1398 "#else",
1399
1400 "vec4 skinnedNormal = skinMatrix * vec4( normal, 0.0 );",
1401
1402 "#endif",
1403
1404 "#endif"
1405
1406 ].join("\n"),
1407
1408 defaultnormal_vertex: [
1409
1410 "vec3 objectNormal;",
1411
1412 "#ifdef USE_SKINNING",
1413
1414 "objectNormal = skinnedNormal.xyz;",
1415
1416 "#endif",
1417
1418 "#if !defined( USE_SKINNING ) && defined( USE_MORPHNORMALS )",
1419
1420 "objectNormal = morphedNormal;",
1421
1422 "#endif",
1423
1424 "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHNORMALS )",
1425
1426 "objectNormal = normal;",
1427
1428 "#endif",
1429
1430 "#ifdef FLIP_SIDED",
1431
1432 "objectNormal = -objectNormal;",
1433
1434 "#endif",
1435
1436 "vec3 transformedNormal = normalMatrix * objectNormal;"
1437
1438 ].join("\n"),
1439
1440 // SHADOW MAP
1441
1442 // based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples
1443 // http://spidergl.org/example.php?id=6
1444 // http://fabiensanglard.net/shadowmapping
1445
1446 shadowmap_pars_fragment: [
1447
1448 "#ifdef USE_SHADOWMAP",
1449
1450 "uniform sampler2D shadowMap[ MAX_SHADOWS ];",
1451 "uniform vec2 shadowMapSize[ MAX_SHADOWS ];",
1452
1453 "uniform float shadowDarkness[ MAX_SHADOWS ];",
1454 "uniform float shadowBias[ MAX_SHADOWS ];",
1455
1456 "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
1457
1458 "float unpackDepth( const in vec4 rgba_depth ) {",
1459
1460 "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 );",
1461 "float depth = dot( rgba_depth, bit_shift );",
1462 "return depth;",
1463
1464 "}",
1465
1466 "#endif"
1467
1468 ].join("\n"),
1469
1470 shadowmap_fragment: [
1471
1472 "#ifdef USE_SHADOWMAP",
1473
1474 "#ifdef SHADOWMAP_DEBUG",
1475
1476 "vec3 frustumColors[3];",
1477 "frustumColors[0] = vec3( 1.0, 0.5, 0.0 );",
1478 "frustumColors[1] = vec3( 0.0, 1.0, 0.8 );",
1479 "frustumColors[2] = vec3( 0.0, 0.5, 1.0 );",
1480
1481 "#endif",
1482
1483 "#ifdef SHADOWMAP_CASCADE",
1484
1485 "int inFrustumCount = 0;",
1486
1487 "#endif",
1488
1489 "float fDepth;",
1490 "vec3 shadowColor = vec3( 1.0 );",
1491
1492 "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
1493
1494 "vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;",
1495
1496 // "if ( something && something )" breaks ATI OpenGL shader compiler
1497 // "if ( all( something, something ) )" using this instead
1498
1499 "bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );",
1500 "bool inFrustum = all( inFrustumVec );",
1501
1502 // don't shadow pixels outside of light frustum
1503 // use just first frustum (for cascades)
1504 // don't shadow pixels behind far plane of light frustum
1505
1506 "#ifdef SHADOWMAP_CASCADE",
1507
1508 "inFrustumCount += int( inFrustum );",
1509 "bvec3 frustumTestVec = bvec3( inFrustum, inFrustumCount == 1, shadowCoord.z <= 1.0 );",
1510
1511 "#else",
1512
1513 "bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );",
1514
1515 "#endif",
1516
1517 "bool frustumTest = all( frustumTestVec );",
1518
1519 "if ( frustumTest ) {",
1520
1521 "shadowCoord.z += shadowBias[ i ];",
1522
1523 "#if defined( SHADOWMAP_TYPE_PCF )",
1524
1525 // Percentage-close filtering
1526 // (9 pixel kernel)
1527 // http://fabiensanglard.net/shadowmappingPCF/
1528
1529 "float shadow = 0.0;",
1530
1531 /*
1532 // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
1533 // must enroll loop manually
1534
1535 "for ( float y = -1.25; y <= 1.25; y += 1.25 )",
1536 "for ( float x = -1.25; x <= 1.25; x += 1.25 ) {",
1537
1538 "vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );",
1539
1540 // doesn't seem to produce any noticeable visual difference compared to simple "texture2D" lookup
1541 //"vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );",
1542
1543 "float fDepth = unpackDepth( rgbaDepth );",
1544
1545 "if ( fDepth < shadowCoord.z )",
1546 "shadow += 1.0;",
1547
1548 "}",
1549
1550 "shadow /= 9.0;",
1551
1552 */
1553
1554 "const float shadowDelta = 1.0 / 9.0;",
1555
1556 "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
1557 "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
1558
1559 "float dx0 = -1.25 * xPixelOffset;",
1560 "float dy0 = -1.25 * yPixelOffset;",
1561 "float dx1 = 1.25 * xPixelOffset;",
1562 "float dy1 = 1.25 * yPixelOffset;",
1563
1564 "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
1565 "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
1566
1567 "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
1568 "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
1569
1570 "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
1571 "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
1572
1573 "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
1574 "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
1575
1576 "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
1577 "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
1578
1579 "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
1580 "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
1581
1582 "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
1583 "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
1584
1585 "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
1586 "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
1587
1588 "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
1589 "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
1590
1591 "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
1592
1593 "#elif defined( SHADOWMAP_TYPE_PCF_SOFT )",
1594
1595 // Percentage-close filtering
1596 // (9 pixel kernel)
1597 // http://fabiensanglard.net/shadowmappingPCF/
1598
1599 "float shadow = 0.0;",
1600
1601 "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
1602 "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
1603
1604 "float dx0 = -1.0 * xPixelOffset;",
1605 "float dy0 = -1.0 * yPixelOffset;",
1606 "float dx1 = 1.0 * xPixelOffset;",
1607 "float dy1 = 1.0 * yPixelOffset;",
1608
1609 "mat3 shadowKernel;",
1610 "mat3 depthKernel;",
1611
1612 "depthKernel[0][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
1613 "depthKernel[0][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
1614 "depthKernel[0][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
1615 "depthKernel[1][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
1616 "depthKernel[1][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
1617 "depthKernel[1][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
1618 "depthKernel[2][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
1619 "depthKernel[2][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
1620 "depthKernel[2][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
1621
1622 "vec3 shadowZ = vec3( shadowCoord.z );",
1623 "shadowKernel[0] = vec3(lessThan(depthKernel[0], shadowZ ));",
1624 "shadowKernel[0] *= vec3(0.25);",
1625
1626 "shadowKernel[1] = vec3(lessThan(depthKernel[1], shadowZ ));",
1627 "shadowKernel[1] *= vec3(0.25);",
1628
1629 "shadowKernel[2] = vec3(lessThan(depthKernel[2], shadowZ ));",
1630 "shadowKernel[2] *= vec3(0.25);",
1631
1632 "vec2 fractionalCoord = 1.0 - fract( shadowCoord.xy * shadowMapSize[i].xy );",
1633
1634 "shadowKernel[0] = mix( shadowKernel[1], shadowKernel[0], fractionalCoord.x );",
1635 "shadowKernel[1] = mix( shadowKernel[2], shadowKernel[1], fractionalCoord.x );",
1636
1637 "vec4 shadowValues;",
1638 "shadowValues.x = mix( shadowKernel[0][1], shadowKernel[0][0], fractionalCoord.y );",
1639 "shadowValues.y = mix( shadowKernel[0][2], shadowKernel[0][1], fractionalCoord.y );",
1640 "shadowValues.z = mix( shadowKernel[1][1], shadowKernel[1][0], fractionalCoord.y );",
1641 "shadowValues.w = mix( shadowKernel[1][2], shadowKernel[1][1], fractionalCoord.y );",
1642
1643 "shadow = dot( shadowValues, vec4( 1.0 ) );",
1644
1645 "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
1646
1647 "#else",
1648
1649 "vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );",
1650 "float fDepth = unpackDepth( rgbaDepth );",
1651
1652 "if ( fDepth < shadowCoord.z )",
1653
1654 // spot with multiple shadows is darker
1655
1656 "shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );",
1657
1658 // spot with multiple shadows has the same color as single shadow spot
1659
1660 //"shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );",
1661
1662 "#endif",
1663
1664 "}",
1665
1666
1667 "#ifdef SHADOWMAP_DEBUG",
1668
1669 "#ifdef SHADOWMAP_CASCADE",
1670
1671 "if ( inFrustum && inFrustumCount == 1 ) gl_FragColor.xyz *= frustumColors[ i ];",
1672
1673 "#else",
1674
1675 "if ( inFrustum ) gl_FragColor.xyz *= frustumColors[ i ];",
1676
1677 "#endif",
1678
1679 "#endif",
1680
1681 "}",
1682
1683 "#ifdef GAMMA_OUTPUT",
1684
1685 "shadowColor *= shadowColor;",
1686
1687 "#endif",
1688
1689 "gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;",
1690
1691 "#endif"
1692
1693 ].join("\n"),
1694
1695 shadowmap_pars_vertex: [
1696
1697 "#ifdef USE_SHADOWMAP",
1698
1699 "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
1700 "uniform mat4 shadowMatrix[ MAX_SHADOWS ];",
1701
1702 "#endif"
1703
1704 ].join("\n"),
1705
1706 shadowmap_vertex: [
1707
1708 "#ifdef USE_SHADOWMAP",
1709
1710 "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
1711
1712 "vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;",
1713
1714 "}",
1715
1716 "#endif"
1717
1718 ].join("\n"),
1719
1720 // ALPHATEST
1721
1722 alphatest_fragment: [
1723
1724 "#ifdef ALPHATEST",
1725
1726 "if ( gl_FragColor.a < ALPHATEST ) discard;",
1727
1728 "#endif"
1729
1730 ].join("\n"),
1731
1732 // LINEAR SPACE
1733
1734 linear_to_gamma_fragment: [
1735
1736 "#ifdef GAMMA_OUTPUT",
1737
1738 "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
1739
1740 "#endif"
1741
1742 ].join("\n")
1743
1744
1745};
Note: See TracBrowser for help on using the repository browser.