Browse Source

WebGLRenderer: Moved loadUniform code out of loadUniformsGeneric.

Mr.doob 9 years ago
parent
commit
1d3f5f9339
1 changed files with 219 additions and 213 deletions
  1. 219 213
      src/renderers/WebGLRenderer.js

+ 219 - 213
src/renderers/WebGLRenderer.js

@@ -2219,119 +2219,153 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 	}
 
-	function loadUniformsGeneric ( uniforms ) {
+	function loadUniform( uniform, type, location, value ) {
 
 		var texture, textureUnit;
 
-		for ( var j = 0, jl = uniforms.length; j < jl; j ++ ) {
+		if ( type === '1i' ) {
 
-			var uniform = uniforms[ j ][ 0 ];
+			_gl.uniform1i( location, value );
 
-			// needsUpdate property is not added to all uniforms.
-			if ( uniform.needsUpdate === false ) continue;
+		} else if ( type === '1f' ) {
 
-			var type = uniform.type;
-			var value = uniform.value;
-			var location = uniforms[ j ][ 1 ];
+			_gl.uniform1f( location, value );
+
+		} else if ( type === '2f' ) {
 
-			if ( type === '1i' ) {
+			_gl.uniform2f( location, value[ 0 ], value[ 1 ] );
 
-				_gl.uniform1i( location, value );
+		} else if ( type === '3f' ) {
 
-			} else if ( type === '1f' ) {
+			_gl.uniform3f( location, value[ 0 ], value[ 1 ], value[ 2 ] );
 
-				_gl.uniform1f( location, value );
+		} else if ( type === '4f' ) {
 
-			} else if ( type === '2f' ) {
+			_gl.uniform4f( location, value[ 0 ], value[ 1 ], value[ 2 ], value[ 3 ] );
 
-				_gl.uniform2f( location, value[ 0 ], value[ 1 ] );
+		} else if ( type === '1iv' ) {
 
-			} else if ( type === '3f' ) {
+			_gl.uniform1iv( location, value );
 
-				_gl.uniform3f( location, value[ 0 ], value[ 1 ], value[ 2 ] );
+		} else if ( type === '3iv' ) {
 
-			} else if ( type === '4f' ) {
+			_gl.uniform3iv( location, value );
 
-				_gl.uniform4f( location, value[ 0 ], value[ 1 ], value[ 2 ], value[ 3 ] );
+		} else if ( type === '1fv' ) {
 
-			} else if ( type === '1iv' ) {
+			_gl.uniform1fv( location, value );
 
-				_gl.uniform1iv( location, value );
+		} else if ( type === '2fv' ) {
 
-			} else if ( type === '3iv' ) {
+			_gl.uniform2fv( location, value );
 
-				_gl.uniform3iv( location, value );
+		} else if ( type === '3fv' ) {
 
-			} else if ( type === '1fv' ) {
+			_gl.uniform3fv( location, value );
 
-				_gl.uniform1fv( location, value );
+		} else if ( type === '4fv' ) {
 
-			} else if ( type === '2fv' ) {
+			_gl.uniform4fv( location, value );
 
-				_gl.uniform2fv( location, value );
+		} else if ( type === 'Matrix2fv' ) {
 
-			} else if ( type === '3fv' ) {
+			_gl.uniformMatrix2fv( location, false, value );
+
+		} else if ( type === 'Matrix3fv' ) {
+
+			_gl.uniformMatrix3fv( location, false, value );
+
+		} else if ( type === 'Matrix4fv' ) {
+
+			_gl.uniformMatrix4fv( location, false, value );
+
+		//
 
-				_gl.uniform3fv( location, value );
+		} else if ( type === 'i' ) {
 
-			} else if ( type === '4fv' ) {
+			// single integer
+			_gl.uniform1i( location, value );
 
-				_gl.uniform4fv( location, value );
+		} else if ( type === 'f' ) {
 
-			} else if ( type === 'Matrix2fv' ) {
+			// single float
+			_gl.uniform1f( location, value );
 
-				_gl.uniformMatrix2fv( location, false, value );
+		} else if ( type === 'v2' ) {
 
-			} else if ( type === 'Matrix3fv' ) {
+			// single THREE.Vector2
+			_gl.uniform2f( location, value.x, value.y );
 
-				_gl.uniformMatrix3fv( location, false, value );
+		} else if ( type === 'v3' ) {
 
-			} else if ( type === 'Matrix4fv' ) {
+			// single THREE.Vector3
+			_gl.uniform3f( location, value.x, value.y, value.z );
 
-				_gl.uniformMatrix4fv( location, false, value );
+		} else if ( type === 'v4' ) {
 
-			//
+			// single THREE.Vector4
+			_gl.uniform4f( location, value.x, value.y, value.z, value.w );
 
-			} else if ( type === 'i' ) {
+		} else if ( type === 'c' ) {
 
-				// single integer
-				_gl.uniform1i( location, value );
+			// single THREE.Color
+			_gl.uniform3f( location, value.r, value.g, value.b );
 
-			} else if ( type === 'f' ) {
+		/*
+		} else if ( type === 's' ) {
 
-				// single float
-				_gl.uniform1f( location, value );
+			// TODO: Optimize this
 
-			} else if ( type === 'v2' ) {
+			for ( var propertyName in uniform.properties ) {
 
-				// single THREE.Vector2
-				_gl.uniform2f( location, value.x, value.y );
+				var property = uniform.properties[ propertyName ];
+				var locationProperty =  location[ propertyName ];
+				var valueProperty = value[ propertyName ];
 
-			} else if ( type === 'v3' ) {
+				type = property.type;
 
-				// single THREE.Vector3
-				_gl.uniform3f( location, value.x, value.y, value.z );
+				if ( type === 'i' ) {
 
-			} else if ( type === 'v4' ) {
+					_gl.uniform1i( locationProperty, valueProperty );
 
-				// single THREE.Vector4
-				_gl.uniform4f( location, value.x, value.y, value.z, value.w );
+				} else if ( type === 'f' ) {
 
-			} else if ( type === 'c' ) {
+					_gl.uniform1f( locationProperty, valueProperty );
+
+				} else if ( type === 'v2' ) {
+
+					_gl.uniform2f( locationProperty, valueProperty.x, valueProperty.y );
+
+				} else if ( type === 'v3' ) {
+
+					_gl.uniform3f( locationProperty, valueProperty.x, valueProperty.y, valueProperty.z );
+
+				} else if ( type === 'v4' ) {
+
+					_gl.uniform4f( locationProperty, valueProperty.x, valueProperty.y, valueProperty.z, valueProperty.w );
+
+				} else if ( type === 'c' ) {
+
+					_gl.uniform3f( locationProperty, valueProperty.r, valueProperty.g, valueProperty.b );
+
+				}
+
+			}
 
-				// single THREE.Color
-				_gl.uniform3f( location, value.r, value.g, value.b );
 
-			/*
-			} else if ( type === 's' ) {
+		*/
 
-				// TODO: Optimize this
+		} else if ( type === 'sa' ) {
+
+			// TODO: Optimize this
+
+			for ( var i = 0; i < value.length; i ++ ) {
 
 				for ( var propertyName in uniform.properties ) {
 
 					var property = uniform.properties[ propertyName ];
-					var locationProperty =  location[ propertyName ];
-					var valueProperty = value[ propertyName ];
+					var locationProperty =  location[ i ][ propertyName ];
+					var valueProperty = value[ i ][ propertyName ];
 
 					type = property.type;
 
@@ -2359,277 +2393,249 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 						_gl.uniform3f( locationProperty, valueProperty.r, valueProperty.g, valueProperty.b );
 
+					} else if ( type === 'm4' ) {
+
+						_gl.uniformMatrix4fv( locationProperty, false, valueProperty.elements );
+
 					}
 
 				}
 
+			}
 
-			*/
-
-			} else if ( type === 'sa' ) {
+		} else if ( type === 'iv1' ) {
 
-				// TODO: Optimize this
+			// flat array of integers (JS or typed array)
+			_gl.uniform1iv( location, value );
 
-				for ( var i = 0; i < value.length; i ++ ) {
+		} else if ( type === 'iv' ) {
 
-					for ( var propertyName in uniform.properties ) {
+			// flat array of integers with 3 x N size (JS or typed array)
+			_gl.uniform3iv( location, value );
 
-						var property = uniform.properties[ propertyName ];
-						var locationProperty =  location[ i ][ propertyName ];
-						var valueProperty = value[ i ][ propertyName ];
+		} else if ( type === 'fv1' ) {
 
-						type = property.type;
+			// flat array of floats (JS or typed array)
+			_gl.uniform1fv( location, value );
 
-						if ( type === 'i' ) {
+		} else if ( type === 'fv' ) {
 
-							_gl.uniform1i( locationProperty, valueProperty );
+			// flat array of floats with 3 x N size (JS or typed array)
+			_gl.uniform3fv( location, value );
 
-						} else if ( type === 'f' ) {
+		} else if ( type === 'v2v' ) {
 
-							_gl.uniform1f( locationProperty, valueProperty );
+			// array of THREE.Vector2
 
-						} else if ( type === 'v2' ) {
+			if ( uniform._array === undefined ) {
 
-							_gl.uniform2f( locationProperty, valueProperty.x, valueProperty.y );
+				uniform._array = new Float32Array( 2 * value.length );
 
-						} else if ( type === 'v3' ) {
+			}
 
-							_gl.uniform3f( locationProperty, valueProperty.x, valueProperty.y, valueProperty.z );
+			for ( var i = 0, i2 = 0, il = value.length; i < il; i ++, i2 += 2 ) {
 
-						} else if ( type === 'v4' ) {
+				uniform._array[ i2 + 0 ] = value[ i ].x;
+				uniform._array[ i2 + 1 ] = value[ i ].y;
 
-							_gl.uniform4f( locationProperty, valueProperty.x, valueProperty.y, valueProperty.z, valueProperty.w );
+			}
 
-						} else if ( type === 'c' ) {
+			_gl.uniform2fv( location, uniform._array );
 
-							_gl.uniform3f( locationProperty, valueProperty.r, valueProperty.g, valueProperty.b );
+		} else if ( type === 'v3v' ) {
 
-						} else if ( type === 'm4' ) {
+			// array of THREE.Vector3
 
-							_gl.uniformMatrix4fv( locationProperty, false, valueProperty.elements );
+			if ( uniform._array === undefined ) {
 
-						}
+				uniform._array = new Float32Array( 3 * value.length );
 
-					}
+			}
 
-				}
+			for ( var i = 0, i3 = 0, il = value.length; i < il; i ++, i3 += 3 ) {
 
-			} else if ( type === 'iv1' ) {
+				uniform._array[ i3 + 0 ] = value[ i ].x;
+				uniform._array[ i3 + 1 ] = value[ i ].y;
+				uniform._array[ i3 + 2 ] = value[ i ].z;
 
-				// flat array of integers (JS or typed array)
-				_gl.uniform1iv( location, value );
+			}
 
-			} else if ( type === 'iv' ) {
+			_gl.uniform3fv( location, uniform._array );
 
-				// flat array of integers with 3 x N size (JS or typed array)
-				_gl.uniform3iv( location, value );
+		} else if ( type === 'v4v' ) {
 
-			} else if ( type === 'fv1' ) {
+			// array of THREE.Vector4
 
-				// flat array of floats (JS or typed array)
-				_gl.uniform1fv( location, value );
+			if ( uniform._array === undefined ) {
 
-			} else if ( type === 'fv' ) {
+				uniform._array = new Float32Array( 4 * value.length );
 
-				// flat array of floats with 3 x N size (JS or typed array)
-				_gl.uniform3fv( location, value );
+			}
 
-			} else if ( type === 'v2v' ) {
+			for ( var i = 0, i4 = 0, il = value.length; i < il; i ++, i4 += 4 ) {
 
-				// array of THREE.Vector2
+				uniform._array[ i4 + 0 ] = value[ i ].x;
+				uniform._array[ i4 + 1 ] = value[ i ].y;
+				uniform._array[ i4 + 2 ] = value[ i ].z;
+				uniform._array[ i4 + 3 ] = value[ i ].w;
 
-				if ( uniform._array === undefined ) {
+			}
 
-					uniform._array = new Float32Array( 2 * value.length );
+			_gl.uniform4fv( location, uniform._array );
 
-				}
+		} else if ( type === 'm2' ) {
 
-				for ( var i = 0, i2 = 0, il = value.length; i < il; i ++, i2 += 2 ) {
+			// single THREE.Matrix2
+			_gl.uniformMatrix2fv( location, false, value.elements );
 
-					uniform._array[ i2 + 0 ] = value[ i ].x;
-					uniform._array[ i2 + 1 ] = value[ i ].y;
+		} else if ( type === 'm3' ) {
 
-				}
+			// single THREE.Matrix3
+			_gl.uniformMatrix3fv( location, false, value.elements );
 
-				_gl.uniform2fv( location, uniform._array );
+		} else if ( type === 'm3v' ) {
 
-			} else if ( type === 'v3v' ) {
+			// array of THREE.Matrix3
 
-				// array of THREE.Vector3
+			if ( uniform._array === undefined ) {
 
-				if ( uniform._array === undefined ) {
+				uniform._array = new Float32Array( 9 * value.length );
 
-					uniform._array = new Float32Array( 3 * value.length );
+			}
 
-				}
+			for ( var i = 0, il = value.length; i < il; i ++ ) {
 
-				for ( var i = 0, i3 = 0, il = value.length; i < il; i ++, i3 += 3 ) {
+				value[ i ].flattenToArrayOffset( uniform._array, i * 9 );
 
-					uniform._array[ i3 + 0 ] = value[ i ].x;
-					uniform._array[ i3 + 1 ] = value[ i ].y;
-					uniform._array[ i3 + 2 ] = value[ i ].z;
+			}
 
-				}
+			_gl.uniformMatrix3fv( location, false, uniform._array );
 
-				_gl.uniform3fv( location, uniform._array );
+		} else if ( type === 'm4' ) {
 
-			} else if ( type === 'v4v' ) {
+			// single THREE.Matrix4
+			_gl.uniformMatrix4fv( location, false, value.elements );
 
-				// array of THREE.Vector4
+		} else if ( type === 'm4v' ) {
 
-				if ( uniform._array === undefined ) {
+			// array of THREE.Matrix4
 
-					uniform._array = new Float32Array( 4 * value.length );
+			if ( uniform._array === undefined ) {
 
-				}
+				uniform._array = new Float32Array( 16 * value.length );
 
-				for ( var i = 0, i4 = 0, il = value.length; i < il; i ++, i4 += 4 ) {
+			}
 
-					uniform._array[ i4 + 0 ] = value[ i ].x;
-					uniform._array[ i4 + 1 ] = value[ i ].y;
-					uniform._array[ i4 + 2 ] = value[ i ].z;
-					uniform._array[ i4 + 3 ] = value[ i ].w;
+			for ( var i = 0, il = value.length; i < il; i ++ ) {
 
-				}
+				value[ i ].flattenToArrayOffset( uniform._array, i * 16 );
 
-				_gl.uniform4fv( location, uniform._array );
-
-			} else if ( type === 'm2' ) {
+			}
 
-				// single THREE.Matrix2
-				_gl.uniformMatrix2fv( location, false, value.elements );
+			_gl.uniformMatrix4fv( location, false, uniform._array );
 
-			} else if ( type === 'm3' ) {
+		} else if ( type === 't' ) {
 
-				// single THREE.Matrix3
-				_gl.uniformMatrix3fv( location, false, value.elements );
+			// single THREE.Texture (2d or cube)
 
-			} else if ( type === 'm3v' ) {
+			texture = value;
+			textureUnit = getTextureUnit();
 
-				// array of THREE.Matrix3
+			_gl.uniform1i( location, textureUnit );
 
-				if ( uniform._array === undefined ) {
+			if ( ! texture ) return;
 
-					uniform._array = new Float32Array( 9 * value.length );
+			if ( texture instanceof THREE.CubeTexture ||
+				 ( Array.isArray( texture.image ) && texture.image.length === 6 ) ) {
 
-				}
+				// CompressedTexture can have Array in image :/
 
-				for ( var i = 0, il = value.length; i < il; i ++ ) {
+				setCubeTexture( texture, textureUnit );
 
-					value[ i ].flattenToArrayOffset( uniform._array, i * 9 );
+			} else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
 
-				}
+				setCubeTextureDynamic( texture.texture, textureUnit );
 
-				_gl.uniformMatrix3fv( location, false, uniform._array );
+			} else if ( texture instanceof THREE.WebGLRenderTarget ) {
 
-			} else if ( type === 'm4' ) {
+				_this.setTexture( texture.texture, textureUnit );
 
-				// single THREE.Matrix4
-				_gl.uniformMatrix4fv( location, false, value.elements );
+			} else {
 
-			} else if ( type === 'm4v' ) {
+				_this.setTexture( texture, textureUnit );
 
-				// array of THREE.Matrix4
+			}
 
-				if ( uniform._array === undefined ) {
+		} else if ( type === 'tv' ) {
 
-					uniform._array = new Float32Array( 16 * value.length );
+			// array of THREE.Texture (2d or cube)
 
-				}
+			if ( uniform._array === undefined ) {
 
-				for ( var i = 0, il = value.length; i < il; i ++ ) {
+				uniform._array = [];
 
-					value[ i ].flattenToArrayOffset( uniform._array, i * 16 );
+			}
 
-				}
+			for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
 
-				_gl.uniformMatrix4fv( location, false, uniform._array );
+				uniform._array[ i ] = getTextureUnit();
 
-			} else if ( type === 't' ) {
+			}
 
-				// single THREE.Texture (2d or cube)
+			_gl.uniform1iv( location, uniform._array );
 
-				texture = value;
-				textureUnit = getTextureUnit();
+			for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
 
-				_gl.uniform1i( location, textureUnit );
+				texture = uniform.value[ i ];
+				textureUnit = uniform._array[ i ];
 
 				if ( ! texture ) continue;
 
 				if ( texture instanceof THREE.CubeTexture ||
-					 ( Array.isArray( texture.image ) && texture.image.length === 6 ) ) {
+					 ( texture.image instanceof Array && texture.image.length === 6 ) ) {
 
 					// CompressedTexture can have Array in image :/
 
 					setCubeTexture( texture, textureUnit );
 
-				} else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
-
-					setCubeTextureDynamic( texture.texture, textureUnit );
-
 				} else if ( texture instanceof THREE.WebGLRenderTarget ) {
 
 					_this.setTexture( texture.texture, textureUnit );
 
-				} else {
-
-					_this.setTexture( texture, textureUnit );
-
-				}
-
-			} else if ( type === 'tv' ) {
-
-				// array of THREE.Texture (2d or cube)
-
-				if ( uniform._array === undefined ) {
-
-					uniform._array = [];
+				} else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
 
-				}
+					setCubeTextureDynamic( texture.texture, textureUnit );
 
