babylon.targetCamera.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. public rotationQuaternion: Quaternion;
  8. @serialize()
  9. public speed = 2.0;
  10. public noRotationConstraint = false;
  11. @serializeAsMeshReference("lockedTargetId")
  12. public lockedTarget: any = null;
  13. public _currentTarget = Vector3.Zero();
  14. public _viewMatrix = Matrix.Zero();
  15. public _camMatrix = Matrix.Zero();
  16. public _cameraTransformMatrix = Matrix.Zero();
  17. public _cameraRotationMatrix = Matrix.Zero();
  18. private _rigCamTransformMatrix: Matrix;
  19. public _referencePoint = new Vector3(0, 0, 1);
  20. private _currentUpVector = new Vector3(0, 1, 0);
  21. public _transformedReferencePoint = Vector3.Zero();
  22. public _lookAtTemp = Matrix.Zero();
  23. public _tempMatrix = Matrix.Zero();
  24. public _reset: () => void;
  25. constructor(name: string, position: Vector3, scene: Scene) {
  26. super(name, position, scene);
  27. }
  28. public getFrontPosition(distance: number): Vector3 {
  29. this.getWorldMatrix();
  30. var direction = this.getTarget().subtract(this.position);
  31. direction.normalize();
  32. direction.scaleInPlace(distance);
  33. return this.globalPosition.add(direction);
  34. }
  35. public _getLockedTargetPosition(): Nullable<Vector3> {
  36. if (!this.lockedTarget) {
  37. return null;
  38. }
  39. if (this.lockedTarget.absolutePosition) {
  40. this.lockedTarget.computeWorldMatrix();
  41. }
  42. return this.lockedTarget.absolutePosition || this.lockedTarget;
  43. }
  44. // State
  45. /**
  46. * Store current camera state (fov, position, etc..)
  47. */
  48. private _storedPosition: Vector3;
  49. private _storedRotation: Vector3;
  50. private _storedRotationQuaternion: Quaternion;
  51. public storeState(): Camera {
  52. this._storedPosition = this.position.clone();
  53. this._storedRotation = this.rotation.clone();
  54. if (this.rotationQuaternion) {
  55. this._storedRotationQuaternion = this.rotationQuaternion.clone();
  56. }
  57. return super.storeState();
  58. }
  59. /**
  60. * Restored camera state. You must call storeState() first
  61. */
  62. public _restoreStateValues(): boolean {
  63. if (!super._restoreStateValues()) {
  64. return false;
  65. }
  66. this.position = this._storedPosition.clone();
  67. this.rotation = this._storedRotation.clone();
  68. if (this.rotationQuaternion) {
  69. this.rotationQuaternion = this._storedRotationQuaternion.clone();
  70. }
  71. this.cameraDirection.copyFromFloats(0, 0, 0);
  72. this.cameraRotation.copyFromFloats(0, 0);
  73. return true;
  74. }
  75. // Cache
  76. public _initCache() {
  77. super._initCache();
  78. this._cache.lockedTarget = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  79. this._cache.rotation = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  80. this._cache.rotationQuaternion = new Quaternion(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  81. }
  82. public _updateCache(ignoreParentClass?: boolean): void {
  83. if (!ignoreParentClass) {
  84. super._updateCache();
  85. }
  86. var lockedTargetPosition = this._getLockedTargetPosition();
  87. if (!lockedTargetPosition) {
  88. this._cache.lockedTarget = null;
  89. }
  90. else {
  91. if (!this._cache.lockedTarget) {
  92. this._cache.lockedTarget = lockedTargetPosition.clone();
  93. }
  94. else {
  95. this._cache.lockedTarget.copyFrom(lockedTargetPosition);
  96. }
  97. }
  98. this._cache.rotation.copyFrom(this.rotation);
  99. if (this.rotationQuaternion)
  100. this._cache.rotationQuaternion.copyFrom(this.rotationQuaternion);
  101. }
  102. // Synchronized
  103. public _isSynchronizedViewMatrix(): boolean {
  104. if (!super._isSynchronizedViewMatrix()) {
  105. return false;
  106. }
  107. var lockedTargetPosition = this._getLockedTargetPosition();
  108. return (this._cache.lockedTarget ? this._cache.lockedTarget.equals(lockedTargetPosition) : !lockedTargetPosition)
  109. && (this.rotationQuaternion ? this.rotationQuaternion.equals(this._cache.rotationQuaternion) : this._cache.rotation.equals(this.rotation));
  110. }
  111. // Methods
  112. public _computeLocalCameraSpeed(): number {
  113. var engine = this.getEngine();
  114. return this.speed * Math.sqrt((engine.getDeltaTime() / (engine.getFps() * 100.0)));
  115. }
  116. // Target
  117. public setTarget(target: Vector3): void {
  118. this.upVector.normalize();
  119. Matrix.LookAtLHToRef(this.position, target, this.upVector, this._camMatrix);
  120. this._camMatrix.invert();
  121. this.rotation.x = Math.atan(this._camMatrix.m[6] / this._camMatrix.m[10]);
  122. var vDir = target.subtract(this.position);
  123. if (vDir.x >= 0.0) {
  124. this.rotation.y = (-Math.atan(vDir.z / vDir.x) + Math.PI / 2.0);
  125. } else {
  126. this.rotation.y = (-Math.atan(vDir.z / vDir.x) - Math.PI / 2.0);
  127. }
  128. this.rotation.z = 0;
  129. if (isNaN(this.rotation.x)) {
  130. this.rotation.x = 0;
  131. }
  132. if (isNaN(this.rotation.y)) {
  133. this.rotation.y = 0;
  134. }
  135. if (isNaN(this.rotation.z)) {
  136. this.rotation.z = 0;
  137. }
  138. if (this.rotationQuaternion) {
  139. Quaternion.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this.rotationQuaternion);
  140. }
  141. }
  142. /**
  143. * Return the current target position of the camera. This value is expressed in local space.
  144. */
  145. public getTarget(): Vector3 {
  146. return this._currentTarget;
  147. }
  148. public _decideIfNeedsToMove(): boolean {
  149. return Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
  150. }
  151. public _updatePosition(): void {
  152. if (this.parent) {
  153. this.parent.getWorldMatrix().invertToRef(Tmp.Matrix[0]);
  154. Vector3.TransformNormalToRef(this.cameraDirection, Tmp.Matrix[0], Tmp.Vector3[0]);
  155. this.position.addInPlace(Tmp.Vector3[0]);
  156. return;
  157. }
  158. this.position.addInPlace(this.cameraDirection);
  159. }
  160. public _checkInputs(): void {
  161. var needToMove = this._decideIfNeedsToMove();
  162. var needToRotate = Math.abs(this.cameraRotation.x) > 0 || Math.abs(this.cameraRotation.y) > 0;
  163. // Move
  164. if (needToMove) {
  165. this._updatePosition();
  166. }
  167. // Rotate
  168. if (needToRotate) {
  169. this.rotation.x += this.cameraRotation.x;
  170. this.rotation.y += this.cameraRotation.y;
  171. //rotate, if quaternion is set and rotation was used
  172. if (this.rotationQuaternion) {
  173. var len = this.rotation.lengthSquared();
  174. if (len) {
  175. Quaternion.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this.rotationQuaternion);
  176. }
  177. }
  178. if (!this.noRotationConstraint) {
  179. var limit = (Math.PI / 2) * 0.95;
  180. if (this.rotation.x > limit)
  181. this.rotation.x = limit;
  182. if (this.rotation.x < -limit)
  183. this.rotation.x = -limit;
  184. }
  185. }
  186. // Inertia
  187. if (needToMove) {
  188. if (Math.abs(this.cameraDirection.x) < this.speed * Epsilon) {
  189. this.cameraDirection.x = 0;
  190. }
  191. if (Math.abs(this.cameraDirection.y) < this.speed * Epsilon) {
  192. this.cameraDirection.y = 0;
  193. }
  194. if (Math.abs(this.cameraDirection.z) < this.speed * Epsilon) {
  195. this.cameraDirection.z = 0;
  196. }
  197. this.cameraDirection.scaleInPlace(this.inertia);
  198. }
  199. if (needToRotate) {
  200. if (Math.abs(this.cameraRotation.x) < this.speed * Epsilon) {
  201. this.cameraRotation.x = 0;
  202. }
  203. if (Math.abs(this.cameraRotation.y) < this.speed * Epsilon) {
  204. this.cameraRotation.y = 0;
  205. }
  206. this.cameraRotation.scaleInPlace(this.inertia);
  207. }
  208. super._checkInputs();
  209. }
  210. protected _updateCameraRotationMatrix() {
  211. if (this.rotationQuaternion) {
  212. this.rotationQuaternion.toRotationMatrix(this._cameraRotationMatrix);
  213. } else {
  214. Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  215. }
  216. //update the up vector!
  217. Vector3.TransformNormalToRef(this.upVector, this._cameraRotationMatrix, this._currentUpVector);
  218. }
  219. public _getViewMatrix(): Matrix {
  220. if (this.lockedTarget) {
  221. this.setTarget(this._getLockedTargetPosition()!);
  222. }
  223. // Compute
  224. this._updateCameraRotationMatrix();
  225. Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
  226. // Computing target and final matrix
  227. this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
  228. if (this.getScene().useRightHandedSystem) {
  229. Matrix.LookAtRHToRef(this.position, this._currentTarget, this._currentUpVector, this._viewMatrix);
  230. } else {
  231. Matrix.LookAtLHToRef(this.position, this._currentTarget, this._currentUpVector, this._viewMatrix);
  232. }
  233. return this._viewMatrix;
  234. }
  235. /**
  236. * @override
  237. * Override Camera.createRigCamera
  238. */
  239. public createRigCamera(name: string, cameraIndex: number): Nullable<Camera> {
  240. if (this.cameraRigMode !== Camera.RIG_MODE_NONE) {
  241. var rigCamera = new TargetCamera(name, this.position.clone(), this.getScene());
  242. if (this.cameraRigMode === Camera.RIG_MODE_VR || this.cameraRigMode === Camera.RIG_MODE_WEBVR) {
  243. if (!this.rotationQuaternion) {
  244. this.rotationQuaternion = new Quaternion();
  245. }
  246. rigCamera._cameraRigParams = {};
  247. rigCamera.rotationQuaternion = new Quaternion();
  248. }
  249. return rigCamera;
  250. }
  251. return null;
  252. }
  253. /**
  254. * @override
  255. * Override Camera._updateRigCameras
  256. */
  257. public _updateRigCameras() {
  258. var camLeft = <TargetCamera>this._rigCameras[0];
  259. var camRight = <TargetCamera>this._rigCameras[1];
  260. switch (this.cameraRigMode) {
  261. case Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  262. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  263. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  264. case Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  265. //provisionnaly using _cameraRigParams.stereoHalfAngle instead of calculations based on _cameraRigParams.interaxialDistance:
  266. var leftSign = (this.cameraRigMode === Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED) ? 1 : -1;
  267. var rightSign = (this.cameraRigMode === Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED) ? -1 : 1;
  268. this._getRigCamPosition(this._cameraRigParams.stereoHalfAngle * leftSign, camLeft.position);
  269. this._getRigCamPosition(this._cameraRigParams.stereoHalfAngle * rightSign, camRight.position);
  270. camLeft.setTarget(this.getTarget());
  271. camRight.setTarget(this.getTarget());
  272. break;
  273. case Camera.RIG_MODE_VR:
  274. if (camLeft.rotationQuaternion) {
  275. camLeft.rotationQuaternion.copyFrom(this.rotationQuaternion);
  276. camRight.rotationQuaternion.copyFrom(this.rotationQuaternion);
  277. } else {
  278. camLeft.rotation.copyFrom(this.rotation);
  279. camRight.rotation.copyFrom(this.rotation);
  280. }
  281. camLeft.position.copyFrom(this.position);
  282. camRight.position.copyFrom(this.position);
  283. break;
  284. }
  285. super._updateRigCameras();
  286. }
  287. private _getRigCamPosition(halfSpace: number, result: Vector3) {
  288. if (!this._rigCamTransformMatrix) {
  289. this._rigCamTransformMatrix = new Matrix();
  290. }
  291. var target = this.getTarget();
  292. Matrix.Translation(-target.x, -target.y, -target.z).multiplyToRef(Matrix.RotationY(halfSpace), this._rigCamTransformMatrix);
  293. this._rigCamTransformMatrix = this._rigCamTransformMatrix.multiply(Matrix.Translation(target.x, target.y, target.z));
  294. Vector3.TransformCoordinatesToRef(this.position, this._rigCamTransformMatrix, result);
  295. }
  296. public getClassName(): string {
  297. return "TargetCamera";
  298. }
  299. }
  300. }