Skip to content

Commit

Permalink
Matrix3: Added lookAt()
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Aug 13, 2018
1 parent cef80a2 commit 46ede50
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 15 deletions.
13 changes: 4 additions & 9 deletions src/core/Object3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ Object3D.prototype = Object.assign( Object.create( EventDispatcher.prototype ),

// This method does not support objects with rotated and/or translated parent(s)

var m1 = new Matrix4();
var m1 = new Matrix3();
var targetDirection = new Vector3();
var vector = new Vector3();

return function lookAt( x, y, z ) {
Expand All @@ -322,15 +323,9 @@ Object3D.prototype = Object.assign( Object.create( EventDispatcher.prototype ),

}

if ( this.isCamera ) {

m1.lookAt( this.position, vector, this.up );

} else {

m1.lookAt( vector, this.position, this.up );
targetDirection.subVectors( vector, this.position ).normalize();

}
m1.lookAt( this.forward, targetDirection, this.up );

this.quaternion.setFromRotationMatrix( m1 );

Expand Down
56 changes: 56 additions & 0 deletions src/math/Matrix3.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,62 @@ Object.assign( Matrix3.prototype, {

}(),

makeBasis: function ( xAxis, yAxis, zAxis ) {

this.set(
xAxis.x, yAxis.x, zAxis.x,
xAxis.y, yAxis.y, zAxis.y,
xAxis.z, yAxis.z, zAxis.z
);

return this;

},

lookAt: function () {

var localRight = new Vector3();
var worldRight = new Vector3();
var worldUp = new Vector3( 0, 1, 0 );
var perpWorldUp = new Vector3();
var temp = new Vector3();

var m1 = new Matrix3();
var m2 = new Matrix3();

return function lookAt( localForward, targetDirection, localUp ) {

localRight.crossVectors( localUp, localForward ).normalize();

// orthonormal linear basis A { localRight, localUp, localForward } for the object local space

worldRight.crossVectors( worldUp, targetDirection ).normalize();

if ( worldRight.lengthSq() === 0 ) {

// handle case when it's not possible to build a basis from targetDirection and worldUp
// slightly shift targetDirection in order to avoid collinearity

temp.copy( targetDirection ).addScalar( Number.EPSILON );
worldRight.crossVectors( worldUp, temp ).normalize();

}

perpWorldUp.crossVectors( targetDirection, worldRight ).normalize();

// orthonormal linear basis B { worldRight, perpWorldUp, targetDirection } for the desired target orientation

m1.makeBasis( worldRight, perpWorldUp, targetDirection );
m2.makeBasis( localRight, localUp, localForward );

// construct a matrix that maps basis A to B

this.multiplyMatrices( m1, m2.transpose() );

};

}(),

multiply: function ( m ) {

return this.multiplyMatrices( this, m );
Expand Down
23 changes: 17 additions & 6 deletions src/math/Quaternion.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,25 @@ Object.assign( Quaternion.prototype, {

// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)

var te = m.elements,
var te = m.elements;

m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ],
var m11, m12, m13, m21, m22, m23, m31, m32, m33;

trace = m11 + m22 + m33,
s;
if ( m.isMatrix3 ) {

m11 = te[ 0 ]; m12 = te[ 3 ]; m13 = te[ 6 ];
m21 = te[ 1 ]; m22 = te[ 4 ]; m23 = te[ 7 ];
m31 = te[ 2 ]; m32 = te[ 5 ]; m33 = te[ 8 ];

} else {

m11 = te[ 0 ]; m12 = te[ 4 ]; m13 = te[ 8 ];
m21 = te[ 1 ]; m22 = te[ 5 ]; m23 = te[ 9 ];
m31 = te[ 2 ]; m32 = te[ 6 ]; m33 = te[ 10 ];

}

var trace = m11 + m22 + m33, s;

if ( trace > 0 ) {

Expand Down

0 comments on commit 46ede50

Please sign in to comment.