babylon.camera.ts 11 KB

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