babylon.touchCamera.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.TouchCamera = function (name, position, scene) {
  5. BABYLON.FreeCamera.call(this, name, position, scene);
  6. // Offset
  7. this._offsetX = null;
  8. this._offsetY = null;
  9. this._pointerCount = 0;
  10. this._pointerPressed = [];
  11. };
  12. BABYLON.TouchCamera.prototype = Object.create(BABYLON.FreeCamera.prototype);
  13. BABYLON.TouchCamera.prototype.angularSensibility = 200000.0;
  14. BABYLON.TouchCamera.prototype.moveSensibility = 500.0;
  15. // Controls
  16. BABYLON.TouchCamera.prototype.attachControl = function (canvas, noPreventDefault) {
  17. var previousPosition;
  18. var that = this;
  19. if (this._attachedCanvas) {
  20. return;
  21. }
  22. this._attachedCanvas = canvas;
  23. if (this._onPointerDown === undefined) {
  24. this._onPointerDown = function(evt) {
  25. if (!noPreventDefault) {
  26. evt.preventDefault();
  27. }
  28. that._pointerPressed.push(evt.pointerId);
  29. if (that._pointerPressed.length !== 1) {
  30. return;
  31. }
  32. previousPosition = {
  33. x: evt.clientX,
  34. y: evt.clientY
  35. };
  36. };
  37. this._onPointerUp = function(evt) {
  38. if (!noPreventDefault) {
  39. evt.preventDefault();
  40. }
  41. var index = that._pointerPressed.indexOf(evt.pointerId);
  42. if (index === -1) {
  43. return;
  44. }
  45. that._pointerPressed.splice(index, 1);
  46. if (index != 0) {
  47. return;
  48. }
  49. previousPosition = null;
  50. that._offsetX = null;
  51. that._offsetY = null;
  52. };
  53. this._onPointerMove = function(evt) {
  54. if (!noPreventDefault) {
  55. evt.preventDefault();
  56. }
  57. if (!previousPosition) {
  58. return;
  59. }
  60. var index = that._pointerPressed.indexOf(evt.pointerId);
  61. if (index != 0) {
  62. return;
  63. }
  64. that._offsetX = evt.clientX - previousPosition.x;
  65. that._offsetY = -(evt.clientY - previousPosition.y);
  66. };
  67. this._onLostFocus = function() {
  68. that._offsetX = null;
  69. that._offsetY = null;
  70. };
  71. }
  72. canvas.addEventListener("pointerdown", this._onPointerDown);
  73. canvas.addEventListener("pointerup", this._onPointerUp);
  74. canvas.addEventListener("pointerout", this._onPointerUp);
  75. canvas.addEventListener("pointermove", this._onPointerMove);
  76. window.addEventListener("blur", this._onLostFocus);
  77. };
  78. BABYLON.TouchCamera.prototype.detachControl = function (canvas) {
  79. if (this._attachedCanvas != canvas) {
  80. return;
  81. }
  82. canvas.removeEventListener("pointerdown", this._onPointerDown);
  83. canvas.removeEventListener("pointerup", this._onPointerUp);
  84. canvas.removeEventListener("pointerout", this._onPointerUp);
  85. canvas.removeEventListener("pointermove", this._onPointerMove);
  86. window.removeEventListener("blur", this._onLostFocus);
  87. this._attachedCanvas = null;
  88. };
  89. BABYLON.TouchCamera.prototype._checkInputs = function () {
  90. if (!this._offsetX) {
  91. return;
  92. }
  93. this.cameraRotation.y += this._offsetX / this.angularSensibility;
  94. if (this._pointerPressed.length > 1) {
  95. this.cameraRotation.x += -this._offsetY / this.angularSensibility;
  96. } else {
  97. var speed = this._computeLocalCameraSpeed();
  98. var direction = new BABYLON.Vector3(0, 0, speed * this._offsetY / this.moveSensibility);
  99. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, 0, this._cameraRotationMatrix);
  100. this.cameraDirection.addInPlace(BABYLON.Vector3.TransformCoordinates(direction, this._cameraRotationMatrix));
  101. }
  102. };
  103. })();