babylon.freeCamera.js 13 KB

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