Browse Source

Revert "Revert closure optimisations"

Mr.doob 8 years ago
parent
commit
b76fff27eb

+ 6 - 18
src/core/BufferGeometry.js

@@ -173,12 +173,10 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, {
 
 		// rotate geometry around world x-axis
 
-		var m1;
+		var m1 = new Matrix4();
 
 		return function rotateX( angle ) {
 
-			if ( m1 === undefined ) m1 = new Matrix4();
-
 			m1.makeRotationX( angle );
 
 			this.applyMatrix( m1 );
@@ -193,12 +191,10 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, {
 
 		// rotate geometry around world y-axis
 
-		var m1;
+		var m1 = new Matrix4();
 
 		return function rotateY( angle ) {
 
-			if ( m1 === undefined ) m1 = new Matrix4();
-
 			m1.makeRotationY( angle );
 
 			this.applyMatrix( m1 );
@@ -213,12 +209,10 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, {
 
 		// rotate geometry around world z-axis
 
-		var m1;
+		var m1 = new Matrix4();
 
 		return function rotateZ( angle ) {
 
-			if ( m1 === undefined ) m1 = new Matrix4();
-
 			m1.makeRotationZ( angle );
 
 			this.applyMatrix( m1 );
@@ -233,12 +227,10 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, {
 
 		// translate geometry
 
-		var m1;
+		var m1 = new Matrix4();
 
 		return function translate( x, y, z ) {
 
-			if ( m1 === undefined ) m1 = new Matrix4();
-
 			m1.makeTranslation( x, y, z );
 
 			this.applyMatrix( m1 );
@@ -253,12 +245,10 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, {
 
 		// scale geometry
 
-		var m1;
+		var m1 = new Matrix4();
 
 		return function scale( x, y, z ) {
 
-			if ( m1 === undefined ) m1 = new Matrix4();
-
 			m1.makeScale( x, y, z );
 
 			this.applyMatrix( m1 );
@@ -271,12 +261,10 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, {
 
 	lookAt: function () {
 
-		var obj;
+		var obj = new Object3D();
 
 		return function lookAt( vector ) {
 
-			if ( obj === undefined ) obj = new Object3D();
-
 			obj.lookAt( vector );
 
 			obj.updateMatrix();

+ 6 - 18
src/core/Geometry.js

@@ -110,12 +110,10 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, {
 
 		// rotate geometry around world x-axis
 
-		var m1;
+		var m1 = new Matrix4();
 
 		return function rotateX( angle ) {
 
-			if ( m1 === undefined ) m1 = new Matrix4();
-
 			m1.makeRotationX( angle );
 
 			this.applyMatrix( m1 );
@@ -130,12 +128,10 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, {
 
 		// rotate geometry around world y-axis
 
-		var m1;
+		var m1 = new Matrix4();
 
 		return function rotateY( angle ) {
 
-			if ( m1 === undefined ) m1 = new Matrix4();
-
 			m1.makeRotationY( angle );
 
 			this.applyMatrix( m1 );
@@ -150,12 +146,10 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, {
 
 		// rotate geometry around world z-axis
 
-		var m1;
+		var m1 = new Matrix4();
 
 		return function rotateZ( angle ) {
 
-			if ( m1 === undefined ) m1 = new Matrix4();
-
 			m1.makeRotationZ( angle );
 
 			this.applyMatrix( m1 );
@@ -170,12 +164,10 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, {
 
 		// translate geometry
 
-		var m1;
+		var m1 = new Matrix4();
 
 		return function translate( x, y, z ) {
 
-			if ( m1 === undefined ) m1 = new Matrix4();
-
 			m1.makeTranslation( x, y, z );
 
 			this.applyMatrix( m1 );
@@ -190,12 +182,10 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, {
 
 		// scale geometry
 
-		var m1;
+		var m1 = new Matrix4();
 
 		return function scale( x, y, z ) {
 
-			if ( m1 === undefined ) m1 = new Matrix4();
-
 			m1.makeScale( x, y, z );
 
 			this.applyMatrix( m1 );
@@ -208,12 +198,10 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, {
 
 	lookAt: function () {
 
-		var obj;
+		var obj = new Object3D();
 
 		return function lookAt( vector ) {
 
-			if ( obj === undefined ) obj = new Object3D();
-
 			obj.lookAt( vector );
 
 			obj.updateMatrix();

+ 3 - 5
src/loaders/Loader.js

@@ -106,14 +106,12 @@ Object.assign( Loader.prototype, {
 			CustomBlending: CustomBlending
 		};
 
-		var color, textureLoader, materialLoader;
+		var color = new Color();
+		var textureLoader = new TextureLoader();
+		var materialLoader = new MaterialLoader();
 
 		return function createMaterial( m, texturePath, crossOrigin ) {
 
-			if ( color === undefined ) color = new Color();
-			if ( textureLoader === undefined ) textureLoader = new TextureLoader();
-			if ( materialLoader === undefined ) materialLoader = new MaterialLoader();
-
 			// convert from old material format
 
 			var textures = {};

+ 1 - 3
src/math/Box3.js

@@ -308,12 +308,10 @@ Object.assign( Box3.prototype, {
 
 	intersectsSphere: ( function () {
 
-		var closestPoint;
+		var closestPoint = new Vector3();
 
 		return function intersectsSphere( sphere ) {
 
-			if ( closestPoint === undefined ) closestPoint = new Vector3();
-
 			// Find the point on the AABB closest to the sphere center.
 			this.clampPoint( sphere.center, closestPoint );
 

+ 1 - 3
src/math/Euler.js

@@ -255,12 +255,10 @@ Object.assign( Euler.prototype, {
 
 	setFromQuaternion: function () {
 
-		var matrix;
+		var matrix = new Matrix4();
 
 		return function setFromQuaternion( q, order, update ) {
 
-			if ( matrix === undefined ) matrix = new Matrix4();
-
 			matrix.makeRotationFromQuaternion( q );
 
 			return this.setFromRotationMatrix( matrix, order, update );

+ 1 - 3
src/math/Matrix3.js

@@ -92,12 +92,10 @@ Object.assign( Matrix3.prototype, {
 
 	applyToBufferAttribute: function () {
 
-		var v1;
+		var v1 = new Vector3();
 
 		return function applyToBufferAttribute( attribute ) {
 
-			if ( v1 === undefined ) v1 = new Vector3();
-
 			for ( var i = 0, l = attribute.count; i < l; i ++ ) {
 
 				v1.x = attribute.getX( i );

+ 7 - 23
src/math/Matrix4.js

@@ -121,12 +121,10 @@ Object.assign( Matrix4.prototype, {
 
 	extractRotation: function () {
 
-		var v1;
+		var v1 = new Vector3();
 
 		return function extractRotation( m ) {
 
-			if ( v1 === undefined ) v1 = new Vector3();
-
 			var te = this.elements;
 			var me = m.elements;
 
@@ -319,18 +317,12 @@ Object.assign( Matrix4.prototype, {
 
 	lookAt: function () {
 
-		var x, y, z;
+		var x = new Vector3();
+		var y = new Vector3();
+		var z = new Vector3();
 
 		return function lookAt( eye, target, up ) {
 
-			if ( x === undefined ) {
-
-				x = new Vector3();
-				y = new Vector3();
-				z = new Vector3();
-
-			}
-
 			var te = this.elements;
 
 			z.subVectors( eye, target );
@@ -442,12 +434,10 @@ Object.assign( Matrix4.prototype, {
 
 	applyToBufferAttribute: function () {
 
-		var v1;
+		var v1 = new Vector3();
 
 		return function applyToBufferAttribute( attribute ) {
 
-			if ( v1 === undefined ) v1 = new Vector3();
-
 			for ( var i = 0, l = attribute.count; i < l; i ++ ) {
 
 				v1.x = attribute.getX( i );
@@ -764,17 +754,11 @@ Object.assign( Matrix4.prototype, {
 
 	decompose: function () {
 
-		var vector, matrix;
+		var vector = new Vector3();
+		var matrix = new Matrix4();
 
 		return function decompose( position, quaternion, scale ) {
 
-			if ( vector === undefined ) {
-
-				vector = new Vector3();
-				matrix = new Matrix4();
-
-			}
-
 			var te = this.elements;
 
 			var sx = vector.set( te[ 0 ], te[ 1 ], te[ 2 ] ).length();

+ 2 - 1
src/math/Quaternion.js

@@ -351,7 +351,8 @@ Object.assign( Quaternion.prototype, {
 
 		// assumes direction vectors vFrom and vTo are normalized
 
-		var v1, r;
+		var v1 = new Vector3();
+		var r;
 
 		var EPS = 0.000001;
 

+ 1 - 3
src/math/Sphere.js

@@ -26,12 +26,10 @@ Object.assign( Sphere.prototype, {
 
 	setFromPoints: function () {
 
-		var box;
+		var box = new Box3();
 
 		return function setFromPoints( points, optionalCenter ) {
 
-			if ( box === undefined ) box = new Box3(); // see #10547
-
 			var center = this.center;
 
 			if ( optionalCenter !== undefined ) {

+ 4 - 10
src/math/Triangle.js

@@ -191,19 +191,13 @@ Object.assign( Triangle.prototype, {
 
 	closestPointToPoint: function () {
 
-		var plane, edgeList, projectedPoint, closestPoint;
+		var plane = new Plane();
+		var edgeList = [ new Line3(), new Line3(), new Line3() ];
+		var projectedPoint = new Vector3();
+		var closestPoint = new Vector3();
 
 		return function closestPointToPoint( point, optionalTarget ) {
 
-			if ( plane === undefined ) {
-
-				plane = new Plane();
-				edgeList = [ new Line3(), new Line3(), new Line3() ];
-				projectedPoint = new Vector3();
-				closestPoint = new Vector3();
-
-			}
-
 			var result = optionalTarget || new Vector3();
 			var minDistance = Infinity;
 

+ 2 - 8
src/math/Vector2.js

@@ -268,17 +268,11 @@ Object.assign( Vector2.prototype, {
 
 	clampScalar: function () {
 
-		var min, max;
+		var min = new Vector2();
+		var max = new Vector2();
 
 		return function clampScalar( minVal, maxVal ) {
 
-			if ( min === undefined ) {
-
-				min = new Vector2();
-				max = new Vector2();
-
-			}
-
 			min.set( minVal, minVal );
 			max.set( maxVal, maxVal );
 

+ 8 - 26
src/math/Vector3.js

@@ -234,7 +234,7 @@ Object.assign( Vector3.prototype, {
 
 	applyEuler: function () {
 
-		var quaternion;
+		var quaternion = new Quaternion();
 
 		return function applyEuler( euler ) {
 
@@ -244,8 +244,6 @@ Object.assign( Vector3.prototype, {
 
 			}
 
-			if ( quaternion === undefined ) quaternion = new Quaternion();
-
 			return this.applyQuaternion( quaternion.setFromEuler( euler ) );
 
 		};
@@ -254,12 +252,10 @@ Object.assign( Vector3.prototype, {
 
 	applyAxisAngle: function () {
 
-		var quaternion;
+		var quaternion = new Quaternion();
 
 		return function applyAxisAngle( axis, angle ) {
 
-			if ( quaternion === undefined ) quaternion = new Quaternion();
-
 			return this.applyQuaternion( quaternion.setFromAxisAngle( axis, angle ) );
 
 		};
@@ -317,12 +313,10 @@ Object.assign( Vector3.prototype, {
 
 	project: function () {
 
-		var matrix;
+		var matrix = new Matrix4();
 
 		return function project( camera ) {
 
-			if ( matrix === undefined ) matrix = new Matrix4();
-
 			matrix.multiplyMatrices( camera.projectionMatrix, matrix.getInverse( camera.matrixWorld ) );
 			return this.applyMatrix4( matrix );
 
@@ -332,12 +326,10 @@ Object.assign( Vector3.prototype, {
 
 	unproject: function () {
 
-		var matrix;
+		var matrix = new Matrix4();
 
 		return function unproject( camera ) {
 
-			if ( matrix === undefined ) matrix = new Matrix4();
-
 			matrix.multiplyMatrices( camera.matrixWorld, matrix.getInverse( camera.projectionMatrix ) );
 			return this.applyMatrix4( matrix );
 
@@ -411,17 +403,11 @@ Object.assign( Vector3.prototype, {
 
 	clampScalar: function () {
 
-		var min, max;
+		var min = new Vector3();
+		var max = new Vector3();
 
 		return function clampScalar( minVal, maxVal ) {
 
-			if ( min === undefined ) {
-
-				min = new Vector3();
-				max = new Vector3();
-
-			}
-
 			min.set( minVal, minVal, minVal );
 			max.set( maxVal, maxVal, maxVal );
 
@@ -585,12 +571,10 @@ Object.assign( Vector3.prototype, {
 
 	projectOnPlane: function () {
 
-		var v1;
+		var v1 = new Vector3();
 
 		return function projectOnPlane( planeNormal ) {
 
-			if ( v1 === undefined ) v1 = new Vector3();
-
 			v1.copy( this ).projectOnVector( planeNormal );
 
 			return this.sub( v1 );
@@ -604,12 +588,10 @@ Object.assign( Vector3.prototype, {
 		// reflect incident vector off plane orthogonal to normal
 		// normal is assumed to have unit length
 
-		var v1;
+		var v1 = new Vector3();
 
 		return function reflect( normal ) {
 
-			if ( v1 === undefined ) v1 = new Vector3();
-
 			return this.sub( v1.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) );
 
 		};