DeviceOrientationControls.js 3.2 KB

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