Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore/threejs/Shaders.js
4627 views
1
/**
2
* @author Thibaut Despoulain / http://bkcore.com
3
* @author alteredq / http://alteredqualia.com/
4
* @author mr.doob / http://mrdoob.com/
5
*/
6
var bkcore = bkcore || {};
7
bkcore.threejs = bkcore.threejs || {};
8
9
bkcore.threejs.Shaders =
10
{
11
'additive' : {
12
uniforms: {
13
tDiffuse: { type: "t", value: 0, texture: null },
14
tAdd: { type: "t", value: 1, texture: null },
15
fCoeff: { type: "f", value: 1.0 }
16
},
17
18
vertexShader: [
19
"varying vec2 vUv;",
20
21
"void main() {",
22
23
"vUv = uv;",
24
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
25
26
"}"
27
].join("\n"),
28
29
fragmentShader: [
30
"uniform sampler2D tDiffuse;",
31
"uniform sampler2D tAdd;",
32
"uniform float fCoeff;",
33
34
"varying vec2 vUv;",
35
36
"void main() {",
37
38
"vec4 texel = texture2D( tDiffuse, vUv );",
39
"vec4 add = texture2D( tAdd, vUv );",
40
"gl_FragColor = texel + add * fCoeff * add.a;",
41
42
"}"
43
].join("\n")
44
},
45
46
/* ------------------------------------------------------------------------------------------------
47
// Hexagonal Vignette shader
48
// by BKcore.com
49
------------------------------------------------------------------------------------------------ */
50
51
'hexvignette': {
52
53
uniforms: {
54
55
tDiffuse: { type: "t", value: 0, texture: null },
56
tHex: {type: "t", value: 1, texture: null},
57
size: {type: "f", value: 512.0},
58
rx: {type: "f", value: 1024.0},
59
ry: {type: "f", value: 768.0},
60
color: {type: "c", value: new THREE.Color(0x458ab1)}
61
62
},
63
64
vertexShader: [
65
66
"varying vec2 vUv;",
67
68
"void main() {",
69
70
"vUv = uv;",
71
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
72
73
"}"
74
75
].join("\n"),
76
77
fragmentShader: [
78
79
"uniform float size;",
80
"uniform float rx;",
81
"uniform float ry;",
82
83
"uniform vec3 color;",
84
85
"uniform sampler2D tDiffuse;",
86
"uniform sampler2D tHex;",
87
88
"varying vec2 vUv;",
89
90
"void main() {",
91
92
"vec4 vcolor = vec4(color,1.0);",
93
94
"vec2 hexuv;",
95
"hexuv.x = mod(vUv.x * rx, size) / size;",
96
"hexuv.y = mod(vUv.y * ry, size) / size;",
97
"vec4 hex = texture2D( tHex, hexuv );",
98
99
"float tolerance = 0.2;",
100
"float vignette_size = 0.6;",
101
"vec2 powers = pow(abs(vec2(vUv.x - 0.5,vUv.y - 0.5)),vec2(2.0));",
102
"float radiusSqrd = vignette_size*vignette_size;",
103
"float gradient = smoothstep(radiusSqrd-tolerance, radiusSqrd+tolerance, powers.x+powers.y);",
104
105
"vec2 uv = ( vUv - vec2( 0.5 ) );",
106
107
"vec2 sample = uv * gradient * 0.5 * (1.0-hex.r);",
108
109
"vec4 texel = texture2D( tDiffuse, vUv-sample );",
110
"gl_FragColor = (((1.0-hex.r)*vcolor) * 0.5 * gradient) + vec4( mix( texel.rgb, vcolor.xyz*0.7, dot( uv, uv ) ), texel.a );",
111
112
"}"
113
114
].join("\n")
115
116
},
117
118
/* -------------------------------------------------------------------------
119
// Normal map shader (perpixel)
120
// - Blinn-Phong
121
// - normal + diffuse + specular + AO + displacement + reflection + shadow maps
122
// - PER-PIXEL point and directional lights (use with "lights: true" material option)
123
// - modified by BKcore
124
------------------------------------------------------------------------- */
125
126
'normal' : {
127
128
uniforms: THREE.UniformsUtils.merge( [
129
130
THREE.UniformsLib[ "fog" ],
131
THREE.UniformsLib[ "lights" ],
132
THREE.UniformsLib[ "shadowmap" ],
133
134
{
135
136
"enableAO" : { type: "i", value: 0 },
137
"enableDiffuse" : { type: "i", value: 0 },
138
"enableSpecular" : { type: "i", value: 0 },
139
"enableReflection": { type: "i", value: 0 },
140
141
"tDiffuse" : { type: "t", value: 0, texture: null },
142
"tCube" : { type: "t", value: 1, texture: null },
143
"tNormal" : { type: "t", value: 2, texture: null },
144
"tSpecular" : { type: "t", value: 3, texture: null },
145
"tAO" : { type: "t", value: 4, texture: null },
146
"tDisplacement": { type: "t", value: 5, texture: null },
147
148
"uNormalScale": { type: "f", value: 1.0 },
149
150
"uDisplacementBias": { type: "f", value: 0.0 },
151
"uDisplacementScale": { type: "f", value: 1.0 },
152
153
"uDiffuseColor": { type: "c", value: new THREE.Color( 0xffffff ) },
154
"uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
155
"uAmbientColor": { type: "c", value: new THREE.Color( 0xffffff ) },
156
"uShininess": { type: "f", value: 30 },
157
"uOpacity": { type: "f", value: 1 },
158
159
"uReflectivity": { type: "f", value: 0.5 },
160
161
"uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },
162
"uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
163
164
"wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
165
166
}
167
168
] ),
169
170
fragmentShader: [
171
172
"uniform vec3 uAmbientColor;",
173
"uniform vec3 uDiffuseColor;",
174
"uniform vec3 uSpecularColor;",
175
"uniform float uShininess;",
176
"uniform float uOpacity;",
177
178
"uniform bool enableDiffuse;",
179
"uniform bool enableSpecular;",
180
"uniform bool enableAO;",
181
"uniform bool enableReflection;",
182
183
"uniform sampler2D tDiffuse;",
184
"uniform sampler2D tNormal;",
185
"uniform sampler2D tSpecular;",
186
"uniform sampler2D tAO;",
187
188
"uniform samplerCube tCube;",
189
190
"uniform float uNormalScale;",
191
"uniform float uReflectivity;",
192
193
"varying vec3 vTangent;",
194
"varying vec3 vBinormal;",
195
"varying vec3 vNormal;",
196
"varying vec2 vUv;",
197
198
"uniform vec3 ambientLightColor;",
199
200
"#if MAX_DIR_LIGHTS > 0",
201
"uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
202
"uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
203
"#endif",
204
205
"#if MAX_POINT_LIGHTS > 0",
206
"uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
207
"uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
208
"uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
209
"#endif",
210
211
"#ifdef WRAP_AROUND",
212
"uniform vec3 wrapRGB;",
213
"#endif",
214
215
"varying vec3 vViewPosition;",
216
217
THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
218
THREE.ShaderChunk[ "fog_pars_fragment" ],
219
220
"void main() {",
221
222
"gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
223
224
"vec3 specularTex = vec3( 1.0 );",
225
226
"vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
227
"normalTex.xy *= uNormalScale;",
228
"normalTex = normalize( normalTex );",
229
230
"if( enableDiffuse ) {",
231
232
"#ifdef GAMMA_INPUT",
233
234
"vec4 texelColor = texture2D( tDiffuse, vUv );",
235
"texelColor.xyz *= texelColor.xyz;",
236
237
"gl_FragColor = gl_FragColor * texelColor;",
238
239
"#else",
240
241
"gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",
242
243
"#endif",
244
245
"}",
246
247
"if( enableAO ) {",
248
249
"#ifdef GAMMA_INPUT",
250
251
"vec4 aoColor = texture2D( tAO, vUv );",
252
"aoColor.xyz *= aoColor.xyz;",
253
254
"gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;",
255
256
"#else",
257
258
"gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",
259
260
"#endif",
261
262
"}",
263
264
"if( enableSpecular )",
265
"specularTex = texture2D( tSpecular, vUv ).xyz;",
266
267
"mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",
268
"vec3 finalNormal = tsb * normalTex;",
269
270
"vec3 normal = normalize( finalNormal );",
271
"vec3 viewPosition = normalize( vViewPosition );",
272
273
"#ifdef DOUBLE_SIDED",
274
275
"normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
276
277
"#endif",
278
279
// point lights
280
281
"#if MAX_POINT_LIGHTS > 0",
282
283
"vec3 pointDiffuse = vec3( 0.0 );",
284
"vec3 pointSpecular = vec3( 0.0 );",
285
286
"for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
287
288
"vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
289
"vec3 pointVector = lPosition.xyz + vViewPosition.xyz;",
290
291
"float pointDistance = 1.0;",
292
"if ( pointLightDistance[ i ] > 0.0 )",
293
"pointDistance = 1.0 - min( ( length( pointVector ) / pointLightDistance[ i ] ), 1.0 );",
294
295
"pointVector = normalize( pointVector );",
296
297
// diffuse
298
299
"#ifdef WRAP_AROUND",
300
301
"float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );",
302
"float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );",
303
304
"vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
305
306
"#else",
307
308
"float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
309
310
"#endif",
311
312
"pointDiffuse += pointDistance * pointLightColor[ i ] * uDiffuseColor * pointDiffuseWeight;",
313
314
// specular
315
316
"vec3 pointHalfVector = normalize( pointVector + viewPosition );",
317
"float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
318
"float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, uShininess ), 0.0 );",
319
320
"#ifdef PHYSICALLY_BASED_SHADING",
321
322
// 2.0 => 2.0001 is hack to work around ANGLE bug
323
324
"float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
325
326
"vec3 schlick = specularTex + vec3( 1.0 - specularTex ) * pow( 1.0 - dot( pointVector, pointHalfVector ), 5.0 );",
327
"pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",
328
329
"#else",
330
331
"pointSpecular += pointDistance * pointLightColor[ i ] * specularTex * pointSpecularWeight * pointDiffuseWeight;",
332
333
"#endif",
334
335
"}",
336
337
"#endif",
338
339
// directional lights
340
341
"#if MAX_DIR_LIGHTS > 0",
342
343
"vec3 dirDiffuse = vec3( 0.0 );",
344
"vec3 dirSpecular = vec3( 0.0 );",
345
346
"for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
347
348
"vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
349
"vec3 dirVector = normalize( lDirection.xyz );",
350
351
// diffuse
352
353
"#ifdef WRAP_AROUND",
354
355
"float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );",
356
"float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
357
358
"vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );",
359
360
"#else",
361
362
"float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
363
364
"#endif",
365
366
"dirDiffuse += directionalLightColor[ i ] * uDiffuseColor * dirDiffuseWeight;",
367
368
// specular
369
370
"vec3 dirHalfVector = normalize( dirVector + viewPosition );",
371
"float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
372
"float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, uShininess ), 0.0 );",
373
374
"#ifdef PHYSICALLY_BASED_SHADING",
375
376
// 2.0 => 2.0001 is hack to work around ANGLE bug
377
378
"float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
379
380
"vec3 schlick = specularTex + vec3( 1.0 - specularTex ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
381
"dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
382
383
"#else",
384
385
"dirSpecular += directionalLightColor[ i ] * specularTex * dirSpecularWeight * dirDiffuseWeight;",
386
387
"#endif",
388
389
"}",
390
391
"#endif",
392
393
// all lights contribution summation
394
395
"vec3 totalDiffuse = vec3( 0.0 );",
396
"vec3 totalSpecular = vec3( 0.0 );",
397
398
"#if MAX_DIR_LIGHTS > 0",
399
400
"totalDiffuse += dirDiffuse;",
401
"totalSpecular += dirSpecular;",
402
403
"#endif",
404
405
"#if MAX_POINT_LIGHTS > 0",
406
407
"totalDiffuse += pointDiffuse;",
408
"totalSpecular += pointSpecular;",
409
410
"#endif",
411
412
"gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor) + totalSpecular;",
413
414
"if ( enableReflection ) {",
415
416
"#ifdef DOUBLE_SIDED",
417
418
"float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
419
420
"#else",
421
422
"float flipNormal = 1.0;",
423
424
"#endif",
425
426
"vec3 wPos = cameraPosition - vViewPosition;",
427
"vec3 vReflect = reflect( normalize( wPos ), normal );",
428
429
"vec4 cubeColor = textureCube( tCube, flipNormal*vec3( -vReflect.x, vReflect.yz ) );",
430
431
"#ifdef GAMMA_INPUT",
432
433
"cubeColor.xyz *= cubeColor.xyz;",
434
435
"#endif",
436
437
"gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * uReflectivity );",
438
439
"}",
440
441
THREE.ShaderChunk[ "shadowmap_fragment" ],
442
THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
443
THREE.ShaderChunk[ "fog_fragment" ],
444
445
"}"
446
447
].join("\n"),
448
449
vertexShader: [
450
451
"attribute vec4 tangent;",
452
453
"uniform vec2 uOffset;",
454
"uniform vec2 uRepeat;",
455
456
"#ifdef VERTEX_TEXTURES",
457
458
"uniform sampler2D tDisplacement;",
459
"uniform float uDisplacementScale;",
460
"uniform float uDisplacementBias;",
461
462
"#endif",
463
464
"varying vec3 vTangent;",
465
"varying vec3 vBinormal;",
466
"varying vec3 vNormal;",
467
"varying vec2 vUv;",
468
469
"varying vec3 vViewPosition;",
470
471
THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
472
473
"void main() {",
474
475
"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
476
477
"vViewPosition = -mvPosition.xyz;",
478
479
// normal, tangent and binormal vectors
480
481
"vNormal = normalMatrix * normal;",
482
"vTangent = normalMatrix * tangent.xyz;",
483
"vBinormal = cross( vNormal, vTangent ) * tangent.w;",
484
485
"vUv = uv * uRepeat + uOffset;",
486
487
// displacement mapping
488
489
"#ifdef VERTEX_TEXTURES",
490
491
"vec3 dv = texture2D( tDisplacement, uv ).xyz;",
492
"float df = uDisplacementScale * dv.x + uDisplacementBias;",
493
"vec4 displacedPosition = vec4( normalize( vNormal.xyz ) * df, 0.0 ) + mvPosition;",
494
"gl_Position = projectionMatrix * displacedPosition;",
495
496
"#else",
497
498
"gl_Position = projectionMatrix * mvPosition;",
499
500
"#endif",
501
502
THREE.ShaderChunk[ "shadowmap_vertex" ],
503
504
"}"
505
506
].join("\n")
507
508
},
509
510
/* -------------------------------------------------------------------------
511
// Normal map shader
512
// - Blinn-Phong
513
// - normal + diffuse + specular + AO + displacement + reflection + shadow maps
514
// - PER-VERTEX point and directional lights (use with "lights: true" material option)
515
------------------------------------------------------------------------- */
516
517
'normalV' : {
518
519
uniforms: THREE.UniformsUtils.merge( [
520
521
THREE.UniformsLib[ "fog" ],
522
THREE.UniformsLib[ "lights" ],
523
THREE.UniformsLib[ "shadowmap" ],
524
525
{
526
527
"enableAO" : { type: "i", value: 0 },
528
"enableDiffuse" : { type: "i", value: 0 },
529
"enableSpecular" : { type: "i", value: 0 },
530
"enableReflection": { type: "i", value: 0 },
531
532
"tDiffuse" : { type: "t", value: 0, texture: null },
533
"tCube" : { type: "t", value: 1, texture: null },
534
"tNormal" : { type: "t", value: 2, texture: null },
535
"tSpecular" : { type: "t", value: 3, texture: null },
536
"tAO" : { type: "t", value: 4, texture: null },
537
"tDisplacement": { type: "t", value: 5, texture: null },
538
539
"uNormalScale": { type: "f", value: 1.0 },
540
541
"uDisplacementBias": { type: "f", value: 0.0 },
542
"uDisplacementScale": { type: "f", value: 1.0 },
543
544
"uDiffuseColor": { type: "c", value: new THREE.Color( 0xffffff ) },
545
"uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
546
"uAmbientColor": { type: "c", value: new THREE.Color( 0xffffff ) },
547
"uShininess": { type: "f", value: 30 },
548
"uOpacity": { type: "f", value: 1 },
549
550
"uReflectivity": { type: "f", value: 0.5 },
551
552
"uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },
553
"uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
554
555
"wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
556
557
}
558
559
] ),
560
561
fragmentShader: [
562
563
"uniform vec3 uAmbientColor;",
564
"uniform vec3 uDiffuseColor;",
565
"uniform vec3 uSpecularColor;",
566
"uniform float uShininess;",
567
"uniform float uOpacity;",
568
569
"uniform bool enableDiffuse;",
570
"uniform bool enableSpecular;",
571
"uniform bool enableAO;",
572
"uniform bool enableReflection;",
573
574
"uniform sampler2D tDiffuse;",
575
"uniform sampler2D tNormal;",
576
"uniform sampler2D tSpecular;",
577
"uniform sampler2D tAO;",
578
579
"uniform samplerCube tCube;",
580
581
"uniform float uNormalScale;",
582
"uniform float uReflectivity;",
583
584
"varying vec3 vTangent;",
585
"varying vec3 vBinormal;",
586
"varying vec3 vNormal;",
587
"varying vec2 vUv;",
588
589
"uniform vec3 ambientLightColor;",
590
591
"#if MAX_DIR_LIGHTS > 0",
592
"uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
593
"uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
594
"#endif",
595
596
"#if MAX_POINT_LIGHTS > 0",
597
"uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
598
"varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
599
"#endif",
600
601
"#ifdef WRAP_AROUND",
602
"uniform vec3 wrapRGB;",
603
"#endif",
604
605
"varying vec3 vViewPosition;",
606
607
THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
608
THREE.ShaderChunk[ "fog_pars_fragment" ],
609
610
"void main() {",
611
612
"gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
613
614
"vec3 specularTex = vec3( 1.0 );",
615
616
"vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
617
"normalTex.xy *= uNormalScale;",
618
"normalTex = normalize( normalTex );",
619
620
"if( enableDiffuse ) {",
621
622
"#ifdef GAMMA_INPUT",
623
624
"vec4 texelColor = texture2D( tDiffuse, vUv );",
625
"texelColor.xyz *= texelColor.xyz;",
626
627
"gl_FragColor = gl_FragColor * texelColor;",
628
629
"#else",
630
631
"gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",
632
633
"#endif",
634
635
"}",
636
637
"if( enableAO ) {",
638
639
"#ifdef GAMMA_INPUT",
640
641
"vec4 aoColor = texture2D( tAO, vUv );",
642
"aoColor.xyz *= aoColor.xyz;",
643
644
"gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;",
645
646
"#else",
647
648
"gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",
649
650
"#endif",
651
652
"}",
653
654
"if( enableSpecular )",
655
"specularTex = texture2D( tSpecular, vUv ).xyz;",
656
657
"mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",
658
"vec3 finalNormal = tsb * normalTex;",
659
660
"vec3 normal = normalize( finalNormal );",
661
"vec3 viewPosition = normalize( vViewPosition );",
662
663
// point lights
664
665
"#if MAX_POINT_LIGHTS > 0",
666
667
"vec3 pointDiffuse = vec3( 0.0 );",
668
"vec3 pointSpecular = vec3( 0.0 );",
669
670
"for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
671
672
"vec3 pointVector = normalize( vPointLight[ i ].xyz );",
673
"float pointDistance = vPointLight[ i ].w;",
674
675
// diffuse
676
677
"#ifdef WRAP_AROUND",
678
679
"float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );",
680
"float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );",
681
682
"vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
683
684
"#else",
685
686
"float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
687
688
"#endif",
689
690
"pointDiffuse += pointDistance * pointLightColor[ i ] * uDiffuseColor * pointDiffuseWeight;",
691
692
// specular
693
694
"vec3 pointHalfVector = normalize( pointVector + viewPosition );",
695
"float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
696
"float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, uShininess ), 0.0 );",
697
698
"#ifdef PHYSICALLY_BASED_SHADING",
699
700
// 2.0 => 2.0001 is hack to work around ANGLE bug
701
702
"float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
703
704
"vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( pointVector, pointHalfVector ), 5.0 );",
705
"pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",
706
707
"#else",
708
709
"pointSpecular += pointDistance * pointLightColor[ i ] * uSpecularColor * pointSpecularWeight * pointDiffuseWeight;",
710
711
"#endif",
712
713
"}",
714
715
"#endif",
716
717
// directional lights
718
719
"#if MAX_DIR_LIGHTS > 0",
720
721
"vec3 dirDiffuse = vec3( 0.0 );",
722
"vec3 dirSpecular = vec3( 0.0 );",
723
724
"for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
725
726
"vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
727
"vec3 dirVector = normalize( lDirection.xyz );",
728
729
// diffuse
730
731
"#ifdef WRAP_AROUND",
732
733
"float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );",
734
"float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
735
736
"vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );",
737
738
"#else",
739
740
"float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
741
742
"#endif",
743
744
"dirDiffuse += directionalLightColor[ i ] * uDiffuseColor * dirDiffuseWeight;",
745
746
// specular
747
748
"vec3 dirHalfVector = normalize( dirVector + viewPosition );",
749
"float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
750
"float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, uShininess ), 0.0 );",
751
752
"#ifdef PHYSICALLY_BASED_SHADING",
753
754
// 2.0 => 2.0001 is hack to work around ANGLE bug
755
756
"float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
757
758
"vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
759
"dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
760
761
"#else",
762
763
"dirSpecular += directionalLightColor[ i ] * uSpecularColor * dirSpecularWeight * dirDiffuseWeight;",
764
765
"#endif",
766
767
"}",
768
769
"#endif",
770
771
// all lights contribution summation
772
773
"vec3 totalDiffuse = vec3( 0.0 );",
774
"vec3 totalSpecular = vec3( 0.0 );",
775
776
"#if MAX_DIR_LIGHTS > 0",
777
778
"totalDiffuse += dirDiffuse;",
779
"totalSpecular += dirSpecular;",
780
781
"#endif",
782
783
"#if MAX_POINT_LIGHTS > 0",
784
785
"totalDiffuse += pointDiffuse;",
786
"totalSpecular += pointSpecular;",
787
788
"#endif",
789
790
"gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor) + totalSpecular;",
791
792
"if ( enableReflection ) {",
793
794
"vec3 wPos = cameraPosition - vViewPosition;",
795
"vec3 vReflect = reflect( normalize( wPos ), normal );",
796
797
"vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
798
799
"#ifdef GAMMA_INPUT",
800
801
"cubeColor.xyz *= cubeColor.xyz;",
802
803
"#endif",
804
805
"gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * uReflectivity );",
806
807
"}",
808
809
THREE.ShaderChunk[ "shadowmap_fragment" ],
810
THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
811
THREE.ShaderChunk[ "fog_fragment" ],
812
813
"}"
814
815
].join("\n"),
816
817
vertexShader: [
818
819
"attribute vec4 tangent;",
820
821
"uniform vec2 uOffset;",
822
"uniform vec2 uRepeat;",
823
824
"#ifdef VERTEX_TEXTURES",
825
826
"uniform sampler2D tDisplacement;",
827
"uniform float uDisplacementScale;",
828
"uniform float uDisplacementBias;",
829
830
"#endif",
831
832
"varying vec3 vTangent;",
833
"varying vec3 vBinormal;",
834
"varying vec3 vNormal;",
835
"varying vec2 vUv;",
836
837
"#if MAX_POINT_LIGHTS > 0",
838
839
"uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
840
"uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
841
842
"varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
843
844
"#endif",
845
846
"varying vec3 vViewPosition;",
847
848
THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
849
850
"void main() {",
851
852
"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
853
854
"vViewPosition = -mvPosition.xyz;",
855
856
// normal, tangent and binormal vectors
857
858
"vNormal = normalMatrix * normal;",
859
"vTangent = normalMatrix * tangent.xyz;",
860
"vBinormal = cross( vNormal, vTangent ) * tangent.w;",
861
862
"vUv = uv * uRepeat + uOffset;",
863
864
// point lights
865
866
"#if MAX_POINT_LIGHTS > 0",
867
868
"for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
869
870
"vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
871
"vec3 lVector = lPosition.xyz - mvPosition.xyz;",
872
873
"float lDistance = 1.0;",
874
"if ( pointLightDistance[ i ] > 0.0 )",
875
"lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
876
877
"lVector = normalize( lVector );",
878
879
"vPointLight[ i ] = vec4( lVector, lDistance );",
880
881
"}",
882
883
"#endif",
884
885
// displacement mapping
886
887
"#ifdef VERTEX_TEXTURES",
888
889
"vec3 dv = texture2D( tDisplacement, uv ).xyz;",
890
"float df = uDisplacementScale * dv.x + uDisplacementBias;",
891
"vec4 displacedPosition = vec4( normalize( vNormal.xyz ) * df, 0.0 ) + mvPosition;",
892
"gl_Position = projectionMatrix * displacedPosition;",
893
894
"#else",
895
896
"gl_Position = projectionMatrix * mvPosition;",
897
898
"#endif",
899
900
THREE.ShaderChunk[ "shadowmap_vertex" ],
901
902
"}"
903
904
].join("\n")
905
906
},
907
908
/* -------------------------------------------------------------------------
909
// Cube map shader
910
------------------------------------------------------------------------- */
911
912
'cube': {
913
914
uniforms: { "tCube": { type: "t", value: 1, texture: null },
915
"tFlip": { type: "f", value: -1 } },
916
917
vertexShader: [
918
919
"varying vec3 vViewPosition;",
920
921
"void main() {",
922
923
"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
924
"vViewPosition = cameraPosition - mPosition.xyz;",
925
926
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
927
928
"}"
929
930
].join("\n"),
931
932
fragmentShader: [
933
934
"uniform samplerCube tCube;",
935
"uniform float tFlip;",
936
937
"varying vec3 vViewPosition;",
938
939
"void main() {",
940
941
"vec3 wPos = cameraPosition - vViewPosition;",
942
"gl_FragColor = textureCube( tCube, vec3( tFlip * wPos.x, wPos.yz ) );",
943
944
"}"
945
946
].join("\n")
947
948
}
949
950
};
951