babylon.targetCamera.ts 12 KB

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