babylon.freeCamera.js 15 KB

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