babylon.vrDeviceOrientationCamera.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. var VRDeviceOrientationFreeCamera = (function (_super) {
  9. __extends(VRDeviceOrientationFreeCamera, _super);
  10. function VRDeviceOrientationFreeCamera(name, position, scene, compensateDistortion) {
  11. if (compensateDistortion === void 0) { compensateDistortion = true; }
  12. _super.call(this, name, position, scene);
  13. this._alpha = 0;
  14. this._beta = 0;
  15. this._gamma = 0;
  16. var metrics = BABYLON.VRCameraMetrics.GetDefault();
  17. metrics.compensateDistortion = compensateDistortion;
  18. this.setCameraRigMode(BABYLON.Camera.RIG_MODE_VR, { vrCameraMetrics: metrics });
  19. this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
  20. }
  21. VRDeviceOrientationFreeCamera.prototype._onOrientationEvent = function (evt) {
  22. this._alpha = +evt.alpha | 0;
  23. this._beta = +evt.beta | 0;
  24. this._gamma = +evt.gamma | 0;
  25. if (this._gamma < 0) {
  26. this._gamma = 90 + this._gamma;
  27. }
  28. else {
  29. // Incline it in the correct angle.
  30. this._gamma = 270 - this._gamma;
  31. }
  32. this.rotation.x = this._gamma / 180.0 * Math.PI;
  33. this.rotation.y = -this._alpha / 180.0 * Math.PI;
  34. this.rotation.z = this._beta / 180.0 * Math.PI;
  35. };
  36. VRDeviceOrientationFreeCamera.prototype.attachControl = function (element, noPreventDefault) {
  37. _super.prototype.attachControl.call(this, element, noPreventDefault);
  38. window.addEventListener("deviceorientation", this._deviceOrientationHandler);
  39. };
  40. VRDeviceOrientationFreeCamera.prototype.detachControl = function (element) {
  41. _super.prototype.detachControl.call(this, element);
  42. window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
  43. };
  44. return VRDeviceOrientationFreeCamera;
  45. })(BABYLON.FreeCamera);
  46. BABYLON.VRDeviceOrientationFreeCamera = VRDeviceOrientationFreeCamera;
  47. })(BABYLON || (BABYLON = {}));