Gyroscope.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. */
  20. class Gyroscope extends Object3D {
  21. /**
  22. * Constructs a new gyroscope.
  23. */
  24. constructor() {
  25. super();
  26. }
  27. updateMatrixWorld( force ) {
  28. this.matrixAutoUpdate && this.updateMatrix();
  29. // update matrixWorld
  30. if ( this.matrixWorldNeedsUpdate || force ) {
  31. if ( this.parent !== null ) {
  32. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  33. this.matrixWorld.decompose( _translationWorld, _quaternionWorld, _scaleWorld );
  34. this.matrix.decompose( _translationObject, _quaternionObject, _scaleObject );
  35. this.matrixWorld.compose( _translationWorld, _quaternionObject, _scaleWorld );
  36. } else {
  37. this.matrixWorld.copy( this.matrix );
  38. }
  39. this.matrixWorldNeedsUpdate = false;
  40. force = true;
  41. }
  42. // update children
  43. for ( let i = 0, l = this.children.length; i < l; i ++ ) {
  44. this.children[ i ].updateMatrixWorld( force );
  45. }
  46. }
  47. }
  48. export { Gyroscope };
粤ICP备19079148号