Matrix4.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. var Matrix4 = Class.extend
  2. ({
  3. n11: null, n12: null, n13: null, n14: null,
  4. n21: null, n22: null, n23: null, n24: null,
  5. n31: null, n32: null, n33: null, n34: null,
  6. x: null, y: null, z: null,
  7. init: function()
  8. {
  9. this.identity();
  10. },
  11. identity: function()
  12. {
  13. this.n11 = 1; this.n12 = 0; this.n13 = 0; this.n14 = 0;
  14. this.n21 = 0; this.n22 = 1; this.n23 = 0; this.n24 = 0;
  15. this.n31 = 0; this.n32 = 0; this.n33 = 1; this.n34 = 0;
  16. this.x = new Vector3(0,0,0);
  17. this.y = new Vector3(0,0,0);
  18. this.z = new Vector3(0,0,0);
  19. },
  20. lookAt: function(eye, center, up)
  21. {
  22. this.z.sub(center, eye);
  23. this.z.normalize();
  24. this.x.copy(this.z);
  25. this.x.cross(up);
  26. this.x.normalize();
  27. this.y.copy(this.x);
  28. this.y.cross(this.z);
  29. this.y.normalize();
  30. this.y.negate(); //
  31. this.n11 = this.x.x;
  32. this.n12 = this.x.y;
  33. this.n13 = this.x.z;
  34. this.n14 = -this.x.dot(eye);
  35. this.n21 = this.y.x;
  36. this.n22 = this.y.y;
  37. this.n23 = this.y.z;
  38. this.n24 = -this.y.dot(eye);
  39. this.n31 = this.z.x;
  40. this.n32 = this.z.y;
  41. this.n33 = this.z.z;
  42. this.n34 = -this.z.dot(eye);
  43. },
  44. transform: function(v)
  45. {
  46. var vx = v.x, vy = v.y, vz = v.z;
  47. v.x = this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14;
  48. v.y = this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24;
  49. v.z = this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34;
  50. },
  51. multiply: function(a, b)
  52. {
  53. this.n11 = a.n11 * b.n11 + a.n12 * b.n21 + a.n13 * b.n31;
  54. this.n12 = a.n11 * b.n12 + a.n12 * b.n22 + a.n13 * b.n32;
  55. this.n13 = a.n11 * b.n13 + a.n12 * b.n23 + a.n13 * b.n33;
  56. this.n14 = a.n11 * b.n14 + a.n12 * b.n24 + a.n13 * b.n34 + a.n14;
  57. this.n21 = a.n21 * b.n11 + a.n22 * b.n21 + a.n23 * b.n31;
  58. this.n22 = a.n21 * b.n12 + a.n22 * b.n22 + a.n23 * b.n32;
  59. this.n23 = a.n21 * b.n13 + a.n22 * b.n23 + a.n23 * b.n33;
  60. this.n24 = a.n21 * b.n14 + a.n22 * b.n24 + a.n23 * b.n34 + a.n24;
  61. this.n31 = a.n31 * b.n11 + a.n32 * b.n21 + a.n33 * b.n31;
  62. this.n32 = a.n31 * b.n12 + a.n32 * b.n22 + a.n33 * b.n32;
  63. this.n33 = a.n31 * b.n13 + a.n32 * b.n23 + a.n33 * b.n33;
  64. this.n34 = a.n31 * b.n14 + a.n32 * b.n24 + a.n33 * b.n34 + a.n34;
  65. },
  66. multiplySelf: function(m)
  67. {
  68. var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14;
  69. var n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24;
  70. var n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34;
  71. this.n11 = n11 * m.n11 + n12 * m.n21 + n13 * m.n31;
  72. this.n12 = n11 * m.n12 + n12 * m.n22 + n13 * m.n32;
  73. this.n13 = n11 * m.n13 + n12 * m.n23 + n13 * m.n33;
  74. this.n14 = n11 * m.n14 + n12 * m.n24 + n13 * m.n34 + n14;
  75. this.n21 = n21 * m.n11 + n22 * m.n21 + n23 * m.n31;
  76. this.n22 = n21 * m.n12 + n22 * m.n22 + n23 * m.n32;
  77. this.n23 = n21 * m.n13 + n22 * m.n23 + n23 * m.n33;
  78. this.n24 = n21 * m.n14 + n22 * m.n24 + n23 * m.n34 + n24;
  79. this.n31 = n31 * m.n11 + n32 * m.n21 + n33 * m.n31;
  80. this.n32 = n31 * m.n12 + n32 * m.n22 + n33 * m.n32;
  81. this.n33 = n31 * m.n13 + n32 * m.n23 + n33 * m.n33;
  82. this.n34 = n31 * m.n14 + n32 * m.n24 + n33 * m.n34 + n34;
  83. },
  84. clone: function()
  85. {
  86. var m = new Matrix4();
  87. m.n11 = this.n11; m.n12 = this.n12; m.n13 = this.n13; m.n14 = this.n14;
  88. m.n21 = this.n21; m.n22 = this.n22; m.n23 = this.n23; m.n24 = this.n24;
  89. m.n31 = this.n31; m.n32 = this.n32; m.n33 = this.n33; m.n34 = this.n34;
  90. return m;
  91. },
  92. toString: function()
  93. {
  94. return "| " + this.n11 + " " + this.n12 + " " + this.n13 + " " + this.n14 + " |\n" +
  95. "| " + this.n21 + " " + this.n22 + " " + this.n23 + " " + this.n24 + " |\n" +
  96. "| " + this.n31 + " " + this.n32 + " " + this.n33 + " " + this.n34 + " |";
  97. }
  98. });
  99. Matrix4.translationMatrix = function(x, y, z)
  100. {
  101. var m = new Matrix4();
  102. m.n14 = x;
  103. m.n24 = y;
  104. m.n34 = z;
  105. return m;
  106. }
  107. Matrix4.scaleMatrix = function(x, y, z)
  108. {
  109. var m = new Matrix4();
  110. m.n11 = x;
  111. m.n22 = y;
  112. m.n33 = z;
  113. return m;
  114. }
  115. Matrix4.rotationXMatrix = function(theta)
  116. {
  117. var rot = new Matrix4();
  118. rot.n22 = rot.n33 = Math.cos(theta);
  119. rot.n32 = Math.sin(theta);
  120. rot.n23 = -rot.n32;
  121. return rot;
  122. }
  123. Matrix4.rotationYMatrix = function(theta)
  124. {
  125. var rot = new Matrix4();
  126. rot.n11 = rot.n33 = Math.cos(theta);
  127. rot.n13 = Math.sin(theta);
  128. rot.n31 = -rot.n13;
  129. return rot;
  130. }
  131. Matrix4.rotationZMatrix = function(theta)
  132. {
  133. var rot = new Matrix4();
  134. rot.n11 = rot.n22 = Math.cos(theta);
  135. rot.n21 = Math.sin(theta);
  136. rot.n12 = -rot.n21;
  137. return rot;
  138. }
粤ICP备19079148号