DeviceOrientationControls.js 3.6 KB

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