babylon.camera.js 13 KB

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