DeviceOrientationControls.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
  37. window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );
  38. scope.enabled = true;
  39. };
  40. this.disconnect = function () {
  41. window.removeEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
  42. window.removeEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );
  43. scope.enabled = false;
  44. };
  45. this.update = function () {
  46. if ( scope.enabled === false ) return;
  47. var device = scope.deviceOrientation;
  48. if ( device ) {
  49. var alpha = device.alpha ? THREE.Math.degToRad( device.alpha ) + scope.alphaOffset : 0; // Z
  50. var beta = device.beta ? THREE.Math.degToRad( device.beta ) : 0; // X'
  51. var gamma = device.gamma ? THREE.Math.degToRad( device.gamma ) : 0; // Y''
  52. var orient = scope.screenOrientation ? THREE.Math.degToRad( scope.screenOrientation ) : 0; // O
  53. setObjectQuaternion( scope.object.quaternion, alpha, beta, gamma, orient );
  54. }
  55. };
  56. this.dispose = function () {
  57. scope.disconnect();
  58. };
  59. this.connect();
  60. };