babylon.freecamera.input.vrdeviceorientation.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var FreeCameraVRDeviceOrientationInput = (function () {
  4. function FreeCameraVRDeviceOrientationInput() {
  5. this.alphaCorrection = 1;
  6. this.betaCorrection = 1;
  7. this.gammaCorrection = 1;
  8. this._alpha = 0;
  9. this._beta = 0;
  10. this._gamma = 0;
  11. this._dirty = false;
  12. this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
  13. }
  14. FreeCameraVRDeviceOrientationInput.prototype.attachControl = function (element, noPreventDefault) {
  15. window.addEventListener("deviceorientation", this._deviceOrientationHandler);
  16. };
  17. FreeCameraVRDeviceOrientationInput.prototype._onOrientationEvent = function (evt) {
  18. var camera = this.camera;
  19. this._alpha = evt.alpha;
  20. this._beta = evt.beta;
  21. this._gamma = evt.gamma;
  22. this._dirty = true;
  23. };
  24. FreeCameraVRDeviceOrientationInput.prototype.checkInputs = function () {
  25. if (this._dirty) {
  26. this._dirty = false;
  27. var rotationX = this._gamma;
  28. if (rotationX < 0) {
  29. rotationX = 90 + rotationX;
  30. }
  31. else {
  32. // Incline it in the correct angle.
  33. rotationX = 270 - rotationX;
  34. }
  35. this.camera.rotation.x = this.gammaCorrection * rotationX / 180.0 * Math.PI;
  36. this.camera.rotation.y = this.alphaCorrection * -this._alpha / 180.0 * Math.PI;
  37. this.camera.rotation.z = this.betaCorrection * this._beta / 180.0 * Math.PI;
  38. }
  39. };
  40. FreeCameraVRDeviceOrientationInput.prototype.detachControl = function (element) {
  41. window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
  42. };
  43. FreeCameraVRDeviceOrientationInput.prototype.getTypeName = function () {
  44. return "FreeCameraVRDeviceOrientationInput";
  45. };
  46. FreeCameraVRDeviceOrientationInput.prototype.getSimpleName = function () {
  47. return "VRDeviceOrientation";
  48. };
  49. return FreeCameraVRDeviceOrientationInput;
  50. }());
  51. BABYLON.FreeCameraVRDeviceOrientationInput = FreeCameraVRDeviceOrientationInput;
  52. BABYLON.CameraInputTypes["FreeCameraVRDeviceOrientationInput"] = FreeCameraVRDeviceOrientationInput;
  53. })(BABYLON || (BABYLON = {}));