2
0

DeviceOrientationControls.js 3.8 KB

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