babylon.camera.js 11 KB

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