babylon.freeCamera.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 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. } else {
  70. offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  71. offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  72. }
  73. _this.cameraRotation.y += offsetX / _this.angularSensibility;
  74. _this.cameraRotation.x += offsetY / _this.angularSensibility;
  75. previousPosition = {
  76. x: evt.clientX,
  77. y: evt.clientY
  78. };
  79. if (!noPreventDefault) {
  80. evt.preventDefault();
  81. }
  82. };
  83. this._onKeyDown = function (evt) {
  84. 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) {
  85. var index = _this._keys.indexOf(evt.keyCode);
  86. if (index === -1) {
  87. _this._keys.push(evt.keyCode);
  88. }
  89. if (!noPreventDefault) {
  90. evt.preventDefault();
  91. }
  92. }
  93. };
  94. this._onKeyUp = function (evt) {
  95. 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) {
  96. var index = _this._keys.indexOf(evt.keyCode);
  97. if (index >= 0) {
  98. _this._keys.splice(index, 1);
  99. }
  100. if (!noPreventDefault) {
  101. evt.preventDefault();
  102. }
  103. }
  104. };
  105. this._onLostFocus = function () {
  106. _this._keys = [];
  107. };
  108. this._reset = function () {
  109. _this._keys = [];
  110. previousPosition = null;
  111. _this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
  112. _this.cameraRotation = new BABYLON.Vector2(0, 0);
  113. };
  114. }
  115. element.addEventListener("mousedown", this._onMouseDown, false);
  116. element.addEventListener("mouseup", this._onMouseUp, false);
  117. element.addEventListener("mouseout", this._onMouseOut, false);
  118. element.addEventListener("mousemove", this._onMouseMove, false);
  119. Tools.RegisterTopRootEvents([
  120. { name: "keydown", handler: this._onKeyDown },
  121. { name: "keyup", handler: this._onKeyUp },
  122. { name: "blur", handler: this._onLostFocus }
  123. ]);
  124. };
  125. FreeCamera.prototype.detachControl = function (element) {
  126. if (this._attachedElement != element) {
  127. return;
  128. }
  129. element.removeEventListener("mousedown", this._onMouseDown);
  130. element.removeEventListener("mouseup", this._onMouseUp);
  131. element.removeEventListener("mouseout", this._onMouseOut);
  132. element.removeEventListener("mousemove", this._onMouseMove);
  133. Tools.UnregisterTopRootEvents([
  134. { name: "keydown", handler: this._onKeyDown },
  135. { name: "keyup", handler: this._onKeyUp },
  136. { name: "blur", handler: this._onLostFocus }
  137. ]);
  138. this._attachedElement = null;
  139. if (this._reset) {
  140. this._reset();
  141. }
  142. };
  143. FreeCamera.prototype._collideWithWorld = function (velocity) {
  144. var globalPosition;
  145. if (this.parent) {
  146. globalPosition = BABYLON.Vector3.TransformCoordinates(this.position, this.parent.getWorldMatrix());
  147. } else {
  148. globalPosition = this.position;
  149. }
  150. globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPosition);
  151. this._collider.radius = this.ellipsoid;
  152. this.getScene()._getNewPosition(this._oldPosition, velocity, this._collider, 3, this._newPosition);
  153. this._newPosition.subtractToRef(this._oldPosition, this._diffPosition);
  154. if (this._diffPosition.length() > Engine.CollisionsEpsilon) {
  155. this.position.addInPlace(this._diffPosition);
  156. if (this.onCollide) {
  157. this.onCollide(this._collider.collidedMesh);
  158. }
  159. }
  160. };
  161. FreeCamera.prototype._checkInputs = function () {
  162. if (!this._localDirection) {
  163. this._localDirection = BABYLON.Vector3.Zero();
  164. this._transformedDirection = BABYLON.Vector3.Zero();
  165. }
  166. for (var index = 0; index < this._keys.length; index++) {
  167. var keyCode = this._keys[index];
  168. var speed = this._computeLocalCameraSpeed();
  169. if (this.keysLeft.indexOf(keyCode) !== -1) {
  170. this._localDirection.copyFromFloats(-speed, 0, 0);
  171. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  172. this._localDirection.copyFromFloats(0, 0, speed);
  173. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  174. this._localDirection.copyFromFloats(speed, 0, 0);
  175. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  176. this._localDirection.copyFromFloats(0, 0, -speed);
  177. }
  178. this.getViewMatrix().invertToRef(this._cameraTransformMatrix);
  179. BABYLON.Vector3.TransformNormalToRef(this._localDirection, this._cameraTransformMatrix, this._transformedDirection);
  180. this.cameraDirection.addInPlace(this._transformedDirection);
  181. }
  182. };
  183. FreeCamera.prototype._decideIfNeedsToMove = function () {
  184. return this._needMoveForGravity || Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
  185. };
  186. FreeCamera.prototype._updatePosition = function () {
  187. if (this.checkCollisions && this.getScene().collisionsEnabled) {
  188. this._collideWithWorld(this.cameraDirection);
  189. if (this.applyGravity) {
  190. var oldPosition = this.position;
  191. this._collideWithWorld(this.getScene().gravity);
  192. this._needMoveForGravity = (BABYLON.Vector3.DistanceSquared(oldPosition, this.position) != 0);
  193. }
  194. } else {
  195. this.position.addInPlace(this.cameraDirection);
  196. }
  197. };
  198. FreeCamera.prototype._update = function () {
  199. this._checkInputs();
  200. _super.prototype._update.call(this);
  201. };
  202. return FreeCamera;
  203. })(TargetCamera);
  204. BABYLON.FreeCamera = FreeCamera;
  205. })(BABYLON || (BABYLON = {}));