浏览代码

WebGLRenderer: loadUniformsGeneric() back to if statements. Chrome seems to be continuously deoptimizing the function. Maybe helps with #8220.

Mr.doob 9 年之前
父节点
当前提交
7da46fd42d
共有 1 个文件被更改,包括 236 次插入263 次删除
  1. 236 263
      src/renderers/WebGLRenderer.js

+ 236 - 263
src/renderers/WebGLRenderer.js

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