babylon.freeCamera.js 15 KB

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