babylon.camera.js 12 KB

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