2
0

DeviceOrientationControls.js 3.6 KB

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