DeviceOrientationControls.js 3.7 KB

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