babylon.camera.js 12 KB

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