babylon.freeCamera.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. //If this is a gravity-enabled camera AND this is the regular inspection, use the new position for grvity inspection.
  36. if (!fromGravity && _this.applyGravity) {
  37. _this._newPositionBuffer.copyFrom(newPosition);
  38. _this._newPositionBuffer.subtractToRef(_this._oldPosition, _this._diffPosition);
  39. _this.position.addToRef(_this._diffPosition, _this._newPositionBuffer);
  40. //send the gravity collision inspection.
  41. _this._collideWithWorld(_this.getScene().gravity, true);
  42. //don't update the position yet, to prevent "jumping".
  43. return;
  44. }
  45. var updatePosition = function (newPos) {
  46. _this._newPosition.copyFrom(newPos);
  47. _this._newPosition.subtractToRef(_this._oldPosition, _this._diffPosition);
  48. var oldPosition = _this.position.clone();
  49. if (_this._diffPosition.length() > BABYLON.Engine.CollisionsEpsilon) {
  50. _this.position.addInPlace(_this._diffPosition);
  51. if (_this.onCollide && collidedMesh) {
  52. _this.onCollide(collidedMesh);
  53. }
  54. }
  55. //check if it is the gravity inspection
  56. if (fromGravity) {
  57. _this._needMoveForGravity = (BABYLON.Vector3.DistanceSquared(oldPosition, _this.position) != 0);
  58. }
  59. };
  60. if (fromGravity) {
  61. //if arrived from gravity, use the buffered diffPosition that was created during the regular collision check.
  62. _this.position.addInPlace(_this._diffPosition);
  63. }
  64. updatePosition(newPosition);
  65. };
  66. }
  67. // Controls
  68. FreeCamera.prototype.attachControl = function (element, noPreventDefault) {
  69. var _this = this;
  70. var previousPosition;
  71. var engine = this.getEngine();
  72. if (this._attachedElement) {
  73. return;
  74. }
  75. this._attachedElement = element;
  76. if (this._onMouseDown === undefined) {
  77. this._onMouseDown = function (evt) {
  78. previousPosition = {
  79. x: evt.clientX,
  80. y: evt.clientY
  81. };
  82. if (!noPreventDefault) {
  83. evt.preventDefault();
  84. }
  85. };
  86. this._onMouseUp = function (evt) {
  87. previousPosition = null;
  88. if (!noPreventDefault) {
  89. evt.preventDefault();
  90. }
  91. };
  92. this._onMouseOut = function (evt) {
  93. previousPosition = null;
  94. _this._keys = [];
  95. if (!noPreventDefault) {
  96. evt.preventDefault();
  97. }
  98. };
  99. this._onMouseMove = function (evt) {
  100. if (!previousPosition && !engine.isPointerLock) {
  101. return;
  102. }
  103. var offsetX;
  104. var offsetY;
  105. if (!engine.isPointerLock) {
  106. offsetX = evt.clientX - previousPosition.x;
  107. offsetY = evt.clientY - previousPosition.y;
  108. }
  109. else {
  110. offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  111. offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  112. }
  113. _this.cameraRotation.y += offsetX / _this.angularSensibility;
  114. _this.cameraRotation.x += offsetY / _this.angularSensibility;
  115. previousPosition = {
  116. x: evt.clientX,
  117. y: evt.clientY
  118. };
  119. if (!noPreventDefault) {
  120. evt.preventDefault();
  121. }
  122. };
  123. this._onKeyDown = function (evt) {
  124. 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) {
  125. var index = _this._keys.indexOf(evt.keyCode);
  126. if (index === -1) {
  127. _this._keys.push(evt.keyCode);
  128. }
  129. if (!noPreventDefault) {
  130. evt.preventDefault();
  131. }
  132. }
  133. };
  134. this._onKeyUp = function (evt) {
  135. 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) {
  136. var index = _this._keys.indexOf(evt.keyCode);
  137. if (index >= 0) {
  138. _this._keys.splice(index, 1);
  139. }
  140. if (!noPreventDefault) {
  141. evt.preventDefault();
  142. }
  143. }
  144. };
  145. this._onLostFocus = function () {
  146. _this._keys = [];
  147. };
  148. this._reset = function () {
  149. _this._keys = [];
  150. previousPosition = null;
  151. _this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
  152. _this.cameraRotation = new BABYLON.Vector2(0, 0);
  153. };
  154. }
  155. element.addEventListener("mousedown", this._onMouseDown, false);
  156. element.addEventListener("mouseup", this._onMouseUp, false);
  157. element.addEventListener("mouseout", this._onMouseOut, false);
  158. element.addEventListener("mousemove", this._onMouseMove, false);
  159. BABYLON.Tools.RegisterTopRootEvents([
  160. { name: "keydown", handler: this._onKeyDown },
  161. { name: "keyup", handler: this._onKeyUp },
  162. { name: "blur", handler: this._onLostFocus }
  163. ]);
  164. };
  165. FreeCamera.prototype.detachControl = function (element) {
  166. if (this._attachedElement != element) {
  167. return;
  168. }
  169. element.removeEventListener("mousedown", this._onMouseDown);
  170. element.removeEventListener("mouseup", this._onMouseUp);
  171. element.removeEventListener("mouseout", this._onMouseOut);
  172. element.removeEventListener("mousemove", this._onMouseMove);
  173. BABYLON.Tools.UnregisterTopRootEvents([
  174. { name: "keydown", handler: this._onKeyDown },
  175. { name: "keyup", handler: this._onKeyUp },
  176. { name: "blur", handler: this._onLostFocus }
  177. ]);
  178. this._attachedElement = null;
  179. if (this._reset) {
  180. this._reset();
  181. }
  182. };
  183. FreeCamera.prototype._collideWithWorld = function (velocity, gravityInspection) {
  184. if (gravityInspection === void 0) { gravityInspection = false; }
  185. var globalPosition;
  186. if (this.parent) {
  187. globalPosition = BABYLON.Vector3.TransformCoordinates(gravityInspection ? this._newPositionBuffer : this.position, this.parent.getWorldMatrix());
  188. }
  189. else {
  190. globalPosition = this.position;
  191. }
  192. globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPosition);
  193. this._collider.radius = this.ellipsoid;
  194. this.getScene().collisionCoordinator.getNewPosition(this._oldPosition, velocity, this._collider, 3, null, this._onCollisionPositionChange, gravityInspection ? this.uniqueId + 100000 : this.uniqueId);
  195. };
  196. FreeCamera.prototype._checkInputs = function () {
  197. if (!this._localDirection) {
  198. this._localDirection = BABYLON.Vector3.Zero();
  199. this._transformedDirection = BABYLON.Vector3.Zero();
  200. }
  201. for (var index = 0; index < this._keys.length; index++) {
  202. var keyCode = this._keys[index];
  203. var speed = this._computeLocalCameraSpeed();
  204. if (this.keysLeft.indexOf(keyCode) !== -1) {
  205. this._localDirection.copyFromFloats(-speed, 0, 0);
  206. }
  207. else if (this.keysUp.indexOf(keyCode) !== -1) {
  208. this._localDirection.copyFromFloats(0, 0, speed);
  209. }
  210. else if (this.keysRight.indexOf(keyCode) !== -1) {
  211. this._localDirection.copyFromFloats(speed, 0, 0);
  212. }
  213. else if (this.keysDown.indexOf(keyCode) !== -1) {
  214. this._localDirection.copyFromFloats(0, 0, -speed);
  215. }
  216. this.getViewMatrix().invertToRef(this._cameraTransformMatrix);
  217. BABYLON.Vector3.TransformNormalToRef(this._localDirection, this._cameraTransformMatrix, this._transformedDirection);
  218. this.cameraDirection.addInPlace(this._transformedDirection);
  219. }
  220. };
  221. FreeCamera.prototype._decideIfNeedsToMove = function () {
  222. return this._needMoveForGravity || Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
  223. };
  224. FreeCamera.prototype._updatePosition = function () {
  225. if (this.checkCollisions && this.getScene().collisionsEnabled) {
  226. this._collideWithWorld(this.cameraDirection, false);
  227. }
  228. else {
  229. this.position.addInPlace(this.cameraDirection);
  230. }
  231. };
  232. FreeCamera.prototype._update = function () {
  233. this._checkInputs();
  234. _super.prototype._update.call(this);
  235. };
  236. return FreeCamera;
  237. })(BABYLON.TargetCamera);
  238. BABYLON.FreeCamera = FreeCamera;
  239. })(BABYLON || (BABYLON = {}));
  240. //# sourceMappingURL=babylon.freeCamera.js.map