babylon.freeCamera.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. var __extends = this.__extends || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var FreeCamera = (function (_super) {
  10. __extends(FreeCamera, _super);
  11. function FreeCamera(name, position, scene) {
  12. var _this = this;
  13. _super.call(this, name, position, scene);
  14. this.ellipsoid = new BABYLON.Vector3(0.5, 1, 0.5);
  15. this.keysUp = [38];
  16. this.keysDown = [40];
  17. this.keysLeft = [37];
  18. this.keysRight = [39];
  19. this.checkCollisions = false;
  20. this.applyGravity = false;
  21. this.angularSensibility = 2000.0;
  22. this._keys = [];
  23. this._collider = new BABYLON.Collider();
  24. this._needMoveForGravity = false;
  25. this._oldPosition = BABYLON.Vector3.Zero();
  26. this._diffPosition = BABYLON.Vector3.Zero();
  27. this._newPosition = BABYLON.Vector3.Zero();
  28. this._newPositionBuffer = BABYLON.Vector3.Zero();
  29. this._onCollisionPositionChange = function (collisionId, newPosition, collidedMesh) {
  30. if (collidedMesh === void 0) { collidedMesh = null; }
  31. var fromGravity = collisionId !== _this.uniqueId;
  32. //TODO move this to the collision coordinator!
  33. if (_this.getScene().workerCollisions)
  34. newPosition.multiplyInPlace(_this._collider.radius);
  35. var updatePosition = function (newPos) {
  36. _this._newPosition.copyFrom(newPos);
  37. _this._newPosition.subtractToRef(_this._oldPosition, _this._diffPosition);
  38. var oldPosition = _this.position.clone();
  39. if (_this._diffPosition.length() > BABYLON.Engine.CollisionsEpsilon) {
  40. _this.position.addInPlace(_this._diffPosition);
  41. if (_this.onCollide && collidedMesh) {
  42. _this.onCollide(collidedMesh);
  43. }
  44. }
  45. //check if it is the gravity inspection
  46. if (fromGravity) {
  47. _this._needMoveForGravity = (BABYLON.Vector3.DistanceSquared(oldPosition, _this.position) != 0);
  48. }
  49. };
  50. updatePosition(newPosition);
  51. };
  52. }
  53. // Controls
  54. FreeCamera.prototype.attachControl = function (element, noPreventDefault) {
  55. var _this = this;
  56. var previousPosition;
  57. var engine = this.getEngine();
  58. if (this._attachedElement) {
  59. return;
  60. }
  61. this._attachedElement = element;
  62. if (this._onMouseDown === undefined) {
  63. this._onMouseDown = function (evt) {
  64. previousPosition = {
  65. x: evt.clientX,
  66. y: evt.clientY
  67. };
  68. if (!noPreventDefault) {
  69. evt.preventDefault();
  70. }
  71. };
  72. this._onMouseUp = function (evt) {
  73. previousPosition = null;
  74. if (!noPreventDefault) {
  75. evt.preventDefault();
  76. }
  77. };
  78. this._onMouseOut = function (evt) {
  79. previousPosition = null;
  80. _this._keys = [];
  81. if (!noPreventDefault) {
  82. evt.preventDefault();
  83. }
  84. };
  85. this._onMouseMove = function (evt) {
  86. if (!previousPosition && !engine.isPointerLock) {
  87. return;
  88. }
  89. var offsetX;
  90. var offsetY;
  91. if (!engine.isPointerLock) {
  92. offsetX = evt.clientX - previousPosition.x;
  93. offsetY = evt.clientY - previousPosition.y;
  94. }
  95. else {
  96. offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  97. offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  98. }
  99. _this.cameraRotation.y += offsetX / _this.angularSensibility;
  100. _this.cameraRotation.x += offsetY / _this.angularSensibility;
  101. previousPosition = {
  102. x: evt.clientX,
  103. y: evt.clientY
  104. };
  105. if (!noPreventDefault) {
  106. evt.preventDefault();
  107. }
  108. };
  109. this._onKeyDown = function (evt) {
  110. if (_this.keysUp.indexOf(evt.keyCode) !== -1 || _this.keysDown.indexOf(evt.keyCode) !== -1 || _this.keysLeft.indexOf(evt.keyCode) !== -1 || _this.keysRight.indexOf(evt.keyCode) !== -1) {
  111. var index = _this._keys.indexOf(evt.keyCode);
  112. if (index === -1) {
  113. _this._keys.push(evt.keyCode);
  114. }
  115. if (!noPreventDefault) {
  116. evt.preventDefault();
  117. }
  118. }
  119. };
  120. this._onKeyUp = function (evt) {
  121. if (_this.keysUp.indexOf(evt.keyCode) !== -1 || _this.keysDown.indexOf(evt.keyCode) !== -1 || _this.keysLeft.indexOf(evt.keyCode) !== -1 || _this.keysRight.indexOf(evt.keyCode) !== -1) {
  122. var index = _this._keys.indexOf(evt.keyCode);
  123. if (index >= 0) {
  124. _this._keys.splice(index, 1);
  125. }
  126. if (!noPreventDefault) {
  127. evt.preventDefault();
  128. }
  129. }
  130. };
  131. this._onLostFocus = function () {
  132. _this._keys = [];
  133. };
  134. this._reset = function () {
  135. _this._keys = [];
  136. previousPosition = null;
  137. _this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
  138. _this.cameraRotation = new BABYLON.Vector2(0, 0);
  139. };
  140. }
  141. element.addEventListener("mousedown", this._onMouseDown, false);
  142. element.addEventListener("mouseup", this._onMouseUp, false);
  143. element.addEventListener("mouseout", this._onMouseOut, false);
  144. element.addEventListener("mousemove", this._onMouseMove, false);
  145. BABYLON.Tools.RegisterTopRootEvents([
  146. { name: "keydown", handler: this._onKeyDown },
  147. { name: "keyup", handler: this._onKeyUp },
  148. { name: "blur", handler: this._onLostFocus }
  149. ]);
  150. };
  151. FreeCamera.prototype.detachControl = function (element) {
  152. if (this._attachedElement != element) {
  153. return;
  154. }
  155. element.removeEventListener("mousedown", this._onMouseDown);
  156. element.removeEventListener("mouseup", this._onMouseUp);
  157. element.removeEventListener("mouseout", this._onMouseOut);
  158. element.removeEventListener("mousemove", this._onMouseMove);
  159. BABYLON.Tools.UnregisterTopRootEvents([
  160. { name: "keydown", handler: this._onKeyDown },
  161. { name: "keyup", handler: this._onKeyUp },
  162. { name: "blur", handler: this._onLostFocus }
  163. ]);
  164. this._attachedElement = null;
  165. if (this._reset) {
  166. this._reset();
  167. }
  168. };
  169. FreeCamera.prototype._collideWithWorld = function (velocity, gravityInspection) {
  170. if (gravityInspection === void 0) { gravityInspection = false; }
  171. var globalPosition;
  172. if (this.parent) {
  173. globalPosition = BABYLON.Vector3.TransformCoordinates(gravityInspection ? this._newPositionBuffer : this.position, this.parent.getWorldMatrix());
  174. }
  175. else {
  176. globalPosition = this.position;
  177. }
  178. globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPosition);
  179. this._collider.radius = this.ellipsoid;
  180. //in case we are using web workers, add gravity to the velocity to prevent the dual-collision checking
  181. if (this.getScene().workerCollisions) {
  182. velocity.addInPlace(this.getScene().gravity);
  183. }
  184. this.getScene().collisionCoordinator.getNewPosition(this._oldPosition, velocity, this._collider, 3, null, this._onCollisionPositionChange, gravityInspection ? this.uniqueId + 100000 : this.uniqueId);
  185. };
  186. FreeCamera.prototype._checkInputs = function () {
  187. if (!this._localDirection) {
  188. this._localDirection = BABYLON.Vector3.Zero();
  189. this._transformedDirection = BABYLON.Vector3.Zero();
  190. }
  191. for (var index = 0; index < this._keys.length; index++) {
  192. var keyCode = this._keys[index];
  193. var speed = this._computeLocalCameraSpeed();
  194. if (this.keysLeft.indexOf(keyCode) !== -1) {
  195. this._localDirection.copyFromFloats(-speed, 0, 0);
  196. }
  197. else if (this.keysUp.indexOf(keyCode) !== -1) {
  198. this._localDirection.copyFromFloats(0, 0, speed);
  199. }
  200. else if (this.keysRight.indexOf(keyCode) !== -1) {
  201. this._localDirection.copyFromFloats(speed, 0, 0);
  202. }
  203. else if (this.keysDown.indexOf(keyCode) !== -1) {
  204. this._localDirection.copyFromFloats(0, 0, -speed);
  205. }
  206. this.getViewMatrix().invertToRef(this._cameraTransformMatrix);
  207. BABYLON.Vector3.TransformNormalToRef(this._localDirection, this._cameraTransformMatrix, this._transformedDirection);
  208. this.cameraDirection.addInPlace(this._transformedDirection);
  209. }
  210. };
  211. FreeCamera.prototype._decideIfNeedsToMove = function () {
  212. return this._needMoveForGravity || Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
  213. };
  214. FreeCamera.prototype._updatePosition = function () {
  215. if (this.checkCollisions && this.getScene().collisionsEnabled) {
  216. this._collideWithWorld(this.cameraDirection, false);
  217. if (this.applyGravity && !this.getScene().workerCollisions) {
  218. this._collideWithWorld(this.getScene().gravity, true);
  219. }
  220. }
  221. else {
  222. this.position.addInPlace(this.cameraDirection);
  223. }
  224. };
  225. FreeCamera.prototype._update = function () {
  226. this._checkInputs();
  227. _super.prototype._update.call(this);
  228. };
  229. return FreeCamera;
  230. })(BABYLON.TargetCamera);
  231. BABYLON.FreeCamera = FreeCamera;
  232. })(BABYLON || (BABYLON = {}));
  233. //# sourceMappingURL=../Cameras/babylon.freeCamera.js.map