diff --git a/src/core/BufferGeometry.js b/src/core/BufferGeometry.js index 0e111a9319b58914076a67083d4de64dd1dc9b97..5f132356cfc509b7b884495837c59811f9c7ee5a 100644 --- a/src/core/BufferGeometry.js +++ b/src/core/BufferGeometry.js @@ -173,10 +173,12 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, { // rotate geometry around world x-axis - var m1 = new Matrix4(); + var m1; return function rotateX( angle ) { + if ( m1 === undefined ) m1 = new Matrix4(); + m1.makeRotationX( angle ); this.applyMatrix( m1 ); @@ -191,10 +193,12 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, { // rotate geometry around world y-axis - var m1 = new Matrix4(); + var m1; return function rotateY( angle ) { + if ( m1 === undefined ) m1 = new Matrix4(); + m1.makeRotationY( angle ); this.applyMatrix( m1 ); @@ -209,10 +213,12 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, { // rotate geometry around world z-axis - var m1 = new Matrix4(); + var m1; return function rotateZ( angle ) { + if ( m1 === undefined ) m1 = new Matrix4(); + m1.makeRotationZ( angle ); this.applyMatrix( m1 ); @@ -227,10 +233,12 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, { // translate geometry - var m1 = new Matrix4(); + var m1; return function translate( x, y, z ) { + if ( m1 === undefined ) m1 = new Matrix4(); + m1.makeTranslation( x, y, z ); this.applyMatrix( m1 ); @@ -245,10 +253,12 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, { // scale geometry - var m1 = new Matrix4(); + var m1; return function scale( x, y, z ) { + if ( m1 === undefined ) m1 = new Matrix4(); + m1.makeScale( x, y, z ); this.applyMatrix( m1 ); @@ -261,10 +271,12 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, { lookAt: function () { - var obj = new Object3D(); + var obj; return function lookAt( vector ) { + if ( obj === undefined ) obj = new Object3D(); + obj.lookAt( vector ); obj.updateMatrix(); diff --git a/src/core/Geometry.js b/src/core/Geometry.js index 6486b086b608a0d19b43e46c8ec8d9ec60802d50..9a61f882e48d69367d55e553af965e0f2c9ddf48 100644 --- a/src/core/Geometry.js +++ b/src/core/Geometry.js @@ -110,10 +110,12 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, { // rotate geometry around world x-axis - var m1 = new Matrix4(); + var m1; return function rotateX( angle ) { + if ( m1 === undefined ) m1 = new Matrix4(); + m1.makeRotationX( angle ); this.applyMatrix( m1 ); @@ -128,10 +130,12 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, { // rotate geometry around world y-axis - var m1 = new Matrix4(); + var m1; return function rotateY( angle ) { + if ( m1 === undefined ) m1 = new Matrix4(); + m1.makeRotationY( angle ); this.applyMatrix( m1 ); @@ -146,10 +150,12 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, { // rotate geometry around world z-axis - var m1 = new Matrix4(); + var m1; return function rotateZ( angle ) { + if ( m1 === undefined ) m1 = new Matrix4(); + m1.makeRotationZ( angle ); this.applyMatrix( m1 ); @@ -164,10 +170,12 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, { // translate geometry - var m1 = new Matrix4(); + var m1; return function translate( x, y, z ) { + if ( m1 === undefined ) m1 = new Matrix4(); + m1.makeTranslation( x, y, z ); this.applyMatrix( m1 ); @@ -182,10 +190,12 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, { // scale geometry - var m1 = new Matrix4(); + var m1; return function scale( x, y, z ) { + if ( m1 === undefined ) m1 = new Matrix4(); + m1.makeScale( x, y, z ); this.applyMatrix( m1 ); @@ -198,10 +208,12 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, { lookAt: function () { - var obj = new Object3D(); + var obj; return function lookAt( vector ) { + if ( obj === undefined ) obj = new Object3D(); + obj.lookAt( vector ); obj.updateMatrix(); diff --git a/src/loaders/Loader.js b/src/loaders/Loader.js index 5b59e89d9cf68b365e74b4ccf4b93edb444bf5c9..3700f2fdc47ca3578d2a4e146d54e8e81a8e4dff 100644 --- a/src/loaders/Loader.js +++ b/src/loaders/Loader.js @@ -106,12 +106,14 @@ Object.assign( Loader.prototype, { CustomBlending: CustomBlending }; - var color = new Color(); - var textureLoader = new TextureLoader(); - var materialLoader = new MaterialLoader(); + var color, textureLoader, 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 = {}; diff --git a/src/math/Box3.js b/src/math/Box3.js index 59c0e4f1ce645d7795b793e1a9c7116592d5c8e4..c24bd910813ae15b39c4dd9ef4dfc057e897cce4 100644 --- a/src/math/Box3.js +++ b/src/math/Box3.js @@ -308,10 +308,12 @@ Object.assign( Box3.prototype, { intersectsSphere: ( function () { - var closestPoint = new Vector3(); + var closestPoint; 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 ); diff --git a/src/math/Euler.js b/src/math/Euler.js index fe4df6b9932e394317808a448815b916c4516998..36bee5ecd514be2ef3d5ed1a05fc1ed5779a1aff 100644 --- a/src/math/Euler.js +++ b/src/math/Euler.js @@ -255,10 +255,12 @@ Object.assign( Euler.prototype, { setFromQuaternion: function () { - var matrix = new Matrix4(); + var matrix; return function setFromQuaternion( q, order, update ) { + if ( matrix === undefined ) matrix = new Matrix4(); + matrix.makeRotationFromQuaternion( q ); return this.setFromRotationMatrix( matrix, order, update ); diff --git a/src/math/Matrix3.js b/src/math/Matrix3.js index d2f810d072dc73db989b2bbf73fc1a6440496689..a6e4b1d07ef39fbefa828543a7a69fe161af1b79 100644 --- a/src/math/Matrix3.js +++ b/src/math/Matrix3.js @@ -92,10 +92,12 @@ Object.assign( Matrix3.prototype, { applyToBufferAttribute: function () { - var v1 = new Vector3(); + var v1; 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 ); diff --git a/src/math/Matrix4.js b/src/math/Matrix4.js index 75c3181decb40253c651eb7b8600a17253a3a96b..747a2d28b26f248f41664f95c7865548deb8e847 100644 --- a/src/math/Matrix4.js +++ b/src/math/Matrix4.js @@ -121,10 +121,12 @@ Object.assign( Matrix4.prototype, { extractRotation: function () { - var v1 = new Vector3(); + var v1; return function extractRotation( m ) { + if ( v1 === undefined ) v1 = new Vector3(); + var te = this.elements; var me = m.elements; @@ -317,12 +319,18 @@ Object.assign( Matrix4.prototype, { lookAt: function () { - var x = new Vector3(); - var y = new Vector3(); - var z = new Vector3(); + var x, y, z; 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 ); @@ -434,10 +442,12 @@ Object.assign( Matrix4.prototype, { applyToBufferAttribute: function () { - var v1 = new Vector3(); + var v1; 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 ); @@ -754,11 +764,17 @@ Object.assign( Matrix4.prototype, { decompose: function () { - var vector = new Vector3(); - var matrix = new Matrix4(); + var vector, matrix; 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(); diff --git a/src/math/Quaternion.js b/src/math/Quaternion.js index 9905d4d87381ab382652b62fa86d49240d76186b..cfca4e9efd28abc8543ee1d89750a9b2b4d20538 100644 --- a/src/math/Quaternion.js +++ b/src/math/Quaternion.js @@ -351,8 +351,7 @@ Object.assign( Quaternion.prototype, { // assumes direction vectors vFrom and vTo are normalized - var v1 = new Vector3(); - var r; + var v1, r; var EPS = 0.000001; diff --git a/src/math/Sphere.js b/src/math/Sphere.js index 7418f46c37b81b3945fa0235c6203f9d30a59ef5..0ba196ae5a32232f4710d27918a20f43f07ce60a 100644 --- a/src/math/Sphere.js +++ b/src/math/Sphere.js @@ -26,10 +26,12 @@ Object.assign( Sphere.prototype, { setFromPoints: function () { - var box = new Box3(); + var box; return function setFromPoints( points, optionalCenter ) { + if ( box === undefined ) box = new Box3(); // see #10547 + var center = this.center; if ( optionalCenter !== undefined ) { diff --git a/src/math/Triangle.js b/src/math/Triangle.js index 7d5727d594f818f633b7b8b70282c62c527a20aa..32ee8011a7e756638dbe84b97c90bfe8f50ec4f8 100644 --- a/src/math/Triangle.js +++ b/src/math/Triangle.js @@ -191,13 +191,19 @@ Object.assign( Triangle.prototype, { closestPointToPoint: function () { - var plane = new Plane(); - var edgeList = [ new Line3(), new Line3(), new Line3() ]; - var projectedPoint = new Vector3(); - var closestPoint = new Vector3(); + var plane, edgeList, projectedPoint, closestPoint; 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; diff --git a/src/math/Vector2.js b/src/math/Vector2.js index 548a010a2141e1376575ed8b99bbb5bfce885464..483c1e491babca5a23c5923aa8799ae58f5cb4e3 100644 --- a/src/math/Vector2.js +++ b/src/math/Vector2.js @@ -268,11 +268,17 @@ Object.assign( Vector2.prototype, { clampScalar: function () { - var min = new Vector2(); - var max = new Vector2(); + var min, max; return function clampScalar( minVal, maxVal ) { + if ( min === undefined ) { + + min = new Vector2(); + max = new Vector2(); + + } + min.set( minVal, minVal ); max.set( maxVal, maxVal ); diff --git a/src/math/Vector3.js b/src/math/Vector3.js index e6054919ea683f77eaa2bf0e3cb922750dccb7b7..23b6b4c7d72369131b9bcc3d6158e3309020d4d5 100644 --- a/src/math/Vector3.js +++ b/src/math/Vector3.js @@ -234,7 +234,7 @@ Object.assign( Vector3.prototype, { applyEuler: function () { - var quaternion = new Quaternion(); + var quaternion; return function applyEuler( euler ) { @@ -244,6 +244,8 @@ Object.assign( Vector3.prototype, { } + if ( quaternion === undefined ) quaternion = new Quaternion(); + return this.applyQuaternion( quaternion.setFromEuler( euler ) ); }; @@ -252,10 +254,12 @@ Object.assign( Vector3.prototype, { applyAxisAngle: function () { - var quaternion = new Quaternion(); + var quaternion; return function applyAxisAngle( axis, angle ) { + if ( quaternion === undefined ) quaternion = new Quaternion(); + return this.applyQuaternion( quaternion.setFromAxisAngle( axis, angle ) ); }; @@ -313,10 +317,12 @@ Object.assign( Vector3.prototype, { project: function () { - var matrix = new Matrix4(); + var matrix; return function project( camera ) { + if ( matrix === undefined ) matrix = new Matrix4(); + matrix.multiplyMatrices( camera.projectionMatrix, matrix.getInverse( camera.matrixWorld ) ); return this.applyMatrix4( matrix ); @@ -326,10 +332,12 @@ Object.assign( Vector3.prototype, { unproject: function () { - var matrix = new Matrix4(); + var matrix; return function unproject( camera ) { + if ( matrix === undefined ) matrix = new Matrix4(); + matrix.multiplyMatrices( camera.matrixWorld, matrix.getInverse( camera.projectionMatrix ) ); return this.applyMatrix4( matrix ); @@ -403,11 +411,17 @@ Object.assign( Vector3.prototype, { clampScalar: function () { - var min = new Vector3(); - var max = new Vector3(); + var min, max; return function clampScalar( minVal, maxVal ) { + if ( min === undefined ) { + + min = new Vector3(); + max = new Vector3(); + + } + min.set( minVal, minVal, minVal ); max.set( maxVal, maxVal, maxVal ); @@ -571,10 +585,12 @@ Object.assign( Vector3.prototype, { projectOnPlane: function () { - var v1 = new Vector3(); + var v1; return function projectOnPlane( planeNormal ) { + if ( v1 === undefined ) v1 = new Vector3(); + v1.copy( this ).projectOnVector( planeNormal ); return this.sub( v1 ); @@ -588,10 +604,12 @@ Object.assign( Vector3.prototype, { // reflect incident vector off plane orthogonal to normal // normal is assumed to have unit length - var v1 = new Vector3(); + var v1; return function reflect( normal ) { + if ( v1 === undefined ) v1 = new Vector3(); + return this.sub( v1.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) ); };