modeling/src/maths/mat4/multiply.js

  1. /**
  2. * Multiplies the two matrices.
  3. *
  4. * @param {mat4} out - receiving matrix
  5. * @param {mat4} a - first operand
  6. * @param {mat4} b - second operand
  7. * @returns {mat4} out
  8. * @alias module:modeling/maths/mat4.multiply
  9. */
  10. const multiply = (out, a, b) => {
  11. const a00 = a[0]
  12. const a01 = a[1]
  13. const a02 = a[2]
  14. const a03 = a[3]
  15. const a10 = a[4]
  16. const a11 = a[5]
  17. const a12 = a[6]
  18. const a13 = a[7]
  19. const a20 = a[8]
  20. const a21 = a[9]
  21. const a22 = a[10]
  22. const a23 = a[11]
  23. const a30 = a[12]
  24. const a31 = a[13]
  25. const a32 = a[14]
  26. const a33 = a[15]
  27. // Cache only the current line of the second matrix
  28. let b0 = b[0]
  29. let b1 = b[1]
  30. let b2 = b[2]
  31. let b3 = b[3]
  32. out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30
  33. out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31
  34. out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32
  35. out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33
  36. b0 = b[4]
  37. b1 = b[5]
  38. b2 = b[6]
  39. b3 = b[7]
  40. out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30
  41. out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31
  42. out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32
  43. out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33
  44. b0 = b[8]
  45. b1 = b[9]
  46. b2 = b[10]
  47. b3 = b[11]
  48. out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30
  49. out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31
  50. out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32
  51. out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33
  52. b0 = b[12]
  53. b1 = b[13]
  54. b2 = b[14]
  55. b3 = b[15]
  56. out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30
  57. out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31
  58. out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32
  59. out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33
  60. return out
  61. }
  62. module.exports = multiply