babylon.freeCamera.js 10 KB

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