babylon.freeCamera.ts 10 KB

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