babylon.camera.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. module BABYLON {
  2. export class Camera extends Node {
  3. // Statics
  4. private static _PERSPECTIVE_CAMERA = 0;
  5. private static _ORTHOGRAPHIC_CAMERA = 1;
  6. private static _FOVMODE_VERTICAL_FIXED = 0;
  7. private static _FOVMODE_HORIZONTAL_FIXED = 1;
  8. public static get PERSPECTIVE_CAMERA(): number {
  9. return Camera._PERSPECTIVE_CAMERA;
  10. }
  11. public static get ORTHOGRAPHIC_CAMERA(): number {
  12. return Camera._ORTHOGRAPHIC_CAMERA;
  13. }
  14. public static get FOVMODE_VERTICAL_FIXED(): number {
  15. return Camera._FOVMODE_VERTICAL_FIXED;
  16. }
  17. public static get FOVMODE_HORIZONTAL_FIXED(): number {
  18. return Camera._FOVMODE_HORIZONTAL_FIXED;
  19. }
  20. // Members
  21. public upVector = Vector3.Up();
  22. public orthoLeft = null;
  23. public orthoRight = null;
  24. public orthoBottom = null;
  25. public orthoTop = null;
  26. public fov = 0.8;
  27. public minZ = 1.0;
  28. public maxZ = 10000.0;
  29. public inertia = 0.9;
  30. public mode = Camera.PERSPECTIVE_CAMERA;
  31. public isIntermediate = false;
  32. public viewport = new Viewport(0, 0, 1.0, 1.0);
  33. public subCameras = [];
  34. public layerMask: number = 0xFFFFFFFF;
  35. public fovMode: number = Camera.FOVMODE_VERTICAL_FIXED;
  36. private _computedViewMatrix = Matrix.Identity();
  37. public _projectionMatrix = new Matrix();
  38. private _worldMatrix: Matrix;
  39. public _postProcesses = new Array<PostProcess>();
  40. public _postProcessesTakenIndices = [];
  41. public _activeMeshes = new SmartArray<Mesh>(256);
  42. constructor(name: string, public position: Vector3, scene: Scene) {
  43. super(name, scene);
  44. scene.cameras.push(this);
  45. if (!scene.activeCamera) {
  46. scene.activeCamera = this;
  47. }
  48. }
  49. public getActiveMeshes(): SmartArray<Mesh> {
  50. return this._activeMeshes;
  51. }
  52. public isActiveMesh(mesh: Mesh): boolean {
  53. return (this._activeMeshes.indexOf(mesh) !== -1);
  54. }
  55. //Cache
  56. public _initCache() {
  57. super._initCache();
  58. this._cache.position = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  59. this._cache.upVector = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  60. this._cache.mode = undefined;
  61. this._cache.minZ = undefined;
  62. this._cache.maxZ = undefined;
  63. this._cache.fov = undefined;
  64. this._cache.aspectRatio = undefined;
  65. this._cache.orthoLeft = undefined;
  66. this._cache.orthoRight = undefined;
  67. this._cache.orthoBottom = undefined;
  68. this._cache.orthoTop = undefined;
  69. this._cache.renderWidth = undefined;
  70. this._cache.renderHeight = undefined;
  71. }
  72. public _updateCache(ignoreParentClass?: boolean): void {
  73. if (!ignoreParentClass) {
  74. super._updateCache();
  75. }
  76. var engine = this.getEngine();
  77. this._cache.position.copyFrom(this.position);
  78. this._cache.upVector.copyFrom(this.upVector);
  79. this._cache.mode = this.mode;
  80. this._cache.minZ = this.minZ;
  81. this._cache.maxZ = this.maxZ;
  82. this._cache.fov = this.fov;
  83. this._cache.aspectRatio = engine.getAspectRatio(this);
  84. this._cache.orthoLeft = this.orthoLeft;
  85. this._cache.orthoRight = this.orthoRight;
  86. this._cache.orthoBottom = this.orthoBottom;
  87. this._cache.orthoTop = this.orthoTop;
  88. this._cache.renderWidth = engine.getRenderWidth();
  89. this._cache.renderHeight = engine.getRenderHeight();
  90. }
  91. public _updateFromScene(): void {
  92. this.updateCache();
  93. this._update();
  94. }
  95. // Synchronized
  96. public _isSynchronized(): boolean {
  97. return this._isSynchronizedViewMatrix() && this._isSynchronizedProjectionMatrix();
  98. }
  99. public _isSynchronizedViewMatrix(): boolean {
  100. if (!super._isSynchronized())
  101. return false;
  102. return this._cache.position.equals(this.position)
  103. && this._cache.upVector.equals(this.upVector)
  104. && this.isSynchronizedWithParent();
  105. }
  106. public _isSynchronizedProjectionMatrix(): boolean {
  107. var check = this._cache.mode === this.mode
  108. && this._cache.minZ === this.minZ
  109. && this._cache.maxZ === this.maxZ;
  110. if (!check) {
  111. return false;
  112. }
  113. var engine = this.getEngine();
  114. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  115. check = this._cache.fov === this.fov
  116. && this._cache.aspectRatio === engine.getAspectRatio(this);
  117. }
  118. else {
  119. check = this._cache.orthoLeft === this.orthoLeft
  120. && this._cache.orthoRight === this.orthoRight
  121. && this._cache.orthoBottom === this.orthoBottom
  122. && this._cache.orthoTop === this.orthoTop
  123. && this._cache.renderWidth === engine.getRenderWidth()
  124. && this._cache.renderHeight === engine.getRenderHeight();
  125. }
  126. return check;
  127. }
  128. // Controls
  129. public attachControl(element: HTMLElement): void {
  130. }
  131. public detachControl(element: HTMLElement): void {
  132. }
  133. public _update(): void {
  134. }
  135. public attachPostProcess(postProcess: PostProcess, insertAt: number = null): number {
  136. if (!postProcess.isReusable() && this._postProcesses.indexOf(postProcess) > -1) {
  137. Tools.Error("You're trying to reuse a post process not defined as reusable.");
  138. return 0;
  139. }
  140. if (insertAt == null || insertAt < 0) {
  141. this._postProcesses.push(postProcess);
  142. this._postProcessesTakenIndices.push(this._postProcesses.length - 1);
  143. return this._postProcesses.length - 1;
  144. }
  145. var add = 0;
  146. if (this._postProcesses[insertAt]) {
  147. var start = this._postProcesses.length - 1;
  148. for (var i = start; i >= insertAt + 1; --i) {
  149. this._postProcesses[i + 1] = this._postProcesses[i];
  150. }
  151. add = 1;
  152. }
  153. for (i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  154. if (this._postProcessesTakenIndices[i] < insertAt) {
  155. continue;
  156. }
  157. start = this._postProcessesTakenIndices.length - 1;
  158. for (var j = start; j >= i; --j) {
  159. this._postProcessesTakenIndices[j + 1] = this._postProcessesTakenIndices[j] + add;
  160. }
  161. this._postProcessesTakenIndices[i] = insertAt;
  162. break;
  163. }
  164. if (!add && this._postProcessesTakenIndices.indexOf(insertAt) == -1) {
  165. this._postProcessesTakenIndices.push(insertAt);
  166. }
  167. var result = insertAt + add;
  168. this._postProcesses[result] = postProcess;
  169. return result;
  170. }
  171. public detachPostProcess(postProcess: PostProcess, atIndices: any = null): number[] {
  172. var result = [];
  173. if (!atIndices) {
  174. var length = this._postProcesses.length;
  175. for (var i = 0; i < length; i++) {
  176. if (this._postProcesses[i] !== postProcess) {
  177. continue;
  178. }
  179. delete this._postProcesses[i];
  180. var index = this._postProcessesTakenIndices.indexOf(i);
  181. this._postProcessesTakenIndices.splice(index, 1);
  182. }
  183. }
  184. else {
  185. atIndices = (atIndices instanceof Array) ? atIndices : [atIndices];
  186. for (i = 0; i < atIndices.length; i++) {
  187. var foundPostProcess = this._postProcesses[atIndices[i]];
  188. if (foundPostProcess !== postProcess) {
  189. result.push(i);
  190. continue;
  191. }
  192. delete this._postProcesses[atIndices[i]];
  193. index = this._postProcessesTakenIndices.indexOf(atIndices[i]);
  194. this._postProcessesTakenIndices.splice(index, 1);
  195. }
  196. }
  197. return result;
  198. }
  199. public getWorldMatrix(): Matrix {
  200. if (!this._worldMatrix) {
  201. this._worldMatrix = Matrix.Identity();
  202. }
  203. var viewMatrix = this.getViewMatrix();
  204. viewMatrix.invertToRef(this._worldMatrix);
  205. return this._worldMatrix;
  206. }
  207. public _getViewMatrix(): Matrix {
  208. return Matrix.Identity();
  209. }
  210. public getViewMatrix(): Matrix {
  211. this._computedViewMatrix = this._computeViewMatrix();
  212. if (!this.parent
  213. || !this.parent.getWorldMatrix
  214. || this.isSynchronized()) {
  215. return this._computedViewMatrix;
  216. }
  217. if (!this._worldMatrix) {
  218. this._worldMatrix = Matrix.Identity();
  219. }
  220. this._computedViewMatrix.invertToRef(this._worldMatrix);
  221. this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
  222. this._computedViewMatrix.invert();
  223. this._currentRenderId = this.getScene().getRenderId();
  224. return this._computedViewMatrix;
  225. }
  226. public _computeViewMatrix(force?: boolean): Matrix {
  227. if (!force && this._isSynchronizedViewMatrix()) {
  228. return this._computedViewMatrix;
  229. }
  230. this._computedViewMatrix = this._getViewMatrix();
  231. if (!this.parent || !this.parent.getWorldMatrix) {
  232. this._currentRenderId = this.getScene().getRenderId();
  233. }
  234. return this._computedViewMatrix;
  235. }
  236. public getProjectionMatrix(force?: boolean): Matrix {
  237. if (!force && this._isSynchronizedProjectionMatrix()) {
  238. return this._projectionMatrix;
  239. }
  240. var engine = this.getEngine();
  241. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  242. if (this.minZ <= 0) {
  243. this.minZ = 0.1;
  244. }
  245. Matrix.PerspectiveFovLHToRef(this.fov, engine.getAspectRatio(this), this.minZ, this.maxZ, this._projectionMatrix, this.fovMode);
  246. return this._projectionMatrix;
  247. }
  248. var halfWidth = engine.getRenderWidth() / 2.0;
  249. var halfHeight = engine.getRenderHeight() / 2.0;
  250. Matrix.OrthoOffCenterLHToRef(this.orthoLeft || -halfWidth, this.orthoRight || halfWidth, this.orthoBottom || -halfHeight, this.orthoTop || halfHeight, this.minZ, this.maxZ, this._projectionMatrix);
  251. return this._projectionMatrix;
  252. }
  253. public dispose(): void {
  254. // Remove from scene
  255. var index = this.getScene().cameras.indexOf(this);
  256. this.getScene().cameras.splice(index, 1);
  257. // Postprocesses
  258. for (var i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  259. this._postProcesses[this._postProcessesTakenIndices[i]].dispose(this);
  260. }
  261. }
  262. }
  263. }