Matrix4.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * @author supereggbert / http://www.paulbrunt.co.uk/
  3. * @author mr.doob / http://mrdoob.com/
  4. */
  5. THREE.Matrix4 = function () {
  6. var x, y, z;
  7. x = new THREE.Vector3();
  8. y = new THREE.Vector3();
  9. z = new THREE.Vector3();
  10. this.n11 = 1; this.n12 = 0; this.n13 = 0; this.n14 = 0;
  11. this.n21 = 0; this.n22 = 1; this.n23 = 0; this.n24 = 0;
  12. this.n31 = 0; this.n32 = 0; this.n33 = 1; this.n34 = 0;
  13. this.n41 = 0; this.n42 = 0; this.n43 = 0; this.n44 = 1;
  14. this.identity = function () {
  15. this.n11 = 1; this.n12 = 0; this.n13 = 0; this.n14 = 0;
  16. this.n21 = 0; this.n22 = 1; this.n23 = 0; this.n24 = 0;
  17. this.n31 = 0; this.n32 = 0; this.n33 = 1; this.n34 = 0;
  18. this.n41 = 0; this.n42 = 0; this.n43 = 0; this.n44 = 1;
  19. };
  20. this.lookAt = function ( eye, center, up ) {
  21. z.sub( eye, center );
  22. z.normalize();
  23. x.cross( up, z );
  24. x.normalize();
  25. y.cross( z, x );
  26. y.normalize();
  27. y.negate();
  28. this.n11 = x.x; this.n12 = x.y; this.n13 = x.z; this.n14 = - x.dot( eye );
  29. this.n21 = y.x; this.n22 = y.y; this.n23 = y.z; this.n24 = - y.dot( eye );
  30. this.n31 = z.x; this.n32 = z.y; this.n33 = z.z; this.n34 = - z.dot( eye );
  31. };
  32. this.transform = function ( v ) {
  33. var vx = v.x, vy = v.y, vz = v.z, vw = v.w ? v.w : 1.0;
  34. v.x = this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 * vw;
  35. v.y = this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 * vw;
  36. v.z = this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 * vw;
  37. vw = this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 * vw;
  38. if( v.w ) {
  39. v.w = vw;
  40. } else {
  41. v.x = v.x / vw;
  42. v.y = v.y / vw;
  43. v.z = v.z / vw;
  44. }
  45. };
  46. this.crossVector = function ( a ) {
  47. var v = new THREE.Vector4();
  48. v.x = this.n11 * a.x + this.n12 * a.y + this.n13 * a.z + this.n14 * a.w;
  49. v.y = this.n21 * a.x + this.n22 * a.y + this.n23 * a.z + this.n24 * a.w;
  50. v.z = this.n31 * a.x + this.n32 * a.y + this.n33 * a.z + this.n34 * a.w;
  51. v.w = ( a.w ) ? this.n41 * a.x + this.n42 * a.y + this.n43 * a.z + this.n44 * a.w : 1;
  52. return v;
  53. };
  54. this.multiply = function ( a, b ) {
  55. this.n11 = a.n11 * b.n11 + a.n12 * b.n21 + a.n13 * b.n31 + a.n14 * b.n41;
  56. this.n12 = a.n11 * b.n12 + a.n12 * b.n22 + a.n13 * b.n32 + a.n14 * b.n42;
  57. this.n13 = a.n11 * b.n13 + a.n12 * b.n23 + a.n13 * b.n33 + a.n14 * b.n43;
  58. this.n14 = a.n11 * b.n14 + a.n12 * b.n24 + a.n13 * b.n34 + a.n14 * b.n44;
  59. this.n21 = a.n21 * b.n11 + a.n22 * b.n21 + a.n23 * b.n31 + a.n24 * b.n41;
  60. this.n22 = a.n21 * b.n12 + a.n22 * b.n22 + a.n23 * b.n32 + a.n24 * b.n42;
  61. this.n23 = a.n21 * b.n13 + a.n22 * b.n23 + a.n23 * b.n33 + a.n24 * b.n43;
  62. this.n24 = a.n21 * b.n14 + a.n22 * b.n24 + a.n23 * b.n34 + a.n24 * b.n44;
  63. this.n31 = a.n31 * b.n11 + a.n32 * b.n21 + a.n33 * b.n31 + a.n34 * b.n41;
  64. this.n32 = a.n31 * b.n12 + a.n32 * b.n22 + a.n33 * b.n32 + a.n34 * b.n42;
  65. this.n33 = a.n31 * b.n13 + a.n32 * b.n23 + a.n33 * b.n33 + a.n34 * b.n43;
  66. this.n34 = a.n31 * b.n14 + a.n32 * b.n24 + a.n33 * b.n34 + a.n34 * b.n44;
  67. this.n41 = a.n41 * b.n11 + a.n42 * b.n21 + a.n43 * b.n31 + a.n44 * b.n41;
  68. this.n42 = a.n41 * b.n12 + a.n42 * b.n22 + a.n43 * b.n32 + a.n44 * b.n42;
  69. this.n43 = a.n41 * b.n13 + a.n42 * b.n23 + a.n43 * b.n33 + a.n44 * b.n43;
  70. this.n44 = a.n41 * b.n14 + a.n42 * b.n24 + a.n43 * b.n34 + a.n44 * b.n44;
  71. };
  72. this.multiplySelf = function ( m ) {
  73. var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14,
  74. n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24,
  75. n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34,
  76. n41 = this.n41, n42 = this.n42, n43 = this.n43, n44 = this.n44;
  77. this.n11 = n11 * m.n11 + n12 * m.n21 + n13 * m.n31 + n14 * m.n41;
  78. this.n12 = n11 * m.n12 + n12 * m.n22 + n13 * m.n32 + n14 * m.n42;
  79. this.n13 = n11 * m.n13 + n12 * m.n23 + n13 * m.n33 + n14 * m.n43;
  80. this.n14 = n11 * m.n14 + n12 * m.n24 + n13 * m.n34 + n14 * m.n44;
  81. this.n21 = n21 * m.n11 + n22 * m.n21 + n23 * m.n31 + n24 * m.n41;
  82. this.n22 = n21 * m.n12 + n22 * m.n22 + n23 * m.n32 + n24 * m.n42;
  83. this.n23 = n21 * m.n13 + n22 * m.n23 + n23 * m.n33 + n24 * m.n43;
  84. this.n24 = n21 * m.n14 + n22 * m.n24 + n23 * m.n34 + n24 * m.n44;
  85. this.n31 = n31 * m.n11 + n32 * m.n21 + n33 * m.n31 + n34 * m.n41;
  86. this.n32 = n31 * m.n12 + n32 * m.n22 + n33 * m.n32 + n34 * m.n42;
  87. this.n33 = n31 * m.n13 + n32 * m.n23 + n33 * m.n33 + n34 * m.n43;
  88. this.n34 = n31 * m.n14 + n32 * m.n24 + n33 * m.n34 + n34 * m.n44;
  89. this.n41 = n41 * m.n11 + n42 * m.n21 + n43 * m.n31 + n44 * m.n41;
  90. this.n42 = n41 * m.n12 + n42 * m.n22 + n43 * m.n32 + n44 * m.n42;
  91. this.n43 = n41 * m.n13 + n42 * m.n23 + n43 * m.n33 + n44 * m.n43;
  92. this.n44 = n41 * m.n14 + n42 * m.n24 + n43 * m.n34 + n44 * m.n44;
  93. };
  94. this.clone = function () {
  95. var m = new THREE.Matrix4();
  96. m.n11 = this.n11; m.n12 = this.n12; m.n13 = this.n13; m.n14 = this.n14;
  97. m.n21 = this.n21; m.n22 = this.n22; m.n23 = this.n23; m.n24 = this.n24;
  98. m.n31 = this.n31; m.n32 = this.n32; m.n33 = this.n33; m.n34 = this.n34;
  99. m.n41 = this.n41; m.n42 = this.n42; m.n43 = this.n43; m.n44 = this.n44;
  100. return m;
  101. };
  102. this.toString = function() {
  103. return "| " + this.n11 + " " + this.n12 + " " + this.n13 + " " + this.n14 + " |\n" +
  104. "| " + this.n21 + " " + this.n22 + " " + this.n23 + " " + this.n24 + " |\n" +
  105. "| " + this.n31 + " " + this.n32 + " " + this.n33 + " " + this.n34 + " |\n" +
  106. "| " + this.n41 + " " + this.n42 + " " + this.n43 + " " + this.n44 + " |";
  107. };
  108. };
  109. THREE.Matrix4.translationMatrix = function ( x, y, z ) {
  110. var m = new THREE.Matrix4();
  111. m.n14 = x;
  112. m.n24 = y;
  113. m.n34 = z;
  114. return m;
  115. };
  116. THREE.Matrix4.scaleMatrix = function ( x, y, z ) {
  117. var m = new THREE.Matrix4();
  118. m.n11 = x;
  119. m.n22 = y;
  120. m.n33 = z;
  121. return m;
  122. };
  123. THREE.Matrix4.rotationXMatrix = function ( theta ) {
  124. var rot = new THREE.Matrix4();
  125. rot.n22 = rot.n33 = Math.cos( theta );
  126. rot.n32 = Math.sin( theta );
  127. rot.n23 = - rot.n32;
  128. return rot;
  129. };
  130. THREE.Matrix4.rotationYMatrix = function ( theta ) {
  131. var rot = new THREE.Matrix4();
  132. rot.n11 = rot.n33 = Math.cos( theta );
  133. rot.n13 = Math.sin( theta );
  134. rot.n31 = - rot.n13;
  135. return rot;
  136. };
  137. THREE.Matrix4.rotationZMatrix = function( theta ) {
  138. var rot = new THREE.Matrix4();
  139. rot.n11 = rot.n22 = Math.cos( theta );
  140. rot.n21 = Math.sin( theta );
  141. rot.n12 = - rot.n21;
  142. return rot;
  143. };
  144. THREE.Matrix4.makeFrustum = function( left, right, bottom, top, near, far ) {
  145. var m, x, y, a, b, c, d;
  146. m = new THREE.Matrix4();
  147. x = 2 * near / ( right - left );
  148. y = 2 * near / ( top - bottom );
  149. a = ( right + left ) / ( right - left );
  150. b = ( top + bottom ) / ( top - bottom );
  151. c = - ( far + near ) / ( far - near );
  152. d = - 2 * far * near / ( far - near );
  153. m.n11 = x; m.n13 = a;
  154. m.n22 = y; m.n23 = b;
  155. m.n33 = c; m.n34 = d;
  156. m.n43 = - 1; m.n44 = 0;
  157. return m;
  158. };
  159. THREE.Matrix4.makePerspective = function( fov, aspect, near, far ) {
  160. var ymax, ymin, xmin, xmax;
  161. ymax = near * Math.tan( fov * Math.PI / 360 );
  162. ymin = - ymax;
  163. xmin = ymin * aspect;
  164. xmax = ymax * aspect;
  165. return THREE.Matrix4.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  166. };