babylon.touchCamera.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. this._referencePoint = BABYLON.Vector3.Zero();
  31. this._currentTarget = BABYLON.Vector3.Zero();
  32. this._camMatrix = BABYLON.Matrix.Zero();
  33. this._transformedReferencePoint = BABYLON.Vector3.Zero();
  34. this._viewMatrix = BABYLON.Matrix.Zero();
  35. this._upVector = BABYLON.Vector3.Up();
  36. this._oldPosition = BABYLON.Vector3.Zero();
  37. this._diffPosition = BABYLON.Vector3.Zero();
  38. this._newPosition = BABYLON.Vector3.Zero();
  39. };
  40. BABYLON.TouchCamera.prototype = Object.create(BABYLON.FreeCamera.prototype);
  41. // Controls
  42. BABYLON.TouchCamera.prototype.attachControl = function (canvas, noPreventDefault) {
  43. var previousPosition;
  44. var that = this;
  45. if (this._attachedCanvas) {
  46. return;
  47. }
  48. this._attachedCanvas = canvas;
  49. if (this._onPointerDown === undefined) {
  50. this._onPointerDown = function(evt) {
  51. if (!noPreventDefault) {
  52. evt.preventDefault();
  53. }
  54. that._pointerPressed.push(evt.pointerId);
  55. if (that._pointerPressed.length !== 1) {
  56. return;
  57. }
  58. previousPosition = {
  59. x: evt.clientX,
  60. y: evt.clientY
  61. };
  62. };
  63. this._onPointerUp = function(evt) {
  64. if (!noPreventDefault) {
  65. evt.preventDefault();
  66. }
  67. var index = that._pointerPressed.indexOf(evt.pointerId);
  68. if (index === -1) {
  69. return;
  70. }
  71. that._pointerPressed.splice(index, 1);
  72. if (index != 0) {
  73. return;
  74. }
  75. previousPosition = null;
  76. that._offsetX = null;
  77. that._offsetY = null;
  78. };
  79. this._onPointerMove = function(evt) {
  80. if (!noPreventDefault) {
  81. evt.preventDefault();
  82. }
  83. if (!previousPosition) {
  84. return;
  85. }
  86. var index = that._pointerPressed.indexOf(evt.pointerId);
  87. if (index != 0) {
  88. return;
  89. }
  90. that._offsetX = evt.clientX - previousPosition.x;
  91. that._offsetY = -(evt.clientY - previousPosition.y);
  92. };
  93. this._onLostFocus = function() {
  94. that._offsetX = null;
  95. that._offsetY = null;
  96. };
  97. }
  98. canvas.addEventListener("pointerdown", this._onPointerDown);
  99. canvas.addEventListener("pointerup", this._onPointerUp);
  100. canvas.addEventListener("pointerout", this._onPointerUp);
  101. canvas.addEventListener("pointermove", this._onPointerMove);
  102. window.addEventListener("blur", this._onLostFocus);
  103. };
  104. BABYLON.TouchCamera.prototype.detachControl = function (canvas) {
  105. if (this._attachedCanvas != canvas) {
  106. return;
  107. }
  108. canvas.removeEventListener("pointerdown", this._onPointerDown);
  109. canvas.removeEventListener("pointerup", this._onPointerUp);
  110. canvas.removeEventListener("pointerout", this._onPointerUp);
  111. canvas.removeEventListener("pointermove", this._onPointerMove);
  112. window.removeEventListener("blur", this._onLostFocus);
  113. this._attachedCanvas = null;
  114. };
  115. BABYLON.TouchCamera.prototype._checkInputs = function () {
  116. if (!this._offsetX) {
  117. return;
  118. }
  119. this.cameraRotation.y += this._offsetX / this.angularSensibility;
  120. if (this._pointerPressed.length > 1) {
  121. this.cameraRotation.x += -this._offsetY / this.angularSensibility;
  122. } else {
  123. var speed = this._computeLocalCameraSpeed();
  124. var direction = new BABYLON.Vector3(0, 0, speed * this._offsetY / this.moveSensibility);
  125. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, 0, this._cameraRotationMatrix);
  126. this.cameraDirection.addInPlace(BABYLON.Vector3.TransformCoordinates(direction, this._cameraRotationMatrix));
  127. }
  128. };
  129. })();