babylon.touchCamera.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.TouchCamera = 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._pointerCount = 0;
  25. this._pointerPressed = [];
  26. // Animations
  27. this.animations = [];
  28. // Internals
  29. this._cameraRotationMatrix = new BABYLON.Matrix();
  30. };
  31. BABYLON.TouchCamera.prototype = Object.create(BABYLON.FreeCamera.prototype);
  32. // Controls
  33. BABYLON.TouchCamera.prototype.attachControl = function (canvas) {
  34. var previousPosition;
  35. var that = this;
  36. this._onPointerDown = function (evt) {
  37. evt.preventDefault();
  38. that._pointerPressed.push(evt.pointerId);
  39. if (that._pointerPressed.length !== 1) {
  40. return;
  41. }
  42. previousPosition = {
  43. x: evt.clientX,
  44. y: evt.clientY
  45. };
  46. };
  47. this._onPointerUp = function (evt) {
  48. evt.preventDefault();
  49. var index = that._pointerPressed.indexOf(evt.pointerId);
  50. if (index === -1) {
  51. return;
  52. }
  53. that._pointerPressed.splice(index, 1);
  54. if (index != 0) {
  55. return;
  56. }
  57. previousPosition = null;
  58. that._offsetX = null;
  59. that._offsetY = null;
  60. };
  61. this._onPointerMove = function (evt) {
  62. evt.preventDefault();
  63. if (!previousPosition) {
  64. return;
  65. }
  66. var index = that._pointerPressed.indexOf(evt.pointerId);
  67. if (index != 0) {
  68. return;
  69. }
  70. that._offsetX = evt.clientX - previousPosition.x;
  71. that._offsetY = -(evt.clientY - previousPosition.y);
  72. };
  73. this._onLostFocus = function () {
  74. that._offsetX = null;
  75. that._offsetY = null;
  76. };
  77. canvas.addEventListener("pointerdown", this._onPointerDown);
  78. canvas.addEventListener("pointerup", this._onPointerUp);
  79. canvas.addEventListener("pointerout", this._onPointerUp);
  80. canvas.addEventListener("pointermove", this._onPointerMove);
  81. window.addEventListener("blur", this._onLostFocus);
  82. };
  83. BABYLON.TouchCamera.prototype.detachControl = function (canvas) {
  84. canvas.removeEventListener("pointerdown", this._onPointerDown);
  85. canvas.removeEventListener("pointerup", this._onPointerUp);
  86. canvas.removeEventListener("pointerout", this._onPointerUp);
  87. canvas.removeEventListener("pointermove", this._onPointerMove);
  88. window.removeEventListener("blur", this._onLostFocus);
  89. };
  90. BABYLON.TouchCamera.prototype._checkInputs = function () {
  91. if (!this._offsetX) {
  92. return;
  93. }
  94. this.cameraRotation.y += this._offsetX / this.angularSensibility;
  95. if (this._pointerPressed.length > 1) {
  96. this.cameraRotation.x += -this._offsetY / this.angularSensibility;
  97. } else {
  98. var speed = this._computeLocalCameraSpeed();
  99. var direction = new BABYLON.Vector3(0, 0, speed * this._offsetY / this.moveSensibility);
  100. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, 0, this._cameraRotationMatrix);
  101. this.cameraDirection.addInPlace(BABYLON.Vector3.TransformCoordinates(direction, this._cameraRotationMatrix));
  102. }
  103. };
  104. })();