DeviceOrientationControls.js 3.2 KB

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