babylon.freecamera.input.deviceorientation.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var FreeCameraDeviceOrientationInput = (function () {
  4. function FreeCameraDeviceOrientationInput() {
  5. var _this = this;
  6. this._screenOrientationAngle = 0;
  7. this._screenQuaternion = new BABYLON.Quaternion();
  8. this._alpha = 0;
  9. this._beta = 0;
  10. this._gamma = 0;
  11. this._orientationChanged = function () {
  12. _this._screenOrientationAngle = (window.orientation !== undefined ? +window.orientation : (window.screen.orientation && window.screen.orientation['angle'] ? window.screen.orientation.angle : 0));
  13. _this._screenOrientationAngle = -BABYLON.Tools.ToRadians(_this._screenOrientationAngle / 2);
  14. _this._screenQuaternion.copyFromFloats(0, Math.sin(_this._screenOrientationAngle), 0, Math.cos(_this._screenOrientationAngle));
  15. };
  16. this._deviceOrientation = function (evt) {
  17. _this._alpha = evt.alpha;
  18. _this._beta = evt.beta;
  19. _this._gamma = evt.gamma;
  20. };
  21. this._constantTranform = new BABYLON.Quaternion(-Math.sqrt(0.5), 0, 0, Math.sqrt(0.5));
  22. this._orientationChanged();
  23. }
  24. Object.defineProperty(FreeCameraDeviceOrientationInput.prototype, "camera", {
  25. get: function () {
  26. return this._camera;
  27. },
  28. set: function (camera) {
  29. this._camera = camera;
  30. if (!this._camera.rotationQuaternion)
  31. this._camera.rotationQuaternion = new BABYLON.Quaternion();
  32. },
  33. enumerable: true,
  34. configurable: true
  35. });
  36. FreeCameraDeviceOrientationInput.prototype.attachControl = function (element, noPreventDefault) {
  37. window.addEventListener("orientationchange", this._orientationChanged);
  38. window.addEventListener("deviceorientation", this._deviceOrientation);
  39. };
  40. FreeCameraDeviceOrientationInput.prototype.detachControl = function (element) {
  41. window.removeEventListener("orientationchange", this._orientationChanged);
  42. window.removeEventListener("deviceorientation", this._deviceOrientation);
  43. };
  44. FreeCameraDeviceOrientationInput.prototype.checkInputs = function () {
  45. BABYLON.Quaternion.RotationYawPitchRollToRef(BABYLON.Tools.ToRadians(this._alpha), BABYLON.Tools.ToRadians(this._beta), -BABYLON.Tools.ToRadians(this._gamma), this.camera.rotationQuaternion);
  46. this._camera.rotationQuaternion.multiplyInPlace(this._screenQuaternion);
  47. this._camera.rotationQuaternion.multiplyInPlace(this._constantTranform);
  48. //Mirror on XY Plane
  49. this._camera.rotationQuaternion.z *= -1;
  50. this._camera.rotationQuaternion.w *= -1;
  51. };
  52. FreeCameraDeviceOrientationInput.prototype.getTypeName = function () {
  53. return "FreeCameraDeviceOrientationInput";
  54. };
  55. FreeCameraDeviceOrientationInput.prototype.getSimpleName = function () {
  56. return "deviceOrientation";
  57. };
  58. return FreeCameraDeviceOrientationInput;
  59. }());
  60. BABYLON.FreeCameraDeviceOrientationInput = FreeCameraDeviceOrientationInput;
  61. BABYLON.CameraInputTypes["FreeCameraDeviceOrientationInput"] = FreeCameraDeviceOrientationInput;
  62. })(BABYLON || (BABYLON = {}));