babylon.freecamera.input.deviceorientation.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. 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;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var FreeCameraDeviceOrientationInput = (function () {
  10. function FreeCameraDeviceOrientationInput() {
  11. this._offsetX = null;
  12. this._offsetY = null;
  13. this._orientationGamma = 0;
  14. this._orientationBeta = 0;
  15. this._initialOrientationGamma = 0;
  16. this._initialOrientationBeta = 0;
  17. this.angularSensibility = 10000.0;
  18. this.moveSensibility = 50.0;
  19. this._resetOrientationGamma = this.resetOrientationGamma.bind(this);
  20. this._orientationChanged = this.orientationChanged.bind(this);
  21. }
  22. FreeCameraDeviceOrientationInput.prototype.attachControl = function (element, noPreventDefault) {
  23. window.addEventListener("resize", this._resetOrientationGamma, false);
  24. window.addEventListener("deviceorientation", this._orientationChanged);
  25. };
  26. FreeCameraDeviceOrientationInput.prototype.resetOrientationGamma = function () {
  27. this._initialOrientationGamma = null;
  28. };
  29. FreeCameraDeviceOrientationInput.prototype.orientationChanged = function (evt) {
  30. if (!this._initialOrientationGamma) {
  31. this._initialOrientationGamma = evt.gamma;
  32. this._initialOrientationBeta = evt.beta;
  33. }
  34. this._orientationGamma = evt.gamma;
  35. this._orientationBeta = evt.beta;
  36. this._offsetY = (this._initialOrientationBeta - this._orientationBeta);
  37. this._offsetX = (this._initialOrientationGamma - this._orientationGamma);
  38. };
  39. FreeCameraDeviceOrientationInput.prototype.detachControl = function (element) {
  40. window.removeEventListener("resize", this._resetOrientationGamma);
  41. window.removeEventListener("deviceorientation", this._orientationChanged);
  42. this._orientationGamma = 0;
  43. this._orientationBeta = 0;
  44. this._initialOrientationGamma = 0;
  45. this._initialOrientationBeta = 0;
  46. this._offsetX = null;
  47. this._offsetY = null;
  48. };
  49. FreeCameraDeviceOrientationInput.prototype.checkInputs = function () {
  50. if (!this._offsetX) {
  51. return;
  52. }
  53. var camera = this.camera;
  54. camera.cameraRotation.y -= this._offsetX / this.angularSensibility;
  55. var speed = camera._computeLocalCameraSpeed();
  56. var direction = new BABYLON.Vector3(0, 0, speed * this._offsetY / this.moveSensibility);
  57. BABYLON.Matrix.RotationYawPitchRollToRef(camera.rotation.y, camera.rotation.x, 0, camera._cameraRotationMatrix);
  58. camera.cameraDirection.addInPlace(BABYLON.Vector3.TransformCoordinates(direction, camera._cameraRotationMatrix));
  59. };
  60. FreeCameraDeviceOrientationInput.prototype.getTypeName = function () {
  61. return "FreeCameraDeviceOrientationInput";
  62. };
  63. FreeCameraDeviceOrientationInput.prototype.getSimpleName = function () {
  64. return "deviceOrientation";
  65. };
  66. __decorate([
  67. BABYLON.serialize()
  68. ], FreeCameraDeviceOrientationInput.prototype, "angularSensibility", void 0);
  69. __decorate([
  70. BABYLON.serialize()
  71. ], FreeCameraDeviceOrientationInput.prototype, "moveSensibility", void 0);
  72. return FreeCameraDeviceOrientationInput;
  73. })();
  74. BABYLON.FreeCameraDeviceOrientationInput = FreeCameraDeviceOrientationInput;
  75. BABYLON.CameraInputTypes["FreeCameraDeviceOrientationInput"] = FreeCameraDeviceOrientationInput;
  76. })(BABYLON || (BABYLON = {}));