babylon.camera.js 11 KB

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