From 41e86558d11bd3d42fcbf9985ec40db74f92235f Mon Sep 17 00:00:00 2001 From: "Mr.doob" Date: Sat, 25 Feb 2017 20:50:39 -0800 Subject: [PATCH] Matrix4: Made lookAt() more robust. Related to #10849. --- src/math/Matrix4.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/math/Matrix4.js b/src/math/Matrix4.js index 45d01506cf..c716140a67 100644 --- a/src/math/Matrix4.js +++ b/src/math/Matrix4.js @@ -322,7 +322,7 @@ Object.assign( Matrix4.prototype, { var te = this.elements; - z.subVectors( eye, target ).normalize(); + z.subVectors( eye, target ); if ( z.lengthSq() === 0 ) { @@ -330,18 +330,26 @@ Object.assign( Matrix4.prototype, { } - x.crossVectors( up, z ).normalize(); + z.normalize(); + x.crossVectors( up, z ); if ( x.lengthSq() === 0 ) { - z.z += 0.0001; - x.crossVectors( up, z ).normalize(); + var t = Math.PI / 2; + var c = Math.cos( t ) * z.y; + var s = Math.sin( t ) * z.y; + + te[ 0 ] = 1; te[ 4 ] = 0; te[ 8 ] = 0; + te[ 1 ] = 0; te[ 5 ] = c; te[ 9 ] = s; + te[ 2 ] = 0; te[ 6 ] = - s; te[ 10 ] = c; + + return this; } + x.normalize(); y.crossVectors( z, x ); - te[ 0 ] = x.x; te[ 4 ] = y.x; te[ 8 ] = z.x; te[ 1 ] = x.y; te[ 5 ] = y.y; te[ 9 ] = z.y; te[ 2 ] = x.z; te[ 6 ] = y.z; te[ 10 ] = z.z; -- GitLab