DeviceOrientationControls.js 3.3 KB

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