babylon.deviceOrientationCamera.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. var __extends = 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._isLandscape = true;
  22. this.angularSensibility = 10000.0;
  23. this.moveSensibility = 50.0;
  24. http:
  25. window.addEventListener("resize", function () {
  26. _this._isLandscape = (window.innerWidth > window.innerHeight) ? true : false;
  27. _this._initialOrientationGamma = null;
  28. }, false);
  29. }
  30. DeviceOrientationCamera.prototype.attachControl = function (canvas, noPreventDefault) {
  31. if (this._attachedCanvas) {
  32. return;
  33. }
  34. this._attachedCanvas = canvas;
  35. var that = this;
  36. if (!this._orientationChanged) {
  37. this._orientationChanged = function (evt) {
  38. if (!that._initialOrientationGamma) {
  39. if (!this._isLandscape) {
  40. that._initialOrientationGamma = evt.gamma;
  41. that._initialOrientationBeta = evt.beta;
  42. } else {
  43. that._initialOrientationGamma = evt.beta;
  44. that._initialOrientationBeta = evt.gamma;
  45. }
  46. }
  47. if (!this._isLandscape) {
  48. that._orientationGamma = evt.gamma;
  49. that._orientationBeta = evt.beta;
  50. } else {
  51. that._orientationGamma = evt.beta;
  52. that._orientationBeta = evt.gamma;
  53. }
  54. that._offsetY = (that._initialOrientationBeta - that._orientationBeta);
  55. that._offsetX = (that._initialOrientationGamma - that._orientationGamma);
  56. };
  57. }
  58. window.addEventListener("deviceorientation", this._orientationChanged);
  59. };
  60. DeviceOrientationCamera.prototype.detachControl = function (canvas) {
  61. if (this._attachedCanvas != canvas) {
  62. return;
  63. }
  64. window.removeEventListener("deviceorientation", this._orientationChanged);
  65. this._attachedCanvas = null;
  66. this._orientationGamma = 0;
  67. this._orientationBeta = 0;
  68. this._initialOrientationGamma = 0;
  69. this._initialOrientationBeta = 0;
  70. };
  71. DeviceOrientationCamera.prototype._checkInputs = function () {
  72. if (!this._offsetX) {
  73. return;
  74. }
  75. this.cameraRotation.y -= this._offsetX / this.angularSensibility;
  76. var speed = this._computeLocalCameraSpeed();
  77. var direction = new BABYLON.Vector3(0, 0, speed * this._offsetY / this.moveSensibility);
  78. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, 0, this._cameraRotationMatrix);
  79. this.cameraDirection.addInPlace(BABYLON.Vector3.TransformCoordinates(direction, this._cameraRotationMatrix));
  80. };
  81. return DeviceOrientationCamera;
  82. })(BABYLON.FreeCamera);
  83. BABYLON.DeviceOrientationCamera = DeviceOrientationCamera;
  84. })(BABYLON || (BABYLON = {}));
  85. //# sourceMappingURL=babylon.deviceOrientationCamera.js.map