DeviceOrientationControls.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. this.object = object;
  9. this.object.rotation.reorder( "YXZ" );
  10. this.freeze = true;
  11. this.deviceOrientation = {};
  12. this.screenOrientation = 0;
  13. this.onDeviceOrientationChangeEvent = function( rawEvtData ) {
  14. this.deviceOrientation = rawEvtData;
  15. };
  16. this.onScreenOrientationChangeEvent = function() {
  17. this.screenOrientation = window.orientation || 0;
  18. };
  19. this.update = function() {
  20. var alpha, beta, gamma;
  21. return function () {
  22. if ( this.freeze ) return;
  23. alpha = this.deviceOrientation.gamma ? THREE.Math.degToRad( this.deviceOrientation.alpha ) : 0; // Z
  24. beta = this.deviceOrientation.beta ? THREE.Math.degToRad( this.deviceOrientation.beta ) : 0; // X'
  25. gamma = this.deviceOrientation.gamma ? THREE.Math.degToRad( this.deviceOrientation.gamma ) : 0; // Y''
  26. orient = this.screenOrientation ? THREE.Math.degToRad( this.screenOrientation ) : 0; // O
  27. setObjectQuaternion( this.object.quaternion, alpha, beta, gamma, orient );
  28. }
  29. }();
  30. function bind( scope, fn ) {
  31. return function () {
  32. fn.apply( scope, arguments );
  33. };
  34. };
  35. this.connect = function() {
  36. this.onScreenOrientationChangeEvent(); // run once on load
  37. window.addEventListener( 'orientationchange', bind( this, this.onScreenOrientationChangeEvent ), false );
  38. window.addEventListener( 'deviceorientation', bind( this, this.onDeviceOrientationChangeEvent ), false );
  39. this.freeze = false;
  40. };
  41. this.disconnect = function() {
  42. this.freeze = true;
  43. window.removeEventListener( 'orientationchange', bind( this, this.onScreenOrientationChangeEvent ), false );
  44. window.removeEventListener( 'deviceorientation', bind( this, this.onDeviceOrientationChangeEvent ), false );
  45. };
  46. // The angles alpha, beta and gamma form a set of intrinsic Tait-Bryan angles of type Z-X'-Y''
  47. setObjectQuaternion = function () {
  48. var zee = new THREE.Vector3( 0, 0, 1 );
  49. var euler = new THREE.Euler();
  50. var q0 = new THREE.Quaternion();
  51. var q1 = new THREE.Quaternion( - Math.sqrt( 0.5 ), 0, 0, Math.sqrt( 0.5 ) ); // - PI/2 around the x-axis
  52. return function ( quaternion, alpha, beta, gamma, orient ) {
  53. euler.set( beta, alpha, - gamma, 'YXZ' ); // 'ZXY' for the device, but 'YXZ' for us
  54. quaternion.setFromEuler( euler ); // orient the device
  55. quaternion.multiply( q1 ); // camera looks out the back of the device, not the top
  56. quaternion.multiply( q0.setFromAxisAngle( zee, - orient ) ); // adjust for screen orientation
  57. }
  58. }();
  59. };