babylon.vrDeviceOrientationCamera.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var VRDeviceOrientationFreeCamera = (function (_super) {
  10. __extends(VRDeviceOrientationFreeCamera, _super);
  11. function VRDeviceOrientationFreeCamera(name, position, scene, compensateDistorsion) {
  12. if (compensateDistorsion === void 0) { compensateDistorsion = true; }
  13. _super.call(this, name, position, scene);
  14. this._alpha = 0;
  15. this._beta = 0;
  16. this._gamma = 0;
  17. var metrics = BABYLON.VRCameraMetrics.GetDefault();
  18. metrics.compensateDistorsion = compensateDistorsion;
  19. this.setSubCameraMode(BABYLON.Camera.SUB_CAMERA_MODE_VR, 0, metrics);
  20. this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
  21. }
  22. VRDeviceOrientationFreeCamera.prototype._onOrientationEvent = function (evt) {
  23. this._alpha = +evt.alpha | 0;
  24. this._beta = +evt.beta | 0;
  25. this._gamma = +evt.gamma | 0;
  26. if (this._gamma < 0) {
  27. this._gamma = 90 + this._gamma;
  28. }
  29. else {
  30. // Incline it in the correct angle.
  31. this._gamma = 270 - this._gamma;
  32. }
  33. this.rotation.x = this._gamma / 180.0 * Math.PI;
  34. this.rotation.y = -this._alpha / 180.0 * Math.PI;
  35. this.rotation.z = this._beta / 180.0 * Math.PI;
  36. };
  37. VRDeviceOrientationFreeCamera.prototype.attachControl = function (element, noPreventDefault) {
  38. _super.prototype.attachControl.call(this, element, noPreventDefault);
  39. window.addEventListener("deviceorientation", this._deviceOrientationHandler);
  40. };
  41. VRDeviceOrientationFreeCamera.prototype.detachControl = function (element) {
  42. _super.prototype.detachControl.call(this, element);
  43. window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
  44. };
  45. return VRDeviceOrientationFreeCamera;
  46. })(BABYLON.FreeCamera);
  47. BABYLON.VRDeviceOrientationFreeCamera = VRDeviceOrientationFreeCamera;
  48. })(BABYLON || (BABYLON = {}));
  49. //# sourceMappingURL=babylon.vrDeviceOrientationCamera.js.map