babylon.targetCamera.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. var __extends = (this && 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 TargetCamera = (function (_super) {
  10. __extends(TargetCamera, _super);
  11. function TargetCamera(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.speed = 2.0;
  17. this.noRotationConstraint = false;
  18. this.lockedTarget = null;
  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 = new BABYLON.Vector3(0, 0, 1);
  25. this._transformedReferencePoint = BABYLON.Vector3.Zero();
  26. this._lookAtTemp = BABYLON.Matrix.Zero();
  27. this._tempMatrix = BABYLON.Matrix.Zero();
  28. }
  29. TargetCamera.prototype._getLockedTargetPosition = function () {
  30. if (!this.lockedTarget) {
  31. return null;
  32. }
  33. return this.lockedTarget.position || this.lockedTarget;
  34. };
  35. // Cache
  36. TargetCamera.prototype._initCache = function () {
  37. _super.prototype._initCache.call(this);
  38. this._cache.lockedTarget = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  39. this._cache.rotation = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  40. };
  41. TargetCamera.prototype._updateCache = function (ignoreParentClass) {
  42. if (!ignoreParentClass) {
  43. _super.prototype._updateCache.call(this);
  44. }
  45. var lockedTargetPosition = this._getLockedTargetPosition();
  46. if (!lockedTargetPosition) {
  47. this._cache.lockedTarget = null;
  48. }
  49. else {
  50. if (!this._cache.lockedTarget) {
  51. this._cache.lockedTarget = lockedTargetPosition.clone();
  52. }
  53. else {
  54. this._cache.lockedTarget.copyFrom(lockedTargetPosition);
  55. }
  56. }
  57. this._cache.rotation.copyFrom(this.rotation);
  58. };
  59. // Synchronized
  60. TargetCamera.prototype._isSynchronizedViewMatrix = function () {
  61. if (!_super.prototype._isSynchronizedViewMatrix.call(this)) {
  62. return false;
  63. }
  64. var lockedTargetPosition = this._getLockedTargetPosition();
  65. return (this._cache.lockedTarget ? this._cache.lockedTarget.equals(lockedTargetPosition) : !lockedTargetPosition)
  66. && this._cache.rotation.equals(this.rotation);
  67. };
  68. // Methods
  69. TargetCamera.prototype._computeLocalCameraSpeed = function () {
  70. var engine = this.getEngine();
  71. return this.speed * ((engine.getDeltaTime() / (engine.getFps() * 10.0)));
  72. };
  73. // Target
  74. TargetCamera.prototype.setTarget = function (target) {
  75. this.upVector.normalize();
  76. BABYLON.Matrix.LookAtLHToRef(this.position, target, this.upVector, this._camMatrix);
  77. this._camMatrix.invert();
  78. this.rotation.x = Math.atan(this._camMatrix.m[6] / this._camMatrix.m[10]);
  79. var vDir = target.subtract(this.position);
  80. if (vDir.x >= 0.0) {
  81. this.rotation.y = (-Math.atan(vDir.z / vDir.x) + Math.PI / 2.0);
  82. }
  83. else {
  84. this.rotation.y = (-Math.atan(vDir.z / vDir.x) - Math.PI / 2.0);
  85. }
  86. this.rotation.z = -Math.acos(BABYLON.Vector3.Dot(new BABYLON.Vector3(0, 1.0, 0), this.upVector));
  87. if (isNaN(this.rotation.x)) {
  88. this.rotation.x = 0;
  89. }
  90. if (isNaN(this.rotation.y)) {
  91. this.rotation.y = 0;
  92. }
  93. if (isNaN(this.rotation.z)) {
  94. this.rotation.z = 0;
  95. }
  96. };
  97. TargetCamera.prototype.getTarget = function () {
  98. return this._currentTarget;
  99. };
  100. TargetCamera.prototype._decideIfNeedsToMove = function () {
  101. return Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
  102. };
  103. TargetCamera.prototype._updatePosition = function () {
  104. this.position.addInPlace(this.cameraDirection);
  105. };
  106. TargetCamera.prototype._checkInputs = function () {
  107. var needToMove = this._decideIfNeedsToMove();
  108. var needToRotate = Math.abs(this.cameraRotation.x) > 0 || Math.abs(this.cameraRotation.y) > 0;
  109. // Move
  110. if (needToMove) {
  111. this._updatePosition();
  112. }
  113. // Rotate
  114. if (needToRotate) {
  115. this.rotation.x += this.cameraRotation.x;
  116. this.rotation.y += this.cameraRotation.y;
  117. if (!this.noRotationConstraint) {
  118. var limit = (Math.PI / 2) * 0.95;
  119. if (this.rotation.x > limit)
  120. this.rotation.x = limit;
  121. if (this.rotation.x < -limit)
  122. this.rotation.x = -limit;
  123. }
  124. }
  125. // Inertia
  126. if (needToMove) {
  127. if (Math.abs(this.cameraDirection.x) < BABYLON.Engine.Epsilon) {
  128. this.cameraDirection.x = 0;
  129. }
  130. if (Math.abs(this.cameraDirection.y) < BABYLON.Engine.Epsilon) {
  131. this.cameraDirection.y = 0;
  132. }
  133. if (Math.abs(this.cameraDirection.z) < BABYLON.Engine.Epsilon) {
  134. this.cameraDirection.z = 0;
  135. }
  136. this.cameraDirection.scaleInPlace(this.inertia);
  137. }
  138. if (needToRotate) {
  139. if (Math.abs(this.cameraRotation.x) < BABYLON.Engine.Epsilon) {
  140. this.cameraRotation.x = 0;
  141. }
  142. if (Math.abs(this.cameraRotation.y) < BABYLON.Engine.Epsilon) {
  143. this.cameraRotation.y = 0;
  144. }
  145. this.cameraRotation.scaleInPlace(this.inertia);
  146. }
  147. _super.prototype._checkInputs.call(this);
  148. };
  149. TargetCamera.prototype._getViewMatrix = function () {
  150. if (!this.lockedTarget) {
  151. // Compute
  152. if (this.upVector.x !== 0 || this.upVector.y !== 1.0 || this.upVector.z !== 0) {
  153. BABYLON.Matrix.LookAtLHToRef(BABYLON.Vector3.Zero(), this._referencePoint, this.upVector, this._lookAtTemp);
  154. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  155. this._lookAtTemp.multiplyToRef(this._cameraRotationMatrix, this._tempMatrix);
  156. this._lookAtTemp.invert();
  157. this._tempMatrix.multiplyToRef(this._lookAtTemp, this._cameraRotationMatrix);
  158. }
  159. else {
  160. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  161. }
  162. BABYLON.Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
  163. // Computing target and final matrix
  164. this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
  165. }
  166. else {
  167. this._currentTarget.copyFrom(this._getLockedTargetPosition());
  168. }
  169. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
  170. return this._viewMatrix;
  171. };
  172. TargetCamera.prototype._getVRViewMatrix = function () {
  173. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  174. BABYLON.Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
  175. BABYLON.Vector3.TransformNormalToRef(this.upVector, this._cameraRotationMatrix, this._cameraRigParams.vrActualUp);
  176. // Computing target and final matrix
  177. this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
  178. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this._cameraRigParams.vrActualUp, this._cameraRigParams.vrWorkMatrix);
  179. this._cameraRigParams.vrWorkMatrix.multiplyToRef(this._cameraRigParams.vrPreViewMatrix, this._viewMatrix);
  180. return this._viewMatrix;
  181. };
  182. /**
  183. * @override
  184. * Override Camera.createRigCamera
  185. */
  186. TargetCamera.prototype.createRigCamera = function (name, cameraIndex) {
  187. if (this.cameraRigMode !== BABYLON.Camera.RIG_MODE_NONE) {
  188. var rigCamera = new TargetCamera(name, this.position.clone(), this.getScene());
  189. if (this.cameraRigMode === BABYLON.Camera.RIG_MODE_VR) {
  190. rigCamera._cameraRigParams.vrActualUp = new BABYLON.Vector3(0, 0, 0);
  191. rigCamera._getViewMatrix = rigCamera._getVRViewMatrix;
  192. }
  193. return rigCamera;
  194. }
  195. return null;
  196. };
  197. /**
  198. * @override
  199. * Override Camera._updateRigCameras
  200. */
  201. TargetCamera.prototype._updateRigCameras = function () {
  202. switch (this.cameraRigMode) {
  203. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  204. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  205. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  206. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  207. case BABYLON.Camera.RIG_MODE_VR:
  208. var camLeft = this._rigCameras[0];
  209. var camRight = this._rigCameras[1];
  210. if (this.cameraRigMode === BABYLON.Camera.RIG_MODE_VR) {
  211. camLeft.rotation.x = camRight.rotation.x = this.rotation.x;
  212. camLeft.rotation.y = camRight.rotation.y = this.rotation.y;
  213. camLeft.rotation.z = camRight.rotation.z = this.rotation.z;
  214. camLeft.position.copyFrom(this.position);
  215. camRight.position.copyFrom(this.position);
  216. }
  217. else {
  218. camLeft.setTarget(this.getTarget());
  219. camRight.setTarget(this.getTarget());
  220. //provisionnaly using _cameraRigParams.stereoHalfAngle instead of calculations based on _cameraRigParams.interaxialDistance:
  221. this._getRigCamPosition(-this._cameraRigParams.stereoHalfAngle, camLeft.position);
  222. this._getRigCamPosition(this._cameraRigParams.stereoHalfAngle, camRight.position);
  223. }
  224. break;
  225. }
  226. _super.prototype._updateRigCameras.call(this);
  227. };
  228. TargetCamera.prototype._getRigCamPosition = function (halfSpace, result) {
  229. if (!this._rigCamTransformMatrix) {
  230. this._rigCamTransformMatrix = new BABYLON.Matrix();
  231. }
  232. var target = this.getTarget();
  233. BABYLON.Matrix.Translation(-target.x, -target.y, -target.z).multiplyToRef(BABYLON.Matrix.RotationY(halfSpace), this._rigCamTransformMatrix);
  234. this._rigCamTransformMatrix = this._rigCamTransformMatrix.multiply(BABYLON.Matrix.Translation(target.x, target.y, target.z));
  235. BABYLON.Vector3.TransformCoordinatesToRef(this.position, this._rigCamTransformMatrix, result);
  236. };
  237. return TargetCamera;
  238. })(BABYLON.Camera);
  239. BABYLON.TargetCamera = TargetCamera;
  240. })(BABYLON || (BABYLON = {}));
  241. //# sourceMappingURL=babylon.targetCamera.js.map