Browse Source

add support for structs not in arrays in WebGLProgram.

Ben Houston 9 years ago
parent
commit
e889e6b980

+ 2 - 1
src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl

@@ -19,7 +19,8 @@ vec3 diffuse = diffuseColor.rgb;
 
 		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
 
-			vec3 halfDir = normalize( lightDir + viewDir );
+
+			vec3 halfDir = normalize( lightDir + viewDir ) * singleTestPointLight.distance;
 			float dotNL = saturate( dot( normal, lightDir ) );
 			float dotNH = saturate( dot( normal, halfDir ) );
 			float dotLH = saturate( dot( lightDir, halfDir ) );

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

@@ -25,6 +25,8 @@ varying vec3 vViewPosition;
 	  vec3 direction;
 	};
 
+	uniform DirectionalLight singleTestDirLight;
+
 	uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];
 
 	void getDirLight( const in DirectionalLight directionalLight, out vec3 lightDir, out vec3 lightColor ) { 
@@ -66,6 +68,9 @@ varying vec3 vViewPosition;
 	  float distance;
 	};
 
+
+	uniform PointLight singleTestPointLight;
+
 	uniform PointLight pointLights[ MAX_POINT_LIGHTS ];
 
 	void getPointLight( const in PointLight pointLight, out vec3 lightDir, out vec3 lightColor ) { 

+ 16 - 16
src/renderers/shaders/UniformsLib.js

@@ -80,22 +80,22 @@ THREE.UniformsLib = {
 		"directionalLightDirection" : { type: "fv", value: [] },
 		"directionalLightColor" : { type: "fv", value: [] },
 
-		"hemisphereLightDirection" : { type: "fv", value: [] },
-		"hemisphereLightSkyColor" : { type: "fv", value: [] },
-		"hemisphereLightGroundColor" : { type: "fv", value: [] },
-
-		"pointLightColor" : { type: "fv", value: [] },
-		"pointLightPosition" : { type: "fv", value: [] },
-		"pointLightDistance" : { type: "fv1", value: [] },
-		"pointLightDecay" : { type: "fv1", value: [] },
-
-		"spotLightColor" : { type: "fv", value: [] },
-		"spotLightPosition" : { type: "fv", value: [] },
-		"spotLightDirection" : { type: "fv", value: [] },
-		"spotLightDistance" : { type: "fv1", value: [] },
-		"spotLightAngleCos" : { type: "fv1", value: [] },
-		"spotLightExponent" : { type: "fv1", value: [] },
-		"spotLightDecay" : { type: "fv1", value: [] }
+		"hemisphereLightDirection" : { type: "fv", value: [], array: 'hemisphereLights', property: 'direction' },
+		"hemisphereLightSkyColor" : { type: "fv", value: [], array: 'hemisphereLights', property: 'skyColor' },
+		"hemisphereLightGroundColor" : { type: "fv", value: [], array: 'hemisphereLights', property: 'groundColor' },
+
+		"pointLightColor" : { type: "fv", value: [], array: 'pointLights', property: 'color' },
+		"pointLightPosition" : { type: "fv", value: [], array: 'pointLights', property: 'position' },
+		"pointLightDistance" : { type: "fv1", value: [], array: 'pointLights', property: 'distance' },
+		"pointLightDecay" : { type: "fv1", value: [], array: 'pointLights', property: 'decay' },
+
+		"spotLightColor" : { type: "fv", value: [], array: 'spotLights', property: 'color' },
+		"spotLightPosition" : { type: "fv", value: [], array: 'spotLights', property: 'position' },
+		"spotLightDirection" : { type: "fv", value: [], array: 'spotLights', property: 'direction' },
+		"spotLightDistance" : { type: "fv1", value: [], array: 'spotLights', property: 'distance' },
+		"spotLightAngleCos" : { type: "fv1", value: [], array: 'spotLights', property: 'angleCos' },
+		"spotLightExponent" : { type: "fv1", value: [], array: 'spotLights', property: 'exponent' },
+		"spotLightDecay" : { type: "fv1", value: [], array: 'spotLights', property: 'decay' }
 
 	},
 

+ 19 - 1
src/renderers/webgl/WebGLProgram.js

@@ -26,6 +26,9 @@ THREE.WebGLProgram = ( function () {
 
 		var n = gl.getProgramParameter( program, gl.ACTIVE_UNIFORMS );
 	
+
+		// TODO: Combine.
+		var structRe = /^([\w\d_]+)\.([\w\d_]+)$/; 
 		var arrayStructRe = /^([\w\d_]+)\[(\d+)\]\.([\w\d_]+)$/; 
 		var arrayRe = /^([\w\d_]+)\[0\]$/; 
 
@@ -37,7 +40,22 @@ THREE.WebGLProgram = ( function () {
 
 			console.log("THREE.WebGLProgram: ACTIVE UNIFORM:", name);
 
-			var matches = arrayStructRe.exec(name);
+			var matches = structRe.exec(name);
+			if( matches ) {
+
+				var structName = matches[1];
+				var structProperty = matches[2];
+
+				var uniformsStruct = uniforms[ structName ];
+				if( ! uniformsStruct ) {
+					uniformsStruct = uniforms[ structName ] = {};
+				}
+				uniformsStruct[ structProperty ] = location;
+
+				continue;
+			}
+
+			matches = arrayStructRe.exec(name);
 			if( matches ) {
 
 				var arrayName = matches[1];