babylon.freeCamera.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. module BABYLON {
  2. export class FreeCamera extends TargetCamera {
  3. public ellipsoid = new BABYLON.Vector3(0.5, 1, 0.5);
  4. public keysUp = [38];
  5. public keysDown = [40];
  6. public keysLeft = [37];
  7. public keysRight = [39];
  8. public checkCollisions = false;
  9. public applyGravity = false;
  10. public angularSensibility = 2000.0;
  11. public onCollide: (collidedMesh: AbstractMesh) => void;
  12. private _keys = [];
  13. private _collider = new Collider();
  14. private _needMoveForGravity = false;
  15. private _oldPosition = BABYLON.Vector3.Zero();
  16. private _diffPosition = BABYLON.Vector3.Zero();
  17. private _newPosition = BABYLON.Vector3.Zero();
  18. private _newPositionBuffer = BABYLON.Vector3.Zero();
  19. private _attachedElement: HTMLElement;
  20. private _localDirection: Vector3;
  21. private _transformedDirection: Vector3;
  22. private _onMouseDown: (e: MouseEvent) => any;
  23. private _onMouseUp: (e: MouseEvent) => any;
  24. private _onMouseOut: (e: MouseEvent) => any;
  25. private _onMouseMove: (e: MouseEvent) => any;
  26. private _onKeyDown: (e: KeyboardEvent) => any;
  27. private _onKeyUp: (e: KeyboardEvent) => any;
  28. public _onLostFocus: (e: FocusEvent) => any;
  29. public _waitingLockedTargetId: string;
  30. constructor(name: string, position: Vector3, scene: Scene) {
  31. super(name, position, scene);
  32. }
  33. // Controls
  34. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  35. var previousPosition;
  36. var engine = this.getEngine();
  37. if (this._attachedElement) {
  38. return;
  39. }
  40. this._attachedElement = element;
  41. if (this._onMouseDown === undefined) {
  42. this._onMouseDown = evt => {
  43. previousPosition = {
  44. x: evt.clientX,
  45. y: evt.clientY
  46. };
  47. if (!noPreventDefault) {
  48. evt.preventDefault();
  49. }
  50. };
  51. this._onMouseUp = evt => {
  52. previousPosition = null;
  53. if (!noPreventDefault) {
  54. evt.preventDefault();
  55. }
  56. };
  57. this._onMouseOut = evt => {
  58. previousPosition = null;
  59. this._keys = [];
  60. if (!noPreventDefault) {
  61. evt.preventDefault();
  62. }
  63. };
  64. this._onMouseMove = evt => {
  65. if (!previousPosition && !engine.isPointerLock) {
  66. return;
  67. }
  68. var offsetX;
  69. var offsetY;
  70. if (!engine.isPointerLock) {
  71. offsetX = evt.clientX - previousPosition.x;
  72. offsetY = evt.clientY - previousPosition.y;
  73. } else {
  74. offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  75. offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  76. }
  77. this.cameraRotation.y += offsetX / this.angularSensibility;
  78. this.cameraRotation.x += offsetY / this.angularSensibility;
  79. previousPosition = {
  80. x: evt.clientX,
  81. y: evt.clientY
  82. };
  83. if (!noPreventDefault) {
  84. evt.preventDefault();
  85. }
  86. };
  87. this._onKeyDown = evt => {
  88. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  89. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  90. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  91. this.keysRight.indexOf(evt.keyCode) !== -1) {
  92. var index = this._keys.indexOf(evt.keyCode);
  93. if (index === -1) {
  94. this._keys.push(evt.keyCode);
  95. }
  96. if (!noPreventDefault) {
  97. evt.preventDefault();
  98. }
  99. }
  100. };
  101. this._onKeyUp = evt => {
  102. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  103. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  104. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  105. this.keysRight.indexOf(evt.keyCode) !== -1) {
  106. var index = this._keys.indexOf(evt.keyCode);
  107. if (index >= 0) {
  108. this._keys.splice(index, 1);
  109. }
  110. if (!noPreventDefault) {
  111. evt.preventDefault();
  112. }
  113. }
  114. };
  115. this._onLostFocus = () => {
  116. this._keys = [];
  117. };
  118. this._reset = () => {
  119. this._keys = [];
  120. previousPosition = null;
  121. this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
  122. this.cameraRotation = new BABYLON.Vector2(0, 0);
  123. };
  124. }
  125. element.addEventListener("mousedown", this._onMouseDown, false);
  126. element.addEventListener("mouseup", this._onMouseUp, false);
  127. element.addEventListener("mouseout", this._onMouseOut, false);
  128. element.addEventListener("mousemove", this._onMouseMove, false);
  129. Tools.RegisterTopRootEvents([
  130. { name: "keydown", handler: this._onKeyDown },
  131. { name: "keyup", handler: this._onKeyUp },
  132. { name: "blur", handler: this._onLostFocus }
  133. ]);
  134. }
  135. public detachControl(element: HTMLElement): void {
  136. if (this._attachedElement != element) {
  137. return;
  138. }
  139. element.removeEventListener("mousedown", this._onMouseDown);
  140. element.removeEventListener("mouseup", this._onMouseUp);
  141. element.removeEventListener("mouseout", this._onMouseOut);
  142. element.removeEventListener("mousemove", this._onMouseMove);
  143. Tools.UnregisterTopRootEvents([
  144. { name: "keydown", handler: this._onKeyDown },
  145. { name: "keyup", handler: this._onKeyUp },
  146. { name: "blur", handler: this._onLostFocus }
  147. ]);
  148. this._attachedElement = null;
  149. if (this._reset) {
  150. this._reset();
  151. }
  152. }
  153. public _collideWithWorld(velocity: Vector3, gravityInspection: boolean = false): void {
  154. var globalPosition: Vector3;
  155. if (this.parent) {
  156. globalPosition = BABYLON.Vector3.TransformCoordinates(gravityInspection ? this._newPositionBuffer : this.position, this.parent.getWorldMatrix());
  157. } else {
  158. globalPosition = this.position;
  159. }
  160. globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPosition);
  161. this._collider.radius = this.ellipsoid;
  162. this.getScene().collisionCoordinator.getNewPosition(this._oldPosition, velocity, this._collider, 3, null, this._onCollisionPositionChange, gravityInspection ? this.uniqueId + 100000 : this.uniqueId);
  163. }
  164. private _onCollisionPositionChange = (collisionId: number, newPosition: Vector3, collidedMesh: AbstractMesh = null) => {
  165. var fromGravity: boolean = collisionId !== this.uniqueId;
  166. //TODO move this to the collision coordinator!
  167. if (this.getScene().workerCollisions)
  168. newPosition.multiplyInPlace(this._collider.radius);
  169. //If this is a gravity-enabled camera AND this is the regular inspection, use the new position for grvity inspection.
  170. if (!fromGravity && this.applyGravity) {
  171. this._newPositionBuffer.copyFrom(newPosition);
  172. this._newPositionBuffer.subtractToRef(this._oldPosition, this._diffPosition);
  173. this.position.addToRef(this._diffPosition, this._newPositionBuffer);
  174. //send the gravity collision inspection.
  175. this._collideWithWorld(this.getScene().gravity, true);
  176. //don't update the position yet, to prevent "jumping".
  177. return;
  178. }
  179. var updatePosition = (newPos) => {
  180. this._newPosition.copyFrom(newPos);
  181. this._newPosition.subtractToRef(this._oldPosition, this._diffPosition);
  182. var oldPosition = this.position.clone();
  183. if (this._diffPosition.length() > Engine.CollisionsEpsilon) {
  184. this.position.addInPlace(this._diffPosition);
  185. if (this.onCollide && collidedMesh) {
  186. this.onCollide(collidedMesh);
  187. }
  188. }
  189. //check if it is the gravity inspection
  190. if (fromGravity) {
  191. this._needMoveForGravity = (BABYLON.Vector3.DistanceSquared(oldPosition, this.position) != 0);
  192. }
  193. }
  194. if (fromGravity) {
  195. //if arrived from gravity, use the buffered diffPosition that was created during the regular collision check.
  196. this.position.addInPlace(this._diffPosition);
  197. }
  198. updatePosition(newPosition);
  199. }
  200. public _checkInputs(): void {
  201. if (!this._localDirection) {
  202. this._localDirection = BABYLON.Vector3.Zero();
  203. this._transformedDirection = BABYLON.Vector3.Zero();
  204. }
  205. // Keyboard
  206. for (var index = 0; index < this._keys.length; index++) {
  207. var keyCode = this._keys[index];
  208. var speed = this._computeLocalCameraSpeed();
  209. if (this.keysLeft.indexOf(keyCode) !== -1) {
  210. this._localDirection.copyFromFloats(-speed, 0, 0);
  211. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  212. this._localDirection.copyFromFloats(0, 0, speed);
  213. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  214. this._localDirection.copyFromFloats(speed, 0, 0);
  215. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  216. this._localDirection.copyFromFloats(0, 0, -speed);
  217. }
  218. this.getViewMatrix().invertToRef(this._cameraTransformMatrix);
  219. BABYLON.Vector3.TransformNormalToRef(this._localDirection, this._cameraTransformMatrix, this._transformedDirection);
  220. this.cameraDirection.addInPlace(this._transformedDirection);
  221. }
  222. }
  223. public _decideIfNeedsToMove(): boolean {
  224. return this._needMoveForGravity || Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
  225. }
  226. public _updatePosition(): void {
  227. if (this.checkCollisions && this.getScene().collisionsEnabled) {
  228. this._collideWithWorld(this.cameraDirection, false);
  229. //if (this.applyGravity) {
  230. // this._collideWithWorld(this.getScene().gravity, true);
  231. //}
  232. } else {
  233. this.position.addInPlace(this.cameraDirection);
  234. }
  235. }
  236. public _update(): void {
  237. this._checkInputs();
  238. super._update();
  239. }
  240. }
  241. }