Browse Source

Vector*: Safe multiplyScalar. See #7326.

Mr.doob 9 years ago
parent
commit
8c4f342ac4
3 changed files with 34 additions and 70 deletions
  1. 10 21
      src/math/Vector2.js
  2. 11 23
      src/math/Vector3.js
  3. 13 26
      src/math/Vector4.js

+ 10 - 21
src/math/Vector2.js

@@ -174,10 +174,15 @@ THREE.Vector2.prototype = {
 
 	},
 
-	multiplyScalar: function ( s ) {
+	multiplyScalar: function ( scalar ) {
 
-		this.x *= s;
-		this.y *= s;
+		if ( isFinite( scalar ) ) {
+			this.x *= scalar;
+			this.y *= scalar;
+		} else {
+			this.x = 0;
+			this.y = 0;
+		}
 
 		return this;
 
@@ -194,21 +199,7 @@ THREE.Vector2.prototype = {
 
 	divideScalar: function ( scalar ) {
 
-		if ( scalar !== 0 ) {
-
-			var invScalar = 1 / scalar;
-
-			this.x *= invScalar;
-			this.y *= invScalar;
-
-		} else {
-
-			this.x = 0;
-			this.y = 0;
-
-		}
-
-		return this;
+		return this.multiplyScalar( 1 / scalar );
 
 	},
 
@@ -363,9 +354,7 @@ THREE.Vector2.prototype = {
 
 	setLength: function ( length ) {
 
-		this.multiplyScalar( length / this.length() );
-
-		return this;
+		return this.multiplyScalar( length / this.length() );
 
 	},
 

+ 11 - 23
src/math/Vector3.js

@@ -198,9 +198,15 @@ THREE.Vector3.prototype = {
 
 	multiplyScalar: function ( scalar ) {
 
-		this.x *= scalar;
-		this.y *= scalar;
-		this.z *= scalar;
+		if ( isFinite( scalar ) ) {
+			this.x *= scalar;
+			this.y *= scalar;
+			this.z *= scalar;
+		} else {
+			this.x = 0;
+			this.y = 0;
+			this.z = 0;
+		}
 
 		return this;
 
@@ -392,23 +398,7 @@ THREE.Vector3.prototype = {
 
 	divideScalar: function ( scalar ) {
 
-		if ( scalar !== 0 ) {
-
-			var invScalar = 1 / scalar;
-
-			this.x *= invScalar;
-			this.y *= invScalar;
-			this.z *= invScalar;
-
-		} else {
-
-			this.x = 0;
-			this.y = 0;
-			this.z = 0;
-
-		}
-
-		return this;
+		return this.multiplyScalar( 1 / scalar );
 
 	},
 
@@ -558,9 +548,7 @@ THREE.Vector3.prototype = {
 
 	setLength: function ( length ) {
 
-		this.multiplyScalar( length / this.length() );
-
-		return this;
+		return this.multiplyScalar( length / this.length() );
 
 	},
 

+ 13 - 26
src/math/Vector4.js

@@ -200,10 +200,17 @@ THREE.Vector4.prototype = {
 
 	multiplyScalar: function ( scalar ) {
 
-		this.x *= scalar;
-		this.y *= scalar;
-		this.z *= scalar;
-		this.w *= scalar;
+		if ( isFinite( scalar ) ) {
+			this.x *= scalar;
+			this.y *= scalar;
+			this.z *= scalar;
+			this.w *= scalar;
+		} else {
+			this.x = 0;
+			this.y = 0;
+			this.z = 0;
+			this.w = 1;
+		}
 
 		return this;
 
@@ -229,25 +236,7 @@ THREE.Vector4.prototype = {
 
 	divideScalar: function ( scalar ) {
 
-		if ( scalar !== 0 ) {
-
-			var invScalar = 1 / scalar;
-
-			this.x *= invScalar;
-			this.y *= invScalar;
-			this.z *= invScalar;
-			this.w *= invScalar;
-
-		} else {
-
-			this.x = 0;
-			this.y = 0;
-			this.z = 0;
-			this.w = 1;
-
-		}
-
-		return this;
+		return this.multiplyScalar( 1 / scalar );
 
 	},
 
@@ -553,9 +542,7 @@ THREE.Vector4.prototype = {
 
 	setLength: function ( length ) {
 
-		this.multiplyScalar( length / this.length() );
-
-		return this;
+		return this.multiplyScalar( length / this.length() );
 
 	},