ShaderUtils.js 13.4 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 176 177 178 179 180 181 182 183 184
				"uniform vec3 ambientLightColor;",

				"#if MAX_DIR_LIGHTS > 0",
					"uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
					"uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
				"#endif",

				"#if MAX_POINT_LIGHTS > 0",
					"uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
					"varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
				"#endif",

185 186 187 188
				"#ifdef WRAP_AROUND",
					"uniform vec3 wrapRGB;",
				"#endif",

189
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
190

191
				THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
A
alteredq 已提交
192 193
				THREE.ShaderChunk[ "fog_pars_fragment" ],

194
				"void main() {",
M
Mr.doob 已提交
195

196
					"gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
M
Mr.doob 已提交
197 198

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

200 201 202
					"vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
					"normalTex.xy *= uNormalScale;",
					"normalTex = normalize( normalTex );",
M
Mr.doob 已提交
203

204
					"if( enableDiffuse ) {",
M
Mr.doob 已提交
205

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
						"#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 已提交
237

238 239 240
					"if( enableSpecular )",
						"specularTex = texture2D( tSpecular, vUv ).xyz;",

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

244 245
					"vec3 normal = normalize( finalNormal );",
					"vec3 viewPosition = normalize( vViewPosition );",
M
Mr.doob 已提交
246

M
Mr.doob 已提交
247 248 249
					// point lights

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

251 252
						"vec3 pointDiffuse = vec3( 0.0 );",
						"vec3 pointSpecular = vec3( 0.0 );",
M
Mr.doob 已提交
253

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

M
Mr.doob 已提交
256 257 258
							"vec3 pointVector = normalize( vPointLight[ i ].xyz );",
							"float pointDistance = vPointLight[ i ].w;",

259
							// diffuse
M
Mr.doob 已提交
260

261 262 263 264 265 266 267 268 269 270 271 272
							"#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 已提交
273

274
							"pointDiffuse += pointDistance * pointLightColor[ i ] * uDiffuseColor * pointDiffuseWeight;",
275 276 277

							// specular

278
							"vec3 pointHalfVector = normalize( pointVector + viewPosition );",
279
							"float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
A
alteredq 已提交
280
							"float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, uShininess ), 0.0 );",
281

282
							"pointSpecular += pointDistance * pointLightColor[ i ] * uSpecularColor * pointSpecularWeight * pointDiffuseWeight;",
M
Mr.doob 已提交
283 284 285 286

						"}",

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

M
Mr.doob 已提交
288
					// directional lights
M
Mr.doob 已提交
289

M
Mr.doob 已提交
290
					"#if MAX_DIR_LIGHTS > 0",
M
Mr.doob 已提交
291

292 293
						"vec3 dirDiffuse = vec3( 0.0 );",
						"vec3 dirSpecular = vec3( 0.0 );",
M
Mr.doob 已提交
294

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

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

300
							// diffuse
M
Mr.doob 已提交
301

302 303 304 305 306 307 308 309 310 311 312 313
							"#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 已提交
314

315
							"dirDiffuse += directionalLightColor[ i ] * uDiffuseColor * dirDiffuseWeight;",
316 317 318

							// specular

319
							"vec3 dirHalfVector = normalize( dirVector + viewPosition );",
320
							"float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
A
alteredq 已提交
321
							"float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, uShininess ), 0.0 );",
322

323
							"dirSpecular += directionalLightColor[ i ] * uSpecularColor * dirSpecularWeight * dirDiffuseWeight;",
M
Mr.doob 已提交
324 325 326 327

						"}",

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

329
					// all lights contribution summation
M
Mr.doob 已提交
330

331 332
					"vec3 totalDiffuse = vec3( 0.0 );",
					"vec3 totalSpecular = vec3( 0.0 );",
M
Mr.doob 已提交
333 334

					"#if MAX_DIR_LIGHTS > 0",
335 336 337 338

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

M
Mr.doob 已提交
339 340 341
					"#endif",

					"#if MAX_POINT_LIGHTS > 0",
342 343 344 345

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

M
Mr.doob 已提交
346
					"#endif",
M
Mr.doob 已提交
347

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

A
alteredq 已提交
350 351 352 353
					"if ( enableReflection ) {",

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

A
alteredq 已提交
355
						"vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
356 357 358 359 360 361 362

						"#ifdef GAMMA_INPUT",

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

						"#endif",

A
alteredq 已提交
363
						"gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * uReflectivity );",
