Sfoglia il codice sorgente

Merge pull request #17093 from Mugen87/dev33

Math: Remove closures part I.
Mr.doob 6 anni fa
parent
commit
d78e365222
4 ha cambiato i file con 59 aggiunte e 67 eliminazioni
  1. 12 18
      src/math/Euler.js
  2. 17 15
      src/math/Line3.js
  3. 13 15
      src/math/Matrix3.js
  4. 17 19
      src/math/Sphere.js

+ 12 - 18
src/math/Euler.js

@@ -9,6 +9,8 @@ import { _Math } from './Math.js';
  * @author bhouston / http://clara.io
  */
 
+var _matrix, _quaternion;
+
 function Euler( x, y, z, order ) {
 
 	this._x = x || 0;
@@ -253,19 +255,15 @@ Object.assign( Euler.prototype, {
 
 	},
 
-	setFromQuaternion: function () {
-
-		var matrix = new Matrix4();
+	setFromQuaternion: function ( q, order, update ) {
 
-		return function setFromQuaternion( q, order, update ) {
+		if ( _matrix === undefined ) _matrix = new Matrix4();
 
-			matrix.makeRotationFromQuaternion( q );
+		_matrix.makeRotationFromQuaternion( q );
 
-			return this.setFromRotationMatrix( matrix, order, update );
+		return this.setFromRotationMatrix( _matrix, order, update );
 
-		};
-
-	}(),
+	},
 
 	setFromVector3: function ( v, order ) {
 
@@ -273,21 +271,17 @@ Object.assign( Euler.prototype, {
 
 	},
 
-	reorder: function () {
+	reorder: function ( newOrder ) {
 
 		// WARNING: this discards revolution information -bhouston
 
-		var q = new Quaternion();
+		if ( _quaternion === undefined ) _quaternion = new Quaternion();
 
-		return function reorder( newOrder ) {
+		_quaternion.setFromEuler( this );
 
-			q.setFromEuler( this );
+		return this.setFromQuaternion( _quaternion, newOrder );
 
-			return this.setFromQuaternion( q, newOrder );
-
-		};
-
-	}(),
+	},
 
 	equals: function ( euler ) {
 

+ 17 - 15
src/math/Line3.js

@@ -5,6 +5,8 @@ import { _Math } from './Math.js';
  * @author bhouston / http://clara.io
  */
 
+var _startP, _startEnd;
+
 function Line3( start, end ) {
 
 	this.start = ( start !== undefined ) ? start : new Vector3();
@@ -89,32 +91,32 @@ Object.assign( Line3.prototype, {
 
 	},
 
-	closestPointToPointParameter: function () {
+	closestPointToPointParameter: function ( point, clampToLine ) {
 
-		var startP = new Vector3();
-		var startEnd = new Vector3();
+		if ( _startP === undefined ) {
 
-		return function closestPointToPointParameter( point, clampToLine ) {
+			_startP = new Vector3();
+			_startEnd = new Vector3();
 
-			startP.subVectors( point, this.start );
-			startEnd.subVectors( this.end, this.start );
+		}
 
-			var startEnd2 = startEnd.dot( startEnd );
-			var startEnd_startP = startEnd.dot( startP );
+		_startP.subVectors( point, this.start );
+		_startEnd.subVectors( this.end, this.start );
 
-			var t = startEnd_startP / startEnd2;
+		var startEnd2 = _startEnd.dot( _startEnd );
+		var startEnd_startP = _startEnd.dot( _startP );
 
-			if ( clampToLine ) {
+		var t = startEnd_startP / startEnd2;
 
-				t = _Math.clamp( t, 0, 1 );
+		if ( clampToLine ) {
 
-			}
+			t = _Math.clamp( t, 0, 1 );
 
-			return t;
+		}
 
-		};
+		return t;
 
-	}(),
+	},
 
 	closestPointToPoint: function ( point, clampToLine, target ) {
 

+ 13 - 15
src/math/Matrix3.js

@@ -7,6 +7,8 @@ import { Vector3 } from './Vector3.js';
  * @author tschw
  */
 
+var _vector;
+
 function Matrix3() {
 
 	this.elements = [
@@ -90,29 +92,25 @@ Object.assign( Matrix3.prototype, {
 
 	},
 
-	applyToBufferAttribute: function () {
-
-		var v1 = new Vector3();
-
-		return function applyToBufferAttribute( attribute ) {
+	applyToBufferAttribute: function ( attribute ) {
 
-			for ( var i = 0, l = attribute.count; i < l; i ++ ) {
+		if ( _vector === undefined ) _vector = new Vector3();
 
-				v1.x = attribute.getX( i );
-				v1.y = attribute.getY( i );
-				v1.z = attribute.getZ( i );
+		for ( var i = 0, l = attribute.count; i < l; i ++ ) {
 
-				v1.applyMatrix3( this );
+			_vector.x = attribute.getX( i );
+			_vector.y = attribute.getY( i );
+			_vector.z = attribute.getZ( i );
 
-				attribute.setXYZ( i, v1.x, v1.y, v1.z );
+			_vector.applyMatrix3( this );
 
-			}
+			attribute.setXYZ( i, _vector.x, _vector.y, _vector.z );
 
-			return attribute;
+		}
 
-		};
+		return attribute;
 
-	}(),
+	},
 
 	multiply: function ( m ) {
 

+ 17 - 19
src/math/Sphere.js

@@ -6,6 +6,8 @@ import { Vector3 } from './Vector3.js';
  * @author mrdoob / http://mrdoob.com/
  */
 
+var _box;
+
 function Sphere( center, radius ) {
 
 	this.center = ( center !== undefined ) ? center : new Vector3();
@@ -24,39 +26,35 @@ Object.assign( Sphere.prototype, {
 
 	},
 
-	setFromPoints: function () {
-
-		var box = new Box3();
+	setFromPoints: function ( points, optionalCenter ) {
 
-		return function setFromPoints( points, optionalCenter ) {
+		if ( _box === undefined ) _box = new Box3();
 
-			var center = this.center;
+		var center = this.center;
 
-			if ( optionalCenter !== undefined ) {
+		if ( optionalCenter !== undefined ) {
 
-				center.copy( optionalCenter );
+			center.copy( optionalCenter );
 
-			} else {
+		} else {
 
-				box.setFromPoints( points ).getCenter( center );
+			_box.setFromPoints( points ).getCenter( center );
 
-			}
-
-			var maxRadiusSq = 0;
+		}
 
-			for ( var i = 0, il = points.length; i < il; i ++ ) {
+		var maxRadiusSq = 0;
 
-				maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
+		for ( var i = 0, il = points.length; i < il; i ++ ) {
 
-			}
+			maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
 
-			this.radius = Math.sqrt( maxRadiusSq );
+		}
 
-			return this;
+		this.radius = Math.sqrt( maxRadiusSq );
 
-		};
+		return this;
 
-	}(),
+	},
 
 	clone: function () {