babylon.camera.ts 11 KB

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