A
alteredq 已提交
364 365 366

					"}",

367
					THREE.ShaderChunk[ "shadowmap_fragment" ],
368
					THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
A
alteredq 已提交
369 370
					THREE.ShaderChunk[ "fog_fragment" ],

371
				"}"
M
Mr.doob 已提交
372

373
			].join("\n"),
M
Mr.doob 已提交
374

375
			vertexShader: [
M
Mr.doob 已提交
376

377
				"attribute vec4 tangent;",
M
Mr.doob 已提交
378

379 380 381
				"uniform vec2 uOffset;",
				"uniform vec2 uRepeat;",

382
				"#ifdef VERTEX_TEXTURES",
M
Mr.doob 已提交
383

384 385 386
					"uniform sampler2D tDisplacement;",
					"uniform float uDisplacementScale;",
					"uniform float uDisplacementBias;",
M
Mr.doob 已提交
387

388
				"#endif",
M
Mr.doob 已提交
389

390 391 392 393
				"varying vec3 vTangent;",
				"varying vec3 vBinormal;",
				"varying vec3 vNormal;",
				"varying vec2 vUv;",
M
Mr.doob 已提交
394

M
Mr.doob 已提交
395 396 397 398 399 400 401 402 403
				"#if MAX_POINT_LIGHTS > 0",

					"uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
					"uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",

					"varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",

				"#endif",

404
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
405

406 407
				THREE.ShaderChunk[ "shadowmap_pars_vertex" ],

408
				"void main() {",
M
Mr.doob 已提交
409

410
					"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
411

412
					"vViewPosition = -mvPosition.xyz;",
413

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

A
alteredq 已提交
416 417
					"vNormal = normalMatrix * normal;",
					"vTangent = normalMatrix * tangent.xyz;",
418
					"vBinormal = cross( vNormal, vTangent ) * tangent.w;",
M
Mr.doob 已提交
419

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

M
Mr.doob 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
					// point lights

					"#if MAX_POINT_LIGHTS > 0",

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

							"vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
							"vec3 lVector = lPosition.xyz - mvPosition.xyz;",

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

							"lVector = normalize( lVector );",

							"vPointLight[ i ] = vec4( lVector, lDistance );",

						"}",

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

443
					// displacement mapping
M
Mr.doob 已提交
444

445
					"#ifdef VERTEX_TEXTURES",
M
Mr.doob 已提交
446

447 448
						"vec3 dv = texture2D( tDisplacement, uv ).xyz;",
						"float df = uDisplacementScale * dv.x + uDisplacementBias;",
A
alteredq 已提交
449
						"vec4 displacedPosition = vec4( normalize( vNormal.xyz ) * df, 0.0 ) + mvPosition;",
450
						"gl_Position = projectionMatrix * displacedPosition;",
M
Mr.doob 已提交
451

452
					"#else",
M
Mr.doob 已提交
453

454
						"gl_Position = projectionMatrix * mvPosition;",
M
Mr.doob 已提交
455

456
					"#endif",
M
Mr.doob 已提交
457

458 459
					THREE.ShaderChunk[ "shadowmap_vertex" ],

460
				"}"
M
Mr.doob 已提交
461

462
			].join("\n")
M
Mr.doob 已提交
463 464 465

		},

466
		/* -------------------------------------------------------------------------
A
alteredq 已提交
467
		//	Cube map shader
468
		 ------------------------------------------------------------------------- */
469

470
		'cube': {
M
Mr.doob 已提交
471

472 473
			uniforms: { "tCube": { type: "t", value: 1, texture: null },
						"tFlip": { type: "f", value: -1 } },
M
Mr.doob 已提交
474

475
			vertexShader: [
M
Mr.doob 已提交
476

477
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
478

479
				"void main() {",
M
Mr.doob 已提交
480

481 482
					"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
					"vViewPosition = cameraPosition - mPosition.xyz;",
483

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

486
				"}"
M
Mr.doob 已提交
487

488
			].join("\n"),
M
Mr.doob 已提交
489

490
			fragmentShader: [
M
Mr.doob 已提交
491

492
				"uniform samplerCube tCube;",
493
				"uniform float tFlip;",
M
Mr.doob 已提交
494

495
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
496

497
				"void main() {",
M
Mr.doob 已提交
498

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

502
				"}"
M
Mr.doob 已提交
503

504
			].join("\n")
505

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

M
Mr.doob 已提交
508 509 510
	}

};
M
Mr.doob 已提交
511 512

};