babylon.camera.ts 11 KB

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