提交 d30c3703 编写于 作者: M Mr.doob

Code clean up and now using inv* pattern.

上级 9949be37
...@@ -199,9 +199,11 @@ THREE.Projector = function () { ...@@ -199,9 +199,11 @@ THREE.Projector = function () {
_vertex.positionWorld.copy( vertices[ v ] ).applyMatrix4( _modelMatrix ); _vertex.positionWorld.copy( vertices[ v ] ).applyMatrix4( _modelMatrix );
_vertex.positionScreen.copy( _vertex.positionWorld ).applyMatrix4( _viewProjectionMatrix ); _vertex.positionScreen.copy( _vertex.positionWorld ).applyMatrix4( _viewProjectionMatrix );
_vertex.positionScreen.x /= _vertex.positionScreen.w; var invW = 1 / _vertex.positionScreen.w;
_vertex.positionScreen.y /= _vertex.positionScreen.w;
_vertex.positionScreen.z /= _vertex.positionScreen.w; _vertex.positionScreen.x *= invW;
_vertex.positionScreen.y *= invW;
_vertex.positionScreen.z *= invW;
_vertex.visible = ! ( _vertex.positionScreen.x < -1 || _vertex.positionScreen.x > 1 || _vertex.visible = ! ( _vertex.positionScreen.x < -1 || _vertex.positionScreen.x > 1 ||
_vertex.positionScreen.y < -1 || _vertex.positionScreen.y > 1 || _vertex.positionScreen.y < -1 || _vertex.positionScreen.y > 1 ||
...@@ -432,14 +434,16 @@ THREE.Projector = function () { ...@@ -432,14 +434,16 @@ THREE.Projector = function () {
_vector4.set( _modelMatrix.elements[12], _modelMatrix.elements[13], _modelMatrix.elements[14], 1 ); _vector4.set( _modelMatrix.elements[12], _modelMatrix.elements[13], _modelMatrix.elements[14], 1 );
_vector4.applyMatrix4( _viewProjectionMatrix ); _vector4.applyMatrix4( _viewProjectionMatrix );
_vector4.z /= _vector4.w; var invW = 1 / _vector4.w;
_vector4.z *= invW;
if ( _vector4.z > 0 && _vector4.z < 1 ) { if ( _vector4.z > 0 && _vector4.z < 1 ) {
_particle = getNextParticleInPool(); _particle = getNextParticleInPool();
_particle.id = object.id; _particle.id = object.id;
_particle.x = _vector4.x / _vector4.w; _particle.x = _vector4.x * invW;
_particle.y = _vector4.y / _vector4.w; _particle.y = _vector4.y * invW;
_particle.z = _vector4.z; _particle.z = _vector4.z;
_particle.object = object; _particle.object = object;
......
...@@ -847,17 +847,21 @@ THREE.Matrix4.prototype = { ...@@ -847,17 +847,21 @@ THREE.Matrix4.prototype = {
matrix.elements.set( this.elements ); // at this point matrix is incomplete so we can't use .copy() matrix.elements.set( this.elements ); // at this point matrix is incomplete so we can't use .copy()
matrix.elements[0] /= sx; var invSX = 1 / sx;
matrix.elements[1] /= sx; var invSY = 1 / sy;
matrix.elements[2] /= sx; var invSZ = 1 / sz;
matrix.elements[4] /= sy; matrix.elements[0] *= invSX;
matrix.elements[5] /= sy; matrix.elements[1] *= invSX;
matrix.elements[6] /= sy; matrix.elements[2] *= invSX;
matrix.elements[8] /= sz; matrix.elements[4] *= invSY;
matrix.elements[9] /= sz; matrix.elements[5] *= invSY;
matrix.elements[10] /= sz; matrix.elements[6] *= invSY;
matrix.elements[8] *= invSZ;
matrix.elements[9] *= invSZ;
matrix.elements[10] *= invSZ;
quaternion.setFromRotationMatrix( matrix ); quaternion.setFromRotationMatrix( matrix );
......
...@@ -112,7 +112,8 @@ THREE.Ray.prototype = { ...@@ -112,7 +112,8 @@ THREE.Ray.prototype = {
var c = diff.lengthSq(); var c = diff.lengthSq();
var det = Math.abs( 1 - a01 * a01 ); var det = Math.abs( 1 - a01 * a01 );
var s0, s1, sqrDist, extDet; var s0, s1, sqrDist, extDet;
if (det >= 0) {
if ( det >= 0 ) {
// The ray and segment are not parallel. // The ray and segment are not parallel.
...@@ -120,11 +121,11 @@ THREE.Ray.prototype = { ...@@ -120,11 +121,11 @@ THREE.Ray.prototype = {
s1 = a01 * b0 - b1; s1 = a01 * b0 - b1;
extDet = segExtent * det; extDet = segExtent * det;
if (s0 >= 0) { if ( s0 >= 0 ) {
if (s1 >= -extDet) { if ( s1 >= - extDet ) {
if (s1 <= extDet) { if ( s1 <= extDet ) {
// region 0 // region 0
// Minimum at interior points of ray and segment. // Minimum at interior points of ray and segment.
...@@ -144,9 +145,7 @@ THREE.Ray.prototype = { ...@@ -144,9 +145,7 @@ THREE.Ray.prototype = {
} }
} } else {
else {
// region 5 // region 5
...@@ -166,7 +165,7 @@ THREE.Ray.prototype = { ...@@ -166,7 +165,7 @@ THREE.Ray.prototype = {
s1 = ( s0 > 0 ) ? - segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent ); s1 = ( s0 > 0 ) ? - segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
} else if (s1 <= extDet) { } else if ( s1 <= extDet ) {
// region 3 // region 3
...@@ -174,7 +173,7 @@ THREE.Ray.prototype = { ...@@ -174,7 +173,7 @@ THREE.Ray.prototype = {
s1 = Math.min( Math.max( - segExtent, - b1 ), segExtent ); s1 = Math.min( Math.max( - segExtent, - b1 ), segExtent );
sqrDist = s1 * ( s1 + 2 * b1 ) + c; sqrDist = s1 * ( s1 + 2 * b1 ) + c;
} else { } else {
// region 2 // region 2
...@@ -186,7 +185,7 @@ THREE.Ray.prototype = { ...@@ -186,7 +185,7 @@ THREE.Ray.prototype = {
} }
} else { } else {
// Ray and segment are parallel. // Ray and segment are parallel.
......
...@@ -143,16 +143,19 @@ THREE.Vector2.prototype = { ...@@ -143,16 +143,19 @@ THREE.Vector2.prototype = {
}, },
divideScalar: function ( s ) { divideScalar: function ( scalar ) {
if ( s !== 0 ) { if ( scalar !== 0 ) {
this.x /= s; var invScalar = 1 / scalar;
this.y /= s;
this.x *= invScalar;
this.y *= invScalar;
} else { } else {
this.set( 0, 0 ); this.x = 0;
this.y = 0;
} }
......
...@@ -170,11 +170,11 @@ THREE.Vector3.prototype = { ...@@ -170,11 +170,11 @@ THREE.Vector3.prototype = {
}, },
multiplyScalar: function ( s ) { multiplyScalar: function ( scalar ) {
this.x *= s; this.x *= scalar;
this.y *= s; this.y *= scalar;
this.z *= s; this.z *= scalar;
return this; return this;
...@@ -296,13 +296,15 @@ THREE.Vector3.prototype = { ...@@ -296,13 +296,15 @@ THREE.Vector3.prototype = {
}, },
divideScalar: function ( s ) { divideScalar: function ( scalar ) {
if ( s !== 0 ) { if ( scalar !== 0 ) {
this.x /= s; var invScalar = 1 / scalar;
this.y /= s;
this.z /= s; this.x *= invScalar;
this.y *= invScalar;
this.z *= invScalar;
} else { } else {
......
...@@ -170,12 +170,12 @@ THREE.Vector4.prototype = { ...@@ -170,12 +170,12 @@ THREE.Vector4.prototype = {
}, },
multiplyScalar: function ( s ) { multiplyScalar: function ( scalar ) {
this.x *= s; this.x *= scalar;
this.y *= s; this.y *= scalar;
this.z *= s; this.z *= scalar;
this.w *= s; this.w *= scalar;
return this; return this;
...@@ -199,14 +199,16 @@ THREE.Vector4.prototype = { ...@@ -199,14 +199,16 @@ THREE.Vector4.prototype = {
}, },
divideScalar: function ( s ) { divideScalar: function ( scalar ) {
if ( s !== 0 ) { if ( scalar !== 0 ) {
this.x /= s; var invScalar = 1 / scalar;
this.y /= s;
this.z /= s; this.x *= invScalar;
this.w /= s; this.y *= invScalar;
this.z *= invScalar;
this.w *= invScalar;
} else { } else {
...@@ -524,6 +526,7 @@ THREE.Vector4.prototype = { ...@@ -524,6 +526,7 @@ THREE.Vector4.prototype = {
if ( oldLength !== 0 && l !== oldLength ) { if ( oldLength !== 0 && l !== oldLength ) {
this.multiplyScalar( l / oldLength ); this.multiplyScalar( l / oldLength );
} }
return this; return this;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册