Browse Source

parse arrays of struct uniforms, code cleanup.

Ben Houston 9 years ago
parent
commit
74d3aa3f12

+ 2 - 2
src/renderers/shaders/ShaderChunk/light_pars.glsl

@@ -26,13 +26,13 @@
 	//uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];
 	//uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];
 
-	struct HemisphericLight {
+	struct HemisphereLight {
 	  vec3 skyColor;
 	  vec3 groundColor;
 	  vec3 direction;
 	};
 
-	uniform HemisphericLight hemisphericLights[ MAX_HEMI_LIGHTS ];
+	uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];
 
 #endif
 

+ 112 - 0
src/renderers/shaders/ShaderChunk/lights_lambert_pars_vertex.glsl

@@ -1 +1,113 @@
 uniform vec3 ambientLightColor;
+
+
+#if MAX_DIR_LIGHTS > 0
+
+	//uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];
+	//uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];
+
+	struct DirectionalLight {
+	  vec3 color;
+	  vec3 direction;
+	};
+
+	uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];
+
+	void getDirLight( const in DirectionalLight directionalLight, out vec3 lightDir, out vec3 lightColor ) { 
+	
+		lightDir = directionalLight.direction; 
+		lightColor = directionalLight.color;
+
+	}
+
+#endif
+
+#if MAX_HEMI_LIGHTS > 0
+
+	//uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];
+	//uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];
+	//uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];
+
+	struct HemisphereLight {
+	  vec3 skyColor;
+	  vec3 groundColor;
+	  vec3 direction;
+	};
+
+	uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];
+
+#endif
+
+#if MAX_POINT_LIGHTS > 0
+
+	//uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];
+	//uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];
+	//uniform float pointLightDistance[ MAX_POINT_LIGHTS ];
+	//uniform float pointLightDecay[ MAX_POINT_LIGHTS ];
+
+	struct PointLight {
+	  vec3 color;
+	  vec3 position;
+	  float decay;
+	  float distance;
+	};
+
+	uniform PointLight pointLights[ MAX_POINT_LIGHTS ];
+
+	void getPointLight( const in PointLight pointLight, out vec3 lightDir, out vec3 lightColor ) { 
+	
+		vec3 lightPosition = pointLight.position; 
+	
+		vec3 lVector = lightPosition + vViewPosition.xyz; 
+		lightDir = normalize( lVector ); 
+	
+		lightColor = pointLight.color; 
+		lightColor *= calcLightAttenuation( length( lVector ), pointLight.distance, pointLight.decay ); 
+	
+	}
+
+#endif
+
+#if MAX_SPOT_LIGHTS > 0
+
+	//uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];
+	//uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];
+	//uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];
+	//uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];
+	//uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];
+	//uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];
+	//uniform float spotLightDecay[ MAX_SPOT_LIGHTS ];
+
+	struct SpotLight {
+	  vec3 color;
+	  vec3 position;
+	  vec3 direction;
+	  float angleCos;
+	  float exponent;
+	  float distance;
+	  float decay;
+	};
+
+	uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];
+
+	void getSpotLight( const in SpotLight spotLight, out vec3 lightDir, out vec3 lightColor ) {
+	
+		vec3 lightPosition = spotLight.position;
+	
+		vec3 lVector = lightPosition + vViewPosition.xyz;
+		lightDir = normalize( lVector );
+	
+		float spotEffect = dot( spotLight.direction, lightDir );
+		spotEffect = saturate( pow( saturate( spotEffect ), spotLight.exponent ) );
+	
+		lightColor = spotLight.color;
+		lightColor *= ( spotEffect * calcLightAttenuation( length( lVector ), spotLight.distance, spotLight.decay ) );
+
+	}
+
+#endif
+
+
+
+
+

+ 4 - 4
src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl

@@ -2,7 +2,7 @@ vec3 viewDir = normalize( vViewPosition );
 
 vec3 totalReflectedLight = vec3( 0.0 );
 
-var diffuse = diffuseColor.rgb;
+vec3 diffuse = diffuseColor.rgb;
 
 #ifdef METAL
 
@@ -15,7 +15,7 @@ var diffuse = diffuseColor.rgb;
 	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
 
 		vec3 lightDir, lightIntensity;
-		getSpotLight( i, lightDir, lightIntensity );
+		getPointLight( pointLights[i], lightDir, lightIntensity );
 
 		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
 
