babylon.freeCamera.js 13 KB

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