babylon.freeCamera.js 15 KB

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