Gyroscope.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {
  2. Object3D,
  3. Quaternion,
  4. Vector3
  5. } from 'three';
  6. const _translationObject = new Vector3();
  7. const _quaternionObject = new Quaternion();
  8. const _scaleObject = new Vector3();
  9. const _translationWorld = new Vector3();
  10. const _quaternionWorld = new Quaternion();
  11. const _scaleWorld = new Vector3();
  12. /**
  13. * A special type of 3D object that takes a position from the scene graph hierarchy
  14. * but uses its local rotation as world rotation. It works like real-world gyroscope -
  15. * you can move it around using hierarchy while its orientation stays fixed with
  16. * respect to the world.
  17. *
  18. * @augments Object3D
  19. * @three_import import { Gyroscope } from 'three/addons/misc/Gyroscope.js';
  20. */
  21. class Gyroscope extends Object3D {
  22. /**
  23. * Constructs a new gyroscope.
  24. */
  25. constructor() {
  26. super();
  27. }
  28. updateMatrixWorld( force ) {
  29. this.matrixAutoUpdate && this.updateMatrix();
  30. // update matrixWorld
  31. if ( this.matrixWorldNeedsUpdate || force ) {
  32. if ( this.parent !== null ) {
  33. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  34. this.matrixWorld.decompose( _translationWorld, _quaternionWorld, _scaleWorld );
  35. this.matrix.decompose( _translationObject, _quaternionObject, _scaleObject );
  36. this.matrixWorld.compose( _translationWorld, _quaternionObject, _scaleWorld );
  37. } else {
  38. this.matrixWorld.copy( this.matrix );
  39. }
  40. this.matrixWorldNeedsUpdate = false;
  41. force = true;
  42. }
  43. // update children
  44. for ( let i = 0, l = this.children.length; i < l; i ++ ) {
  45. this.children[ i ].updateMatrixWorld( force );
  46. }
  47. }
  48. }
  49. export { Gyroscope };