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

M
Mr.doob 已提交
15 16
if ( THREE.WebGLRenderer ) {

17 18 19 20
THREE.ShaderUtils = {

	lib: {

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

26
		'fresnel': {
M
Mr.doob 已提交
27

28
			uniforms: {
M
Mr.doob 已提交
29

30 31 32 33 34
				"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 已提交
35

36 37 38 39 40
			},

			fragmentShader: [

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

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

46
				"void main() {",
M
Mr.doob 已提交
47

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

51 52 53 54
					"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 已提交
55

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

58
				"}"
M
Mr.doob 已提交
59

60
			].join("\n"),
M
Mr.doob 已提交
61

62
			vertexShader: [
M
Mr.doob 已提交
63

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

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

73
				"void main() {",
M
Mr.doob 已提交
74

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

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

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

82 83 84 85 86
					"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 已提交
87

88 89 90 91 92
					"gl_Position = projectionMatrix * mvPosition;",

				"}"

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

94
		},
M
Mr.doob 已提交
95

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

103
		'normal' : {
M
Mr.doob 已提交
104

M
Mr.doob 已提交
105 106
			uniforms: THREE.UniformsUtils.merge( [

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

				{
M
Mr.doob 已提交
111

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

116 117 118 119
				"tDiffuse"	: { type: "t", value: 0, texture: null },
				"tNormal"	: { type: "t", value: 2, texture: null },
				"tSpecular"	: { type: "t", value: 3, texture: null },
				"tAO"		: { type: "t", value: 4, texture: null },
M
Mr.doob 已提交
120

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

123
				"tDisplacement": { type: "t", value: 5, texture: null },
M
Mr.doob 已提交
124 125
				"uDisplacementBias": { type: "f", value: 0.0 },
				"uDisplacementScale": { type: "f", value: 1.0 },
M
Mr.doob 已提交
126

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

M
Mr.doob 已提交
133
				}
M
Mr.doob 已提交
134

M
Mr.doob 已提交
135
			] ),
M
Mr.doob 已提交
136

M
Mr.doob 已提交
137
			fragmentShader: [
M
Mr.doob 已提交
138

139 140 141 142
				"uniform vec3 uAmbientColor;",
				"uniform vec3 uDiffuseColor;",
				"uniform vec3 uSpecularColor;",
				"uniform float uShininess;",
M
Mr.doob 已提交
143
				"uniform float uOpacity;",
M
Mr.doob 已提交
144

145
				"uniform bool enableDiffuse;",
146
				"uniform bool enableSpecular;",
147
				"uniform bool enableAO;",
M
Mr.doob 已提交
148

149 150
				"uniform sampler2D tDiffuse;",
				"uniform sampler2D tNormal;",
151
				"uniform sampler2D tSpecular;",
152
				"uniform sampler2D tAO;",
M
Mr.doob 已提交
153

154
				"uniform float uNormalScale;",
M
Mr.doob 已提交
155

156 157 158 159
				"varying vec3 vTangent;",
				"varying vec3 vBinormal;",
				"varying vec3 vNormal;",
				"varying vec2 vUv;",
M
Mr.doob 已提交
160

M
Mr.doob 已提交
161 162 163 164 165 166 167 168 169 170 171 172
				"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",

173
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
174

A
alteredq 已提交
175 176
				THREE.ShaderChunk[ "fog_pars_fragment" ],

177
				"void main() {",
M
Mr.doob 已提交
178

M
Mr.doob 已提交
179 180 181 182 183 184
					"gl_FragColor = vec4( 1.0 );",

					"vec4 mColor = vec4( uDiffuseColor, uOpacity );",
					"vec4 mSpecular = vec4( uSpecularColor, uOpacity );",

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

186 187 188
					"vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
					"normalTex.xy *= uNormalScale;",
					"normalTex = normalize( normalTex );",
M
Mr.doob 已提交
189

190
					"if( enableDiffuse )",
M
Mr.doob 已提交
191
						"gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",
M
Mr.doob 已提交
192

193
					"if( enableAO )",
M
Mr.doob 已提交
194
						"gl_FragColor = gl_FragColor * texture2D( tAO, vUv );",
M
Mr.doob 已提交
195

196 197 198
					"if( enableSpecular )",
						"specularTex = texture2D( tSpecular, vUv ).xyz;",

199 200
					"mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
					"vec3 finalNormal = tsb * normalTex;",
M
Mr.doob 已提交
201

202 203
					"vec3 normal = normalize( finalNormal );",
					"vec3 viewPosition = normalize( vViewPosition );",
M
Mr.doob 已提交
204

M
Mr.doob 已提交
205 206 207
					// point lights

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

209
						"vec4 pointTotal = vec4( vec3( 0.0 ), 1.0 );",
M
Mr.doob 已提交
210

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

M
Mr.doob 已提交
213
							"vec3 pointVector = normalize( vPointLight[ i ].xyz );",
214
							"vec3 pointHalfVector = normalize( vPointLight[ i ].xyz + viewPosition );",
M
Mr.doob 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228
							"float pointDistance = vPointLight[ i ].w;",

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

							"float pointSpecularWeight = 0.0;",
							"if ( pointDotNormalHalf >= 0.0 )",
								"pointSpecularWeight = specularTex.r * pow( pointDotNormalHalf, uShininess );",

							"pointTotal  += pointDistance * vec4( pointLightColor[ i ], 1.0 ) * ( mColor * pointDiffuseWeight + mSpecular * pointSpecularWeight * pointDiffuseWeight );",

						"}",

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

M
Mr.doob 已提交
230
					// directional lights
M
Mr.doob 已提交
231

M
Mr.doob 已提交
232
					"#if MAX_DIR_LIGHTS > 0",
M
Mr.doob 已提交
233

234
						"vec4 dirTotal = vec4( vec3( 0.0 ), 1.0 );",
M
Mr.doob 已提交
235

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

M
Mr.doob 已提交
238
							"vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
M
Mr.doob 已提交
239

M
Mr.doob 已提交
240
							"vec3 dirVector = normalize( lDirection.xyz );",
241
							"vec3 dirHalfVector = normalize( lDirection.xyz + viewPosition );",
M
Mr.doob 已提交
242

M
Mr.doob 已提交
243 244
							"float dirDotNormalHalf = dot( normal, dirHalfVector );",
							"float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
M
Mr.doob 已提交
245

M
Mr.doob 已提交
246 247 248
							"float dirSpecularWeight = 0.0;",
							"if ( dirDotNormalHalf >= 0.0 )",
								"dirSpecularWeight = specularTex.r * pow( dirDotNormalHalf, uShininess );",
M
Mr.doob 已提交
249

M
Mr.doob 已提交
250 251 252 253 254
							"dirTotal  += vec4( directionalLightColor[ i ], 1.0 ) * ( mColor * dirDiffuseWeight + mSpecular * dirSpecularWeight * dirDiffuseWeight );",

						"}",

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

256
					// all lights contribution summation
M
Mr.doob 已提交
257

M
Mr.doob 已提交
258 259 260 261 262 263 264 265 266
					"vec4 totalLight = vec4( ambientLightColor * uAmbientColor, uOpacity );",

					"#if MAX_DIR_LIGHTS > 0",
						"totalLight += dirTotal;",
					"#endif",

					"#if MAX_POINT_LIGHTS > 0",
						"totalLight += pointTotal;",
					"#endif",
M
Mr.doob 已提交
267

M
Mr.doob 已提交
268
					"gl_FragColor = gl_FragColor * totalLight;",
M
Mr.doob 已提交
269

A
alteredq 已提交
270 271
					THREE.ShaderChunk[ "fog_fragment" ],

272
				"}"
M
Mr.doob 已提交
273

274
			].join("\n"),
M
Mr.doob 已提交
275

276
			vertexShader: [
M
Mr.doob 已提交
277

278
				"attribute vec4 tangent;",
M
Mr.doob 已提交
279

280
				"#ifdef VERTEX_TEXTURES",
M
Mr.doob 已提交
281

282 283 284
					"uniform sampler2D tDisplacement;",
					"uniform float uDisplacementScale;",
					"uniform float uDisplacementBias;",
M
Mr.doob 已提交
285

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

288 289 290 291
				"varying vec3 vTangent;",
				"varying vec3 vBinormal;",
				"varying vec3 vNormal;",
				"varying vec2 vUv;",
M
Mr.doob 已提交
292

M
Mr.doob 已提交
293 294 295 296 297 298 299 300 301
				"#if MAX_POINT_LIGHTS > 0",

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

					"varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",

				"#endif",

302
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
303

304
				"void main() {",
M
Mr.doob 已提交
305

306
					"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
M
Mr.doob 已提交
307

308
					"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
309
					"vViewPosition = -mvPosition.xyz;",
310
					"vNormal = normalize( normalMatrix * normal );",
M
Mr.doob 已提交
311

312
					// tangent and binormal vectors
M
Mr.doob 已提交
313

314
					"vTangent = normalize( normalMatrix * tangent.xyz );",
M
Mr.doob 已提交
315

316 317
					"vBinormal = cross( vNormal, vTangent ) * tangent.w;",
					"vBinormal = normalize( vBinormal );",
M
Mr.doob 已提交
318

319
					"vUv = uv;",
M
Mr.doob 已提交
320

M
Mr.doob 已提交
321 322 323 324 325 326 327 328 329 330 331
					// 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;",
M
Mr.doob 已提交
332

M
Mr.doob 已提交
333 334 335 336 337 338 339 340 341 342
							"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 已提交
343

344
					// displacement mapping
M
Mr.doob 已提交
345

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

348 349 350 351
						"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;",
M
Mr.doob 已提交
352

353
					"#else",
M
Mr.doob 已提交
354

355
						"gl_Position = projectionMatrix * mvPosition;",
M
Mr.doob 已提交
356

357
					"#endif",
M
Mr.doob 已提交
358

359
				"}"
M
Mr.doob 已提交
360

361
			].join("\n")
M
Mr.doob 已提交
362 363 364

		},

365
		/* -------------------------------------------------------------------------
A
alteredq 已提交
366
		//	Cube map shader
367
		 ------------------------------------------------------------------------- */
368

369
		'cube': {
M
Mr.doob 已提交
370

371
			uniforms: { "tCube": { type: "t", value: 1, texture: null } },
M
Mr.doob 已提交
372

373
			vertexShader: [
M
Mr.doob 已提交
374

375
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
376

377
				"void main() {",
M
Mr.doob 已提交
378

379 380
					"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
					"vViewPosition = cameraPosition - mPosition.xyz;",
381

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

384
				"}"
M
Mr.doob 已提交
385

386
			].join("\n"),
M
Mr.doob 已提交
387

388
			fragmentShader: [
M
Mr.doob 已提交
389

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

392
				"varying vec3 vViewPosition;",
M
Mr.doob 已提交
393

394
				"void main() {",
M
Mr.doob 已提交
395

396 397
					"vec3 wPos = cameraPosition - vViewPosition;",
					"gl_FragColor = textureCube( tCube, vec3( - wPos.x, wPos.yz ) );",
M
Mr.doob 已提交
398

399
				"}"
M
Mr.doob 已提交
400

401
			].join("\n")
402 403 404 405

		},

		/* ------------------------------------------------------------------------
406
		//	Convolution shader
A
alteredq 已提交
407 408
		//	  - ported from o3d sample to WebGL / GLSL
		//			http://o3d.googlecode.com/svn/trunk/samples/convolution.html
409
		------------------------------------------------------------------------ */
410

411 412
		'convolution': {

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
			uniforms: {

				"tDiffuse" : { type: "t", value: 0, texture: null },
				"uImageIncrement" : { type: "v2", value: new THREE.Vector2( 0.001953125, 0.0 ) },
				"cKernel" : { type: "fv1", value: [] }

			},

			vertexShader: [

				"varying vec2 vUv;",

				"uniform vec2 uImageIncrement;",
				//"#define KERNEL_SIZE 25.0",

				"void main(void) {",

					"vUv = uv - ((KERNEL_SIZE - 1.0) / 2.0) * uImageIncrement;",
					"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
432

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
				"}"

			].join("\n"),

			fragmentShader: [

				"varying vec2 vUv;",

				"uniform sampler2D tDiffuse;",
				"uniform vec2 uImageIncrement;",

				//"#define KERNEL_SIZE 25",
				"uniform float cKernel[KERNEL_SIZE];",

				"void main(void) {",

					"vec2 imageCoord = vUv;",
					"vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );",
					"for( int i=0; i<KERNEL_SIZE; ++i ) {",
						"sum += texture2D( tDiffuse, imageCoord ) * cKernel[i];",
						"imageCoord += uImageIncrement;",
					"}",
					"gl_FragColor = sum;",

				"}"


			].join("\n")
461 462 463 464

		},

		/* -------------------------------------------------------------------------
465

A
alteredq 已提交
466
		// Film grain & scanlines shader
467

A
alteredq 已提交
468 469 470 471 472 473 474 475 476 477 478 479
		//	- ported from HLSL to WebGL / GLSL
		//	  http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html

		// Screen Space Static Postprocessor
		//
		// Produces an analogue noise overlay similar to a film grain / TV static
		//
		// Original implementation and noise algorithm
		// Pat 'Hawthorne' Shearon
		//
		// Optimized scanlines + noise version with intensity scaling
		// Georg 'Leviathan' Steinrohder
480

A
alteredq 已提交
481 482 483 484 485 486
		// This version is provided under a Creative Commons Attribution 3.0 License
		// http://creativecommons.org/licenses/by/3.0/
		 ------------------------------------------------------------------------- */

		'film': {

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
			uniforms: {

				tDiffuse:   { type: "t", value: 0, texture: null },
				time: 	    { type: "f", value: 0.0 },
				nIntensity: { type: "f", value: 0.5 },
				sIntensity: { type: "f", value: 0.05 },
				sCount: 	{ type: "f", value: 4096 },
				grayscale:  { type: "i", value: 1 }

			},

			vertexShader: [

				"varying vec2 vUv;",

				"void main() {",

					"vUv = vec2( uv.x, 1.0 - uv.y );",
					"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",

				"}"

			].join("\n"),

			fragmentShader: [

				"varying vec2 vUv;",
				"uniform sampler2D tDiffuse;",

				// control parameter
				"uniform float time;",

				"uniform bool grayscale;",

				// noise effect intensity value (0 = no effect, 1 = full effect)
				"uniform float nIntensity;",

				// scanlines effect intensity value (0 = no effect, 1 = full effect)
				"uniform float sIntensity;",

				// scanlines effect count value (0 = no effect, 4096 = full effect)
				"uniform float sCount;",

				"void main() {",

					// sample the source
					"vec4 cTextureScreen = texture2D( tDiffuse, vUv );",

					// make some noise
					"float x = vUv.x * vUv.y * time *  1000.0;",
					"x = mod( x, 13.0 ) * mod( x, 123.0 );",
					"float dx = mod( x, 0.01 );",

					// add noise
					"vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx * 100.0, 0.0, 1.0 );",

					// get us a sine and cosine
					"vec2 sc = vec2( sin( vUv.y * sCount ), cos( vUv.y * sCount ) );",

					// add scanlines
					"cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity;",

					// interpolate between source and result by intensity
					"cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );",

					// convert to grayscale if desired
					"if( grayscale ) {",
						"cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );",
					"}",

					"gl_FragColor =  vec4( cResult, cTextureScreen.a );",

				"}"

			].join("\n")
A
alteredq 已提交
562 563 564 565 566

		},

		/* -------------------------------------------------------------------------
		//	Full-screen textured quad shader
567
		 ------------------------------------------------------------------------- */
568

569 570
		'screen': {

571 572 573 574 575 576
			uniforms: {

				tDiffuse: { type: "t", value: 0, texture: null },
				opacity: { type: "f", value: 1.0 }

			},
577

578
			vertexShader: [
579

580
				"varying vec2 vUv;",
581

582
				"void main() {",
583

584 585
					"vUv = vec2( uv.x, 1.0 - uv.y );",
					"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
586

587
				"}"
588

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
			].join("\n"),

			fragmentShader: [

				"varying vec2 vUv;",
				"uniform sampler2D tDiffuse;",
				"uniform float opacity;",

				"void main() {",

					"vec4 texel = texture2D( tDiffuse, vUv );",
					"gl_FragColor = opacity * texel;",

				"}"

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

		},
607

608

609
		/* -------------------------------------------------------------------------
A
alteredq 已提交
610
		//	Simple test shader
611
		 ------------------------------------------------------------------------- */
612

613
		'basic': {
M
Mr.doob 已提交
614

615
			uniforms: {},
M
Mr.doob 已提交
616

617
			vertexShader: [
M
Mr.doob 已提交
618

619
				"void main() {",
M
Mr.doob 已提交
620

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

623
				"}"
M
Mr.doob 已提交
624

625
			].join("\n"),
M
Mr.doob 已提交
626

627
			fragmentShader: [
M
Mr.doob 已提交
628

629
				"void main() {",
M
Mr.doob 已提交
630

631
					"gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );",
M
Mr.doob 已提交
632

633
				"}"
M
Mr.doob 已提交
634

635
			].join("\n")
636

M
Mr.doob 已提交
637
		}
M
Mr.doob 已提交
638

639
	},
640

641
	buildKernel: function( sigma ) {
642

643
		// We lop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.
644

645
		function gauss( x, sigma ) {
646 647 648

			return Math.exp( - ( x * x ) / ( 2.0 * sigma * sigma ) );

649
		}
650

651
		var i, values, sum, halfWidth, kMaxKernelSize = 25, kernelSize = 2 * Math.ceil( sigma * 3.0 ) + 1;
652

653 654
		if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;
		halfWidth = ( kernelSize - 1 ) * 0.5
655

656 657
		values = new Array( kernelSize );
		sum = 0.0;
658 659
		for ( i = 0; i < kernelSize; ++i ) {

660 661
			values[ i ] = gauss( i - halfWidth, sigma );
			sum += values[ i ];
662

663
		}
664

665
		// normalize the kernel
666 667 668

		for ( i = 0; i < kernelSize; ++i ) values[ i ] /= sum;

669
		return values;
670

M
Mr.doob 已提交
671 672 673
	}

};
M
Mr.doob 已提交
674 675

};