babylon.freeCamera.js 16 KB

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