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