babylon.freeCamera.js 15 KB

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