babylon.touchCamera.js 4.1 KB

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