babylon.targetCamera.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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._vrActualUp);
  176. // Computing target and final matrix
  177. this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
  178. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this._vrActualUp, this._vrWorkMatrix);
  179. this._vrWorkMatrix.multiplyToRef(this._vrPreViewMatrix, this._viewMatrix);
  180. return this._viewMatrix;
  181. };
  182. /**
  183. * @override
  184. * needs to be overridden, so sub has required properties to be copied
  185. */
  186. TargetCamera.prototype.getSubCamera = function (name, isA) {
  187. var subCamera = new BABYLON.TargetCamera(name, this.position.clone(), this.getScene());
  188. if (this._subCameraMode === BABYLON.Camera.SUB_CAMERA_MODE_VR) {
  189. subCamera._vrActualUp = new BABYLON.Vector3(0, 0, 0);
  190. subCamera._getViewMatrix = subCamera._getVRViewMatrix;
  191. }
  192. return subCamera;
  193. };
  194. /**
  195. * @override
  196. * needs to be overridden, adding copy of position, and rotation for VR, or target for rest
  197. */
  198. TargetCamera.prototype._updateSubCameras = function () {
  199. var camA = this.subCameras[BABYLON.Camera.SUB_CAMERAID_A];
  200. var camB = this.subCameras[BABYLON.Camera.SUB_CAMERAID_B];
  201. if (this._subCameraMode === BABYLON.Camera.SUB_CAMERA_MODE_VR) {
  202. camA.rotation.x = camB.rotation.x = this.rotation.x;
  203. camA.rotation.y = camB.rotation.y = this.rotation.y;
  204. camA.rotation.z = camB.rotation.z = this.rotation.z;
  205. camA.position.copyFrom(this.position);
  206. camB.position.copyFrom(this.position);
  207. }
  208. else {
  209. camA.setTarget(this.getTarget());
  210. camB.setTarget(this.getTarget());
  211. this._getSubCamPosition(-this._subCamHalfSpace, camA.position);
  212. this._getSubCamPosition(this._subCamHalfSpace, camB.position);
  213. }
  214. _super.prototype._updateSubCameras.call(this);
  215. };
  216. TargetCamera.prototype._getSubCamPosition = function (halfSpace, result) {
  217. if (!this._subCamTransformMatrix) {
  218. this._subCamTransformMatrix = new BABYLON.Matrix();
  219. }
  220. var target = this.getTarget();
  221. BABYLON.Matrix.Translation(-target.x, -target.y, -target.z).multiplyToRef(BABYLON.Matrix.RotationY(halfSpace), this._subCamTransformMatrix);
  222. this._subCamTransformMatrix = this._subCamTransformMatrix.multiply(BABYLON.Matrix.Translation(target.x, target.y, target.z));
  223. BABYLON.Vector3.TransformCoordinatesToRef(this.position, this._subCamTransformMatrix, result);
  224. };
  225. return TargetCamera;
  226. })(BABYLON.Camera);
  227. BABYLON.TargetCamera = TargetCamera;
  228. })(BABYLON || (BABYLON = {}));
  229. //# sourceMappingURL=babylon.targetCamera.js.map