babylon.camera.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. } else {
  96. 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();
  97. }
  98. return check;
  99. };
  100. // Controls
  101. Camera.prototype.attachControl = function (element) {
  102. };
  103. Camera.prototype.detachControl = function (element) {
  104. };
  105. Camera.prototype._update = function () {
  106. };
  107. Camera.prototype.attachPostProcess = function (postProcess, insertAt) {
  108. if (typeof insertAt === "undefined") { insertAt = null; }
  109. if (!postProcess.isReusable() && this._postProcesses.indexOf(postProcess) > -1) {
  110. BABYLON.Tools.Error("You're trying to reuse a post process not defined as reusable.");
  111. return 0;
  112. }
  113. if (insertAt == null || insertAt < 0) {
  114. this._postProcesses.push(postProcess);
  115. this._postProcessesTakenIndices.push(this._postProcesses.length - 1);
  116. return this._postProcesses.length - 1;
  117. }
  118. var add = 0;
  119. if (this._postProcesses[insertAt]) {
  120. var start = this._postProcesses.length - 1;
  121. for (var i = start; i >= insertAt + 1; --i) {
  122. this._postProcesses[i + 1] = this._postProcesses[i];
  123. }
  124. add = 1;
  125. }
  126. for (i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  127. if (this._postProcessesTakenIndices[i] < insertAt) {
  128. continue;
  129. }
  130. start = this._postProcessesTakenIndices.length - 1;
  131. for (var j = start; j >= i; --j) {
  132. this._postProcessesTakenIndices[j + 1] = this._postProcessesTakenIndices[j] + add;
  133. }
  134. this._postProcessesTakenIndices[i] = insertAt;
  135. break;
  136. }
  137. if (!add && this._postProcessesTakenIndices.indexOf(insertAt) == -1) {
  138. this._postProcessesTakenIndices.push(insertAt);
  139. }
  140. var result = insertAt + add;
  141. this._postProcesses[result] = postProcess;
  142. return result;
  143. };
  144. Camera.prototype.detachPostProcess = function (postProcess, atIndices) {
  145. if (typeof atIndices === "undefined") { atIndices = null; }
  146. var result = [];
  147. if (!atIndices) {
  148. var length = this._postProcesses.length;
  149. for (var i = 0; i < length; i++) {
  150. if (this._postProcesses[i] !== postProcess) {
  151. continue;
  152. }
  153. delete this._postProcesses[i];
  154. var index = this._postProcessesTakenIndices.indexOf(i);
  155. this._postProcessesTakenIndices.splice(index, 1);
  156. }
  157. } else {
  158. atIndices = (atIndices instanceof Array) ? atIndices : [atIndices];
  159. for (i = 0; i < atIndices.length; i++) {
  160. var foundPostProcess = this._postProcesses[atIndices[i]];
  161. if (foundPostProcess !== postProcess) {
  162. result.push(i);
  163. continue;
  164. }
  165. delete this._postProcesses[atIndices[i]];
  166. index = this._postProcessesTakenIndices.indexOf(atIndices[i]);
  167. this._postProcessesTakenIndices.splice(index, 1);
  168. }
  169. }
  170. return result;
  171. };
  172. Camera.prototype.getWorldMatrix = function () {
  173. if (!this._worldMatrix) {
  174. this._worldMatrix = BABYLON.Matrix.Identity();
  175. }
  176. var viewMatrix = this.getViewMatrix();
  177. viewMatrix.invertToRef(this._worldMatrix);
  178. return this._worldMatrix;
  179. };
  180. Camera.prototype._getViewMatrix = function () {
  181. return BABYLON.Matrix.Identity();
  182. };
  183. Camera.prototype.getViewMatrix = function () {
  184. this._computedViewMatrix = this._computeViewMatrix();
  185. if (!this.parent || !this.parent.getWorldMatrix || this.isSynchronized()) {
  186. return this._computedViewMatrix;
  187. }
  188. if (!this._worldMatrix) {
  189. this._worldMatrix = BABYLON.Matrix.Identity();
  190. }
  191. this._computedViewMatrix.invertToRef(this._worldMatrix);
  192. this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
  193. this._computedViewMatrix.invert();
  194. this._currentRenderId = this.getScene().getRenderId();
  195. return this._computedViewMatrix;
  196. };
  197. Camera.prototype._computeViewMatrix = function (force) {
  198. if (!force && this._isSynchronizedViewMatrix()) {
  199. return this._computedViewMatrix;
  200. }
  201. this._computedViewMatrix = this._getViewMatrix();
  202. if (!this.parent || !this.parent.getWorldMatrix) {
  203. this._currentRenderId = this.getScene().getRenderId();
  204. }
  205. return this._computedViewMatrix;
  206. };
  207. Camera.prototype.getProjectionMatrix = function (force) {
  208. if (!force && this._isSynchronizedProjectionMatrix()) {
  209. return this._projectionMatrix;
  210. }
  211. var engine = this.getEngine();
  212. if (this.mode === BABYLON.Camera.PERSPECTIVE_CAMERA) {
  213. if (this.minZ <= 0) {
  214. this.minZ = 0.1;
  215. }
  216. BABYLON.Matrix.PerspectiveFovLHToRef(this.fov, engine.getAspectRatio(this), this.minZ, this.maxZ, this._projectionMatrix);
  217. return this._projectionMatrix;
  218. }
  219. var halfWidth = engine.getRenderWidth() / 2.0;
  220. var halfHeight = engine.getRenderHeight() / 2.0;
  221. BABYLON.Matrix.OrthoOffCenterLHToRef(this.orthoLeft || -halfWidth, this.orthoRight || halfWidth, this.orthoBottom || -halfHeight, this.orthoTop || halfHeight, this.minZ, this.maxZ, this._projectionMatrix);
  222. return this._projectionMatrix;
  223. };
  224. Camera.prototype.dispose = function () {
  225. // Remove from scene
  226. var index = this.getScene().cameras.indexOf(this);
  227. this.getScene().cameras.splice(index, 1);
  228. for (var i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  229. this._postProcesses[this._postProcessesTakenIndices[i]].dispose(this);
  230. }
  231. };
  232. Camera.PERSPECTIVE_CAMERA = 0;
  233. Camera.ORTHOGRAPHIC_CAMERA = 1;
  234. return Camera;
  235. })(BABYLON.Node);
  236. BABYLON.Camera = Camera;
  237. })(BABYLON || (BABYLON = {}));
  238. //# sourceMappingURL=babylon.camera.js.map