babylon.freeCamera.js 11 KB

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