Created
November 30, 2010 14:02
-
-
Save mrdoob/721717 to your computer and use it in GitHub Desktop.
Matrix4New.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
THREE.Matrix4 = function () { | |
var _x = new THREE.Vector3(), _y = new THREE.Vector3(), _z = new THREE.Vector3(), | |
_array = new Float32Array( [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ] ); | |
this.array = _array; | |
this.copy = function ( m ) { | |
_array[ 0 ] = m.array[ 0 ]; _array[ 1 ] = m.array[ 1 ]; _array[ 2 ] = m.array[ 2 ]; _array[ 3 ] = m.array[ 3 ]; | |
_array[ 4 ] = m.array[ 4 ]; _array[ 5 ] = m.array[ 5 ]; _array[ 6 ] = m.array[ 6 ]; _array[ 7 ] = m.array[ 7 ]; | |
_array[ 8 ] = m.array[ 8 ]; _array[ 9 ] = m.array[ 9 ]; _array[ 10 ] = m.array[ 10 ]; _array[ 11 ] = m.array[ 11 ]; | |
_array[ 12 ] = m.array[ 12 ]; _array[ 13 ] = m.array[ 13 ]; _array[ 14 ] = m.array[ 14 ]; _array[ 15 ] = m.array[ 15 ]; | |
} | |
this.lookAt = function ( eye, center, up ) { | |
_z.sub( eye, center ).normalize(); | |
_x.cross( up, _z ).normalize(); | |
_y.cross( _z, _x ).normalize(); | |
_array[ 0 ] = _x.x; _array[ 1 ] = _x.y; _array[ 2 ] = _x.z; _array[ 3 ] = - _x.dot( eye ); | |
_array[ 4 ] = _y.x; _array[ 5 ] = _y.y; _array[ 6 ] = _y.z; _array[ 7 ] = - _y.dot( eye ); | |
_array[ 8 ] = _z.x; _array[ 9 ] = _z.y; _array[ 10 ] = _z.z; _array[ 11 ] = - _z.dot( eye ); | |
_array[ 12 ] = 0; _array[ 13 ] = 0; _array[ 14 ] = 0; _array[ 15 ] = 1; | |
} | |
this.transform = function ( v ) { | |
var vx = v.x, vy = v.y, vz = v.z, vw = v.w || 1, vwi; | |
v.x = _array[ 0 ] * vx + _array[ 1 ] * vy + _array[ 2 ] * vz + _array[ 3 ] * vw; | |
v.y = _array[ 4 ] * vx + _array[ 5 ] * vy + _array[ 6 ] * vz + _array[ 7 ] * vw; | |
v.z = _array[ 8 ] * vx + _array[ 9 ] * vy + _array[ 10 ] * vz + _array[ 11 ] * vw; | |
vw = _array[ 12 ] * vx + _array[ 13 ] * vy + _array[ 14 ] * vz + _array[ 15 ] * vw; | |
if ( v.w ) { | |
v.w = vw; | |
} else { | |
vwi = 1 / vw; | |
v.x *= vwi; | |
v.y *= vwi; | |
v.z *= vwi; | |
} | |
return v; | |
} | |
this.multiply = function ( a, b ) { | |
_array[ 0 ] = a.array[ 0 ] * b.array[ 0 ] + a.array[ 1 ] * b.array[ 4 ] + a.array[ 2 ] * b.array[ 8 ] + a.array[ 3 ] * b.array[ 12 ]; | |
_array[ 1 ] = a.array[ 0 ] * b.array[ 1 ] + a.array[ 1 ] * b.array[ 5 ] + a.array[ 2 ] * b.array[ 9 ] + a.array[ 3 ] * b.array[ 13 ]; | |
_array[ 2 ] = a.array[ 0 ] * b.array[ 2 ] + a.array[ 1 ] * b.array[ 6 ] + a.array[ 2 ] * b.array[ 10 ] + a.array[ 3 ] * b.array[ 14 ]; | |
_array[ 3 ] = a.array[ 0 ] * b.array[ 3 ] + a.array[ 1 ] * b.array[ 7 ] + a.array[ 2 ] * b.array[ 11 ] + a.array[ 3 ] * b.array[ 15 ]; | |
_array[ 4 ] = a.array[ 4 ] * b.array[ 0 ] + a.array[ 5 ] * b.array[ 4 ] + a.array[ 6 ] * b.array[ 8 ] + a.array[ 7 ] * b.array[ 12 ]; | |
_array[ 5 ] = a.array[ 4 ] * b.array[ 1 ] + a.array[ 5 ] * b.array[ 5 ] + a.array[ 6 ] * b.array[ 9 ] + a.array[ 7 ] * b.array[ 13 ]; | |
_array[ 6 ] = a.array[ 4 ] * b.array[ 2 ] + a.array[ 5 ] * b.array[ 6 ] + a.array[ 6 ] * b.array[ 10 ] + a.array[ 7 ] * b.array[ 14 ]; | |
_array[ 7 ] = a.array[ 4 ] * b.array[ 3 ] + a.array[ 5 ] * b.array[ 7 ] + a.array[ 6 ] * b.array[ 11 ] + a.array[ 7 ] * b.array[ 15 ]; | |
_array[ 8 ] = a.array[ 8 ] * b.array[ 0 ] + a.array[ 9 ] * b.array[ 4 ] + a.array[ 10 ] * b.array[ 8 ] + a.array[ 11 ] * b.array[ 12 ]; | |
_array[ 9 ] = a.array[ 8 ] * b.array[ 1 ] + a.array[ 9 ] * b.array[ 5 ] + a.array[ 10 ] * b.array[ 9 ] + a.array[ 11 ] * b.array[ 13 ]; | |
_array[ 10 ] = a.array[ 8 ] * b.array[ 2 ] + a.array[ 9 ] * b.array[ 6 ] + a.array[ 10 ] * b.array[ 10 ] + a.array[ 11 ] * b.array[ 14 ]; | |
_array[ 11 ] = a.array[ 8 ] * b.array[ 3 ] + a.array[ 9 ] * b.array[ 7 ] + a.array[ 10 ] * b.array[ 11 ] + a.array[ 11 ] * b.array[ 15 ]; | |
_array[ 12 ] = a.array[ 12 ] * b.array[ 0 ] + a.array[ 13 ] * b.array[ 4 ] + a.array[ 14 ] * b.array[ 8 ] + a.array[ 15 ] * b.array[ 12 ]; | |
_array[ 13 ] = a.array[ 12 ] * b.array[ 1 ] + a.array[ 13 ] * b.array[ 5 ] + a.array[ 14 ] * b.array[ 9 ] + a.array[ 15 ] * b.array[ 13 ]; | |
_array[ 14 ] = a.array[ 12 ] * b.array[ 2 ] + a.array[ 13 ] * b.array[ 6 ] + a.array[ 14 ] * b.array[ 10 ] + a.array[ 15 ] * b.array[ 14 ]; | |
_array[ 15 ] = a.array[ 12 ] * b.array[ 3 ] + a.array[ 13 ] * b.array[ 7 ] + a.array[ 14 ] * b.array[ 11 ] + a.array[ 15 ] * b.array[ 15 ]; | |
} | |
this.multiplySelf = function ( m ) { | |
var n11 = _array[ 0 ], n12 = _array[ 1 ], n13 = _array[ 2 ], n14 = _array[ 3 ], | |
n21 = _array[ 4 ], n22 = _array[ 5 ], n23 = _array[ 6 ], n24 = _array[ 7 ], | |
n31 = _array[ 8 ], n32 = _array[ 9 ], n33 = _array[ 10 ], n34 = _array[ 11 ], | |
n41 = _array[ 12 ], n42 = _array[ 13 ], n43 = _array[ 14 ], n44 = _array[ 15 ]; | |
_array[ 0 ] = n11 * m.array[ 0 ] + n12 * m.array[ 4 ] + n13 * m.array[ 8 ] + n14 * m.array[ 12 ]; | |
_array[ 1 ] = n11 * m.array[ 1 ] + n12 * m.array[ 5 ] + n13 * m.array[ 9 ] + n14 * m.array[ 13 ]; | |
_array[ 2 ] = n11 * m.array[ 2 ] + n12 * m.array[ 6 ] + n13 * m.array[ 10 ] + n14 * m.array[ 14 ]; | |
_array[ 3 ] = n11 * m.array[ 3 ] + n12 * m.array[ 7 ] + n13 * m.array[ 11 ] + n14 * m.array[ 15 ]; | |
_array[ 4 ] = n21 * m.array[ 0 ] + n22 * m.array[ 4 ] + n23 * m.array[ 8 ] + n24 * m.array[ 12 ]; | |
_array[ 5 ] = n21 * m.array[ 1 ] + n22 * m.array[ 5 ] + n23 * m.array[ 9 ] + n24 * m.array[ 13 ]; | |
_array[ 6 ] = n21 * m.array[ 2 ] + n22 * m.array[ 6 ] + n23 * m.array[ 10 ] + n24 * m.array[ 14 ]; | |
_array[ 7 ] = n21 * m.array[ 3 ] + n22 * m.array[ 7 ] + n23 * m.array[ 11 ] + n24 * m.array[ 15 ]; | |
_array[ 8 ] = n31 * m.array[ 0 ] + n32 * m.array[ 4 ] + n33 * m.array[ 8 ] + n34 * m.array[ 12 ]; | |
_array[ 9 ] = n31 * m.array[ 1 ] + n32 * m.array[ 5 ] + n33 * m.array[ 9 ] + n34 * m.array[ 13 ]; | |
_array[ 10 ] = n31 * m.array[ 2 ] + n32 * m.array[ 6 ] + n33 * m.array[ 10 ] + n34 * m.array[ 14 ]; | |
_array[ 11 ] = n31 * m.array[ 3 ] + n32 * m.array[ 7 ] + n33 * m.array[ 11 ] + n34 * m.array[ 15 ]; | |
_array[ 12 ] = n41 * m.array[ 0 ] + n42 * m.array[ 4 ] + n43 * m.array[ 8 ] + n44 * m.array[ 12 ]; | |
_array[ 13 ] = n41 * m.array[ 1 ] + n42 * m.array[ 5 ] + n43 * m.array[ 9 ] + n44 * m.array[ 13 ]; | |
_array[ 14 ] = n41 * m.array[ 2 ] + n42 * m.array[ 6 ] + n43 * m.array[ 10 ] + n44 * m.array[ 14 ]; | |
_array[ 15 ] = n41 * m.array[ 3 ] + n42 * m.array[ 7 ] + n43 * m.array[ 11 ] + n44 * m.array[ 15 ]; | |
} | |
}; | |
THREE.Matrix4.translationMatrix = function ( x, y, z ) { | |
var m = new THREE.Matrix4(); | |
m.array[ 3 ] = x; | |
m.array[ 7 ] = y; | |
m.array[ 11 ] = z; | |
return m; | |
}; | |
THREE.Matrix4.scaleMatrix = function ( x, y, z ) { | |
var m = new THREE.Matrix4(); | |
m.array[ 0 ] = x; | |
m.array[ 5 ] = y; | |
m.array[ 10 ] = z; | |
return m; | |
}; | |
THREE.Matrix4.rotationXMatrix = function ( theta ) { | |
var rot = new THREE.Matrix4(); | |
rot.array[ 5 ] = rot.array[ 10 ] = Math.cos( theta ); | |
rot.array[ 9 ] = Math.sin( theta ); | |
rot.array[ 6 ] = - rot.array[ 9 ]; | |
return rot; | |
}; | |
THREE.Matrix4.rotationYMatrix = function ( theta ) { | |
var rot = new THREE.Matrix4(); | |
rot.array[ 0 ] = rot.array[ 10 ] = Math.cos( theta ); | |
rot.array[ 2 ] = Math.sin( theta ); | |
rot.array[ 8 ] = - rot.array[ 2 ]; | |
return rot; | |
}; | |
THREE.Matrix4.rotationZMatrix = function ( theta ) { | |
var rot = new THREE.Matrix4(); | |
rot.array[ 0 ] = rot.array[ 5 ] = Math.cos( theta ); | |
rot.array[ 4 ] = Math.sin( theta ); | |
rot.array[ 1 ] = - rot.array[ 4 ]; | |
return rot; | |
}; | |
THREE.Matrix4.rotationAxisAngleMatrix = function ( axis, angle ) { | |
//Based on http://www.gamedev.net/reference/articles/article1199.asp | |
var rot = new THREE.Matrix4(), | |
c = Math.cos( angle ), | |
s = Math.sin( angle ), | |
t = 1 - c, | |
x = axis.x, y = axis.y, z = axis.z; | |
rot.array[ 0 ] = t * x * x + c; | |
rot.array[ 1 ] = t * x * y - s * z; | |
rot.array[ 2 ] = t * x * z + s * y; | |
rot.array[ 4 ] = t * x * y + s * z; | |
rot.array[ 5 ] = t * y * y + c; | |
rot.array[ 6 ] = t * y * z - s * x; | |
rot.array[ 8 ] = t * x * z - s * y; | |
rot.array[ 9 ] = t * y * z + s * x; | |
rot.array[ 10 ] = t * z * z + c; | |
return rot; | |
}; | |
THREE.Matrix4.makeInvert = function ( m1 ) { | |
//TODO: make this more efficient | |
//( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm ) | |
var m2 = new THREE.Matrix4(); | |
m2.array[ 0 ] = m1.array[ 6 ]*m1.array[ 11 ]*m1.array[ 13 ] - m1.array[ 7 ]*m1.array[ 10 ]*m1.array[ 13 ] + m1.array[ 7 ]*m1.array[ 9 ]*m1.array[ 14 ] - m1.array[ 5 ]*m1.array[ 11 ]*m1.array[ 14 ] - m1.array[ 6 ]*m1.array[ 9 ]*m1.array[ 15 ] + m1.array[ 5 ]*m1.array[ 10 ]*m1.array[ 15 ]; | |
m2.array[ 1 ] = m1.array[ 3 ]*m1.array[ 10 ]*m1.array[ 13 ] - m1.array[ 2 ]*m1.array[ 11 ]*m1.array[ 13 ] - m1.array[ 3 ]*m1.array[ 9 ]*m1.array[ 14 ] + m1.array[ 1 ]*m1.array[ 11 ]*m1.array[ 14 ] + m1.array[ 2 ]*m1.array[ 9 ]*m1.array[ 15 ] - m1.array[ 1 ]*m1.array[ 10 ]*m1.array[ 15 ]; | |
m2.array[ 2 ] = m1.array[ 2 ]*m1.array[ 7 ]*m1.array[ 13 ] - m1.array[ 3 ]*m1.array[ 6 ]*m1.array[ 13 ] + m1.array[ 3 ]*m1.array[ 5 ]*m1.array[ 14 ] - m1.array[ 1 ]*m1.array[ 7 ]*m1.array[ 14 ] - m1.array[ 2 ]*m1.array[ 5 ]*m1.array[ 15 ] + m1.array[ 1 ]*m1.array[ 6 ]*m1.array[ 15 ]; | |
m2.array[ 3 ] = m1.array[ 3 ]*m1.array[ 6 ]*m1.array[ 9 ] - m1.array[ 2 ]*m1.array[ 7 ]*m1.array[ 9 ] - m1.array[ 3 ]*m1.array[ 5 ]*m1.array[ 10 ] + m1.array[ 1 ]*m1.array[ 7 ]*m1.array[ 10 ] + m1.array[ 2 ]*m1.array[ 5 ]*m1.array[ 11 ] - m1.array[ 1 ]*m1.array[ 6 ]*m1.array[ 11 ]; | |
m2.array[ 4 ] = m1.array[ 7 ]*m1.array[ 10 ]*m1.array[ 12 ] - m1.array[ 6 ]*m1.array[ 11 ]*m1.array[ 12 ] - m1.array[ 7 ]*m1.array[ 8 ]*m1.array[ 14 ] + m1.array[ 4 ]*m1.array[ 11 ]*m1.array[ 14 ] + m1.array[ 6 ]*m1.array[ 8 ]*m1.array[ 15 ] - m1.array[ 4 ]*m1.array[ 10 ]*m1.array[ 15 ]; | |
m2.array[ 5 ] = m1.array[ 2 ]*m1.array[ 11 ]*m1.array[ 12 ] - m1.array[ 3 ]*m1.array[ 10 ]*m1.array[ 12 ] + m1.array[ 3 ]*m1.array[ 8 ]*m1.array[ 14 ] - m1.array[ 0 ]*m1.array[ 11 ]*m1.array[ 14 ] - m1.array[ 2 ]*m1.array[ 8 ]*m1.array[ 15 ] + m1.array[ 0 ]*m1.array[ 10 ]*m1.array[ 15 ]; | |
m2.array[ 6 ] = m1.array[ 3 ]*m1.array[ 6 ]*m1.array[ 12 ] - m1.array[ 2 ]*m1.array[ 7 ]*m1.array[ 12 ] - m1.array[ 3 ]*m1.array[ 4 ]*m1.array[ 14 ] + m1.array[ 0 ]*m1.array[ 7 ]*m1.array[ 14 ] + m1.array[ 2 ]*m1.array[ 4 ]*m1.array[ 15 ] - m1.array[ 0 ]*m1.array[ 6 ]*m1.array[ 15 ]; | |
m2.array[ 7 ] = m1.array[ 2 ]*m1.array[ 7 ]*m1.array[ 8 ] - m1.array[ 3 ]*m1.array[ 6 ]*m1.array[ 8 ] + m1.array[ 3 ]*m1.array[ 4 ]*m1.array[ 10 ] - m1.array[ 0 ]*m1.array[ 7 ]*m1.array[ 10 ] - m1.array[ 2 ]*m1.array[ 4 ]*m1.array[ 11 ] + m1.array[ 0 ]*m1.array[ 6 ]*m1.array[ 11 ]; | |
m2.array[ 8 ] = m1.array[ 5 ]*m1.array[ 11 ]*m1.array[ 12 ] - m1.array[ 7 ]*m1.array[ 9 ]*m1.array[ 12 ] + m1.array[ 7 ]*m1.array[ 8 ]*m1.array[ 13 ] - m1.array[ 4 ]*m1.array[ 11 ]*m1.array[ 13 ] - m1.array[ 5 ]*m1.array[ 8 ]*m1.array[ 15 ] + m1.array[ 4 ]*m1.array[ 9 ]*m1.array[ 15 ]; | |
m2.array[ 9 ] = m1.array[ 3 ]*m1.array[ 9 ]*m1.array[ 12 ] - m1.array[ 1 ]*m1.array[ 11 ]*m1.array[ 12 ] - m1.array[ 3 ]*m1.array[ 8 ]*m1.array[ 13 ] + m1.array[ 0 ]*m1.array[ 11 ]*m1.array[ 13 ] + m1.array[ 1 ]*m1.array[ 8 ]*m1.array[ 15 ] - m1.array[ 0 ]*m1.array[ 9 ]*m1.array[ 15 ]; | |
m2.array[ 10 ] = m1.array[ 2 ]*m1.array[ 7 ]*m1.array[ 12 ] - m1.array[ 3 ]*m1.array[ 5 ]*m1.array[ 12 ] + m1.array[ 3 ]*m1.array[ 4 ]*m1.array[ 13 ] - m1.array[ 0 ]*m1.array[ 7 ]*m1.array[ 13 ] - m1.array[ 1 ]*m1.array[ 4 ]*m1.array[ 15 ] + m1.array[ 0 ]*m1.array[ 5 ]*m1.array[ 15 ]; | |
m2.array[ 11 ] = m1.array[ 3 ]*m1.array[ 5 ]*m1.array[ 8 ] - m1.array[ 1 ]*m1.array[ 7 ]*m1.array[ 8 ] - m1.array[ 3 ]*m1.array[ 4 ]*m1.array[ 9 ] + m1.array[ 0 ]*m1.array[ 7 ]*m1.array[ 9 ] + m1.array[ 1 ]*m1.array[ 4 ]*m1.array[ 11 ] - m1.array[ 0 ]*m1.array[ 5 ]*m1.array[ 11 ]; | |
m2.array[ 12 ] = m1.array[ 6 ]*m1.array[ 9 ]*m1.array[ 12 ] - m1.array[ 5 ]*m1.array[ 10 ]*m1.array[ 12 ] - m1.array[ 6 ]*m1.array[ 8 ]*m1.array[ 13 ] + m1.array[ 4 ]*m1.array[ 10 ]*m1.array[ 13 ] + m1.array[ 5 ]*m1.array[ 8 ]*m1.array[ 14 ] - m1.array[ 4 ]*m1.array[ 9 ]*m1.array[ 14 ]; | |
m2.array[ 13 ] = m1.array[ 1 ]*m1.array[ 10 ]*m1.array[ 12 ] - m1.array[ 2 ]*m1.array[ 9 ]*m1.array[ 12 ] + m1.array[ 2 ]*m1.array[ 8 ]*m1.array[ 13 ] - m1.array[ 0 ]*m1.array[ 10 ]*m1.array[ 13 ] - m1.array[ 1 ]*m1.array[ 8 ]*m1.array[ 14 ] + m1.array[ 0 ]*m1.array[ 9 ]*m1.array[ 14 ]; | |
m2.array[ 14 ] = m1.array[ 2 ]*m1.array[ 5 ]*m1.array[ 12 ] - m1.array[ 1 ]*m1.array[ 6 ]*m1.array[ 12 ] - m1.array[ 2 ]*m1.array[ 4 ]*m1.array[ 13 ] + m1.array[ 0 ]*m1.array[ 6 ]*m1.array[ 13 ] + m1.array[ 1 ]*m1.array[ 4 ]*m1.array[ 14 ] - m1.array[ 0 ]*m1.array[ 5 ]*m1.array[ 14 ]; | |
m2.array[ 15 ] = m1.array[ 1 ]*m1.array[ 6 ]*m1.array[ 8 ] - m1.array[ 2 ]*m1.array[ 5 ]*m1.array[ 8 ] + m1.array[ 2 ]*m1.array[ 4 ]*m1.array[ 9 ] - m1.array[ 0 ]*m1.array[ 6 ]*m1.array[ 9 ] - m1.array[ 1 ]*m1.array[ 4 ]*m1.array[ 10 ] + m1.array[ 0 ]*m1.array[ 5 ]*m1.array[ 10 ]; | |
m2.multiplyScalar( 1 / m1.determinant() ); | |
return m2; | |
}; | |
THREE.Matrix4.makeInvert3x3 = function ( m1 ) { | |
// input: THREE.Matrix4, output: THREE.Matrix3 | |
// ( based on http://code.google.com/p/webgl-mjs/ ) | |
var m = m1.flatten(), | |
m2 = new THREE.Matrix3(), | |
a11 = m[ 10 ] * m[ 5 ] - m[ 6 ] * m[ 9 ], | |
a21 = - m[ 10 ] * m[ 1 ] + m[ 2 ] * m[ 9 ], | |
a31 = m[ 6 ] * m[ 1 ] - m[ 2 ] * m[ 5 ], | |
a12 = - m[ 10 ] * m[ 4 ] + m[ 6 ] * m[ 8 ], | |
a22 = m[ 10 ] * m[ 0 ] - m[ 2 ] * m[ 8 ], | |
a32 = - m[ 6 ] * m[ 0 ] + m[ 2 ] * m[ 4 ], | |
a13 = m[ 9 ] * m[ 4 ] - m[ 5 ] * m[ 8 ], | |
a23 = - m[ 9 ] * m[ 0 ] + m[ 1 ] * m[ 8 ], | |
a33 = m[ 5 ] * m[ 0 ] - m[ 1 ] * m[ 4 ], | |
det = m[ 0 ] * ( a11 ) + m[ 1 ] * ( a12 ) + m[ 2 ] * ( a13 ), | |
idet; | |
// no inverse | |
if (det == 0) throw "matrix not invertible"; | |
idet = 1.0 / det; | |
m2.m[ 0 ] = idet * a11; m2.m[ 1 ] = idet * a21; m2.m[ 2 ] = idet * a31; | |
m2.m[ 3 ] = idet * a12; m2.m[ 4 ] = idet * a22; m2.m[ 5 ] = idet * a32; | |
m2.m[ 6 ] = idet * a13; m2.m[ 7 ] = idet * a23; m2.m[ 8 ] = idet * a33; | |
return m2; | |
} | |
THREE.Matrix4.makeFrustum = function( left, right, bottom, top, near, far ) { | |
var m, x, y, a, b, c, d; | |
m = new THREE.Matrix4(); | |
x = 2 * near / ( right - left ); | |
y = 2 * near / ( top - bottom ); | |
a = ( right + left ) / ( right - left ); | |
b = ( top + bottom ) / ( top - bottom ); | |
c = - ( far + near ) / ( far - near ); | |
d = - 2 * far * near / ( far - near ); | |
m.array[ 0 ] = x; m.array[ 1 ] = 0; m.array[ 2 ] = a; m.array[ 3 ] = 0; | |
m.array[ 4 ] = 0; m.array[ 5 ] = y; m.array[ 6 ] = b; m.array[ 7 ] = 0; | |
m.array[ 8 ] = 0; m.array[ 9 ] = 0; m.array[ 10 ] = c; m.array[ 11 ] = d; | |
m.array[ 12 ] = 0; m.array[ 13 ] = 0; m.array[ 14 ] = - 1; m.array[ 15 ] = 0; | |
return m; | |
}; | |
THREE.Matrix4.makePerspective = function( fov, aspect, near, far ) { | |
var ymax, ymin, xmin, xmax; | |
ymax = near * Math.tan( fov * Math.PI / 360 ); | |
ymin = - ymax; | |
xmin = ymin * aspect; | |
xmax = ymax * aspect; | |
return THREE.Matrix4.makeFrustum( xmin, xmax, ymin, ymax, near, far ); | |
}; | |
THREE.Matrix4.makeOrtho = function( left, right, top, bottom, near, far ) { | |
var m, x, y, z, w, h, p; | |
m = new THREE.Matrix4(); | |
w = right - left; | |
h = top - bottom; | |
p = far - near; | |
x = ( right + left ) / w; | |
y = ( top + bottom ) / h; | |
z = ( far + near ) / p; | |
m.array[ 0 ] = 2 / w; m.array[ 1 ] = 0; m.array[ 2 ] = 0; m.array[ 3 ] = -x; | |
m.array[ 4 ] = 0; m.array[ 5 ] = 2 / h; m.array[ 6 ] = 0; m.array[ 7 ] = -y; | |
m.array[ 8 ] = 0; m.array[ 9 ] = 0; m.array[ 10 ] = -2 / p; m.array[ 11 ] = -z; | |
m.array[ 12 ] = 0; m.array[ 13 ] = 0; m.array[ 14 ] = 0; m.array[ 15 ] = 1; | |
return m; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment