babylon.deviceOrientationCamera.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 = 200000.0;
  14. this.moveSensibility = 500.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. if (!this._orientationChanged) {
  49. this._orientationChanged = function (evt) {
  50. if (!this._initialOrientationGamma) {
  51. this._initialOrientationGamma = evt.gamma;
  52. this._initialOrientationBeta = evt.beta;
  53. }
  54. this._orientationGamma = evt.gamma;
  55. this._orientationBeta = evt.beta;
  56. this._offsetY = (this._initialOrientationBeta - this._orientationBeta) * 0.05;
  57. this._offsetX = (this._initialOrientationGamma - this._orientationGamma) * -0.05;
  58. };
  59. }
  60. window.addEventListener("deviceorientation", this._orientationChanged);
  61. };
  62. BABYLON.DeviceOrientationCamera.prototype.detachControl = function (canvas) {
  63. if (this._attachedCanvas != canvas) {
  64. return;
  65. }
  66. window.removeEventListener("deviceorientation", this._orientationChanged);
  67. this._attachedCanvas = null;
  68. this._orientationGamma = 0;
  69. this._orientationBeta = 0;
  70. this._initialOrientationGamma = 0;
  71. this._initialOrientationBeta = 0;
  72. };
  73. BABYLON.DeviceOrientationCamera.prototype._checkInputs = function () {
  74. if (!this._offsetX) {
  75. return;
  76. }
  77. this.cameraRotation.y += this._offsetX / this.angularSensibility;
  78. if (this._pointerPressed.length > 1) {
  79. this.cameraRotation.x += -this._offsetY / this.angularSensibility;
  80. } else {
  81. var speed = this._computeLocalCameraSpeed();
  82. var direction = new BABYLON.Vector3(0, 0, speed * this._offsetY / this.moveSensibility);
  83. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, 0, this._cameraRotationMatrix);
  84. this.cameraDirection.addInPlace(BABYLON.Vector3.TransformCoordinates(direction, this._cameraRotationMatrix));
  85. }
  86. };
  87. })();