Skip to content

Commit a1d4a3a

Browse files
loldrupmrdoob
authored andcommitted
Faster projectOnVector function (mrdoob#9101)
This implementation of projectOnVector is faster and shorter than the current version, and it requires no intermediate Vector3 allocation
1 parent 578978c commit a1d4a3a

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

src/math/Vector3.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -594,23 +594,13 @@ THREE.Vector3.prototype = {
594594

595595
},
596596

597-
projectOnVector: function () {
597+
projectOnVector: function ( vector ) {
598598

599-
var v1, dot;
600-
601-
return function projectOnVector( vector ) {
602-
603-
if ( v1 === undefined ) v1 = new THREE.Vector3();
604-
605-
v1.copy( vector ).normalize();
606-
607-
dot = this.dot( v1 );
608-
609-
return this.copy( v1 ).multiplyScalar( dot );
610-
611-
};
612-
613-
}(),
599+
var scalar = vector.dot( this ) / vector.lengthSq();
600+
601+
return this.copy( vector ).multiplyScalar( scalar );
602+
603+
},
614604

615605
projectOnPlane: function () {
616606

0 commit comments

Comments
 (0)