DeviceOrientationControls.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {
  2. Euler,
  3. EventDispatcher,
  4. MathUtils,
  5. Quaternion,
  6. Vector3
  7. } from 'three';
  8. const _zee = new Vector3( 0, 0, 1 );
  9. const _euler = new Euler();
  10. const _q0 = new Quaternion();
  11. const _q1 = new Quaternion( - Math.sqrt( 0.5 ), 0, 0, Math.sqrt( 0.5 ) ); // - PI/2 around the x-axis
  12. const _changeEvent = { type: 'change' };
  13. class DeviceOrientationControls extends EventDispatcher {
  14. constructor( object ) {
  15. super();
  16. if ( window.isSecureContext === false ) {
  17. console.error( 'THREE.DeviceOrientationControls: DeviceOrientationEvent is only available in secure contexts (https)' );
  18. }
  19. const scope = this;
  20. const EPS = 0.000001;
  21. const lastQuaternion = new Quaternion();
  22. this.object = object;
  23. this.object.rotation.reorder( 'YXZ' );
  24. this.enabled = true;
  25. this.deviceOrientation = {};
  26. this.screenOrientation = 0;
  27. this.alphaOffset = 0; // radians
  28. const onDeviceOrientationChangeEvent = function ( event ) {
  29. scope.deviceOrientation = event;
  30. };
  31. const onScreenOrientationChangeEvent = function () {
  32. scope.screenOrientation = window.orientation || 0;
  33. };
  34. // The angles alpha, beta and gamma form a set of intrinsic Tait-Bryan angles of type Z-X'-Y''
  35. const setObjectQuaternion = function ( quaternion, alpha, beta, gamma, orient ) {
  36. _euler.set( beta, alpha, - gamma, 'YXZ' ); // 'ZXY' for the device, but 'YXZ' for us
  37. quaternion.setFromEuler( _euler ); // orient the device
  38. quaternion.multiply( _q1 ); // camera looks out the back of the device, not the top
  39. quaternion.multiply( _q0.setFromAxisAngle( _zee, - orient ) ); // adjust for screen orientation
  40. };
  41. this.connect = function () {
  42. onScreenOrientationChangeEvent(); // run once on load
  43. // iOS 13+
  44. if ( window.DeviceOrientationEvent !== undefined && typeof window.DeviceOrientationEvent.requestPermission === 'function' ) {
  45. window.DeviceOrientationEvent.requestPermission().then( function ( response ) {
  46. if ( response == 'granted' ) {
  47. window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent );
  48. window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent );
  49. }
  50. } ).catch( function ( error ) {
  51. console.error( 'THREE.DeviceOrientationControls: Unable to use DeviceOrientation API:', error );
  52. } );
  53. } else {
  54. window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent );
  55. window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent );
  56. }
  57. scope.enabled = true;
  58. };
  59. this.disconnect = function () {
  60. window.removeEventListener( 'orientationchange', onScreenOrientationChangeEvent );
  61. window.removeEventListener( 'deviceorientation', onDeviceOrientationChangeEvent );
  62. scope.enabled = false;
  63. };
  64. this.update = function () {
  65. if ( scope.enabled === false ) return;
  66. const device = scope.deviceOrientation;
  67. if ( device ) {
  68. const alpha = device.alpha ? MathUtils.degToRad( device.alpha ) + scope.alphaOffset : 0; // Z
  69. const beta = device.beta ? MathUtils.degToRad( device.beta ) : 0; // X'
  70. const gamma = device.gamma ? MathUtils.degToRad( device.gamma ) : 0; // Y''
  71. const orient = scope.screenOrientation ? MathUtils.degToRad( scope.screenOrientation ) : 0; // O
  72. setObjectQuaternion( scope.object.quaternion, alpha, beta, gamma, orient );
  73. if ( 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  74. lastQuaternion.copy( scope.object.quaternion );
  75. scope.dispatchEvent( _changeEvent );
  76. }
  77. }
  78. };
  79. this.dispose = function () {
  80. scope.disconnect();
  81. };
  82. this.connect();
  83. }
  84. }
  85. export { DeviceOrientationControls };