Browse Source

Moved textured quad screen shader into ShaderUtils.

alteredq 14 years ago
parent
commit
84d1b582ae
3 changed files with 112 additions and 73 deletions
  1. 1 1
      build/ThreeExtras.js
  2. 17 27
      examples/postprocessing.html
  3. 94 45
      src/extras/ShaderUtils.js

File diff suppressed because it is too large
+ 1 - 1
build/ThreeExtras.js


+ 17 - 27
examples/postprocessing.html

@@ -127,20 +127,6 @@
 			gl_FragColor =  vec4( cResult, cTextureScreen.a );
 			gl_FragColor =  vec4( cResult, cTextureScreen.a );
 		}
 		}
 		</script>
 		</script>
-        
-    
-		<!-- Render parameter modulated texture fragment shader -->
-        <script id="fs-screen" type="x-shader/x-fragment">
-			varying vec2 vUv;
-			uniform sampler2D tDiffuse;
-			uniform float opacity;
-			
-            void main(void)
-            {
-				vec4 texel = texture2D( tDiffuse, vUv );
-				gl_FragColor = opacity * texel;
-            }
-        </script>
 
 
 		<!-- Time modulated procedural color fragment shader -->
 		<!-- Time modulated procedural color fragment shader -->
         <script id="fs-colors" type="x-shader/x-fragment">
         <script id="fs-colors" type="x-shader/x-fragment">
@@ -219,13 +205,17 @@
 
 
                 } );
                 } );
 
 
+				var screen_shader = ShaderUtils.lib["screen"];
+				var screen_uniforms = Uniforms.clone( screen_shader.uniforms );
+				
+				screen_uniforms["tDiffuse"].texture = rtTexture1;
+				screen_uniforms["opacity"].value = 0.4;
+				
                 materialScreen = new THREE.MeshShaderMaterial( {
                 materialScreen = new THREE.MeshShaderMaterial( {
 
 
-                    uniforms: { tDiffuse: { type: "t", value: 0, texture: rtTexture1 },
-								opacity: { type: "f", value: 0.4 } 
-							  },
-                    vertex_shader: getText( 'vs-generic' ),
-                    fragment_shader: getText( 'fs-screen' ),
+                    uniforms: screen_uniforms,
+                    vertex_shader: screen_shader.vertex_shader,
+                    fragment_shader: screen_shader.fragment_shader,
 					blending: THREE.AdditiveBlending
 					blending: THREE.AdditiveBlending
 
 
                 } );
                 } );
@@ -245,18 +235,18 @@
 				blurx = new THREE.Vector2( increment, 0.0 ),
 				blurx = new THREE.Vector2( increment, 0.0 ),
 				blury = new THREE.Vector2( 0.0, increment );
 				blury = new THREE.Vector2( 0.0, increment );
 				
 				
-				var shader = ShaderUtils.lib["convolution"];
-				var uniforms = Uniforms.clone( shader.uniforms );
+				var convolution_shader = ShaderUtils.lib["convolution"];
+				var convolution_uniforms = Uniforms.clone( convolution_shader.uniforms );
 				
 				
-				uniforms["tDiffuse"].texture = rtTexture1;
-				uniforms["uImageIncrement"].value = blurx;
-				uniforms["cKernel"].value = ShaderUtils.buildKernel( 4.0 );
+				convolution_uniforms["tDiffuse"].texture = rtTexture1;
+				convolution_uniforms["uImageIncrement"].value = blurx;
+				convolution_uniforms["cKernel"].value = ShaderUtils.buildKernel( 4.0 );
 				
 				
                 materialConvolution = new THREE.MeshShaderMaterial( {
                 materialConvolution = new THREE.MeshShaderMaterial( {
 
 
-                    uniforms: uniforms,
-                    vertex_shader: shader.vertex_shader,
-                    fragment_shader: shader.fragment_shader
+                    uniforms: convolution_uniforms,
+                    vertex_shader: convolution_shader.vertex_shader,
+                    fragment_shader: convolution_shader.fragment_shader
 
 
                 } );
                 } );
 
 

+ 94 - 45
src/extras/ShaderUtils.js

