ShaderUtils.js 16.9 KB
Newer Older
1 2 3
/**
 * @author alteredq / http://alteredqualia.com/
 * @author mr.doob / http://mrdoob.com/
4
 *
5 6
 * ShaderUtils currently contains:
 *
7 8 9
 *	fresnel
 *	normal
 * 	cube
10
 *
11
 */
M
Mr.doob 已提交
12

M
Mr.doob 已提交
13 14
if ( THREE.WebGLRenderer ) {

15 16 17 18
THREE.ShaderUtils = {

	lib: {

19
		/* -------------------------------------------------------------------------
A
alteredq 已提交
20 21
		//	Fresnel shader
		//	- based on Nvidia Cg tutorial
22
		 ------------------------------------------------------------------------- */
23

24
		'fresnel': {
M
Mr.doob 已提交
25

26
			uniforms: {
M
Mr.doob 已提交
27

28 29 30 31 32
				"mRefractionRatio": { type: "f", value: 1.02 },
				"mFresnelBias": { type: "f", value: 0.1 },
				"mFresnelPower": { type: "f", value: 2.0 },
				"mFresnelScale": { type: "f", value: 1.0 },
				"tCube": { type: "t", value: 1, texture: null }
M
Mr.doob 已提交
33

34 35 36 37 38
			},

			fragmentShader: [

				"uniform samplerCube tCube;",
M
Mr.doob 已提交
39

40 41 42
				"varying vec3 vReflect;",
				"varying vec3 vRefract[3];",
				"varying float vReflectionFactor;",
M
Mr.doob 已提交
43

44
				"void main() {",
M
Mr.doob 已提交
45

46 47
					"vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
					"vec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );",
M
Mr.doob 已提交
48

49 50 51 52
					"refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;",
					"refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;",
					"refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;",
					"refractedColor.a = 1.0;",
M
Mr.doob 已提交
53

54
					"gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );",
M
Mr.doob 已提交
55

56
				"}"
M
Mr.doob 已提交
57

58
			].join("\n"),
M
Mr.doob 已提交
59

60
			vertexShader: [
M
Mr.doob 已提交
61

62 63 64 65
				"uniform float mRefractionRatio;",
				"uniform float mFresnelBias;",
				"uniform float mFresnelScale;",
				"uniform float mFresnelPower;",
M
Mr.doob 已提交
66

67 68 69
				"varying vec3 vReflect;",
				"varying vec3 vRefract[3];",
				"varying float vReflectionFactor;",
M
Mr.doob 已提交
70

71
				"void main() {",
M
Mr.doob 已提交
72

73 74
					"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
					"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
M
Mr.doob 已提交
75

76
					"vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );",
M
Mr.doob 已提交
77

78
					"vec3 I = mPosition.xyz - cameraPosition;",
M
Mr.doob 已提交
79

80 81 82 83 84
					"vReflect = reflect( I, nWorld );",
					"vRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );",
					"vRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );",
					"vRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );",
					"vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );",
M
Mr.doob 已提交
85

86 87 88 89 90
					"gl_Position = projectionMatrix * mvPosition;",

				"}"

			].join("\n")
M
Mr.doob 已提交
91

92
		},
M
Mr.doob 已提交
93

94
		/* -------------------------------------------------------------------------
95 96
		//	Normal map shader
		//		- Blinn-Phong
A
alteredq 已提交
97
		//		- normal + diffuse + specular + AO + displacement + reflection + shadow maps
M
Mr.doob 已提交
98
		//		- point and directional lights (use with "lights: true" material option)
99
		 ------------------------------------------------------------------------- */
100

101
		'normal' : {
M
Mr.doob 已提交
102

M
Mr.doob 已提交
103 104
			uniforms: THREE.UniformsUtils.merge( [

A
alteredq 已提交
105
				THREE.UniformsLib[ "fog" ],
M
Mr.doob 已提交
106
				THREE.UniformsLib[ "lights" ],
107
				THREE.UniformsLib[ "shadowmap" ],
M
Mr.doob 已提交
108 109

				{
M
Mr.doob 已提交
110

A
alteredq 已提交
111 112 113 114
				"enableAO"		  : { type: "i", value: 0 },
				"enableDiffuse"	  : { type: "i", value: 0 },
				"enableSpecular"  : { type: "i", value: 0 },
				"enableReflection": { type: "i", value: 0 },
M
Mr.doob 已提交
115

A
alteredq 已提交
116 117 118 119 120 121
				"tDiffuse"	   : { type: "t", value: 0, texture: null },
				"tCube"		   : { type: "t", value: 1, texture: null },
				"tNormal"	   : { type: "t", value: 2, texture: null },
				"tSpecular"	   : { type: "t", value: 3, texture: null },
				"tAO"		   : { type: "t", value: 4, texture: null },
				"tDisplacement": { type: "t", value: 5, texture: null },
M
Mr.doob 已提交
122

123
				"uNormalScale": { type: "f", value: 1.0 },
M
Mr.doob 已提交
124

M
Mr.doob 已提交
125 126
				"uDisplacementBias": { type: "f", value: 0.0 },
				"uDisplacementScale": { type: "f", value: 1.0 },
M
Mr.doob 已提交
127

128
				"uDiffuseColor": { type: "c", value: new THREE.Color( 0xffffff ) },
129
				"uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
130
				"uAmbientColor": { type: "c", value: new THREE.Color( 0xffffff ) },
M
Mr.doob 已提交
131
				"uShininess": { type: "f", value: 30 },
132 133
				"uOpacity": { type: "f", value: 1 },

A
alteredq 已提交
134 135
				"uReflectivity": { type: "f", value: 0.5 },

136
				"uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },
137 138 139
				"uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },

				"wrapRGB"  : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
M
Mr.doob 已提交
140

M
Mr.doob 已提交
141
				}
M
Mr.doob 已提交
142

M
Mr.doob 已提交
143
			] ),
M
Mr.doob 已提交
144

M
Mr.doob 已提交
145
			fragmentShader: [
M
Mr.doob 已提交
146

147 148 149 150
				"uniform vec3 uAmbientColor;",
				"uniform vec3 uDiffuseColor;",
				"uniform vec3 uSpecularColor;",
				"uniform float uShininess;",
M
Mr.doob 已提交
151
				"uniform float uOpacity;",
M
Mr.doob 已提交
152

153
				"uniform bool enableDiffuse;",
154
				"uniform bool enableSpecular;",
155
				"uniform bool enableAO;",
A
alteredq 已提交
156
				"uniform bool enableReflection;",
M
Mr.doob 已提交
157

158 159
				"uniform sampler2D tDiffuse;",
				"uniform sampler2D tNormal;",
160
				"uniform sampler2D tSpecular;",
161
				"uniform sampler2D tAO;",
M
Mr.doob 已提交
162

A
alteredq 已提交
163 164
				"uniform samplerCube tCube;",

165
				"uniform float uNormalScale;",
A
alteredq 已提交
166
				"uniform float uReflectivity;",
M
Mr.doob 已提交
167

168 169 170 171
				"varying vec3 vTangent;",
				"varying vec3 vBinormal;",
				"varying vec3 vNormal;",
				"varying vec2 vUv;",
M
Mr.doob 已提交
172

M
Mr.doob 已提交
173 174 175
				"uniform vec3 ambientLightColor;",

				"#if MAX_DIR_LIGHTS > 0",
176

M
Mr.doob 已提交
177 178
					"uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
					"uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
179

M
Mr.doob 已提交
180 181 182
				"#endif",

				"#if MAX_POINT_LIGHTS > 0",
183

M
Mr.doob 已提交
184
					"uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
185 186 187
					"uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
					"uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",

M
Mr.doob 已提交
188 189
				"#endif",

190
				"#if MAX_SPOT_LIGHTS > 0",
191

192 193 194 195 196
					"uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
					"uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
					"uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
					"uniform float spotLightAngle[ MAX_SPOT_LIGHTS ];",
					"uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
197
					"uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
198 199

					"varying vec3 vWorldPosition;",
200

201 202
				"#endif",

203
				"#ifdef WRAP_AROUND",
204

205
					"uniform vec3 wrapRGB;",
206

207 208
				"#endif",

209
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
210

211
				THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
A
alteredq 已提交
212 213
				THREE.ShaderChunk[ "fog_pars_fragment" ],

214
				"void main() {",
M
Mr.doob 已提交
215

216
					"gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
M
Mr.doob 已提交
217 218

					"vec3 specularTex = vec3( 1.0 );",
M
Mr.doob 已提交
219

220 221 222
					"vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
					"normalTex.xy *= uNormalScale;",
					"normalTex = normalize( normalTex );",
M
Mr.doob 已提交
223

224
					"if( enableDiffuse ) {",
M
Mr.doob 已提交
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
						"#ifdef GAMMA_INPUT",

							"vec4 texelColor = texture2D( tDiffuse, vUv );",
							"texelColor.xyz *= texelColor.xyz;",

							"gl_FragColor = gl_FragColor * texelColor;",

						"#else",

							"gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",

						"#endif",

					"}",

					"if( enableAO ) {",

						"#ifdef GAMMA_INPUT",

							"vec4 aoColor = texture2D( tAO, vUv );",
							"aoColor.xyz *= aoColor.xyz;",

							"gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;",

						"#else",

							"gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",

						"#endif",

					"}",
M
Mr.doob 已提交
257

258 259 260
					"if( enableSpecular )",
						"specularTex = texture2D( tSpecular, vUv ).xyz;",

A
alteredq 已提交
261
					"mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",
262
					"vec3 finalNormal = tsb * normalTex;",
M
Mr.doob 已提交
263

264 265
					"vec3 normal = normalize( finalNormal );",
					"vec3 viewPosition = normalize( vViewPosition );",
M
Mr.doob 已提交
266

M
Mr.doob 已提交
267 268 269
					// point lights

					"#if MAX_POINT_LIGHTS > 0",
M
Mr.doob 已提交
270

271 272
						"vec3 pointDiffuse = vec3( 0.0 );",
						"vec3 pointSpecular = vec3( 0.0 );",
M
Mr.doob 已提交
273

M
Mr.doob 已提交
274
						"for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
M
Mr.doob 已提交
275

276 277 278 279 280 281 282 283
							"vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
							"vec3 pointVector = lPosition.xyz + vViewPosition.xyz;",

							"float pointDistance = 1.0;",
							"if ( pointLightDistance[ i ] > 0.0 )",
								"pointDistance = 1.0 - min( ( length( pointVector ) / pointLightDistance[ i ] ), 1.0 );",

							"pointVector = normalize( pointVector );",
M
Mr.doob 已提交
284

285
							// diffuse
M
Mr.doob 已提交
286

287 288 289 290 291 292 293 294 295 296 297 298
							"#ifdef WRAP_AROUND",

								"float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );",
								"float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );",

								"vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",

							"#else",

								"float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",

							"#endif",
M
Mr.doob 已提交
299

300
							"pointDiffuse += pointDistance * pointLightColor[ i ] * uDiffuseColor * pointDiffuseWeight;",
301 302 303

							// specular

304
							"vec3 pointHalfVector = normalize( pointVector + viewPosition );",
305
							"float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
A
alteredq 已提交
306
							"float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, uShininess ), 0.0 );",
307

308 309 310 311 312 313 314 315 316 317 318 319 320 321
							"#ifdef PHYSICALLY_BASED_SHADING",

								// 2.0 => 2.0001 is hack to work around ANGLE bug

								"float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",

								"vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( pointVector, pointHalfVector ), 5.0 );",
								"pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",

							"#else",

								"pointSpecular += pointDistance * pointLightColor[ i ] * uSpecularColor * pointSpecularWeight * pointDiffuseWeight;",

							"#endif",
M
Mr.doob 已提交
322 323 324 325

						"}",

					"#endif",
M
Mr.doob 已提交
326

327 328 329 330 331 332 333 334 335
					// spot lights

					"#if MAX_SPOT_LIGHTS > 0",

						"vec3 spotDiffuse = vec3( 0.0 );",
						"vec3 spotSpecular = vec3( 0.0 );",

						"for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",

336 337 338 339 340 341 342 343
							"vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
							"vec3 spotVector = lPosition.xyz + vViewPosition.xyz;",

							"float spotDistance = 1.0;",
							"if ( spotLightDistance[ i ] > 0.0 )",
								"spotDistance = 1.0 - min( ( length( spotVector ) / spotLightDistance[ i ] ), 1.0 );",

							"spotVector = normalize( spotVector );",
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

							"float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );",

							"if ( spotEffect > spotLightAngle[ i ] ) {",

								"spotEffect = pow( spotEffect, spotLightExponent[ i ] );",

								// diffuse

								"#ifdef WRAP_AROUND",

									"float spotDiffuseWeightFull = max( dot( normal, spotVector ), 0.0 );",
									"float spotDiffuseWeightHalf = max( 0.5 * dot( normal, spotVector ) + 0.5, 0.0 );",

									"vec3 spotDiffuseWeight = mix( vec3 ( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );",

								"#else",

									"float spotDiffuseWeight = max( dot( normal, spotVector ), 0.0 );",

								"#endif",

								"spotDiffuse += spotDistance * spotLightColor[ i ] * uDiffuseColor * spotDiffuseWeight * spotEffect;",

								// specular

								"vec3 spotHalfVector = normalize( spotVector + viewPosition );",
								"float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );",
								"float spotSpecularWeight = specularTex.r * max( pow( spotDotNormalHalf, uShininess ), 0.0 );",

								"#ifdef PHYSICALLY_BASED_SHADING",

									// 2.0 => 2.0001 is hack to work around ANGLE bug

									"float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",

									"vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( spotVector, spotHalfVector ), 5.0 );",
									"spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * spotDistance * specularNormalization * spotEffect;",

								"#else",

									"spotSpecular += spotDistance * spotLightColor[ i ] * uSpecularColor * spotSpecularWeight * spotDiffuseWeight * spotEffect;",

								"#endif",

							"}",

						"}",

					"#endif",

M
Mr.doob 已提交
395
					// directional lights
M
Mr.doob 已提交
396

M
Mr.doob 已提交
397
					"#if MAX_DIR_LIGHTS > 0",
M
Mr.doob 已提交
398

399 400
						"vec3 dirDiffuse = vec3( 0.0 );",
						"vec3 dirSpecular = vec3( 0.0 );",
M
Mr.doob 已提交
401

M
Mr.doob 已提交
402
						"for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
M
Mr.doob 已提交
403

M
Mr.doob 已提交
404 405
							"vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
							"vec3 dirVector = normalize( lDirection.xyz );",
M
Mr.doob 已提交
406

407
							// diffuse
M
Mr.doob 已提交
408

409 410 411 412 413 414 415 416 417 418 419 420
							"#ifdef WRAP_AROUND",

								"float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );",
								"float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",

								"vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );",

							"#else",

								"float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",

							"#endif",
M
Mr.doob 已提交
421

422
							"dirDiffuse += directionalLightColor[ i ] * uDiffuseColor * dirDiffuseWeight;",
423 424 425

							// specular

426
							"vec3 dirHalfVector = normalize( dirVector + viewPosition );",
427
							"float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
A
alteredq 已提交
428
							"float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, uShininess ), 0.0 );",
429

430 431 432 433 434 435 436 437 438 439 440 441 442 443
							"#ifdef PHYSICALLY_BASED_SHADING",

								// 2.0 => 2.0001 is hack to work around ANGLE bug

								"float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",

								"vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
								"dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",

							"#else",

								"dirSpecular += directionalLightColor[ i ] * uSpecularColor * dirSpecularWeight * dirDiffuseWeight;",

							"#endif",
M
Mr.doob 已提交
444 445 446 447

						"}",

					"#endif",
M
Mr.doob 已提交
448

449
					// all lights contribution summation
M
Mr.doob 已提交
450

451 452
					"vec3 totalDiffuse = vec3( 0.0 );",
					"vec3 totalSpecular = vec3( 0.0 );",
M
Mr.doob 已提交
453 454

					"#if MAX_DIR_LIGHTS > 0",
455 456 457 458

						"totalDiffuse += dirDiffuse;",
						"totalSpecular += dirSpecular;",

M
Mr.doob 已提交
459 460 461
					"#endif",

					"#if MAX_POINT_LIGHTS > 0",
462 463 464 465

						"totalDiffuse += pointDiffuse;",
						"totalSpecular += pointSpecular;",

M
Mr.doob 已提交
466
					"#endif",
M
Mr.doob 已提交
467

468 469 470 471 472 473 474
					"#if MAX_SPOT_LIGHTS > 0",

						"totalDiffuse += spotDiffuse;",
						"totalSpecular += spotSpecular;",

					"#endif",

475
					"gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor) + totalSpecular;",
M
Mr.doob 已提交
476

A
alteredq 已提交
477 478 479 480
					"if ( enableReflection ) {",

						"vec3 wPos = cameraPosition - vViewPosition;",
						"vec3 vReflect = reflect( normalize( wPos ), normal );",
481

A
alteredq 已提交
482
						"vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
483 484 485 486 487 488 489

						"#ifdef GAMMA_INPUT",

							"cubeColor.xyz *= cubeColor.xyz;",

						"#endif",

A
alteredq 已提交
490
						"gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * uReflectivity );",
A
alteredq 已提交
491 492 493

					"}",

494
					THREE.ShaderChunk[ "shadowmap_fragment" ],
495
					THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
A
alteredq 已提交
496 497
					THREE.ShaderChunk[ "fog_fragment" ],

498
				"}"
M
Mr.doob 已提交
499

500
			].join("\n"),
M
Mr.doob 已提交
501

502
			vertexShader: [
M
Mr.doob 已提交
503

504
				"attribute vec4 tangent;",
M
Mr.doob 已提交
505

506 507 508
				"uniform vec2 uOffset;",
				"uniform vec2 uRepeat;",

509
				"#ifdef VERTEX_TEXTURES",
M
Mr.doob 已提交
510

511 512 513
					"uniform sampler2D tDisplacement;",
					"uniform float uDisplacementScale;",
					"uniform float uDisplacementBias;",
M
Mr.doob 已提交
514

515
				"#endif",
M
Mr.doob 已提交
516

517 518 519 520
				"varying vec3 vTangent;",
				"varying vec3 vBinormal;",
				"varying vec3 vNormal;",
				"varying vec2 vUv;",
M
Mr.doob 已提交
521

522 523 524 525 526 527
				"#if MAX_SPOT_LIGHTS > 0",

					"varying vec3 vWorldPosition;",

				"#endif",

528
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
529

530 531
				THREE.ShaderChunk[ "shadowmap_pars_vertex" ],

532
				"void main() {",
M
Mr.doob 已提交
533

534
					"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
535

536
					"vViewPosition = -mvPosition.xyz;",
537

A
alteredq 已提交
538
					// normal, tangent and binormal vectors
M
Mr.doob 已提交
539

A
alteredq 已提交
540 541
					"vNormal = normalMatrix * normal;",
					"vTangent = normalMatrix * tangent.xyz;",
542
					"vBinormal = cross( vNormal, vTangent ) * tangent.w;",
M
Mr.doob 已提交
543

544
					"vUv = uv * uRepeat + uOffset;",
M
Mr.doob 已提交
545

546 547 548 549
					// spot lights

					"#if MAX_SPOT_LIGHTS > 0",

550 551
						"vec4 wPosition = objectMatrix * vec4( position, 1.0 );",
						"vWorldPosition = wPosition.xyz;",
552 553 554

					"#endif",

555
					// displacement mapping
M
Mr.doob 已提交
556

557
					"#ifdef VERTEX_TEXTURES",
M
Mr.doob 已提交
558

559 560
						"vec3 dv = texture2D( tDisplacement, uv ).xyz;",
						"float df = uDisplacementScale * dv.x + uDisplacementBias;",
A
alteredq 已提交
561
						"vec4 displacedPosition = vec4( normalize( vNormal.xyz ) * df, 0.0 ) + mvPosition;",
562
						"gl_Position = projectionMatrix * displacedPosition;",
M
Mr.doob 已提交
563

564
					"#else",
M
Mr.doob 已提交
565

566
						"gl_Position = projectionMatrix * mvPosition;",
M
Mr.doob 已提交
567

568
					"#endif",
M
Mr.doob 已提交
569

570 571
					THREE.ShaderChunk[ "shadowmap_vertex" ],

572
				"}"
M
Mr.doob 已提交
573

574
			].join("\n")
M
Mr.doob 已提交
575 576 577

		},

578
		/* -------------------------------------------------------------------------
A
alteredq 已提交
579
		//	Cube map shader
580
		 ------------------------------------------------------------------------- */
581

582
		'cube': {
M
Mr.doob 已提交
583

584 585
			uniforms: { "tCube": { type: "t", value: 1, texture: null },
						"tFlip": { type: "f", value: -1 } },
M
Mr.doob 已提交
586

587
			vertexShader: [
M
Mr.doob 已提交
588

589
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
590

591
				"void main() {",
M
Mr.doob 已提交
592

593 594
					"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
					"vViewPosition = cameraPosition - mPosition.xyz;",
595

596
					"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
M
Mr.doob 已提交
597

598
				"}"
M
Mr.doob 已提交
599

600
			].join("\n"),
M
Mr.doob 已提交
601

602
			fragmentShader: [
M
Mr.doob 已提交
603

604
				"uniform samplerCube tCube;",
605
				"uniform float tFlip;",
M
Mr.doob 已提交
606

607
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
608

609
				"void main() {",
M
Mr.doob 已提交
610

611
					"vec3 wPos = cameraPosition - vViewPosition;",
612
					"gl_FragColor = textureCube( tCube, vec3( tFlip * wPos.x, wPos.yz ) );",
M
Mr.doob 已提交
613

614
				"}"
M
Mr.doob 已提交
615

616
			].join("\n")
617

M
Mr.doob 已提交
618
		}
M
Mr.doob 已提交
619

M
Mr.doob 已提交
620 621 622
	}

};
M
Mr.doob 已提交
623 624

};