DeviceOrientationControls.js 3.9 KB

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