babylon.camera.ts 11 KB

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