Browse Source

Matrix4: Made lookAt() more robust. Related to #10849.

Mr.doob 8 years ago
parent
commit
41e86558d1
1 changed files with 13 additions and 5 deletions
  1. 13 5
      src/math/Matrix4.js

+ 13 - 5
src/math/Matrix4.js

@@ -322,7 +322,7 @@ Object.assign( Matrix4.prototype, {
 
 
 			var te = this.elements;
 			var te = this.elements;
 
 
-			z.subVectors( eye, target ).normalize();
+			z.subVectors( eye, target );
 
 
 			if ( z.lengthSq() === 0 ) {
 			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 ) {
 			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 );
 			y.crossVectors( z, x );
 
 
-
 			te[ 0 ] = x.x; te[ 4 ] = y.x; te[ 8 ] = 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[ 1 ] = x.y; te[ 5 ] = y.y; te[ 9 ] = z.y;
 			te[ 2 ] = x.z; te[ 6 ] = y.z; te[ 10 ] = z.z;
 			te[ 2 ] = x.z; te[ 6 ] = y.z; te[ 10 ] = z.z;