babylon.deviceOrientationCamera.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // We're mainly based on the logic defined into the FreeCamera code
  10. var DeviceOrientationCamera = (function (_super) {
  11. __extends(DeviceOrientationCamera, _super);
  12. function DeviceOrientationCamera(name, position, scene) {
  13. var _this = this;
  14. _super.call(this, name, position, scene);
  15. this._offsetX = null;
  16. this._offsetY = null;
  17. this._orientationGamma = 0;
  18. this._orientationBeta = 0;
  19. this._initialOrientationGamma = 0;
  20. this._initialOrientationBeta = 0;
  21. this.angularSensibility = 10000.0;
  22. this.moveSensibility = 50.0;
  23. window.addEventListener("resize", function () {
  24. _this._initialOrientationGamma = null;
  25. }, false);
  26. }
  27. DeviceOrientationCamera.prototype.attachControl = function (canvas, noPreventDefault) {
  28. var _this = this;
  29. if (this._attachedCanvas) {
  30. return;
  31. }
  32. this._attachedCanvas = canvas;
  33. if (!this._orientationChanged) {
  34. this._orientationChanged = function (evt) {
  35. if (!_this._initialOrientationGamma) {
  36. _this._initialOrientationGamma = evt.gamma;
  37. _this._initialOrientationBeta = evt.beta;
  38. }
  39. _this._orientationGamma = evt.gamma;
  40. _this._orientationBeta = evt.beta;
  41. _this._offsetY = (_this._initialOrientationBeta - _this._orientationBeta);
  42. _this._offsetX = (_this._initialOrientationGamma - _this._orientationGamma);
  43. };
  44. }
  45. window.addEventListener("deviceorientation", this._orientationChanged);
  46. };
  47. DeviceOrientationCamera.prototype.detachControl = function (canvas) {
  48. if (this._attachedCanvas != canvas) {
  49. return;
  50. }
  51. window.removeEventListener("deviceorientation", this._orientationChanged);
  52. this._attachedCanvas = null;
  53. this._orientationGamma = 0;
  54. this._orientationBeta = 0;
  55. this._initialOrientationGamma = 0;
  56. this._initialOrientationBeta = 0;
  57. };
  58. DeviceOrientationCamera.prototype._checkInputs = function () {
  59. if (!this._offsetX) {
  60. return;
  61. }
  62. this.cameraRotation.y -= this._offsetX / this.angularSensibility;
  63. var speed = this._computeLocalCameraSpeed();
  64. var direction = new BABYLON.Vector3(0, 0, speed * this._offsetY / this.moveSensibility);
  65. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, 0, this._cameraRotationMatrix);
  66. this.cameraDirection.addInPlace(BABYLON.Vector3.TransformCoordinates(direction, this._cameraRotationMatrix));
  67. _super.prototype._checkInputs.call(this);
  68. };
  69. return DeviceOrientationCamera;
  70. })(BABYLON.FreeCamera);
  71. BABYLON.DeviceOrientationCamera = DeviceOrientationCamera;
  72. })(BABYLON || (BABYLON = {}));
  73. //# sourceMappingURL=babylon.deviceOrientationCamera.js.map