瀏覽代碼

Optimised Projector's getNext*InPool.

Mr.doob 13 年之前
父節點
當前提交
8dad156189
共有 2 個文件被更改,包括 95 次插入124 次删除
  1. 43 72
      src/core/Projector.js
  2. 52 52
      src/core/Vector4.js

+ 43 - 72
src/core/Projector.js

@@ -6,11 +6,12 @@
 
 
 THREE.Projector = function() {
 THREE.Projector = function() {
 
 
-	var _object, _objectCount, _objectPool = [],
-	_vertex, _vertexCount, _vertexPool = [],
-	_face, _face3Count, _face3Pool = [], _face4Count, _face4Pool = [],
-	_line, _lineCount, _linePool = [],
-	_particle, _particleCount, _particlePool = [],
+	var _object, _objectCount, _objectPool = [], _objectPoolLength = 0,
+	_vertex, _vertexCount, _vertexPool = [], _vertexPoolLength = 0,
+	_face, _face3Count, _face3Pool = [], _face3PoolLength = 0,
+	_face4Count, _face4Pool = [], _face4PoolLength = 0,
+	_line, _lineCount, _linePool = [], _linePoolLength = 0,
+	_particle, _particleCount, _particlePool = [], _particlePoolLength = 0,
 
 
 	_renderData = { objects: [], sprites: [], lights: [], elements: [] },
 	_renderData = { objects: [], sprites: [], lights: [], elements: [] },
 
 
@@ -322,7 +323,7 @@ THREE.Projector = function() {
 				_modelViewProjectionMatrix.multiply( _viewProjectionMatrix, modelMatrix );
 				_modelViewProjectionMatrix.multiply( _viewProjectionMatrix, modelMatrix );
 
 
 				vertices = object.geometry.vertices;
 				vertices = object.geometry.vertices;
-				
+
 				v1 = getNextVertexInPool();
 				v1 = getNextVertexInPool();
 				v1.positionScreen.copy( vertices[ 0 ] );
 				v1.positionScreen.copy( vertices[ 0 ] );
 				_modelViewProjectionMatrix.multiplyVector4( v1.positionScreen );
 				_modelViewProjectionMatrix.multiplyVector4( v1.positionScreen );
@@ -413,128 +414,98 @@ THREE.Projector = function() {
 
 
 	function getNextObjectInPool() {
 	function getNextObjectInPool() {
 
 
-		var object;
-
-		if ( _objectCount === _objectPool.length ) {
+		if ( _objectCount === _objectPoolLength ) {
 
 
-			object = new THREE.RenderableObject();
+			var object = new THREE.RenderableObject();
 			_objectPool.push( object );
 			_objectPool.push( object );
-
-		} else {
-
-			object =  _objectPool[ _objectCount ];
+			_objectPoolLength ++;
+			_objectCount ++;
+			return object;
 
 
 		}
 		}
 
 
-		_objectCount ++;
-
-		return object;
+		return _objectPool[ _objectCount ++ ];
 
 
 	}
 	}
 
 
 	function getNextVertexInPool() {
 	function getNextVertexInPool() {
 
 
-		var vertex;
+		if ( _vertexCount === _vertexPoolLength ) {
 
 
-		if ( _vertexCount === _vertexPool.length ) {
-
-			vertex = new THREE.RenderableVertex();
+			var vertex = new THREE.RenderableVertex();
 			_vertexPool.push( vertex );
 			_vertexPool.push( vertex );
-
-		} else {
-
-			vertex =  _vertexPool[ _vertexCount ];
+			_vertexPoolLength ++;
+			_vertexCount ++;
+			return vertex;
 
 
 		}
 		}
 
 
-		_vertexCount ++;
-
-		return vertex;
+		return _vertexPool[ _vertexCount ++ ];
 
 
 	}
 	}
 
 
 	function getNextFace3InPool() {
 	function getNextFace3InPool() {
 
 
-		var face;
-
-		if ( _face3Count === _face3Pool.length ) {
+		if ( _face3Count === _face3PoolLength ) {
 
 
-			face = new THREE.RenderableFace3();
+			var face = new THREE.RenderableFace3();
 			_face3Pool.push( face );
 			_face3Pool.push( face );
-
-		} else {
-
-			face =  _face3Pool[ _face3Count ];
+			_face3PoolLength ++;
+			_face3Count ++;
+			return face;
 
 
 		}
 		}
 
 
-		_face3Count ++;
-
-		return face;
+		return _face3Pool[ _face3Count ++ ];
 
 
 
 
 	}
 	}
 
 
 	function getNextFace4InPool() {
 	function getNextFace4InPool() {
 
 
-		var face;
+		if ( _face4Count === _face4PoolLength ) {
 
 
-		if ( _face4Count === _face4Pool.length ) {
-
-			face = new THREE.RenderableFace4();
+			var face = new THREE.RenderableFace4();
 			_face4Pool.push( face );
 			_face4Pool.push( face );
-
-		} else {
-
-			face =  _face4Pool[ _face4Count ];
+			_face4PoolLength ++;
+			_face4Count ++;
+			return face;
 
 
 		}
 		}
 
 
-		_face4Count ++;
-
-		return face;
+		return _face4Pool[ _face4Count ++ ];
 
 
 	}
 	}
 
 
 	function getNextLineInPool() {
 	function getNextLineInPool() {
 
 
-		var line;
-
-		if ( _lineCount === _linePool.length ) {
+		if ( _lineCount === _linePoolLength ) {
 
 
-			line = new THREE.RenderableLine();
+			var line = new THREE.RenderableLine();
 			_linePool.push( line );
 			_linePool.push( line );
-
-		} else {
-
-			line =  _linePool[ _lineCount ];
+			_linePoolLength ++;
+			_lineCount ++
+			return line;
 
 
 		}
 		}
 
 
-		_lineCount ++;
-
-		return line;
+		return _linePool[ _lineCount ++ ];
 
 
 	}
 	}
 
 
 	function getNextParticleInPool() {
 	function getNextParticleInPool() {
 
 
-		var particle;
+		if ( _particleCount === _particlePoolLength ) {
 
 
-		if ( _particleCount === _particlePool.length ) {
-
-			particle = new THREE.RenderableParticle();
+			var particle = new THREE.RenderableParticle();
 			_particlePool.push( particle );
 			_particlePool.push( particle );
-
-		} else {
-
-			particle =  _particlePool[ _particleCount ];
+			_particlePoolLength ++;
+			_particleCount ++
+			return particle;
 
 
 		}
 		}
 
 
-		_particleCount ++;
-
-		return particle;
+		return _particlePool[ _particleCount ++ ];
 
 
 	}
 	}
 
 

+ 52 - 52
src/core/Vector4.js

@@ -142,7 +142,7 @@ THREE.Vector4.prototype = {
 		return Math.sqrt( this.lengthSq() );
 		return Math.sqrt( this.lengthSq() );
 
 
 	},
 	},
-	
+
 	lengthManhattan: function () {
 	lengthManhattan: function () {
 
 
 		return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
 		return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
@@ -181,27 +181,27 @@ THREE.Vector4.prototype = {
 	setAxisAngleFromQuaternion: function ( q ) {
 	setAxisAngleFromQuaternion: function ( q ) {
 
 
 		// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
 		// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
-		
+
 		// q is assumed to be normalized
 		// q is assumed to be normalized
-		
+
 		this.w = 2 * Math.acos( q.w );
 		this.w = 2 * Math.acos( q.w );
-		
+
 		var s = Math.sqrt( 1 - q.w * q.w );
 		var s = Math.sqrt( 1 - q.w * q.w );
-		
+
 		if ( s < 0.0001 ) {
 		if ( s < 0.0001 ) {
-		
+
 			 this.x = 1;
 			 this.x = 1;
 			 this.y = 0;
 			 this.y = 0;
 			 this.z = 0;
 			 this.z = 0;
-		 
+
 		} else {
 		} else {
-		
+
 			 this.x = q.x / s;
 			 this.x = q.x / s;
 			 this.y = q.y / s;
 			 this.y = q.y / s;
 			 this.z = q.z / s;
 			 this.z = q.z / s;
-		 
+
 		}
 		}
-	
+
 		return this;
 		return this;
 
 
 	},
 	},
@@ -209,15 +209,15 @@ THREE.Vector4.prototype = {
 	setAxisAngleFromRotationMatrix: function ( m ) {
 	setAxisAngleFromRotationMatrix: function ( m ) {
 
 
 		// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
 		// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
-		
+
 		// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
 		// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
-		
+
 		var angle, x, y, z,		// variables for result
 		var angle, x, y, z,		// variables for result
 			epsilon = 0.01,		// margin to allow for rounding errors
 			epsilon = 0.01,		// margin to allow for rounding errors
 			epsilon2 = 0.1,		// margin to distinguish between 0 and 180 degrees
 			epsilon2 = 0.1,		// margin to distinguish between 0 and 180 degrees
-		
+
 			te = m.elements,
 			te = m.elements,
-			
+
 			m11 = te[0], m12 = te[4], m13 = te[8],
 			m11 = te[0], m12 = te[4], m13 = te[8],
 			m21 = te[1], m22 = te[5], m23 = te[9],
 			m21 = te[1], m22 = te[5], m23 = te[9],
 			m31 = te[2], m32 = te[6], m33 = te[10];
 			m31 = te[2], m32 = te[6], m33 = te[10];
@@ -225,107 +225,107 @@ THREE.Vector4.prototype = {
 		if ( ( Math.abs( m12 - m21 ) < epsilon )
 		if ( ( Math.abs( m12 - m21 ) < epsilon )
 		  && ( Math.abs( m13 - m31 ) < epsilon )
 		  && ( Math.abs( m13 - m31 ) < epsilon )
 		  && ( Math.abs( m23 - m32 ) < epsilon ) ) {
 		  && ( Math.abs( m23 - m32 ) < epsilon ) ) {
-			
+
 			// singularity found
 			// singularity found
 			// first check for identity matrix which must have +1 for all terms
 			// first check for identity matrix which must have +1 for all terms
 			// in leading diagonal and zero in other terms
 			// in leading diagonal and zero in other terms
-			
+
 			if ( ( Math.abs( m12 + m21 ) < epsilon2 )
 			if ( ( Math.abs( m12 + m21 ) < epsilon2 )
 			  && ( Math.abs( m13 + m31 ) < epsilon2 )
 			  && ( Math.abs( m13 + m31 ) < epsilon2 )
 			  && ( Math.abs( m23 + m32 ) < epsilon2 )
 			  && ( Math.abs( m23 + m32 ) < epsilon2 )
 			  && ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
 			  && ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
-				
+
 				// this singularity is identity matrix so angle = 0
 				// this singularity is identity matrix so angle = 0
-				
+
 				this.set( 1, 0, 0, 0 );
 				this.set( 1, 0, 0, 0 );
-				
+
 				return this; // zero angle, arbitrary axis
 				return this; // zero angle, arbitrary axis
-				
+
 			}
 			}
-			
+
 			// otherwise this singularity is angle = 180
 			// otherwise this singularity is angle = 180
-			
+
 			angle = Math.PI;
 			angle = Math.PI;
-			
+
 			var xx = ( m11 + 1 ) / 2;
 			var xx = ( m11 + 1 ) / 2;
 			var yy = ( m22 + 1 ) / 2;
 			var yy = ( m22 + 1 ) / 2;
 			var zz = ( m33 + 1 ) / 2;
 			var zz = ( m33 + 1 ) / 2;
 			var xy = ( m12 + m21 ) / 4;
 			var xy = ( m12 + m21 ) / 4;
 			var xz = ( m13 + m31 ) / 4;
 			var xz = ( m13 + m31 ) / 4;
 			var yz = ( m23 + m32 ) / 4;
 			var yz = ( m23 + m32 ) / 4;
-			
+
 			if ( ( xx > yy ) && ( xx > zz ) ) { // m11 is the largest diagonal term
 			if ( ( xx > yy ) && ( xx > zz ) ) { // m11 is the largest diagonal term
-			
+
 				if ( xx < epsilon ) {
 				if ( xx < epsilon ) {
-				
+
 					x = 0;
 					x = 0;
 					y = 0.707106781;
 					y = 0.707106781;
 					z = 0.707106781;
 					z = 0.707106781;
-					
+
 				} else {
 				} else {
-				
+
 					x = Math.sqrt( xx );
 					x = Math.sqrt( xx );
 					y = xy / x;
 					y = xy / x;
 					z = xz / x;
 					z = xz / x;
-					
+
 				}
 				}
-				
+
 			} else if ( yy > zz ) { // m22 is the largest diagonal term
 			} else if ( yy > zz ) { // m22 is the largest diagonal term
-			
+
 				if ( yy < epsilon ) {
 				if ( yy < epsilon ) {
-				
+
 					x = 0.707106781;
 					x = 0.707106781;
 					y = 0;
 					y = 0;
 					z = 0.707106781;
 					z = 0.707106781;
-					
+
 				} else {
 				} else {
-				
+
 					y = Math.sqrt( yy );
 					y = Math.sqrt( yy );
 					x = xy / y;
 					x = xy / y;
 					z = yz / y;
 					z = yz / y;
-					
-				}	
-				
+
+				}
+
 			} else { // m33 is the largest diagonal term so base result on this
 			} else { // m33 is the largest diagonal term so base result on this
-			
+
 				if ( zz < epsilon ) {
 				if ( zz < epsilon ) {
-				
+
 					x = 0.707106781;
 					x = 0.707106781;
 					y = 0.707106781;
 					y = 0.707106781;
 					z = 0;
 					z = 0;
-					
+
 				} else {
 				} else {
-				
+
 					z = Math.sqrt( zz );
 					z = Math.sqrt( zz );
 					x = xz / z;
 					x = xz / z;
 					y = yz / z;
 					y = yz / z;
-					
+
 				}
 				}
-				
+
 			}
 			}
-			
+
 			this.set( x, y, z, angle );
 			this.set( x, y, z, angle );
-			
+
 			return this; // return 180 deg rotation
 			return this; // return 180 deg rotation
-	
+
 		}
 		}
-		
+
 		// as we have reached here there are no singularities so we can handle normally
 		// as we have reached here there are no singularities so we can handle normally
-		
+
 		var s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 )
 		var s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 )
 						 + ( m13 - m31 ) * ( m13 - m31 )
 						 + ( m13 - m31 ) * ( m13 - m31 )
 						 + ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
 						 + ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
-			
+
 		if ( Math.abs( s ) < 0.001 ) s = 1; 
 		if ( Math.abs( s ) < 0.001 ) s = 1; 
-		
+
 		// prevent divide by zero, should not happen if matrix is orthogonal and should be
 		// prevent divide by zero, should not happen if matrix is orthogonal and should be
 		// caught by singularity test above, but I've left it in just in case
 		// caught by singularity test above, but I've left it in just in case
-		
+
 		this.x = ( m32 - m23 ) / s;
 		this.x = ( m32 - m23 ) / s;
 		this.y = ( m13 - m31 ) / s;
 		this.y = ( m13 - m31 ) / s;
 		this.z = ( m21 - m12 ) / s;
 		this.z = ( m21 - m12 ) / s;
 		this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );
 		this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );
-	
+
 		return this;
 		return this;
 
 
 	}
 	}