babylon.freeCamera.js 15 KB

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