DeviceOrientationControls.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. console.warn( "THREE.DeviceOrientationControls: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. /**
  3. * W3C Device Orientation control (http://w3c.github.io/deviceorientation/spec-source-orientation.html)
  4. */
  5. THREE.DeviceOrientationControls = function ( object ) {
  6. var scope = this;
  7. var changeEvent = { type: "change" };
  8. var EPS = 0.000001;
  9. this.object = object;
  10. this.object.rotation.reorder( 'YXZ' );
  11. this.enabled = true;
  12. this.deviceOrientation = {};
  13. this.screenOrientation = 0;
  14. this.alphaOffset = 0; // radians
  15. var onDeviceOrientationChangeEvent = function ( event ) {
  16. scope.deviceOrientation = event;
  17. };
  18. var onScreenOrientationChangeEvent = function () {
  19. scope.screenOrientation = window.orientation || 0;
  20. };
  21. // The angles alpha, beta and gamma form a set of intrinsic Tait-Bryan angles of type Z-X'-Y''
  22. var setObjectQuaternion = function () {
  23. var zee = new THREE.Vector3( 0, 0, 1 );
  24. var euler = new THREE.Euler();
  25. var q0 = new THREE.Quaternion();
  26. var q1 = new THREE.Quaternion( - Math.sqrt( 0.5 ), 0, 0, Math.sqrt( 0.5 ) ); // - PI/2 around the x-axis
  27. return function ( quaternion, alpha, beta, gamma, orient ) {
  28. euler.set( beta, alpha, - gamma, 'YXZ' ); // 'ZXY' for the device, but 'YXZ' for us
  29. quaternion.setFromEuler( euler ); // orient the device
  30. quaternion.multiply( q1 ); // camera looks out the back of the device, not the top
  31. quaternion.multiply( q0.setFromAxisAngle( zee, - orient ) ); // adjust for screen orientation
  32. };
  33. }();
  34. this.connect = function () {
  35. onScreenOrientationChangeEvent(); // run once on load
  36. // iOS 13+
  37. if ( window.DeviceOrientationEvent !== undefined && typeof window.DeviceOrientationEvent.requestPermission === 'function' ) {
  38. window.DeviceOrientationEvent.requestPermission().then( function ( response ) {
  39. if ( response == 'granted' ) {
  40. window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
  41. window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );
  42. }
  43. } ).catch( function ( error ) {
  44. console.error( 'THREE.DeviceOrientationControls: Unable to use DeviceOrientation API:', error );
  45. } );
  46. } else {
  47. window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
  48. window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );
  49. }
  50. scope.enabled = true;
  51. };
  52. this.disconnect = function () {
  53. window.removeEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
  54. window.removeEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );
  55. scope.enabled = false;
  56. };
  57. this.update = ( function () {
  58. var lastQuaternion = new THREE.Quaternion();
  59. return function () {
  60. if ( scope.enabled === false ) return;
  61. var device = scope.deviceOrientation;
  62. if ( device ) {
  63. var alpha = device.alpha ? THREE.MathUtils.degToRad( device.alpha ) + scope.alphaOffset : 0; // Z
  64. var beta = device.beta ? THREE.MathUtils.degToRad( device.beta ) : 0; // X'
  65. var gamma = device.gamma ? THREE.MathUtils.degToRad( device.gamma ) : 0; // Y''
  66. var 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. } )();
  75. this.dispose = function () {
  76. scope.disconnect();
  77. };
  78. this.connect();
  79. };
  80. THREE.DeviceOrientationControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  81. THREE.DeviceOrientationControls.prototype.constructor = THREE.DeviceOrientationControls;