ShaderUtils.js 18.1 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
					"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
74
					"vec4 mPosition = modelMatrix * vec4( position, 1.0 );",
M
Mr.doob 已提交
75

76
					"vec3 nWorld = normalize( mat3( modelMatrix[0].xyz, modelMatrix[1].xyz, modelMatrix[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 },
115
				"enableDisplacement": { type: "i", value: 0 },
M
Mr.doob 已提交
116

A
alteredq 已提交
117 118 119 120 121 122
				"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 已提交
123

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

				"#if MAX_DIR_LIGHTS > 0",
177

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

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

				"#if MAX_POINT_LIGHTS > 0",
184

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

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

191
				"#if MAX_SPOT_LIGHTS > 0",
192

193 194 195 196 197
					"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 ];",
198
					"uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
199 200 201

				"#endif",

202
				"#ifdef WRAP_AROUND",
203

204
					"uniform vec3 wrapRGB;",
205

206 207
				"#endif",

208
				"varying vec3 vWorldPosition;",
M
Mr.doob 已提交
209

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

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

215 216
					"vec3 vViewPosition = cameraPosition - vWorldPosition;",

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

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

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

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

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
						"#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 已提交
258

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

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

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

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

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

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

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

277 278 279 280 281 282 283 284
							"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 已提交
285

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

288 289 290 291 292 293 294 295 296 297 298 299
							"#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 已提交
300

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

							// specular

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

309 310 311 312 313 314 315 316 317 318 319 320 321 322
							"#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 已提交
323 324 325 326

						"}",

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

328 329 330 331 332 333 334 335 336
					// 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 ++ ) {",

337 338 339 340 341 342 343 344
							"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 );",
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

							"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 已提交
396
					// directional lights
M
Mr.doob 已提交
397

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

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

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

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

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

410 411 412 413 414 415 416 417 418 419 420 421
							"#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 已提交
422

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

							// specular

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

431 432 433 434 435 436 437 438 439 440 441 442 443 444
							"#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 已提交
445 446 447 448

						"}",

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

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

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

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

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

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

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

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

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

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

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

					"#endif",

476 477 478 479 480 481 482 483 484
					"#ifdef METAL",

						"gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor + totalSpecular );",

					"#else",

						"gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor ) + totalSpecular;",

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

A
alteredq 已提交
486 487
					"if ( enableReflection ) {",

488
						"vec3 vReflect = reflect( normalize( vWorldPosition ), normal );",
489

A
alteredq 已提交
490
						"vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
491 492 493 494 495 496 497

						"#ifdef GAMMA_INPUT",

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

						"#endif",

A
alteredq 已提交
498
						"gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * uReflectivity );",
A
alteredq 已提交
499 500 501

					"}",

502
					THREE.ShaderChunk[ "shadowmap_fragment" ],
503
					THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
A
alteredq 已提交
504 505
					THREE.ShaderChunk[ "fog_fragment" ],

506
				"}"
M
Mr.doob 已提交
507

508
			].join("\n"),
M
Mr.doob 已提交
509

