瀏覽代碼

Added fog parameter (boolean, true by default) to common materials.
Cleaned up shader snippets in WebGLRenderer2.

Mr.doob 14 年之前
父節點
當前提交
309fffe98f

+ 1 - 1
examples/webglrenderer2_sandbox.html

@@ -152,7 +152,7 @@
 					new THREE.MeshNormalMaterial( { shading: THREE.SmoothShading } ),
 					new THREE.MeshBasicMaterial( { color: 0xffaa00, wireframe: true } ),
 					new THREE.MeshDepthMaterial( { near: 1, far: 2000 } ),
-					new THREE.MeshBasicMaterial( { map: generatedTexture } ),
+					new THREE.MeshBasicMaterial( { map: generatedTexture, fog: false } ),
 					new THREE.MeshLambertMaterial( { map: generatedTexture } ),
 					new THREE.MeshShaderMaterial( { uniforms: uniforms, vertex_shader: vertex_shader, fragment_shader: fragment_shader } )
 

+ 4 - 0
src/materials/MeshBasicMaterial.js

@@ -31,6 +31,8 @@ THREE.MeshBasicMaterial = function ( parameters ) {
 	this.reflectivity = 1;
 	this.refraction_ratio = 0.98;
 
+	this.fog = true;
+
 	this.opacity = 1;
 	this.shading = THREE.SmoothShading;
 	this.blending = THREE.NormalBlending;
@@ -50,6 +52,8 @@ THREE.MeshBasicMaterial = function ( parameters ) {
 		if ( parameters.reflectivity !== undefined ) this.reflectivity  = parameters.reflectivity;
 		if ( parameters.refraction_ratio !== undefined ) this.refraction_ratio  = parameters.refraction_ratio;
 
+		if ( parameters.fog !== undefined ) this.fog  = parameters.fog;
+
 		if ( parameters.opacity !== undefined ) this.opacity  = parameters.opacity;
 		if ( parameters.shading !== undefined ) this.shading = parameters.shading;
 		if ( parameters.blending !== undefined ) this.blending = parameters.blending;

+ 1 - 1
src/materials/MeshDepthMaterial.js

@@ -13,7 +13,7 @@ THREE.MeshDepthMaterial = function ( parameters ) {
 
 	this.near = 1;
 	this.far = 1000;
-	
+
 	this.opacity = 1;
 	this.shading = THREE.SmoothShading;
 	this.blending = THREE.NormalBlending;

+ 4 - 0
src/materials/MeshLambertMaterial.js

@@ -31,6 +31,8 @@ THREE.MeshLambertMaterial = function ( parameters ) {
 	this.reflectivity = 1;
 	this.refraction_ratio = 0.98;
 
+	this.fog = true;
+
 	this.opacity = 1;
 	this.shading = THREE.SmoothShading;
 	this.blending = THREE.NormalBlending;
@@ -50,6 +52,8 @@ THREE.MeshLambertMaterial = function ( parameters ) {
 		if ( parameters.reflectivity !== undefined ) this.reflectivity  = parameters.reflectivity;
 		if ( parameters.refraction_ratio !== undefined ) this.refraction_ratio  = parameters.refraction_ratio;
 
+		if ( parameters.fog !== undefined ) this.fog  = parameters.fog;
+
 		if ( parameters.opacity !== undefined ) this.opacity  = parameters.opacity;
 		if ( parameters.shading !== undefined ) this.shading = parameters.shading;
 		if ( parameters.blending !== undefined ) this.blending = parameters.blending;

+ 4 - 0
src/materials/MeshPhongMaterial.js

@@ -41,6 +41,8 @@ THREE.MeshPhongMaterial = function ( parameters ) {
 	this.reflectivity = 1;
 	this.refraction_ratio = 0.98;
 
+	this.fog = true;
+
 	this.opacity = 1;
 	this.shading = THREE.SmoothShading;
 	this.blending = THREE.NormalBlending;
@@ -65,6 +67,8 @@ THREE.MeshPhongMaterial = function ( parameters ) {
 		if ( parameters.reflectivity !== undefined ) this.reflectivity  = parameters.reflectivity;
 		if ( parameters.refraction_ratio !== undefined ) this.refraction_ratio  = parameters.refraction_ratio;
 
+		if ( parameters.fog !== undefined ) this.fog  = parameters.fog;
+
 		if ( parameters.opacity !== undefined ) this.opacity = parameters.opacity;
 		if ( parameters.shading !== undefined ) this.shading = parameters.shading;
 		if ( parameters.blending !== undefined ) this.blending = parameters.blending;

+ 72 - 67
src/renderers/WebGLRenderer2.js

@@ -468,99 +468,98 @@ THREE.WebGLRenderer2 = function ( antialias ) {
 
 		function createProgram( material ) {
 
-			var pvs = '', vs = '', pfs = '', fs = '',
-			identifiers = [ 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition' ];
+			var vs, fs, identifiers = [ 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition' ];
 
 			if ( material instanceof THREE.MeshBasicMaterial ||
 			material instanceof THREE.MeshLambertMaterial ||
 			material instanceof THREE.MeshPhongMaterial ) {
 
-				vs += 'void main() {\n';
-				fs += 'void main() {\n';
+				vs = [
+					material.map ? 'varying vec2 vUv;' : null,
 
-				vs += 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n';
+					'void main() {',
+						material.map ? 'vUv = uv;' : null,
+						'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
+					'}'
+				].filter( removeNull ).join( '\n' );
 
-				pfs += 'uniform vec3 mColor;\n';
-				pfs += 'uniform float mOpacity;\n';
+				fs = [
+					'uniform vec3 mColor;',
+					'uniform float mOpacity;',
 
-				fs += 'gl_FragColor = vec4( mColor.xyz, mOpacity );\n';
+					material.map ? 'uniform sampler2D tMap;' : null,
+					material.map ? 'varying vec2 vUv;' : null,
 
-				identifiers.push( 'mColor', 'mOpacity' );
-
-				// uvmap
-
-				if ( material.map ) {
-
-					pvs += 'varying vec2 vUv;\n',
-					vs += 'vUv = uv;\n',
-
-					pfs += 'uniform sampler2D tMap;\n';
-					pfs += 'varying vec2 vUv;\n';
-
-					/* Premultiply alpha
-					fs += 'vec4 mapColor = texture2D( tMap, vUv );\n';
-					fs += 'mapColor.xyz *= mapColor.w;\n';
-					*/
-
-					fs += 'gl_FragColor *= texture2D( tMap, vUv );\n';
-
-					identifiers.push( 'tMap' );
-
-				}
+					material.fog ? 'uniform float fog;' : null,
+					material.fog ? 'uniform vec3 fogColor;' : null,
+					material.fog ? 'uniform float fogDensity;' : null,
 
-				// fog
+					'void main() {',
+						'gl_FragColor = vec4( mColor.xyz, mOpacity );',
 
-				pfs += 'uniform float fog;\n';
-				pfs += 'uniform vec3 fogColor;\n';
-				pfs += 'uniform float fogDensity;\n';
+						/* Premultiply alpha
+						material.map ? 'vec4 mapColor = texture2D( tMap, vUv );' : null,
+						material.map ? 'mapColor.xyz *= mapColor.w;' : null,
+						*/
 
-				fs += 'const float LOG2 = 1.442695;\n';
-				fs += 'float z = fog * ( gl_FragCoord.z / gl_FragCoord.w );\n';
-				fs += 'float fogFactor = exp2( - fogDensity * fogDensity * z * z * LOG2 );\n';
-				fs += 'gl_FragColor = mix( gl_FragColor, vec4( fogColor, 1.0 ), 1.0 - clamp( fogFactor, 0.0, 1.0 ) );\n',
+						material.map ? 'gl_FragColor *= texture2D( tMap, vUv );' : null,
 
-				identifiers.push( 'fog', 'fogColor', 'fogDensity' );
+						material.fog ? 'const float LOG2 = 1.442695;' : null,
+						material.fog ? 'float z = fog * ( gl_FragCoord.z / gl_FragCoord.w );' : null,
+						material.fog ? 'float fogFactor = exp2( - fogDensity * fogDensity * z * z * LOG2 );' : null,
+						material.fog ? 'gl_FragColor = mix( gl_FragColor, vec4( fogColor, 1.0 ), 1.0 - clamp( fogFactor, 0.0, 1.0 ) );' : null,
+					'}'
+				].filter( removeNull ).join( '\n' );
 
-				vs += '}';
-				fs += '}';
+				identifiers.push( 'mColor', 'mOpacity' );
+				material.map ? identifiers.push( 'tMap' ) : null;
+				material.fog ? identifiers.push( 'fog', 'fogColor', 'fogDensity' ) : null;
 
 
 			} else if ( material instanceof THREE.MeshNormalMaterial ) {
 
-				vs += 'void main() {\n';
-				fs += 'void main() {\n';
+				vs = [
+					'varying vec3 vNormal;',
 
-				pvs += 'varying vec3 vNormal;\n';
-				vs += 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n';
-				vs += 'vNormal = normalize( normalMatrix * normal );\n';
+					'void main() {',
+						'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
+						'vNormal = normalize( normalMatrix * normal );',
+					'}'
+				].join( '\n' );
 
-				pfs += 'uniform float mOpacity;\n';
-				pfs += 'varying vec3 vNormal;\n';
-				fs += 'gl_FragColor = vec4( ( normalize( vNormal ) + 1.0 ) * 0.5, mOpacity );\n';
+				fs = [
+					'uniform float mOpacity;',
+					'varying vec3 vNormal;',
 
-				identifiers.push( 'mOpacity' );
+					'void main() {',
+						'gl_FragColor = vec4( ( normalize( vNormal ) + 1.0 ) * 0.5, mOpacity );',
+					'}'
+				].join( '\n' );
 
-				vs += '}';
-				fs += '}';
+				identifiers.push( 'mOpacity' );
 
 			} else if ( material instanceof THREE.MeshDepthMaterial ) {
 
-				vs += 'void main() {\n';
-				fs += 'void main() {\n';
+				vs = [
+					'void main() {',
+						'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
+					'}'
 
-				vs += 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n';
+				].join( '\n' );
 
-				pfs += 'uniform float mOpacity;\n';
-				pfs += 'uniform float m2Near;\n';
-				pfs += 'uniform float mFarPlusNear;\n';
-				pfs += 'uniform float mFarMinusNear;\n';
-				fs += 'float w = 1.0 - ( m2Near / ( mFarPlusNear - gl_FragCoord.z * mFarMinusNear ) );\n';
-				fs += 'gl_FragColor = vec4( w, w, w, mOpacity );\n';
+				fs = [
+					'uniform float m2Near;',
+					'uniform float mFarPlusNear;',
+					'uniform float mFarMinusNear;',
+					'uniform float mOpacity;',
 
-				identifiers.push( 'm2Near', 'mFarPlusNear', 'mFarMinusNear', 'mOpacity' );
+					'void main() {',
+						'float w = 1.0 - ( m2Near / ( mFarPlusNear - gl_FragCoord.z * mFarMinusNear ) );',
+						'gl_FragColor = vec4( w, w, w, mOpacity );',
+					'}'
+				].join( '\n' );
 
-				vs += '}';
-				fs += '}';
+				identifiers.push( 'm2Near', 'mFarPlusNear', 'mFarMinusNear', 'mOpacity' );
 
 			} else if ( material instanceof THREE.MeshShaderMaterial ) {
 
@@ -579,7 +578,7 @@ THREE.WebGLRenderer2 = function ( antialias ) {
 
 			}
 
-			material.__webglProgram = compileProgram( pvs + vs, pfs + fs );
+			material.__webglProgram = compileProgram( vs, fs );
 
 			cacheUniformLocations( material.__webglProgram, identifiers );
 			cacheAttributeLocations( material.__webglProgram, [ "position", "normal", "uv"/*, "tangent"*/ ] );
@@ -650,8 +649,8 @@ THREE.WebGLRenderer2 = function ( antialias ) {
 				alert( "Could not initialise shaders.\n" +
 				"VALIDATE_STATUS: " + _gl.getProgramParameter( program, _gl.VALIDATE_STATUS ) + "\n" +
 				"ERROR: " + _gl.getError() + "\n\n" +
-				"Vertex Shader: \n" + prefix_vertex + vertex_shader + "\n\n" +
-				"Fragment Shader: \n" + prefix_fragment + fragment_shader );
+				"- Vertex Shader -\n" + prefix_vertex + vertex_shader + "\n\n" +
+				"- Fragment Shader -\n" + prefix_fragment + fragment_shader );
 
 			}
 
@@ -747,6 +746,12 @@ THREE.WebGLRenderer2 = function ( antialias ) {
 
 		}
 
+		function removeNull( element ) {
+
+			return element !== null;
+
+		}
+
 
 	};