babylon.camera.ts 10 KB

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