@@ -40,7 +40,7 @@ var diffuse = diffuseColor.rgb;
 	for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {
 
 		vec3 lightDir, lightIntensity;
-		getSpotLight( i, lightDir, lightIntensity );
+		getSpotLight( spotLights[i], lightDir, lightIntensity );
 
 		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
 
@@ -65,7 +65,7 @@ var diffuse = diffuseColor.rgb;
 	for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {
 
 		vec3 lightDir, lightIntensity;
-		getDirLight( i, lightDir, lightIntensity );
+		getDirLight( directionalLights[i], lightDir, lightIntensity );
 
 		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
 

+ 112 - 0
src/renderers/shaders/ShaderChunk/lights_phong_pars_fragment.glsl

@@ -13,3 +13,115 @@ varying vec3 vViewPosition;
 	varying vec3 vNormal;
 
 #endif
+
+
+#if MAX_DIR_LIGHTS > 0
+
+	//uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];
+	//uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];
+
+	struct DirectionalLight {
+	  vec3 color;
+	  vec3 direction;
+	};
+
+	uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];
+
+	void getDirLight( const in DirectionalLight directionalLight, out vec3 lightDir, out vec3 lightColor ) { 
+	
+		lightDir = directionalLight.direction; 
+		lightColor = directionalLight.color;
+
+	}
+
+#endif
+
+#if MAX_HEMI_LIGHTS > 0
+
+	//uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];
+	//uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];
+	//uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];
+
+	struct HemisphereLight {
+	  vec3 skyColor;
+	  vec3 groundColor;
+	  vec3 direction;
+	};
+
+	uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];
+
+#endif
+
+#if MAX_POINT_LIGHTS > 0
+
+	//uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];
+	//uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];
+	//uniform float pointLightDistance[ MAX_POINT_LIGHTS ];
+	//uniform float pointLightDecay[ MAX_POINT_LIGHTS ];
+
+	struct PointLight {
+	  vec3 color;
+	  vec3 position;
+	  float decay;
+	  float distance;
+	};
+
+	uniform PointLight pointLights[ MAX_POINT_LIGHTS ];
+
+	void getPointLight( const in PointLight pointLight, out vec3 lightDir, out vec3 lightColor ) { 
+	
+		vec3 lightPosition = pointLight.position; 
+	
+		vec3 lVector = lightPosition + vViewPosition.xyz; 
+		lightDir = normalize( lVector ); 
+	
+		lightColor = pointLight.color; 
+		lightColor *= calcLightAttenuation( length( lVector ), pointLight.distance, pointLight.decay ); 
+	
+	}
+
+#endif
+
+#if MAX_SPOT_LIGHTS > 0
+
+	//uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];
+	//uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];
+	//uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];
+	//uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];
+	//uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];
+	//uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];
+	//uniform float spotLightDecay[ MAX_SPOT_LIGHTS ];
+
+	struct SpotLight {
+	  vec3 color;
+	  vec3 position;
+	  vec3 direction;
+	  float angleCos;
+	  float exponent;
+	  float distance;
+	  float decay;
+	};
+
+	uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];
+
+	void getSpotLight( const in SpotLight spotLight, out vec3 lightDir, out vec3 lightColor ) {
+	
+		vec3 lightPosition = spotLight.position;
+	
+		vec3 lVector = lightPosition + vViewPosition.xyz;
+		lightDir = normalize( lVector );
+	
+		float spotEffect = dot( spotLight.direction, lightDir );
+		spotEffect = saturate( pow( saturate( spotEffect ), spotLight.exponent ) );
+	
+		lightColor = spotLight.color;
+		lightColor *= ( spotEffect * calcLightAttenuation( length( lVector ), spotLight.distance, spotLight.decay ) );
+
+	}
+
+#endif
+
+
+
+
+

+ 2 - 0
src/renderers/shaders/ShaderLib.js

@@ -143,6 +143,7 @@ THREE.ShaderLib = {
 			THREE.ShaderChunk[ "uv2_pars_vertex" ],
 			THREE.ShaderChunk[ "envmap_pars_vertex" ],
 			THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
+			THREE.ShaderChunk[ "lights_pars" ],
 			THREE.ShaderChunk[ "color_pars_vertex" ],
 			THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
 			THREE.ShaderChunk[ "skinning_pars_vertex" ],
@@ -347,6 +348,7 @@ THREE.ShaderLib = {
 			THREE.ShaderChunk[ "envmap_pars_fragment" ],
 			THREE.ShaderChunk[ "fog_pars_fragment" ],
 			THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
+			THREE.ShaderChunk[ "lights_pars" ],
 			THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
 			THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
 			THREE.ShaderChunk[ "normalmap_pars_fragment" ],

+ 31 - 4
src/renderers/webgl/WebGLProgram.js

@@ -25,6 +25,9 @@ THREE.WebGLProgram = ( function () {
 		var uniforms = {};
 
 		var n = gl.getProgramParameter( program, gl.ACTIVE_UNIFORMS );
+	
+		var arrayStructRe = /^([\w\d_]+)\[(\d+)\]\.([\w\d_]+)$/; 
+		var arrayRe = /^([\w\d_]+)\[0\]$/; 
 
 		for ( var i = 0; i < n; i ++ ) {
 
@@ -32,19 +35,43 @@ THREE.WebGLProgram = ( function () {
 			var name = info.name;
 			var location = gl.getUniformLocation( program, name );
 
-			// console.log("THREE.WebGLProgram: ACTIVE UNIFORM:", name);
+			console.log("THREE.WebGLProgram: ACTIVE UNIFORM:", name);
 
-			var suffixPos = name.lastIndexOf( '[0]' );
-			if ( suffixPos !== - 1 && suffixPos === name.length - 3 ) {
+			var matches = arrayStructRe.exec(name);
+			if( matches ) {
 
-				uniforms[ name.substr( 0, suffixPos ) ] = location;
+				var arrayName = matches[1];
+				var arrayIndex = matches[2];
+				var arrayProperty = matches[3];
 
+				var uniformsArray = uniforms[ arrayName ];
+				if( ! uniformsArray ) {
+					uniformsArray = uniforms[ arrayName ] = [];
+				}
+				var uniformsArrayIndex = uniformsArray[ arrayIndex ];
+				if( ! uniformsArrayIndex ) {
+					uniformsArrayIndex = uniformsArray[ arrayIndex ] = {};
+				}
+				uniformsArrayIndex[ arrayProperty ] = location;
+
+				continue;
+			}
+
+			matches = arrayRe.exec(name)
+			if( matches ) {
+
+				var arrayName = matches[1];
+
+				uniforms[ arrayName ] = location;
+
+				continue;
 			}
 
 			uniforms[ name ] = location;
 
 		}
 
+		console.log("uniforms", uniforms);
 		return uniforms;
 
 	}