ShaderSkin.js 18.5 KB
Newer Older
1 2 3 4 5 6 7 8
/**
 * @author alteredq / http://alteredqualia.com/
 *
 */


THREE.ShaderSkin = {

A
alteredq 已提交
9 10 11 12 13 14
	/* ------------------------------------------------------------------------------------------
	//	Simple skin shader
	//		- per-pixel Blinn-Phong diffuse term mixed with half-Lambert wrap-around term (per color component)
	//		- physically based specular term (Kelemen/Szirmay-Kalos specular reflectance)
	//
	//		- diffuse map
15 16
	//		- bump map
	//		- specular map
17
	//		- point, directional and hemisphere lights (use with "lights: true" material option)
A
alteredq 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
	//		- fog (use with "fog: true" material option)
	//		- shadow maps
	//
	// ------------------------------------------------------------------------------------------ */

	'skinSimple' : {

		uniforms: THREE.UniformsUtils.merge( [

			THREE.UniformsLib[ "fog" ],
			THREE.UniformsLib[ "lights" ],
			THREE.UniformsLib[ "shadowmap" ],

			{

G
gero3 已提交
33 34
				"enableBump"	: { type: "i", value: 0 },
				"enableSpecular": { type: "i", value: 0 },
35

G
gero3 已提交
36 37
				"tDiffuse"	: { type: "t", value: null },
				"tBeckmann"	: { type: "t", value: null },
A
alteredq 已提交
38

G
gero3 已提交
39 40 41
				"diffuse":  { type: "c", value: new THREE.Color( 0xeeeeee ) },
				"specular": { type: "c", value: new THREE.Color( 0x111111 ) },
				"opacity": 	  { type: "f", value: 1 },
A
alteredq 已提交
42

G
gero3 已提交
43 44
				"uRoughness": 	  		{ type: "f", value: 0.15 },
				"uSpecularBrightness": 	{ type: "f", value: 0.75 },
A
alteredq 已提交
45

G
gero3 已提交
46 47
				"bumpMap"	: { type: "t", value: null },
				"bumpScale" : { type: "f", value: 1 },
48

G
gero3 已提交
49
				"specularMap" : { type: "t", value: null },
50

G
gero3 已提交
51
				"offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
52

G
gero3 已提交
53
				"uWrapRGB":	{ type: "v3", value: new THREE.Vector3( 0.75, 0.375, 0.1875 ) }
A
alteredq 已提交
54 55 56 57 58 59 60

			}

		] ),

		fragmentShader: [

61 62 63 64 65
			"#define USE_BUMPMAP",

			"uniform bool enableBump;",
			"uniform bool enableSpecular;",

B
Ben Houston 已提交
66 67 68
			"uniform vec3 diffuse;",
			"uniform vec3 specular;",
			"uniform float opacity;",
A
alteredq 已提交
69 70 71 72 73 74 75 76 77

			"uniform float uRoughness;",
			"uniform float uSpecularBrightness;",

			"uniform vec3 uWrapRGB;",

			"uniform sampler2D tDiffuse;",
			"uniform sampler2D tBeckmann;",

78 79
			"uniform sampler2D specularMap;",

A
alteredq 已提交
80 81 82 83 84
			"varying vec3 vNormal;",
			"varying vec2 vUv;",

			"varying vec3 vViewPosition;",

85
			THREE.ShaderChunk[ "common" ],
B
Ben Houston 已提交
86
			THREE.ShaderChunk[ "bsdfs" ],
87
			THREE.ShaderChunk[ "lights_pars" ],
A
alteredq 已提交
88 89
			THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
			THREE.ShaderChunk[ "fog_pars_fragment" ],
90
			THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
A
alteredq 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

			// Fresnel term

			"float fresnelReflectance( vec3 H, vec3 V, float F0 ) {",

				"float base = 1.0 - dot( V, H );",
				"float exponential = pow( base, 5.0 );",

				"return exponential + F0 * ( 1.0 - exponential );",

			"}",

			// Kelemen/Szirmay-Kalos specular BRDF

			"float KS_Skin_Specular( vec3 N,", 		// Bumped surface normal
									"vec3 L,", 		// Points to light
									"vec3 V,", 		// Points to eye
									"float m,",  	// Roughness
									"float rho_s", 	// Specular brightness
									") {",

				"float result = 0.0;",
				"float ndotl = dot( N, L );",

				"if( ndotl > 0.0 ) {",

					"vec3 h = L + V;", // Unnormalized half-way vector
					"vec3 H = normalize( h );",

					"float ndoth = dot( N, H );",

					"float PH = pow( 2.0 * texture2D( tBeckmann, vec2( ndoth, m ) ).x, 10.0 );",

					"float F = fresnelReflectance( H, V, 0.028 );",
					"float frSpec = max( PH * F / dot( h, h ), 0.0 );",

					"result = ndotl * rho_s * frSpec;", // BRDF * dot(N,L) * rho_s

				"}",

				"return result;",

			"}",

			"void main() {",

137 138
				"vec3 outgoingLight = vec3( 0.0 );",	// outgoing light does not have an alpha, the surface does
				"vec4 diffuseColor = vec4( diffuse, opacity );",
M
Mr.doob 已提交
139
				"vec3 shadowMask = vec3( 1.0 );",
A
alteredq 已提交
140 141 142 143

				"vec4 colDiffuse = texture2D( tDiffuse, vUv );",
				"colDiffuse.rgb *= colDiffuse.rgb;",

144
				"diffuseColor = diffuseColor * colDiffuse;",
A
alteredq 已提交
145 146

				"vec3 normal = normalize( vNormal );",
T
tschw 已提交
147
				"vec3 viewerDirection = normalize( vViewPosition );",
A
alteredq 已提交
148

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
				"float specularStrength;",

				"if ( enableSpecular ) {",

					"vec4 texelSpecular = texture2D( specularMap, vUv );",
					"specularStrength = texelSpecular.r;",

				"} else {",

					"specularStrength = 1.0;",

				"}",

				"#ifdef USE_BUMPMAP",

					"if ( enableBump ) normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );",

				"#endif",

A
alteredq 已提交
168 169
				// point lights

170 171
				"vec3 totalSpecularLight = vec3( 0.0 );",
				"vec3 totalDiffuseLight = vec3( 0.0 );",
A
alteredq 已提交
172

173
				"#if NUM_POINT_LIGHTS > 0",
A
alteredq 已提交
174

175
					"for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {",
A
alteredq 已提交
176

B
Ben Houston 已提交
177
						"vec3 lVector = pointLights[ i ].position + vViewPosition.xyz;",
M
Mr.doob 已提交
178

B
Ben Houston 已提交
179
						"float attenuation = calcLightAttenuation( length( lVector ), pointLights[ i ].distance, pointLights[ i ].decay );",
A
alteredq 已提交
180 181 182 183 184 185 186

						"lVector = normalize( lVector );",

						"float pointDiffuseWeightFull = max( dot( normal, lVector ), 0.0 );",
						"float pointDiffuseWeightHalf = max( 0.5 * dot( normal, lVector ) + 0.5, 0.0 );",
						"vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), uWrapRGB );",

T
tschw 已提交
187
						"float pointSpecularWeight = KS_Skin_Specular( normal, lVector, viewerDirection, uRoughness, uSpecularBrightness );",
A
alteredq 已提交
188

B
Ben Houston 已提交
189 190
						"totalDiffuseLight += pointLight[ i ].color * ( pointDiffuseWeight * attenuation );",
						"totalSpecularLight += pointLight[ i ].color * specular * ( pointSpecularWeight * specularStrength * attenuation );",
A
alteredq 已提交
191 192 193 194 195 196 197

					"}",

				"#endif",

				// directional lights

198
				"#if NUM_DIR_LIGHTS > 0",
A
alteredq 已提交
199

200
					"for( int i = 0; i < NUM_DIR_LIGHTS; i++ ) {",
A
alteredq 已提交
201

202
						"vec3 dirVector = directionalLights[ i ].direction;",
A
alteredq 已提交
203 204 205 206 207

						"float dirDiffuseWeightFull = max( dot( normal, dirVector ), 0.0 );",
						"float dirDiffuseWeightHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
						"vec3 dirDiffuseWeight = mix( vec3 ( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), uWrapRGB );",

T
tschw 已提交
208
						"float dirSpecularWeight = KS_Skin_Specular( normal, dirVector, viewerDirection, uRoughness, uSpecularBrightness );",
A
alteredq 已提交
209

210 211
						"totalDiffuseLight += directionalLights[ i ].color * dirDiffuseWeight;",
						"totalSpecularLight += directionalLights[ i ].color * ( dirSpecularWeight * specularStrength );",
A
alteredq 已提交
212 213 214 215 216

					"}",

				"#endif",

217 218
				// hemisphere lights

219
				"#if NUM_HEMI_LIGHTS > 0",
220

221
					"for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {",
222

T
tschw 已提交
223
						"vec3 lVector = hemisphereLightDirection[ i ];",
224 225 226 227

						"float dotProduct = dot( normal, lVector );",
						"float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",

228
						"totalDiffuseLight += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
229 230 231 232

						// specular (sky light)

						"float hemiSpecularWeight = 0.0;",
T
tschw 已提交
233
						"hemiSpecularWeight += KS_Skin_Specular( normal, lVector, viewerDirection, uRoughness, uSpecularBrightness );",
234 235 236

						// specular (ground light)

A
alteredq 已提交
237
						"vec3 lVectorGround = -lVector;",
T
tschw 已提交
238
						"hemiSpecularWeight += KS_Skin_Specular( normal, lVectorGround, viewerDirection, uRoughness, uSpecularBrightness );",
239

T
tschw 已提交
240 241 242
						"vec3 hemiSpecularColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",

						"totalSpecularLight += hemiSpecularColor * specular * ( hemiSpecularWeight * specularStrength );",
243 244 245 246 247

					"}",

				"#endif",

M
Mr.doob 已提交
248 249 250 251 252
				THREE.ShaderChunk[ "shadowmap_fragment" ],

				"totalDiffuseLight *= shadowMask;",
				"totalSpecularLight *= shadowMask;",

253
				"outgoingLight += diffuseColor.xyz * ( totalDiffuseLight + ambientLightColor * diffuse ) + totalSpecularLight;",
A
alteredq 已提交
254 255 256 257

				THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
				THREE.ShaderChunk[ "fog_fragment" ],

258
				"gl_FragColor = vec4( outgoingLight, diffuseColor.a );",	// TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects
259 260


A
alteredq 已提交
261 262
			"}"

G
gero3 已提交
263
		].join( "\n" ),
A
alteredq 已提交
264 265 266

		vertexShader: [

267 268
			"uniform vec4 offsetRepeat;",

A
alteredq 已提交
269 270 271 272 273
			"varying vec3 vNormal;",
			"varying vec2 vUv;",

			"varying vec3 vViewPosition;",

274
			THREE.ShaderChunk[ "common" ],
A
alteredq 已提交
275 276 277 278 279
			THREE.ShaderChunk[ "shadowmap_pars_vertex" ],

			"void main() {",

				"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
280
				"vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
M
Mr.doob 已提交
281

A
alteredq 已提交
282 283
				"vViewPosition = -mvPosition.xyz;",

284
				"vNormal = normalize( normalMatrix * normal );",
A
alteredq 已提交
285

286
				"vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
A
alteredq 已提交
287 288 289 290 291 292 293 294 295 296 297

				"gl_Position = projectionMatrix * mvPosition;",

				THREE.ShaderChunk[ "shadowmap_vertex" ],

			"}"

		].join( "\n" )

	},

298 299
	/* ------------------------------------------------------------------------------------------
	//	Skin shader
300 301 302 303
	//		- Blinn-Phong diffuse term (using normal + diffuse maps)
	//		- subsurface scattering approximation by four blur layers
	//		- physically based specular term (Kelemen/Szirmay-Kalos specular reflectance)
	//
304 305 306
	//		- point and directional lights (use with "lights: true" material option)
	//
	//		- based on Nvidia Advanced Skin Rendering GDC 2007 presentation
307 308
	//		  and GPU Gems 3 Chapter 14. Advanced Techniques for Realistic Real-Time Skin Rendering
	//
309
	//			http://developer.download.nvidia.com/presentations/2007/gdc/Advanced_Skin.pdf
310
	//			http://http.developer.nvidia.com/GPUGems3/gpugems3_ch14.html
311 312 313 314 315 316 317 318 319 320 321
	// ------------------------------------------------------------------------------------------ */

	'skin' : {

		uniforms: THREE.UniformsUtils.merge( [

			THREE.UniformsLib[ "fog" ],
			THREE.UniformsLib[ "lights" ],

			{

G
gero3 已提交
322
				"passID": { type: "i", value: 0 },
323

G
gero3 已提交
324 325
				"tDiffuse"	: { type: "t", value: null },
				"tNormal"	: { type: "t", value: null },
326

G
gero3 已提交
327 328 329 330
				"tBlur1"	: { type: "t", value: null },
				"tBlur2"	: { type: "t", value: null },
				"tBlur3"	: { type: "t", value: null },
				"tBlur4"	: { type: "t", value: null },
331

G
gero3 已提交
332
				"tBeckmann"	: { type: "t", value: null },
333

G
gero3 已提交
334
				"uNormalScale": { type: "f", value: 1.0 },
335

G
gero3 已提交
336 337 338
				"diffuse":  { type: "c", value: new THREE.Color( 0xeeeeee ) },
				"specular": { type: "c", value: new THREE.Color( 0x111111 ) },
				"opacity": 	  { type: "f", value: 1 },
339

G
gero3 已提交
340
				"uRoughness": 	  		{ type: "f", value: 0.15 },
T
tschw 已提交
341
				"uSpecularBrightness": 	{ type: "f", value: 0.75 }
342 343 344 345 346 347 348

			}

		] ),

		fragmentShader: [

B
Ben Houston 已提交
349 350 351
			"uniform vec3 diffuse;",
			"uniform vec3 specular;",
			"uniform float opacity;",
352

353 354 355
			"uniform float uRoughness;",
			"uniform float uSpecularBrightness;",

356 357 358 359 360 361 362 363 364 365
			"uniform int passID;",

			"uniform sampler2D tDiffuse;",
			"uniform sampler2D tNormal;",

			"uniform sampler2D tBlur1;",
			"uniform sampler2D tBlur2;",
			"uniform sampler2D tBlur3;",
			"uniform sampler2D tBlur4;",

366 367
			"uniform sampler2D tBeckmann;",

368 369 370 371 372 373 374
			"uniform float uNormalScale;",

			"varying vec3 vNormal;",
			"varying vec2 vUv;",

			"varying vec3 vViewPosition;",

375
			THREE.ShaderChunk[ "common" ],
376
			THREE.ShaderChunk[ "lights_pars" ],
377 378
			THREE.ShaderChunk[ "fog_pars_fragment" ],

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
			"float fresnelReflectance( vec3 H, vec3 V, float F0 ) {",

				"float base = 1.0 - dot( V, H );",
				"float exponential = pow( base, 5.0 );",

				"return exponential + F0 * ( 1.0 - exponential );",

			"}",

			// Kelemen/Szirmay-Kalos specular BRDF

			"float KS_Skin_Specular( vec3 N,", 		// Bumped surface normal
									"vec3 L,", 		// Points to light
									"vec3 V,", 		// Points to eye
									"float m,",  	// Roughness
									"float rho_s", 	// Specular brightness
									") {",

				"float result = 0.0;",
				"float ndotl = dot( N, L );",

				"if( ndotl > 0.0 ) {",

					"vec3 h = L + V;", // Unnormalized half-way vector
					"vec3 H = normalize( h );",

					"float ndoth = dot( N, H );",

					"float PH = pow( 2.0 * texture2D( tBeckmann, vec2( ndoth, m ) ).x, 10.0 );",
					"float F = fresnelReflectance( H, V, 0.028 );",
					"float frSpec = max( PH * F / dot( h, h ), 0.0 );",

					"result = ndotl * rho_s * frSpec;", // BRDF * dot(N,L) * rho_s

				"}",

				"return result;",

			"}",

419 420
			"void main() {",

421 422
				"vec3 outgoingLight = vec3( 0.0 );",	// outgoing light does not have an alpha, the surface does
				"vec4 diffuseColor = vec4( diffuse, opacity );",
423

B
Ben Houston 已提交
424
				"vec4 mSpecular = vec4( specular, opacity );",
425 426 427 428

				"vec4 colDiffuse = texture2D( tDiffuse, vUv );",
				"colDiffuse *= colDiffuse;",

429
				"diffuseColor *= colDiffuse;",
430

431 432
				// normal mapping

T
tschw 已提交
433 434 435 436 437 438 439
				"vec4 posAndU = vec4( -vViewPosition, vUv.x );",
				"vec4 posAndU_dx = dFdx( posAndU ),  posAndU_dy = dFdy( posAndU );",
				"vec3 tangent = posAndU_dx.w * posAndU_dx.xyz + posAndU_dy.w * posAndU_dy.xyz;",
				"vec3 normal = normalize( vNormal );",
				"vec3 binormal = normalize( cross( tangent, normal ) );",
				"tangent = cross( normal, binormal );",	// no normalization required
				"mat3 tsb = mat3( tangent, binormal, normal );",
440 441 442 443 444

				"vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
				"normalTex.xy *= uNormalScale;",
				"normalTex = normalize( normalTex );",

445
				"vec3 finalNormal = tsb * normalTex;",
T
tschw 已提交
446
				"normal = normalize( finalNormal );",
447

T
tschw 已提交
448
				"vec3 viewerDirection = normalize( vViewPosition );",
449 450 451

				// point lights

452 453
				"vec3 totalDiffuseLight = vec3( 0.0 );",
				"vec3 totalSpecularLight = vec3( 0.0 );",
454

455
				"#if NUM_POINT_LIGHTS > 0",
M
Mr.doob 已提交
456

457
					"for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {",
458

459
						"vec3 pointVector = normalize( pointLights[ i ].direction );",
460
						"float attenuation = calcLightAttenuation( length( lVector ), pointLights[ i ].distance, pointLights[ i ].decay );",
M
Mr.doob 已提交
461

462
						"float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
M
Mr.doob 已提交
463

T
tschw 已提交
464 465 466 467 468 469 470
						"totalDiffuseLight += pointLightColor[ i ] * ( pointDiffuseWeight * attenuation );",

						"if ( passID == 1 ) {",

							"float pointSpecularWeight = KS_Skin_Specular( normal, pointVector, viewerDirection, uRoughness, uSpecularBrightness );",

							"totalSpecularLight += pointLightColor[ i ] * mSpecular.xyz * ( pointSpecularWeight * attenuation );",
471

T
tschw 已提交
472
						"}",
473 474 475 476 477 478 479

					"}",

				"#endif",

				// directional lights

480
				"#if NUM_DIR_LIGHTS > 0",
481

482
					"for( int i = 0; i < NUM_DIR_LIGHTS; i++ ) {",
483

484
						"vec3 dirVector = directionalLights[ i ].direction;",
485 486 487

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

M
Mr.doob 已提交
488

489
						"totalDiffuseLight += directionalLights[ i ].color * dirDiffuseWeight;",
490

T
tschw 已提交
491 492 493 494
						"if ( passID == 1 ) {",

							"float dirSpecularWeight = KS_Skin_Specular( normal, dirVector, viewerDirection, uRoughness, uSpecularBrightness );",

495
							"totalSpecularLight += directionalLights[ i ].color * mSpecular.xyz * dirSpecularWeight;",
T
tschw 已提交
496 497

						"}",
498 499 500 501 502 503

					"}",

				"#endif",


504
				"outgoingLight += diffuseColor.rgb * ( totalDiffuseLight + totalSpecularLight );",
505 506 507

				"if ( passID == 0 ) {",

508
					"outgoingLight = sqrt( outgoingLight );",
509 510 511 512 513 514 515

				"} else if ( passID == 1 ) {",

					//"#define VERSION1",

					"#ifdef VERSION1",

516
						"vec3 nonblurColor = sqrt(outgoingLight );",
517 518 519

					"#else",

520
						"vec3 nonblurColor = outgoingLight;",
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536

					"#endif",

					"vec3 blur1Color = texture2D( tBlur1, vUv ).xyz;",
					"vec3 blur2Color = texture2D( tBlur2, vUv ).xyz;",
					"vec3 blur3Color = texture2D( tBlur3, vUv ).xyz;",
					"vec3 blur4Color = texture2D( tBlur4, vUv ).xyz;",


					//"gl_FragColor = vec4( blur1Color, gl_FragColor.w );",

					//"gl_FragColor = vec4( vec3( 0.22, 0.5, 0.7 ) * nonblurColor + vec3( 0.2, 0.5, 0.3 ) * blur1Color + vec3( 0.58, 0.0, 0.0 ) * blur2Color, gl_FragColor.w );",

					//"gl_FragColor = vec4( vec3( 0.25, 0.6, 0.8 ) * nonblurColor + vec3( 0.15, 0.25, 0.2 ) * blur1Color + vec3( 0.15, 0.15, 0.0 ) * blur2Color + vec3( 0.45, 0.0, 0.0 ) * blur3Color, gl_FragColor.w );",


537
					"outgoingLight = vec3( vec3( 0.22,  0.437, 0.635 ) * nonblurColor + ",
538 539 540
										 "vec3( 0.101, 0.355, 0.365 ) * blur1Color + ",
										 "vec3( 0.119, 0.208, 0.0 )   * blur2Color + ",
										 "vec3( 0.114, 0.0,   0.0 )   * blur3Color + ",
541
										 "vec3( 0.444, 0.0,   0.0 )   * blur4Color );",
542

543
					"outgoingLight *= sqrt( colDiffuse.xyz );",
544

545
					"outgoingLight += ambientLightColor * diffuse * colDiffuse.xyz + totalSpecularLight;",
546 547 548

					"#ifndef VERSION1",

549
						"outgoingLight = sqrt( outgoingLight );",
550 551 552 553 554 555 556

					"#endif",

				"}",

				THREE.ShaderChunk[ "fog_fragment" ],

557
				"gl_FragColor = vec4( outgoingLight, diffuseColor.a );",	// TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects
558

559 560
			"}"

G
gero3 已提交
561
		].join( "\n" ),
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

		vertexShader: [

			"#ifdef VERTEX_TEXTURES",

				"uniform sampler2D tDisplacement;",
				"uniform float uDisplacementScale;",
				"uniform float uDisplacementBias;",

			"#endif",

			"varying vec3 vNormal;",
			"varying vec2 vUv;",

			"varying vec3 vViewPosition;",

578 579
			THREE.ShaderChunk[ "common" ],

580 581
			"void main() {",

582
				"vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

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

				"vViewPosition = -mvPosition.xyz;",

				"vNormal = normalize( normalMatrix * normal );",

				"vUv = uv;",

				// displacement mapping

				"#ifdef VERTEX_TEXTURES",

					"vec3 dv = texture2D( tDisplacement, uv ).xyz;",
					"float df = uDisplacementScale * dv.x + uDisplacementBias;",
					"vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
					"gl_Position = projectionMatrix * displacedPosition;",

				"#else",

					"gl_Position = projectionMatrix * mvPosition;",

				"#endif",

			"}"

G
gero3 已提交
609
		].join( "\n" ),
610 611 612 613 614 615 616 617

		vertexShaderUV: [

			"varying vec3 vNormal;",
			"varying vec2 vUv;",

			"varying vec3 vViewPosition;",

618 619
			THREE.ShaderChunk[ "common" ],

620 621
			"void main() {",

622
				"vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
623 624 625 626 627 628 629 630 631 632 633 634 635

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

				"vViewPosition = -mvPosition.xyz;",

				"vNormal = normalize( normalMatrix * normal );",

				"vUv = uv;",

				"gl_Position = vec4( uv.x * 2.0 - 1.0, uv.y * 2.0 - 1.0, 0.0, 1.0 );",

			"}"

G
gero3 已提交
636
		].join( "\n" )
637

638 639 640 641 642 643 644 645 646 647 648
	},

	/* ------------------------------------------------------------------------------------------
	// Beckmann distribution function
	//	- to be used in specular term of skin shader
	//	- render a screen-aligned quad to precompute a 512 x 512 texture
	//
	//		- from http://developer.nvidia.com/node/171
	 ------------------------------------------------------------------------------------------ */

	"beckmann" : {
649

650 651 652 653 654 655 656 657
		uniforms: {},

		vertexShader: [

			"varying vec2 vUv;",

			"void main() {",

658
				"vUv = uv;",
659 660 661 662
				"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",

			"}"

G
gero3 已提交
663
		].join( "\n" ),
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680

		fragmentShader: [

			"varying vec2 vUv;",

			"float PHBeckmann( float ndoth, float m ) {",

				"float alpha = acos( ndoth );",
				"float ta = tan( alpha );",

				"float val = 1.0 / ( m * m * pow( ndoth, 4.0 ) ) * exp( -( ta * ta ) / ( m * m ) );",
				"return val;",

			"}",

			"float KSTextureCompute( vec2 tex ) {",

681
				// Scale the value to fit within [0,1]  invert upon lookup.
682 683 684 685 686 687 688 689 690 691 692 693 694

				"return 0.5 * pow( PHBeckmann( tex.x, tex.y ), 0.1 );",

			"}",

			"void main() {",

				"float x = KSTextureCompute( vUv );",

				"gl_FragColor = vec4( x, x, x, 1.0 );",

			"}"

G
gero3 已提交
695
		].join( "\n" )
696 697

	}
698

699
};