@@ -7,69 +7,73 @@ var ShaderUtils = {
 			- based on Nvidia Cg tutorial
 			- based on Nvidia Cg tutorial
 		 ------------------------------------------------------------------------- */
 		 ------------------------------------------------------------------------- */
 		
 		
-			'fresnel': {
+		'fresnel': {
 
 
-			uniforms: {
+		uniforms: {
 
 
-			"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 }
+		"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 }
 
 
-			},
+		},
 
 
-			fragment_shader: [
+		fragment_shader: [
 
 
-			"uniform samplerCube tCube;",
+		"uniform samplerCube tCube;",
 
 
-			"varying vec3 vReflect;",
-			"varying vec3 vRefract[3];",
-			"varying float vReflectionFactor;",
+		"varying vec3 vReflect;",
+		"varying vec3 vRefract[3];",
+		"varying float vReflectionFactor;",
 
 
-			"void main() {",
-				"vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
-				"vec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );",
+		"void main() {",
+		
+			"vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
+			"vec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );",
 
 
-				"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;",
+			"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;",
 
 
-				"gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );",
-			"}"
+			"gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );",
+		
+		"}"
 
 
-			].join("\n"),
+		].join("\n"),
 
 
-			vertex_shader: [
+		vertex_shader: [
 
 
-			"uniform float mRefractionRatio;",
-			"uniform float mFresnelBias;",
-			"uniform float mFresnelScale;",
-			"uniform float mFresnelPower;",
+		"uniform float mRefractionRatio;",
+		"uniform float mFresnelBias;",
+		"uniform float mFresnelScale;",
+		"uniform float mFresnelPower;",
 
 
-			"varying vec3 vReflect;",
-			"varying vec3 vRefract[3];",
-			"varying float vReflectionFactor;",
+		"varying vec3 vReflect;",
+		"varying vec3 vRefract[3];",
+		"varying float vReflectionFactor;",
 
 
-			"void main(void) {",
-				"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
-				"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
+		"void main() {",
+		
+			"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
+			"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
 
 
-				"vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );",
+			"vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );",
 
 
-				"vec3 I = mPosition.xyz - cameraPosition;",
+			"vec3 I = mPosition.xyz - cameraPosition;",
 
 
-				"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 );",
+			"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 );",
 
 
-				"gl_Position = projectionMatrix * mvPosition;",
-			"}"
+			"gl_Position = projectionMatrix * mvPosition;",
+			
+		"}"
 
 
-			].join("\n")
+		].join("\n")
 
 
 		},
 		},
 
 
@@ -340,8 +344,10 @@ var ShaderUtils = {
 		"#define KERNEL_SIZE 25.0",
 		"#define KERNEL_SIZE 25.0",
 		
 		
 		"void main(void) {",
 		"void main(void) {",
+		
 			"vUv = uv - ((KERNEL_SIZE - 1.0) / 2.0) * uImageIncrement;",
 			"vUv = uv - ((KERNEL_SIZE - 1.0) / 2.0) * uImageIncrement;",
 			"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
 			"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
+		
 		"}"
 		"}"
 
 
 		].join("\n"),
 		].join("\n"),
@@ -357,6 +363,7 @@ var ShaderUtils = {
 		"uniform float cKernel[KERNEL_SIZE];",
 		"uniform float cKernel[KERNEL_SIZE];",
 		
 		
 		"void main(void) {",
 		"void main(void) {",
+		
 			"vec2 imageCoord = vUv;",
 			"vec2 imageCoord = vUv;",
 			"vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );",
 			"vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );",
 			"for( int i=0; i<KERNEL_SIZE; ++i ) {",
 			"for( int i=0; i<KERNEL_SIZE; ++i ) {",
@@ -364,13 +371,55 @@ var ShaderUtils = {
 				"imageCoord += uImageIncrement;",
 				"imageCoord += uImageIncrement;",
 			"}",
 			"}",
 			"gl_FragColor = sum;",
 			"gl_FragColor = sum;",
+			
+		"}"
+			
+
+		].join("\n")
+
+		},
+
+		/* -------------------------------------------------------------------------
+			Full-screen textured quad shader
+		 ------------------------------------------------------------------------- */
+		
+		'screen': {
+
+		uniforms: { tDiffuse: { type: "t", value: 0, texture: null },
+					opacity: { type: "f", value: 1.0 } 
+				  },
+
+		vertex_shader: [
+
+		"varying vec2 vUv;",
+
+		"void main() {",
+			
+			"vUv = vec2( uv.x, 1.0 - uv.y );",
+			"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
+			
 		"}"
 		"}"
+
+		].join("\n"),
+
+		fragment_shader: [
+		
+		"varying vec2 vUv;",
+		"uniform sampler2D tDiffuse;",
+		"uniform float opacity;",
+		
+		"void main() {",
+		
+			"vec4 texel = texture2D( tDiffuse, vUv );",
+			"gl_FragColor = opacity * texel;",
 			
 			
+		"}"
 
 
 		].join("\n")
 		].join("\n")
 
 
 		},
 		},
 
 
+		
 		/* -------------------------------------------------------------------------
 		/* -------------------------------------------------------------------------
 			Simple test shader
 			Simple test shader
 		 ------------------------------------------------------------------------- */
 		 ------------------------------------------------------------------------- */
@@ -393,7 +442,7 @@ var ShaderUtils = {
 
 
 		"void main() {",
 		"void main() {",
 
 
-			"gl_FragColor = vec4(1.0, 0.0, 0.0, 0.5);",
+			"gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );",
 
 
 		"}"
 		"}"
 
 

Some files were not shown because too many files changed in this diff