Source: ui/gl_matrix/matrix_quaternion.js

  1. /*! @license
  2. * glMatrix: https://github.com/toji/gl-matrix/
  3. * Copyright 2015-2021, Brandon Jones, Colin MacKenzie IV
  4. * SPDX-License-Identifier: MIT
  5. */
  6. goog.provide('shaka.ui.MatrixQuaternion');
  7. /**
  8. * Quaternion in the format XYZW
  9. */
  10. shaka.ui.MatrixQuaternion = class {
  11. /**
  12. * Creates a new identity quaternion
  13. *
  14. * @return {!Float32Array} a new quaternion
  15. */
  16. static create() {
  17. const out = new Float32Array(4);
  18. out[3] = 1;
  19. return out;
  20. }
  21. /**
  22. * Normalize a quaternion
  23. *
  24. * @param {!Float32Array} out the receiving quaternion
  25. * @param {!Float32Array} a quaternion to normalize
  26. */
  27. static normalize(out, a) {
  28. const x = a[0];
  29. const y = a[1];
  30. const z = a[2];
  31. const w = a[3];
  32. let len = x * x + y * y + z * z + w * w;
  33. if (len > 0) {
  34. len = 1 / Math.sqrt(len);
  35. }
  36. out[0] = x * len;
  37. out[1] = y * len;
  38. out[2] = z * len;
  39. out[3] = w * len;
  40. }
  41. };