DeviceOrientationControls.js 3.7 KB

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