ShaderUtils.js 17.9 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 307
					"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
					"vViewPosition = cameraPosition - mPosition.xyz;",
M
Mr.doob 已提交
308

309 310
					"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
					"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
			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 );",
A
alteredq 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 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 609

				"}"

			].join("\n")

		},


		/* -------------------------------------------------------------------------
		//	Sepia tone shader
		//  - based on glfx.js sepia shader
		//		https://github.com/evanw/glfx.js
		 ------------------------------------------------------------------------- */

		'sepia': {

			uniforms: {

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

			},

			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;",
				"uniform float amount;",

				"void main() {",

					"vec4 color = texture2D( tDiffuse, vUv );",
					"vec3 c = color.rgb;",

					"color.r = dot( c, vec3( 1.0 - 0.607 * amount, 0.769 * amount, 0.189 * amount ) );",
					"color.g = dot( c, vec3( 0.349 * amount, 1.0 - 0.314 * amount, 0.168 * amount ) );",
					"color.b = dot( c, vec3( 0.272 * amount, 0.534 * amount, 1.0 - 0.869 * amount ) );",

					"gl_FragColor = vec4( min( vec3( 1.0 ), color.rgb ), color.a );",
610 611 612 613

				"}"

			].join("\n")
A
alteredq 已提交
614 615 616 617 618

		},

		/* -------------------------------------------------------------------------
		//	Full-screen textured quad shader
619
		 ------------------------------------------------------------------------- */
620

621 622
		'screen': {

623 624 625 626 627 628
			uniforms: {

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

			},
629

630
			vertexShader: [
631

632
				"varying vec2 vUv;",
633

634
				"void main() {",
635

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

639
				"}"
640

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
			].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 已提交
657 658

		},
659

660

661
		/* -------------------------------------------------------------------------
A
alteredq 已提交
662
		//	Simple test shader
663
		 ------------------------------------------------------------------------- */
664

665
		'basic': {
M
Mr.doob 已提交
666

667
			uniforms: {},
M
Mr.doob 已提交
668

669
			vertexShader: [
M
Mr.doob 已提交
670

671
				"void main() {",
M
Mr.doob 已提交
672

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

675
				"}"
M
Mr.doob 已提交
676

677
			].join("\n"),
M
Mr.doob 已提交
678

679
			fragmentShader: [
M
Mr.doob 已提交
680

681
				"void main() {",
M
Mr.doob 已提交
682

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

685
				"}"
M
Mr.doob 已提交
686

687
			].join("\n")
688

M
Mr.doob 已提交
689
		}
M
Mr.doob 已提交
690

691
	},
692

693
	buildKernel: function( sigma ) {
694

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

697
		function gauss( x, sigma ) {
698 699 700

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

701
		}
702

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

705 706
		if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;
		halfWidth = ( kernelSize - 1 ) * 0.5
707

708 709
		values = new Array( kernelSize );
		sum = 0.0;
710 711
		for ( i = 0; i < kernelSize; ++i ) {

712 713
			values[ i ] = gauss( i - halfWidth, sigma );
			sum += values[ i ];
714

715
		}
716

717
		// normalize the kernel
718 719 720

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

721
		return values;
722

M
Mr.doob 已提交
723 724 725
	}

};
M
Mr.doob 已提交
726 727

};