babylon.freeCamera.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.FreeCamera = function (name, position, scene) {
  5. BABYLON.Camera.call(this, name, position, scene);
  6. this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
  7. this.cameraRotation = new BABYLON.Vector2(0, 0);
  8. this.rotation = new BABYLON.Vector3(0, 0, 0);
  9. this.ellipsoid = new BABYLON.Vector3(0.5, 1, 0.5);
  10. this._keys = [];
  11. this.keysUp = [38];
  12. this.keysDown = [40];
  13. this.keysLeft = [37];
  14. this.keysRight = [39];
  15. // Collisions
  16. this._collider = new BABYLON.Collider();
  17. this._needMoveForGravity = true;
  18. // Internals
  19. this._currentTarget = BABYLON.Vector3.Zero();
  20. this._viewMatrix = BABYLON.Matrix.Zero();
  21. this._camMatrix = BABYLON.Matrix.Zero();
  22. this._cameraTransformMatrix = BABYLON.Matrix.Zero();
  23. this._cameraRotationMatrix = BABYLON.Matrix.Zero();
  24. this._referencePoint = BABYLON.Vector3.Zero();
  25. this._transformedReferencePoint = BABYLON.Vector3.Zero();
  26. this._oldPosition = BABYLON.Vector3.Zero();
  27. this._diffPosition = BABYLON.Vector3.Zero();
  28. this._newPosition = BABYLON.Vector3.Zero();
  29. this._lookAtTemp = BABYLON.Matrix.Zero();
  30. this._tempMatrix = BABYLON.Matrix.Zero();
  31. };
  32. BABYLON.FreeCamera.prototype = Object.create(BABYLON.Camera.prototype);
  33. // Members
  34. BABYLON.FreeCamera.prototype.speed = 2.0;
  35. BABYLON.FreeCamera.prototype.checkCollisions = false;
  36. BABYLON.FreeCamera.prototype.applyGravity = false;
  37. BABYLON.FreeCamera.prototype.noRotationConstraint = false;
  38. BABYLON.FreeCamera.prototype.angularSensibility = 2000.0;
  39. BABYLON.FreeCamera.prototype.lockedTarget = null;
  40. BABYLON.FreeCamera.prototype.onCollide = null;
  41. // Methods
  42. BABYLON.FreeCamera.prototype._computeLocalCameraSpeed = function () {
  43. return this.speed * ((BABYLON.Tools.GetDeltaTime() / (BABYLON.Tools.GetFps() * 10.0)));
  44. };
  45. // Target
  46. BABYLON.FreeCamera.prototype.setTarget = function (target) {
  47. this.upVector.normalize();
  48. BABYLON.Matrix.LookAtLHToRef(this.position, target, this.upVector, this._camMatrix);
  49. this._camMatrix.invert();
  50. this.rotation.x = Math.atan(this._camMatrix.m[6] / this._camMatrix.m[10]);
  51. var vDir = target.subtract(this.position);
  52. if (vDir.x >= 0.0) {
  53. this.rotation.y = (-Math.atan(vDir.z / vDir.x) + Math.PI / 2.0);
  54. } else {
  55. this.rotation.y = (-Math.atan(vDir.z / vDir.x) - Math.PI / 2.0);
  56. }
  57. this.rotation.z = -Math.acos(BABYLON.Vector3.Dot(new BABYLON.Vector3(0, 1.0, 0), this.upVector));
  58. if (isNaN(this.rotation.x))
  59. this.rotation.x = 0;
  60. if (isNaN(this.rotation.y))
  61. this.rotation.y = 0;
  62. if (isNaN(this.rotation.z))
  63. this.rotation.z = 0;
  64. };
  65. // Controls
  66. BABYLON.FreeCamera.prototype.attachControl = function (canvas, noPreventDefault) {
  67. var previousPosition;
  68. var that = this;
  69. var engine = this._scene.getEngine();
  70. if (this._attachedCanvas) {
  71. return;
  72. }
  73. this._attachedCanvas = canvas;
  74. if (this._onMouseDown === undefined) {
  75. this._onMouseDown = function (evt) {
  76. previousPosition = {
  77. x: evt.clientX,
  78. y: evt.clientY
  79. };
  80. if (!noPreventDefault) {
  81. evt.preventDefault();
  82. }
  83. };
  84. this._onMouseUp = function (evt) {
  85. previousPosition = null;
  86. if (!noPreventDefault) {
  87. evt.preventDefault();
  88. }
  89. };
  90. this._onMouseOut = function (evt) {
  91. previousPosition = null;
  92. that._keys = [];
  93. if (!noPreventDefault) {
  94. evt.preventDefault();
  95. }
  96. };
  97. this._onMouseMove = function (evt) {
  98. if (!previousPosition && !engine.isPointerLock) {
  99. return;
  100. }
  101. var offsetX;
  102. var offsetY;
  103. if (!engine.isPointerLock) {
  104. offsetX = evt.clientX - previousPosition.x;
  105. offsetY = evt.clientY - previousPosition.y;
  106. } else {
  107. offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  108. offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  109. }
  110. that.cameraRotation.y += offsetX / that.angularSensibility;
  111. that.cameraRotation.x += offsetY / that.angularSensibility;
  112. previousPosition = {
  113. x: evt.clientX,
  114. y: evt.clientY
  115. };
  116. if (!noPreventDefault) {
  117. evt.preventDefault();
  118. }
  119. };
  120. this._onKeyDown = function (evt) {
  121. if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
  122. that.keysDown.indexOf(evt.keyCode) !== -1 ||
  123. that.keysLeft.indexOf(evt.keyCode) !== -1 ||
  124. that.keysRight.indexOf(evt.keyCode) !== -1) {
  125. var index = that._keys.indexOf(evt.keyCode);
  126. if (index === -1) {
  127. that._keys.push(evt.keyCode);
  128. }
  129. if (!noPreventDefault) {
  130. evt.preventDefault();
  131. }
  132. }
  133. };
  134. this._onKeyUp = function (evt) {
  135. if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
  136. that.keysDown.indexOf(evt.keyCode) !== -1 ||
  137. that.keysLeft.indexOf(evt.keyCode) !== -1 ||
  138. that.keysRight.indexOf(evt.keyCode) !== -1) {
  139. var index = that._keys.indexOf(evt.keyCode);
  140. if (index >= 0) {
  141. that._keys.splice(index, 1);
  142. }
  143. if (!noPreventDefault) {
  144. evt.preventDefault();
  145. }
  146. }
  147. };
  148. this._onLostFocus = function () {
  149. that._keys = [];
  150. };
  151. this._reset = function() {
  152. that._keys = [];
  153. previousPosition = null;
  154. that.cameraDirection = new BABYLON.Vector3(0, 0, 0);
  155. that.cameraRotation = new BABYLON.Vector2(0, 0);
  156. };
  157. }
  158. canvas.addEventListener("mousedown", this._onMouseDown, false);
  159. canvas.addEventListener("mouseup", this._onMouseUp, false);
  160. canvas.addEventListener("mouseout", this._onMouseOut, false);
  161. canvas.addEventListener("mousemove", this._onMouseMove, false);
  162. window.addEventListener("keydown", this._onKeyDown, false);
  163. window.addEventListener("keyup", this._onKeyUp, false);
  164. window.addEventListener("blur", this._onLostFocus, false);
  165. };
  166. BABYLON.FreeCamera.prototype.detachControl = function (canvas) {
  167. if (this._attachedCanvas != canvas) {
  168. return;
  169. }
  170. canvas.removeEventListener("mousedown", this._onMouseDown);
  171. canvas.removeEventListener("mouseup", this._onMouseUp);
  172. canvas.removeEventListener("mouseout", this._onMouseOut);
  173. canvas.removeEventListener("mousemove", this._onMouseMove);
  174. window.removeEventListener("keydown", this._onKeyDown);
  175. window.removeEventListener("keyup", this._onKeyUp);
  176. window.removeEventListener("blur", this._onLostFocus);
  177. this._attachedCanvas = null;
  178. if (this._reset) {
  179. this._reset();
  180. }
  181. };
  182. BABYLON.FreeCamera.prototype._collideWithWorld = function (velocity) {
  183. this.position.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPosition);
  184. this._collider.radius = this.ellipsoid;
  185. this._scene._getNewPosition(this._oldPosition, velocity, this._collider, 3, this._newPosition);
  186. this._newPosition.subtractToRef(this._oldPosition, this._diffPosition);
  187. if (this._diffPosition.length() > BABYLON.Engine.collisionsEpsilon) {
  188. this.position.addInPlace(this._diffPosition);
  189. if (this.onCollide) {
  190. this.onCollide(this._collider.collidedMesh);
  191. }
  192. }
  193. };
  194. BABYLON.FreeCamera.prototype._checkInputs = function () {
  195. if (!this._localDirection) {
  196. this._localDirection = BABYLON.Vector3.Zero();
  197. this._transformedDirection = BABYLON.Vector3.Zero();
  198. }
  199. // Keyboard
  200. for (var index = 0; index < this._keys.length; index++) {
  201. var keyCode = this._keys[index];
  202. var speed = this._computeLocalCameraSpeed();
  203. if (this.keysLeft.indexOf(keyCode) !== -1) {
  204. this._localDirection.copyFromFloats(-speed, 0, 0);
  205. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  206. this._localDirection.copyFromFloats(0, 0, speed);
  207. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  208. this._localDirection.copyFromFloats(speed, 0, 0);
  209. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  210. this._localDirection.copyFromFloats(0, 0, -speed);
  211. }
  212. this.getViewMatrix().invertToRef(this._cameraTransformMatrix);
  213. BABYLON.Vector3.TransformNormalToRef(this._localDirection, this._cameraTransformMatrix, this._transformedDirection);
  214. this.cameraDirection.addInPlace(this._transformedDirection);
  215. }
  216. };
  217. BABYLON.FreeCamera.prototype._update = function () {
  218. this._checkInputs();
  219. var needToMove = this._needMoveForGravity || Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
  220. var needToRotate = Math.abs(this.cameraRotation.x) > 0 || Math.abs(this.cameraRotation.y) > 0;
  221. // Move
  222. if (needToMove) {
  223. if (this.checkCollisions && this._scene.collisionsEnabled) {
  224. this._collideWithWorld(this.cameraDirection);
  225. if (this.applyGravity) {
  226. var oldPosition = this.position;
  227. this._collideWithWorld(this._scene.gravity);
  228. this._needMoveForGravity = (BABYLON.Vector3.DistanceSquared(oldPosition, this.position) != 0);
  229. }
  230. } else {
  231. this.position.addInPlace(this.cameraDirection);
  232. }
  233. }
  234. // Rotate
  235. if (needToRotate) {
  236. this.rotation.x += this.cameraRotation.x;
  237. this.rotation.y += this.cameraRotation.y;
  238. if (!this.noRotationConstraint) {
  239. var limit = (Math.PI / 2) * 0.95;
  240. if (this.rotation.x > limit)
  241. this.rotation.x = limit;
  242. if (this.rotation.x < -limit)
  243. this.rotation.x = -limit;
  244. }
  245. }
  246. // Inertia
  247. if (needToMove) {
  248. this.cameraDirection.scaleInPlace(this.inertia);
  249. }
  250. if (needToRotate) {
  251. this.cameraRotation.scaleInPlace(this.inertia);
  252. }
  253. };
  254. BABYLON.FreeCamera.prototype._getViewMatrix = function () {
  255. BABYLON.Vector3.FromFloatsToRef(0, 0, 1, this._referencePoint);
  256. if (!this.lockedTarget) {
  257. // Compute
  258. if (this.upVector.x != 0 || this.upVector.y != 1.0 || this.upVector.z != 0) {
  259. BABYLON.Matrix.LookAtLHToRef(BABYLON.Vector3.Zero(), this._referencePoint, this.upVector, this._lookAtTemp);
  260. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  261. this._lookAtTemp.multiplyToRef(this._cameraRotationMatrix, this._tempMatrix);
  262. this._lookAtTemp.invert();
  263. this._tempMatrix.multiplyToRef(this._lookAtTemp, this._cameraRotationMatrix);
  264. } else {
  265. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  266. }
  267. BABYLON.Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
  268. // Computing target and final matrix
  269. this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
  270. } else {
  271. if (this.lockedTarget.position) {
  272. this._currentTarget.copyFrom(this.lockedTarget.position);
  273. } else {
  274. this._currentTarget.copyFrom(this.lockedTarget);
  275. }
  276. }
  277. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
  278. return this._viewMatrix;
  279. };
  280. })();