babylon.deviceOrientationCamera.js 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  7. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  8. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  9. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  10. return c > 3 && r && Object.defineProperty(target, key, r), r;
  11. };
  12. var BABYLON;
  13. (function (BABYLON) {
  14. // We're mainly based on the logic defined into the FreeCamera code
  15. var DeviceOrientationCamera = (function (_super) {
  16. __extends(DeviceOrientationCamera, _super);
  17. function DeviceOrientationCamera(name, position, scene) {
  18. var _this = this;
  19. _super.call(this, name, position, scene);
  20. this._offsetX = null;
  21. this._offsetY = null;
  22. this._orientationGamma = 0;
  23. this._orientationBeta = 0;
  24. this._initialOrientationGamma = 0;
  25. this._initialOrientationBeta = 0;
  26. this.angularSensibility = 10000.0;
  27. this.moveSensibility = 50.0;
  28. window.addEventListener("resize", function () {
  29. _this._initialOrientationGamma = null;
  30. }, false);
  31. }
  32. DeviceOrientationCamera.prototype.attachControl = function (canvas, noPreventDefault) {
  33. var _this = this;
  34. if (this._attachedCanvas) {
  35. return;
  36. }
  37. this._attachedCanvas = canvas;
  38. if (!this._orientationChanged) {
  39. this._orientationChanged = function (evt) {
  40. if (!_this._initialOrientationGamma) {
  41. _this._initialOrientationGamma = evt.gamma;
  42. _this._initialOrientationBeta = evt.beta;
  43. }
  44. _this._orientationGamma = evt.gamma;
  45. _this._orientationBeta = evt.beta;
  46. _this._offsetY = (_this._initialOrientationBeta - _this._orientationBeta);
  47. _this._offsetX = (_this._initialOrientationGamma - _this._orientationGamma);
  48. };
  49. }
  50. window.addEventListener("deviceorientation", this._orientationChanged);
  51. };
  52. DeviceOrientationCamera.prototype.detachControl = function (canvas) {
  53. if (this._attachedCanvas !== canvas) {
  54. return;
  55. }
  56. window.removeEventListener("deviceorientation", this._orientationChanged);
  57. this._attachedCanvas = null;
  58. this._orientationGamma = 0;
  59. this._orientationBeta = 0;
  60. this._initialOrientationGamma = 0;
  61. this._initialOrientationBeta = 0;
  62. };
  63. DeviceOrientationCamera.prototype._checkInputs = function () {
  64. if (!this._offsetX) {
  65. return;
  66. }
  67. this.cameraRotation.y -= this._offsetX / this.angularSensibility;
  68. var speed = this._computeLocalCameraSpeed();
  69. var direction = new BABYLON.Vector3(0, 0, speed * this._offsetY / this.moveSensibility);
  70. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, 0, this._cameraRotationMatrix);
  71. this.cameraDirection.addInPlace(BABYLON.Vector3.TransformCoordinates(direction, this._cameraRotationMatrix));
  72. _super.prototype._checkInputs.call(this);
  73. };
  74. DeviceOrientationCamera.prototype.getTypeName = function () {
  75. return "DeviceOrientationCamera";
  76. };
  77. __decorate([
  78. BABYLON.serialize()
  79. ], DeviceOrientationCamera.prototype, "angularSensibility", void 0);
  80. __decorate([
  81. BABYLON.serialize()
  82. ], DeviceOrientationCamera.prototype, "moveSensibility", void 0);
  83. return DeviceOrientationCamera;
  84. })(BABYLON.FreeCamera);
  85. BABYLON.DeviceOrientationCamera = DeviceOrientationCamera;
  86. })(BABYLON || (BABYLON = {}));