babylon.freeCamera.js 10.0 KB

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