babylon.freeCamera.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. var __extends = (this && 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._onCollisionPositionChange = function (collisionId, newPosition, collidedMesh) {
  29. if (collidedMesh === void 0) { collidedMesh = null; }
  30. //TODO move this to the collision coordinator!
  31. if (_this.getScene().workerCollisions)
  32. newPosition.multiplyInPlace(_this._collider.radius);
  33. var updatePosition = function (newPos) {
  34. _this._newPosition.copyFrom(newPos);
  35. _this._newPosition.subtractToRef(_this._oldPosition, _this._diffPosition);
  36. var oldPosition = _this.position.clone();
  37. if (_this._diffPosition.length() > BABYLON.Engine.CollisionsEpsilon) {
  38. _this.position.addInPlace(_this._diffPosition);
  39. if (_this.onCollide && collidedMesh) {
  40. _this.onCollide(collidedMesh);
  41. }
  42. }
  43. };
  44. updatePosition(newPosition);
  45. };
  46. }
  47. // Controls
  48. FreeCamera.prototype.attachControl = function (element, noPreventDefault) {
  49. var _this = this;
  50. var previousPosition;
  51. var engine = this.getEngine();
  52. if (this._attachedElement) {
  53. return;
  54. }
  55. this._attachedElement = element;
  56. if (this._onMouseDown === undefined) {
  57. this._onMouseDown = function (evt) {
  58. previousPosition = {
  59. x: evt.clientX,
  60. y: evt.clientY
  61. };
  62. if (!noPreventDefault) {
  63. evt.preventDefault();
  64. }
  65. };
  66. this._onMouseUp = function (evt) {
  67. previousPosition = null;
  68. if (!noPreventDefault) {
  69. evt.preventDefault();
  70. }
  71. };
  72. this._onMouseOut = function (evt) {
  73. previousPosition = null;
  74. _this._keys = [];
  75. if (!noPreventDefault) {
  76. evt.preventDefault();
  77. }
  78. };
  79. this._onMouseMove = function (evt) {
  80. if (!previousPosition && !engine.isPointerLock) {
  81. return;
  82. }
  83. var offsetX;
  84. var offsetY;
  85. if (!engine.isPointerLock) {
  86. offsetX = evt.clientX - previousPosition.x;
  87. offsetY = evt.clientY - previousPosition.y;
  88. }
  89. else {
  90. offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  91. offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  92. }
  93. _this.cameraRotation.y += offsetX / _this.angularSensibility;
  94. _this.cameraRotation.x += offsetY / _this.angularSensibility;
  95. previousPosition = {
  96. x: evt.clientX,
  97. y: evt.clientY
  98. };
  99. if (!noPreventDefault) {
  100. evt.preventDefault();
  101. }
  102. };
  103. this._onKeyDown = function (evt) {
  104. if (_this.keysUp.indexOf(evt.keyCode) !== -1 ||
  105. _this.keysDown.indexOf(evt.keyCode) !== -1 ||
  106. _this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  107. _this.keysRight.indexOf(evt.keyCode) !== -1) {
  108. var index = _this._keys.indexOf(evt.keyCode);
  109. if (index === -1) {
  110. _this._keys.push(evt.keyCode);
  111. }
  112. if (!noPreventDefault) {
  113. evt.preventDefault();
  114. }
  115. }
  116. };
  117. this._onKeyUp = function (evt) {
  118. if (_this.keysUp.indexOf(evt.keyCode) !== -1 ||
  119. _this.keysDown.indexOf(evt.keyCode) !== -1 ||
  120. _this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  121. _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) {
  170. var globalPosition;
  171. if (this.parent) {
  172. globalPosition = BABYLON.Vector3.TransformCoordinates(this.position, this.parent.getWorldMatrix());
  173. }
  174. else {
  175. globalPosition = this.position;
  176. }
  177. globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPosition);
  178. this._collider.radius = this.ellipsoid;
  179. //add gravity to the velocity to prevent the dual-collision checking
  180. if (this.applyGravity) {
  181. velocity.addInPlace(this.getScene().gravity);
  182. }
  183. this.getScene().collisionCoordinator.getNewPosition(this._oldPosition, velocity, this._collider, 3, null, this._onCollisionPositionChange, this.uniqueId);
  184. };
  185. FreeCamera.prototype._checkInputs = function () {
  186. if (!this._localDirection) {
  187. this._localDirection = BABYLON.Vector3.Zero();
  188. this._transformedDirection = BABYLON.Vector3.Zero();
  189. }
  190. // Keyboard
  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. _super.prototype._checkInputs.call(this);
  211. };
  212. FreeCamera.prototype._decideIfNeedsToMove = function () {
  213. return this._needMoveForGravity || Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
  214. };
  215. FreeCamera.prototype._updatePosition = function () {
  216. if (this.checkCollisions && this.getScene().collisionsEnabled) {
  217. this._collideWithWorld(this.cameraDirection);
  218. }
  219. else {
  220. this.position.addInPlace(this.cameraDirection);
  221. }
  222. };
  223. return FreeCamera;
  224. })(BABYLON.TargetCamera);
  225. BABYLON.FreeCamera = FreeCamera;
  226. })(BABYLON || (BABYLON = {}));
  227. //# sourceMappingURL=babylon.freeCamera.js.map