Skip to content

Commit 8c4f342

Browse files
committed
Vector*: Safe multiplyScalar. See mrdoob#7326.
1 parent 5218134 commit 8c4f342

File tree

3 files changed

+34
-70
lines changed

3 files changed

+34
-70
lines changed

src/math/Vector2.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,15 @@ THREE.Vector2.prototype = {
174174

175175
},
176176

177-
multiplyScalar: function ( s ) {
177+
multiplyScalar: function ( scalar ) {
178178

179-
this.x *= s;
180-
this.y *= s;
179+
if ( isFinite( scalar ) ) {
180+
this.x *= scalar;
181+
this.y *= scalar;
182+
} else {
183+
this.x = 0;
184+
this.y = 0;
185+
}
181186

182187
return this;
183188

@@ -194,21 +199,7 @@ THREE.Vector2.prototype = {
194199

195200
divideScalar: function ( scalar ) {
196201

197-
if ( scalar !== 0 ) {
198-
199-
var invScalar = 1 / scalar;
200-
201-
this.x *= invScalar;
202-
this.y *= invScalar;
203-
204-
} else {
205-
206-
this.x = 0;
207-
this.y = 0;
208-
209-
}
210-
211-
return this;
202+
return this.multiplyScalar( 1 / scalar );
212203

213204
},
214205

@@ -363,9 +354,7 @@ THREE.Vector2.prototype = {
363354

364355
setLength: function ( length ) {
365356

366-
this.multiplyScalar( length / this.length() );
367-
368-
return this;
357+
return this.multiplyScalar( length / this.length() );
369358

370359
},
371360

src/math/Vector3.js

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,15 @@ THREE.Vector3.prototype = {
198198

199199
multiplyScalar: function ( scalar ) {
200200

201-
this.x *= scalar;
202-
this.y *= scalar;
203-
this.z *= scalar;
201+
if ( isFinite( scalar ) ) {
202+
this.x *= scalar;
203+
this.y *= scalar;
204+
this.z *= scalar;
205+
} else {
206+
this.x = 0;
207+
this.y = 0;
208+
this.z = 0;
209+
}
204210

205211
return this;
206212

@@ -392,23 +398,7 @@ THREE.Vector3.prototype = {
392398

393399
divideScalar: function ( scalar ) {
394400

395-
if ( scalar !== 0 ) {
396-
397-
var invScalar = 1 / scalar;
398-
399-
this.x *= invScalar;
400-
this.y *= invScalar;
401-
this.z *= invScalar;
402-
403-
} else {
404-
405-
this.x = 0;
406-
this.y = 0;
407-
this.z = 0;
408-
409-
}
410-
411-
return this;
401+
return this.multiplyScalar( 1 / scalar );
412402

413403
},
414404

@@ -558,9 +548,7 @@ THREE.Vector3.prototype = {
558548

559549
setLength: function ( length ) {
560550

561-
this.multiplyScalar( length / this.length() );
562-
563-
return this;
551+
return this.multiplyScalar( length / this.length() );
564552

565553
},
566554

src/math/Vector4.js

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,17 @@ THREE.Vector4.prototype = {
200200

201201
multiplyScalar: function ( scalar ) {
202202

203-
this.x *= scalar;
204-
this.y *= scalar;
205-
this.z *= scalar;
206-
this.w *= scalar;
203+
if ( isFinite( scalar ) ) {
204+
this.x *= scalar;
205+
this.y *= scalar;
206+
this.z *= scalar;
207+
this.w *= scalar;
208+
} else {
209+
this.x = 0;
210+
this.y = 0;
211+
this.z = 0;
212+
this.w = 1;
213+
}
207214

208215
return this;
209216

@@ -229,25 +236,7 @@ THREE.Vector4.prototype = {
229236

230237
divideScalar: function ( scalar ) {
231238

232-
if ( scalar !== 0 ) {
233-
234-
var invScalar = 1 / scalar;
235-
236-
this.x *= invScalar;
237-
this.y *= invScalar;
238-
this.z *= invScalar;
239-
this.w *= invScalar;
240-
241-
} else {
242-
243-
this.x = 0;
244-
this.y = 0;
245-
this.z = 0;
246-
this.w = 1;
247-
248-
}
249-
250-
return this;
239+
return this.multiplyScalar( 1 / scalar );
251240

252241
},
253242

@@ -553,9 +542,7 @@ THREE.Vector4.prototype = {
553542

554543
setLength: function ( length ) {
555544

556-
this.multiplyScalar( length / this.length() );
557-
558-
return this;
545+
return this.multiplyScalar( length / this.length() );
559546

560547
},
561548

0 commit comments

Comments
 (0)