DeviceOrientationControls.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. Math as _Math,
  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. window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
  43. window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );
  44. scope.enabled = true;
  45. };
  46. this.disconnect = function () {
  47. window.removeEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
  48. window.removeEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );
  49. scope.enabled = false;
  50. };
  51. this.update = function () {
  52. if ( scope.enabled === false ) return;
  53. var device = scope.deviceOrientation;
  54. if ( device ) {
  55. var alpha = device.alpha ? _Math.degToRad( device.alpha ) + scope.alphaOffset : 0; // Z
  56. var beta = device.beta ? _Math.degToRad( device.beta ) : 0; // X'
  57. var gamma = device.gamma ? _Math.degToRad( device.gamma ) : 0; // Y''
  58. var orient = scope.screenOrientation ? _Math.degToRad( scope.screenOrientation ) : 0; // O
  59. setObjectQuaternion( scope.object.quaternion, alpha, beta, gamma, orient );
  60. }
  61. };
  62. this.dispose = function () {
  63. scope.disconnect();
  64. };
  65. this.connect();
  66. };
  67. export { DeviceOrientationControls };