modeling/src/maths/mat4/rotateY.js

  1. const { sin, cos } = require('../utils/trigonometry')
  2. /**
  3. * Rotates a matrix by the given angle around the Y axis.
  4. *
  5. * @param {mat4} out - receiving matrix
  6. * @param {mat4} matrix - matrix to rotate
  7. * @param {Number} radians - angle to rotate the matrix by
  8. * @returns {mat4} out
  9. * @alias module:modeling/maths/mat4.rotateY
  10. */
  11. const rotateY = (out, matrix, radians) => {
  12. const s = sin(radians)
  13. const c = cos(radians)
  14. const a00 = matrix[0]
  15. const a01 = matrix[1]
  16. const a02 = matrix[2]
  17. const a03 = matrix[3]
  18. const a20 = matrix[8]
  19. const a21 = matrix[9]
  20. const a22 = matrix[10]
  21. const a23 = matrix[11]
  22. if (matrix !== out) { // If the source and destination differ, copy the unchanged rows
  23. out[4] = matrix[4]
  24. out[5] = matrix[5]
  25. out[6] = matrix[6]
  26. out[7] = matrix[7]
  27. out[12] = matrix[12]
  28. out[13] = matrix[13]
  29. out[14] = matrix[14]
  30. out[15] = matrix[15]
  31. }
  32. // Perform axis-specific matrix multiplication
  33. out[0] = a00 * c - a20 * s
  34. out[1] = a01 * c - a21 * s
  35. out[2] = a02 * c - a22 * s
  36. out[3] = a03 * c - a23 * s
  37. out[8] = a00 * s + a20 * c
  38. out[9] = a01 * s + a21 * c
  39. out[10] = a02 * s + a22 * c
  40. out[11] = a03 * s + a23 * c
  41. return out
  42. }
  43. module.exports = rotateY