-				for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
+				} else {
 
-					uniform._array[ i ] = getTextureUnit();
+					_this.setTexture( texture, textureUnit );
 
 				}
 
-				_gl.uniform1iv( location, uniform._array );
-
-				for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
-
-					texture = uniform.value[ i ];
-					textureUnit = uniform._array[ i ];
-
-					if ( ! texture ) continue;
-
-					if ( texture instanceof THREE.CubeTexture ||
-						 ( texture.image instanceof Array && texture.image.length === 6 ) ) {
-
-						// CompressedTexture can have Array in image :/
-
-						setCubeTexture( texture, textureUnit );
-
-					} else if ( texture instanceof THREE.WebGLRenderTarget ) {
+			}
 
-						_this.setTexture( texture.texture, textureUnit );
+		} else {
 
-					} else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
+			console.warn( 'THREE.WebGLRenderer: Unknown uniform type: ' + type );
 
-						setCubeTextureDynamic( texture.texture, textureUnit );
+		}
 
-					} else {
+	}
 
-						_this.setTexture( texture, textureUnit );
+	function loadUniformsGeneric( uniforms ) {
 
-					}
+		for ( var i = 0, l = uniforms.length; i < l; i ++ ) {
 
-				}
+			var uniform = uniforms[ i ][ 0 ];
 
-			} else {
+			// needsUpdate property is not added to all uniforms.
+			if ( uniform.needsUpdate === false ) continue;
 
-				console.warn( 'THREE.WebGLRenderer: Unknown uniform type: ' + type );
+			var type = uniform.type;
+			var location = uniforms[ i ][ 1 ];
+			var value = uniform.value;
 
-			}
+			loadUniform( uniform, type, location, value );
 
 		}