babylon.deviceOrientationCamera.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.DeviceOrientationCamera = function (name, position, scene) {
  5. BABYLON.FreeCamera.call(this, name, position, scene);
  6. // Offset
  7. this._offsetX = null;
  8. this._offsetY = null;
  9. this._orientationGamma = 0;
  10. this._orientationBeta = 0;
  11. this._initialOrientationGamma = 0;
  12. this._initialOrientationBeta = 0;
  13. };
  14. BABYLON.DeviceOrientationCamera.prototype = Object.create(BABYLON.FreeCamera.prototype);
  15. // Members
  16. BABYLON.DeviceOrientationCamera.prototype.angularSensibility = 10000.0;
  17. BABYLON.DeviceOrientationCamera.prototype.moveSensibility = 50.0;
  18. // Controls
  19. BABYLON.DeviceOrientationCamera.prototype.attachControl = function (canvas, noPreventDefault) {
  20. if (this._attachedCanvas) {
  21. return;
  22. }
  23. this._attachedCanvas = canvas;
  24. var that = this;
  25. if (!this._orientationChanged) {
  26. this._orientationChanged = function (evt) {
  27. if (!that._initialOrientationGamma) {
  28. that._initialOrientationGamma = evt.gamma;
  29. that._initialOrientationBeta = evt.beta;
  30. }
  31. that._orientationGamma = evt.gamma;
  32. that._orientationBeta = evt.beta;
  33. that._offsetY = (that._initialOrientationBeta - that._orientationBeta);
  34. that._offsetX = (that._initialOrientationGamma - that._orientationGamma);
  35. };
  36. }
  37. window.addEventListener("deviceorientation", this._orientationChanged);
  38. };
  39. BABYLON.DeviceOrientationCamera.prototype.detachControl = function (canvas) {
  40. if (this._attachedCanvas != canvas) {
  41. return;
  42. }
  43. window.removeEventListener("deviceorientation", this._orientationChanged);
  44. this._attachedCanvas = null;
  45. this._orientationGamma = 0;
  46. this._orientationBeta = 0;
  47. this._initialOrientationGamma = 0;
  48. this._initialOrientationBeta = 0;
  49. };
  50. BABYLON.DeviceOrientationCamera.prototype._checkInputs = function () {
  51. if (!this._offsetX) {
  52. return;
  53. }
  54. this.cameraRotation.y -= this._offsetX / this.angularSensibility;
  55. var speed = this._computeLocalCameraSpeed();
  56. var direction = new BABYLON.Vector3(0, 0, speed * this._offsetY / this.moveSensibility);
  57. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, 0, this._cameraRotationMatrix);
  58. this.cameraDirection.addInPlace(BABYLON.Vector3.TransformCoordinates(direction, this._cameraRotationMatrix));
  59. };
  60. })();