510
			vertexShader: [
M
Mr.doob 已提交
511

512
				"attribute vec4 tangent;",
M
Mr.doob 已提交
513

514 515 516
				"uniform vec2 uOffset;",
				"uniform vec2 uRepeat;",

517 518
				"uniform bool enableDisplacement;",

519
				"#ifdef VERTEX_TEXTURES",
M
Mr.doob 已提交
520

521 522 523
					"uniform sampler2D tDisplacement;",
					"uniform float uDisplacementScale;",
					"uniform float uDisplacementBias;",
M
Mr.doob 已提交
524

525
				"#endif",
M
Mr.doob 已提交
526

527 528 529 530
				"varying vec3 vTangent;",
				"varying vec3 vBinormal;",
				"varying vec3 vNormal;",
				"varying vec2 vUv;",
M
Mr.doob 已提交
531

532
				"varying vec3 vWorldPosition;",
M
Mr.doob 已提交
533

534
				THREE.ShaderChunk[ "skinning_pars_vertex" ],
535 536
				THREE.ShaderChunk[ "shadowmap_pars_vertex" ],

537
				"void main() {",
M
Mr.doob 已提交
538

539 540 541
					THREE.ShaderChunk[ "skinbase_vertex" ],
					THREE.ShaderChunk[ "skinnormal_vertex" ],

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

544 545 546 547 548 549 550 551 552 553 554 555 556 557
					"#ifdef USE_SKINNING",

						"vNormal = normalMatrix * skinnedNormal.xyz;",

						"vec4 skinnedTangent = skinMatrix * vec4( tangent.xyz, 0.0 );",
						"vTangent = normalMatrix * skinnedTangent.xyz;",

					"#else",

						"vNormal = normalMatrix * normal;",
						"vTangent = normalMatrix * tangent.xyz;",

					"#endif",

558
					"vBinormal = cross( vNormal, vTangent ) * tangent.w;",
M
Mr.doob 已提交
559

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

562
					// displacement mapping
563

564
					"vec3 displacedPosition;",
565

566
					"#ifdef VERTEX_TEXTURES",
567

568
						"if ( enableDisplacement ) {",
569

570 571 572
							"vec3 dv = texture2D( tDisplacement, uv ).xyz;",
							"float df = uDisplacementScale * dv.x + uDisplacementBias;",
							"displacedPosition = position + normalize( normal ) * df;",
M
Mr.doob 已提交
573

574
						"} else {",
M
Mr.doob 已提交
575

576 577 578 579 580 581 582 583 584 585 586 587
							"#ifdef USE_SKINNING",

								"vec4 skinned  = boneMatX * skinVertexA * skinWeight.x;",
								"skinned 	  += boneMatY * skinVertexB * skinWeight.y;",

								"displacedPosition  = skinned.xyz;",

							"#else",

								"displacedPosition = position;",

							"#endif",
588 589

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

591
					"#else",
M
Mr.doob 已提交
592

593 594 595 596 597 598 599 600 601 602 603 604
						"#ifdef USE_SKINNING",

							"vec4 skinned  = boneMatX * skinVertexA * skinWeight.x;",
							"skinned 	  += boneMatY * skinVertexB * skinWeight.y;",

							"displacedPosition  = skinned.xyz;",

						"#else",

							"displacedPosition = position;",

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

606
					"#endif",
M
Mr.doob 已提交
607

608 609 610
					//

					"vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
611
					"vec4 wPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629

					"gl_Position = projectionMatrix * mvPosition;",

					//

					"vWorldPosition = wPosition.xyz;",

					// shadows

					"#ifdef USE_SHADOWMAP",

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

							"vShadowCoord[ i ] = shadowMatrix[ i ] * wPosition;",

						"}",

					"#endif",
630

631
				"}"
M
Mr.doob 已提交
632

633
			].join("\n")
M
Mr.doob 已提交
634 635 636

		},

637
		/* -------------------------------------------------------------------------
A
alteredq 已提交
638
		//	Cube map shader
639
		 ------------------------------------------------------------------------- */
640

641
		'cube': {
M
Mr.doob 已提交
642

643 644
			uniforms: { "tCube": { type: "t", value: 1, texture: null },
						"tFlip": { type: "f", value: -1 } },
M
Mr.doob 已提交
645

646
			vertexShader: [
M
Mr.doob 已提交
647

648
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
649

650
				"void main() {",
M
Mr.doob 已提交
651

652
					"vec4 mPosition = modelMatrix * vec4( position, 1.0 );",
653
					"vViewPosition = cameraPosition - mPosition.xyz;",
654

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

657
				"}"
M
Mr.doob 已提交
658

659
			].join("\n"),
M
Mr.doob 已提交
660

661
			fragmentShader: [
M
Mr.doob 已提交
662

663
				"uniform samplerCube tCube;",
664
				"uniform float tFlip;",
M
Mr.doob 已提交
665

666
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
667

668
				"void main() {",
M
Mr.doob 已提交
669

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

673
				"}"
M
Mr.doob 已提交
674

675
			].join("\n")
676

M
Mr.doob 已提交
677
		}
M
Mr.doob 已提交
678

M
Mr.doob 已提交
679 680 681
	}

};
M
Mr.doob 已提交
682

683
};