babylon.deviceOrientationCamera.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.DeviceOrientationCamera = function (name, position, scene) {
  4. this.name = name;
  5. this.id = name;
  6. this._scene = scene;
  7. this.position = position;
  8. scene.cameras.push(this);
  9. this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
  10. this.cameraRotation = new BABYLON.Vector2(0, 0);
  11. this.rotation = new BABYLON.Vector3(0, 0, 0);
  12. this.ellipsoid = new BABYLON.Vector3(0.5, 1, 0.5);
  13. this.angularSensibility = 10000.0;
  14. this.moveSensibility = 50.0;
  15. if (!scene.activeCamera) {
  16. scene.activeCamera = this;
  17. }
  18. // Collisions
  19. this._collider = new BABYLON.Collider();
  20. this._needMoveForGravity = true;
  21. // Offset
  22. this._offsetX = null;
  23. this._offsetY = null;
  24. this._orientationGamma = 0;
  25. this._orientationBeta = 0;
  26. this._initialOrientationGamma = 0;
  27. this._initialOrientationBeta = 0;
  28. // Animations
  29. this.animations = [];
  30. // Internals
  31. this._cameraRotationMatrix = new BABYLON.Matrix();
  32. this._referencePoint = BABYLON.Vector3.Zero();
  33. this._currentTarget = BABYLON.Vector3.Zero();
  34. this._transformedReferencePoint = BABYLON.Vector3.Zero();
  35. this._viewMatrix = BABYLON.Matrix.Zero();
  36. this._upVector = BABYLON.Vector3.Up();
  37. this._oldPosition = BABYLON.Vector3.Zero();
  38. this._diffPosition = BABYLON.Vector3.Zero();
  39. this._newPosition = BABYLON.Vector3.Zero();
  40. };
  41. BABYLON.DeviceOrientationCamera.prototype = Object.create(BABYLON.FreeCamera.prototype);
  42. // Controls
  43. BABYLON.DeviceOrientationCamera.prototype.attachControl = function (canvas, noPreventDefault) {
  44. if (this._attachedCanvas) {
  45. return;
  46. }
  47. this._attachedCanvas = canvas;
  48. var that = this;
  49. if (!this._orientationChanged) {
  50. this._orientationChanged = function (evt) {
  51. if (!that._initialOrientationGamma) {
  52. that._initialOrientationGamma = evt.gamma;
  53. that._initialOrientationBeta = evt.beta;
  54. }
  55. that._orientationGamma = evt.gamma;
  56. that._orientationBeta = evt.beta;
  57. that._offsetY = (that._initialOrientationBeta - that._orientationBeta);
  58. that._offsetX = (that._initialOrientationGamma - that._orientationGamma);
  59. };
  60. }
  61. window.addEventListener("deviceorientation", this._orientationChanged);
  62. };
  63. BABYLON.DeviceOrientationCamera.prototype.detachControl = function (canvas) {
  64. if (this._attachedCanvas != canvas) {
  65. return;
  66. }
  67. window.removeEventListener("deviceorientation", this._orientationChanged);
  68. this._attachedCanvas = null;
  69. this._orientationGamma = 0;
  70. this._orientationBeta = 0;
  71. this._initialOrientationGamma = 0;
  72. this._initialOrientationBeta = 0;
  73. };
  74. BABYLON.DeviceOrientationCamera.prototype._checkInputs = function () {
  75. if (!this._offsetX) {
  76. return;
  77. }
  78. this.cameraRotation.y -= this._offsetX / this.angularSensibility;
  79. var speed = this._computeLocalCameraSpeed();
  80. var direction = new BABYLON.Vector3(0, 0, speed * this._offsetY / this.moveSensibility);
  81. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, 0, this._cameraRotationMatrix);
  82. this.cameraDirection.addInPlace(BABYLON.Vector3.TransformCoordinates(direction, this._cameraRotationMatrix));
  83. };
  84. })();