babylon.deviceOrientationCamera.js 3.3 KB

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