babylon.camera.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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)
  130. && this._cache.upVector.equals(this.upVector)
  131. && this.isSynchronizedWithParent();
  132. };
  133. Camera.prototype._isSynchronizedProjectionMatrix = function () {
  134. var check = this._cache.mode === this.mode
  135. && this._cache.minZ === this.minZ
  136. && this._cache.maxZ === this.maxZ;
  137. if (!check) {
  138. return false;
  139. }
  140. var engine = this.getEngine();
  141. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  142. check = this._cache.fov === this.fov
  143. && this._cache.aspectRatio === engine.getAspectRatio(this);
  144. }
  145. else {
  146. check = this._cache.orthoLeft === this.orthoLeft
  147. && this._cache.orthoRight === this.orthoRight
  148. && this._cache.orthoBottom === this.orthoBottom
  149. && this._cache.orthoTop === this.orthoTop
  150. && this._cache.renderWidth === engine.getRenderWidth()
  151. && this._cache.renderHeight === engine.getRenderHeight();
  152. }
  153. return check;
  154. };
  155. // Controls
  156. Camera.prototype.attachControl = function (element) {
  157. };
  158. Camera.prototype.detachControl = function (element) {
  159. };
  160. Camera.prototype._update = function () {
  161. };
  162. Camera.prototype.attachPostProcess = function (postProcess, insertAt) {
  163. if (insertAt === void 0) { insertAt = null; }
  164. if (!postProcess.isReusable() && this._postProcesses.indexOf(postProcess) > -1) {
  165. BABYLON.Tools.Error("You're trying to reuse a post process not defined as reusable.");
  166. return 0;
  167. }
  168. if (insertAt == null || insertAt < 0) {
  169. this._postProcesses.push(postProcess);
  170. this._postProcessesTakenIndices.push(this._postProcesses.length - 1);
  171. return this._postProcesses.length - 1;
  172. }
  173. var add = 0;
  174. if (this._postProcesses[insertAt]) {
  175. var start = this._postProcesses.length - 1;
  176. for (var i = start; i >= insertAt + 1; --i) {
  177. this._postProcesses[i + 1] = this._postProcesses[i];
  178. }
  179. add = 1;
  180. }
  181. for (i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  182. if (this._postProcessesTakenIndices[i] < insertAt) {
  183. continue;
  184. }
  185. start = this._postProcessesTakenIndices.length - 1;
  186. for (var j = start; j >= i; --j) {
  187. this._postProcessesTakenIndices[j + 1] = this._postProcessesTakenIndices[j] + add;
  188. }
  189. this._postProcessesTakenIndices[i] = insertAt;
  190. break;
  191. }
  192. if (!add && this._postProcessesTakenIndices.indexOf(insertAt) == -1) {
  193. this._postProcessesTakenIndices.push(insertAt);
  194. }
  195. var result = insertAt + add;
  196. this._postProcesses[result] = postProcess;
  197. return result;
  198. };
  199. Camera.prototype.detachPostProcess = function (postProcess, atIndices) {
  200. if (atIndices === void 0) { atIndices = null; }
  201. var result = [];
  202. if (!atIndices) {
  203. var length = this._postProcesses.length;
  204. for (var i = 0; i < length; i++) {
  205. if (this._postProcesses[i] !== postProcess) {
  206. continue;
  207. }
  208. delete this._postProcesses[i];
  209. var index = this._postProcessesTakenIndices.indexOf(i);
  210. this._postProcessesTakenIndices.splice(index, 1);
  211. }
  212. }
  213. else {
  214. atIndices = (atIndices instanceof Array) ? atIndices : [atIndices];
  215. for (i = 0; i < atIndices.length; i++) {
  216. var foundPostProcess = this._postProcesses[atIndices[i]];
  217. if (foundPostProcess !== postProcess) {
  218. result.push(i);
  219. continue;
  220. }
  221. delete this._postProcesses[atIndices[i]];
  222. index = this._postProcessesTakenIndices.indexOf(atIndices[i]);
  223. this._postProcessesTakenIndices.splice(index, 1);
  224. }
  225. }
  226. return result;
  227. };
  228. Camera.prototype.getWorldMatrix = function () {
  229. if (!this._worldMatrix) {
  230. this._worldMatrix = BABYLON.Matrix.Identity();
  231. }
  232. var viewMatrix = this.getViewMatrix();
  233. viewMatrix.invertToRef(this._worldMatrix);
  234. return this._worldMatrix;
  235. };
  236. Camera.prototype._getViewMatrix = function () {
  237. return BABYLON.Matrix.Identity();
  238. };
  239. Camera.prototype.getViewMatrix = function (force) {
  240. this._computedViewMatrix = this._computeViewMatrix(force);
  241. if (!force && this._isSynchronizedViewMatrix()) {
  242. return this._computedViewMatrix;
  243. }
  244. if (!this.parent || !this.parent.getWorldMatrix) {
  245. this._globalPosition.copyFrom(this.position);
  246. }
  247. else {
  248. if (!this._worldMatrix) {
  249. this._worldMatrix = BABYLON.Matrix.Identity();
  250. }
  251. this._computedViewMatrix.invertToRef(this._worldMatrix);
  252. this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
  253. this._globalPosition.copyFromFloats(this._computedViewMatrix.m[12], this._computedViewMatrix.m[13], this._computedViewMatrix.m[14]);
  254. this._computedViewMatrix.invert();
  255. this._markSyncedWithParent();
  256. }
  257. this._currentRenderId = this.getScene().getRenderId();
  258. return this._computedViewMatrix;
  259. };
  260. Camera.prototype._computeViewMatrix = function (force) {
  261. if (!force && this._isSynchronizedViewMatrix()) {
  262. return this._computedViewMatrix;
  263. }
  264. this._computedViewMatrix = this._getViewMatrix();
  265. this._currentRenderId = this.getScene().getRenderId();
  266. return this._computedViewMatrix;
  267. };
  268. Camera.prototype.getProjectionMatrix = function (force) {
  269. if (!force && this._isSynchronizedProjectionMatrix()) {
  270. return this._projectionMatrix;
  271. }
  272. var engine = this.getEngine();
  273. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  274. if (this.minZ <= 0) {
  275. this.minZ = 0.1;
  276. }
  277. BABYLON.Matrix.PerspectiveFovLHToRef(this.fov, engine.getAspectRatio(this), this.minZ, this.maxZ, this._projectionMatrix, this.fovMode);
  278. return this._projectionMatrix;
  279. }
  280. var halfWidth = engine.getRenderWidth() / 2.0;
  281. var halfHeight = engine.getRenderHeight() / 2.0;
  282. BABYLON.Matrix.OrthoOffCenterLHToRef(this.orthoLeft || -halfWidth, this.orthoRight || halfWidth, this.orthoBottom || -halfHeight, this.orthoTop || halfHeight, this.minZ, this.maxZ, this._projectionMatrix);
  283. return this._projectionMatrix;
  284. };
  285. Camera.prototype.dispose = function () {
  286. // Remove from scene
  287. this.getScene().removeCamera(this);
  288. // Postprocesses
  289. for (var i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  290. this._postProcesses[this._postProcessesTakenIndices[i]].dispose(this);
  291. }
  292. };
  293. // Statics
  294. Camera._PERSPECTIVE_CAMERA = 0;
  295. Camera._ORTHOGRAPHIC_CAMERA = 1;
  296. Camera._FOVMODE_VERTICAL_FIXED = 0;
  297. Camera._FOVMODE_HORIZONTAL_FIXED = 1;
  298. return Camera;
  299. })(BABYLON.Node);
  300. BABYLON.Camera = Camera;
  301. })(BABYLON || (BABYLON = {}));
  302. //# sourceMappingURL=babylon.camera.js.map