DeviceOrientationControls.js 3.4 KB

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