babylon.freeCamera.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. var __extends = this.__extends || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var FreeCamera = (function (_super) {
  10. __extends(FreeCamera, _super);
  11. function FreeCamera(name, position, scene) {
  12. _super.call(this, name, position, scene);
  13. this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
  14. this.cameraRotation = new BABYLON.Vector2(0, 0);
  15. this.rotation = new BABYLON.Vector3(0, 0, 0);
  16. this.ellipsoid = new BABYLON.Vector3(0.5, 1, 0.5);
  17. this.keysUp = [38];
  18. this.keysDown = [40];
  19. this.keysLeft = [37];
  20. this.keysRight = [39];
  21. this.speed = 2.0;
  22. this.checkCollisions = false;
  23. this.applyGravity = false;
  24. this.noRotationConstraint = false;
  25. this.angularSensibility = 2000.0;
  26. this.lockedTarget = null;
  27. this.onCollide = null;
  28. this._keys = [];
  29. this._collider = new BABYLON.Collider();
  30. this._needMoveForGravity = true;
  31. this._currentTarget = BABYLON.Vector3.Zero();
  32. this._viewMatrix = BABYLON.Matrix.Zero();
  33. this._camMatrix = BABYLON.Matrix.Zero();
  34. this._cameraTransformMatrix = BABYLON.Matrix.Zero();
  35. this._cameraRotationMatrix = BABYLON.Matrix.Zero();
  36. this._referencePoint = BABYLON.Vector3.Zero();
  37. this._transformedReferencePoint = BABYLON.Vector3.Zero();
  38. this._oldPosition = BABYLON.Vector3.Zero();
  39. this._diffPosition = BABYLON.Vector3.Zero();
  40. this._newPosition = BABYLON.Vector3.Zero();
  41. this._lookAtTemp = BABYLON.Matrix.Zero();
  42. this._tempMatrix = BABYLON.Matrix.Zero();
  43. }
  44. FreeCamera.prototype._getLockedTargetPosition = function () {
  45. if (!this.lockedTarget) {
  46. return null;
  47. }
  48. return this.lockedTarget.position || this.lockedTarget;
  49. };
  50. // Cache
  51. FreeCamera.prototype._initCache = function () {
  52. _super.prototype._initCache.call(this);
  53. this._cache.lockedTarget = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  54. this._cache.rotation = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  55. };
  56. FreeCamera.prototype._updateCache = function (ignoreParentClass) {
  57. if (!ignoreParentClass) {
  58. _super.prototype._updateCache.call(this);
  59. }
  60. var lockedTargetPosition = this._getLockedTargetPosition();
  61. if (!lockedTargetPosition) {
  62. this._cache.lockedTarget = null;
  63. } else {
  64. if (!this._cache.lockedTarget) {
  65. this._cache.lockedTarget = lockedTargetPosition.clone();
  66. } else {
  67. this._cache.lockedTarget.copyFrom(lockedTargetPosition);
  68. }
  69. }
  70. this._cache.rotation.copyFrom(this.rotation);
  71. };
  72. // Synchronized
  73. FreeCamera.prototype._isSynchronizedViewMatrix = function () {
  74. if (!_super.prototype._isSynchronizedViewMatrix.call(this)) {
  75. return false;
  76. }
  77. var lockedTargetPosition = this._getLockedTargetPosition();
  78. return (this._cache.lockedTarget ? this._cache.lockedTarget.equals(lockedTargetPosition) : !lockedTargetPosition) && this._cache.rotation.equals(this.rotation);
  79. };
  80. // Methods
  81. FreeCamera.prototype._computeLocalCameraSpeed = function () {
  82. return this.speed * ((BABYLON.Tools.GetDeltaTime() / (BABYLON.Tools.GetFps() * 10.0)));
  83. };
  84. // Target
  85. FreeCamera.prototype.setTarget = function (target) {
  86. this.upVector.normalize();
  87. BABYLON.Matrix.LookAtLHToRef(this.position, target, this.upVector, this._camMatrix);
  88. this._camMatrix.invert();
  89. this.rotation.x = Math.atan(this._camMatrix.m[6] / this._camMatrix.m[10]);
  90. var vDir = target.subtract(this.position);
  91. if (vDir.x >= 0.0) {
  92. this.rotation.y = (-Math.atan(vDir.z / vDir.x) + Math.PI / 2.0);
  93. } else {
  94. this.rotation.y = (-Math.atan(vDir.z / vDir.x) - Math.PI / 2.0);
  95. }
  96. this.rotation.z = -Math.acos(BABYLON.Vector3.Dot(new BABYLON.Vector3(0, 1.0, 0), this.upVector));
  97. if (isNaN(this.rotation.x)) {
  98. this.rotation.x = 0;
  99. }
  100. if (isNaN(this.rotation.y)) {
  101. this.rotation.y = 0;
  102. }
  103. if (isNaN(this.rotation.z)) {
  104. this.rotation.z = 0;
  105. }
  106. };
  107. FreeCamera.prototype.getTarget = function () {
  108. return this._currentTarget;
  109. };
  110. // Controls
  111. FreeCamera.prototype.attachControl = function (element, noPreventDefault) {
  112. var _this = this;
  113. var previousPosition;
  114. var engine = this.getEngine();
  115. if (this._attachedElement) {
  116. return;
  117. }
  118. this._attachedElement = element;
  119. if (this._onMouseDown === undefined) {
  120. this._onMouseDown = function (evt) {
  121. previousPosition = {
  122. x: evt.clientX,
  123. y: evt.clientY
  124. };
  125. if (!noPreventDefault) {
  126. evt.preventDefault();
  127. }
  128. };
  129. this._onMouseUp = function (evt) {
  130. previousPosition = null;
  131. if (!noPreventDefault) {
  132. evt.preventDefault();
  133. }
  134. };
  135. this._onMouseOut = function (evt) {
  136. previousPosition = null;
  137. _this._keys = [];
  138. if (!noPreventDefault) {
  139. evt.preventDefault();
  140. }
  141. };
  142. this._onMouseMove = function (evt) {
  143. if (!previousPosition && !engine.isPointerLock) {
  144. return;
  145. }
  146. var offsetX;
  147. var offsetY;
  148. if (!engine.isPointerLock) {
  149. offsetX = evt.clientX - previousPosition.x;
  150. offsetY = evt.clientY - previousPosition.y;
  151. } else {
  152. offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  153. offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  154. }
  155. _this.cameraRotation.y += offsetX / _this.angularSensibility;
  156. _this.cameraRotation.x += offsetY / _this.angularSensibility;
  157. previousPosition = {
  158. x: evt.clientX,
  159. y: evt.clientY
  160. };
  161. if (!noPreventDefault) {
  162. evt.preventDefault();
  163. }
  164. };
  165. this._onKeyDown = function (evt) {
  166. if (_this.keysUp.indexOf(evt.keyCode) !== -1 || _this.keysDown.indexOf(evt.keyCode) !== -1 || _this.keysLeft.indexOf(evt.keyCode) !== -1 || _this.keysRight.indexOf(evt.keyCode) !== -1) {
  167. var index = _this._keys.indexOf(evt.keyCode);
  168. if (index === -1) {
  169. _this._keys.push(evt.keyCode);
  170. }
  171. if (!noPreventDefault) {
  172. evt.preventDefault();
  173. }
  174. }
  175. };
  176. this._onKeyUp = function (evt) {
  177. if (_this.keysUp.indexOf(evt.keyCode) !== -1 || _this.keysDown.indexOf(evt.keyCode) !== -1 || _this.keysLeft.indexOf(evt.keyCode) !== -1 || _this.keysRight.indexOf(evt.keyCode) !== -1) {
  178. var index = _this._keys.indexOf(evt.keyCode);
  179. if (index >= 0) {
  180. _this._keys.splice(index, 1);
  181. }
  182. if (!noPreventDefault) {
  183. evt.preventDefault();
  184. }
  185. }
  186. };
  187. this._onLostFocus = function () {
  188. _this._keys = [];
  189. };
  190. this._reset = function () {
  191. _this._keys = [];
  192. previousPosition = null;
  193. _this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
  194. _this.cameraRotation = new BABYLON.Vector2(0, 0);
  195. };
  196. }
  197. element.addEventListener("mousedown", this._onMouseDown, false);
  198. element.addEventListener("mouseup", this._onMouseUp, false);
  199. element.addEventListener("mouseout", this._onMouseOut, false);
  200. element.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. FreeCamera.prototype.detachControl = function (element) {
  206. if (this._attachedElement != element) {
  207. return;
  208. }
  209. element.removeEventListener("mousedown", this._onMouseDown);
  210. element.removeEventListener("mouseup", this._onMouseUp);
  211. element.removeEventListener("mouseout", this._onMouseOut);
  212. element.removeEventListener("mousemove", this._onMouseMove);
  213. window.removeEventListener("keydown", this._onKeyDown);
  214. window.removeEventListener("keyup", this._onKeyUp);
  215. window.removeEventListener("blur", this._onLostFocus);
  216. this._attachedElement = null;
  217. if (this._reset) {
  218. this._reset();
  219. }
  220. };
  221. FreeCamera.prototype._collideWithWorld = function (velocity) {
  222. var globalPosition;
  223. if (this.parent) {
  224. globalPosition = BABYLON.Vector3.TransformCoordinates(this.position, this.parent.getWorldMatrix());
  225. } else {
  226. globalPosition = this.position;
  227. }
  228. globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPosition);
  229. this._collider.radius = this.ellipsoid;
  230. this.getScene()._getNewPosition(this._oldPosition, velocity, this._collider, 3, this._newPosition);
  231. this._newPosition.subtractToRef(this._oldPosition, this._diffPosition);
  232. if (this._diffPosition.length() > BABYLON.Engine.CollisionsEpsilon) {
  233. this.position.addInPlace(this._diffPosition);
  234. if (this.onCollide) {
  235. this.onCollide(this._collider.collidedMesh);
  236. }
  237. }
  238. };
  239. FreeCamera.prototype._checkInputs = function () {
  240. if (!this._localDirection) {
  241. this._localDirection = BABYLON.Vector3.Zero();
  242. this._transformedDirection = BABYLON.Vector3.Zero();
  243. }
  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. 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.getScene().collisionsEnabled) {
  268. this._collideWithWorld(this.cameraDirection);
  269. if (this.applyGravity) {
  270. var oldPosition = this.position;
  271. this._collideWithWorld(this.getScene().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. }
  295. if (Math.abs(this.cameraDirection.y) < BABYLON.Engine.Epsilon) {
  296. this.cameraDirection.y = 0;
  297. }
  298. if (Math.abs(this.cameraDirection.z) < BABYLON.Engine.Epsilon) {
  299. this.cameraDirection.z = 0;
  300. }
  301. this.cameraDirection.scaleInPlace(this.inertia);
  302. }
  303. if (needToRotate) {
  304. if (Math.abs(this.cameraRotation.x) < BABYLON.Engine.Epsilon) {
  305. this.cameraRotation.x = 0;
  306. }
  307. if (Math.abs(this.cameraRotation.y) < BABYLON.Engine.Epsilon) {
  308. this.cameraRotation.y = 0;
  309. }
  310. this.cameraRotation.scaleInPlace(this.inertia);
  311. }
  312. };
  313. FreeCamera.prototype._getViewMatrix = function () {
  314. BABYLON.Vector3.FromFloatsToRef(0, 0, 1, this._referencePoint);
  315. if (!this.lockedTarget) {
  316. // Compute
  317. if (this.upVector.x != 0 || this.upVector.y != 1.0 || this.upVector.z != 0) {
  318. BABYLON.Matrix.LookAtLHToRef(BABYLON.Vector3.Zero(), this._referencePoint, this.upVector, this._lookAtTemp);
  319. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  320. this._lookAtTemp.multiplyToRef(this._cameraRotationMatrix, this._tempMatrix);
  321. this._lookAtTemp.invert();
  322. this._tempMatrix.multiplyToRef(this._lookAtTemp, this._cameraRotationMatrix);
  323. } else {
  324. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  325. }
  326. BABYLON.Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
  327. // Computing target and final matrix
  328. this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
  329. } else {
  330. this._currentTarget.copyFrom(this._getLockedTargetPosition());
  331. }
  332. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
  333. return this._viewMatrix;
  334. };
  335. return FreeCamera;
  336. })(BABYLON.Camera);
  337. BABYLON.FreeCamera = FreeCamera;
  338. })(BABYLON || (BABYLON = {}));
  339. //# sourceMappingURL=babylon.freeCamera.js.map