babylon.camera.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.Camera = function (name, position, scene) {
  5. BABYLON.Node.call(this, scene);
  6. this.name = name;
  7. this.id = name;
  8. this.position = position.clone();
  9. this.upVector = BABYLON.Vector3.Up();
  10. scene.cameras.push(this);
  11. if (!scene.activeCamera) {
  12. scene.activeCamera = this;
  13. }
  14. this._computedViewMatrix = BABYLON.Matrix.Identity();
  15. this._projectionMatrix = new BABYLON.Matrix();
  16. // Sub-cameras
  17. this.subCameras = [];
  18. // Animations
  19. this.animations = [];
  20. // Postprocesses
  21. this._postProcesses = [];
  22. this._postProcessesTakenIndices = [];
  23. // Viewport
  24. this.viewport = new BABYLON.Viewport(0, 0, 1.0, 1.0);
  25. //Cache
  26. BABYLON.Camera.prototype._initCache.call(this);
  27. };
  28. BABYLON.Camera.prototype = Object.create(BABYLON.Node.prototype);
  29. // Statics
  30. BABYLON.Camera.PERSPECTIVE_CAMERA = 0;
  31. BABYLON.Camera.ORTHOGRAPHIC_CAMERA = 1;
  32. // Members
  33. BABYLON.Camera.prototype.orthoLeft = null;
  34. BABYLON.Camera.prototype.orthoRight = null;
  35. BABYLON.Camera.prototype.orthoBottom = null;
  36. BABYLON.Camera.prototype.orthoTop = null;
  37. BABYLON.Camera.prototype.fov = 0.8;
  38. BABYLON.Camera.prototype.minZ = 0.1;
  39. BABYLON.Camera.prototype.maxZ = 1000.0;
  40. BABYLON.Camera.prototype.inertia = 0.9;
  41. BABYLON.Camera.prototype.mode = BABYLON.Camera.PERSPECTIVE_CAMERA;
  42. BABYLON.Camera.prototype.isIntermediate = false;
  43. // Properties
  44. BABYLON.Camera.prototype.getScene = function () {
  45. return this._scene;
  46. };
  47. // Methods
  48. //Cache
  49. BABYLON.Camera.prototype._initCache = function () {
  50. this._cache.position = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  51. this._cache.upVector = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  52. this._cache.mode = undefined;
  53. this._cache.minZ = undefined;
  54. this._cache.maxZ = undefined;
  55. this._cache.fov = undefined;
  56. this._cache.aspectRatio = undefined;
  57. this._cache.orthoLeft = undefined;
  58. this._cache.orthoRight = undefined;
  59. this._cache.orthoBottom = undefined;
  60. this._cache.orthoTop = undefined;
  61. this._cache.renderWidth = undefined;
  62. this._cache.renderHeight = undefined;
  63. };
  64. BABYLON.Camera.prototype._updateCache = function (ignoreParentClass) {
  65. if (!ignoreParentClass) {
  66. BABYLON.Node.prototype._updateCache.call(this);
  67. }
  68. var engine = this._scene.getEngine();
  69. this._cache.position.copyFrom(this.position);
  70. this._cache.upVector.copyFrom(this.upVector);
  71. this._cache.mode = this.mode;
  72. this._cache.minZ = this.minZ;
  73. this._cache.maxZ = this.maxZ;
  74. this._cache.fov = this.fov;
  75. this._cache.aspectRatio = engine.getAspectRatio(this);
  76. this._cache.orthoLeft = this.orthoLeft;
  77. this._cache.orthoRight = this.orthoRight;
  78. this._cache.orthoBottom = this.orthoBottom;
  79. this._cache.orthoTop = this.orthoTop;
  80. this._cache.renderWidth = engine.getRenderWidth();
  81. this._cache.renderHeight = engine.getRenderHeight();
  82. };
  83. BABYLON.Camera.prototype._updateFromScene = function () {
  84. this.updateCache();
  85. this._update();
  86. };
  87. // Synchronized
  88. BABYLON.Camera.prototype._isSynchronized = function () {
  89. return this._isSynchronizedViewMatrix() && this._isSynchronizedProjectionMatrix();
  90. };
  91. BABYLON.Camera.prototype._isSynchronizedViewMatrix = function () {
  92. if (!BABYLON.Node.prototype._isSynchronized.call(this))
  93. return false;
  94. return this._cache.position.equals(this.position)
  95. && this._cache.upVector.equals(this.upVector)
  96. && this.isSynchronizedWithParent();
  97. };
  98. BABYLON.Camera.prototype._isSynchronizedProjectionMatrix = function () {
  99. var check = this._cache.mode === this.mode
  100. && this._cache.minZ === this.minZ
  101. && this._cache.maxZ === this.maxZ;
  102. if (!check) {
  103. return false;
  104. }
  105. var engine = this._scene.getEngine();
  106. if (this.mode === BABYLON.Camera.PERSPECTIVE_CAMERA) {
  107. check = this._cache.fov === this.fov
  108. && this._cache.aspectRatio === engine.getAspectRatio(this);
  109. }
  110. else {
  111. check = 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. return check;
  119. };
  120. // Controls
  121. BABYLON.Camera.prototype.attachControl = function (canvas) {
  122. };
  123. BABYLON.Camera.prototype.detachControl = function (canvas) {
  124. };
  125. BABYLON.Camera.prototype._update = function () {
  126. };
  127. BABYLON.Camera.prototype.attachPostProcess = function (postProcess, insertAt) {
  128. if (!postProcess._reusable && this._postProcesses.indexOf(postProcess) > -1) {
  129. console.error("You're trying to reuse a post process not defined as reusable.");
  130. return;
  131. }
  132. if (insertAt == null || insertAt < 0) {
  133. this._postProcesses.push(postProcess);
  134. this._postProcessesTakenIndices.push(this._postProcesses.length - 1);
  135. return this._postProcesses.length - 1;
  136. }
  137. var add = 0;
  138. if (this._postProcesses[insertAt]) {
  139. var start = this._postProcesses.length - 1;
  140. for (var i = start; i >= insertAt + 1; --i) {
  141. this._postProcesses[i + 1] = this._postProcesses[i];
  142. }
  143. add = 1;
  144. }
  145. for (var i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  146. if (this._postProcessesTakenIndices[i] < insertAt) {
  147. continue;
  148. }
  149. var start = this._postProcessesTakenIndices.length - 1;
  150. for (var j = start; j >= i; --j) {
  151. this._postProcessesTakenIndices[j + 1] = this._postProcessesTakenIndices[j] + add;
  152. }
  153. this._postProcessesTakenIndices[i] = insertAt;
  154. break;
  155. }
  156. if (!add && this._postProcessesTakenIndices.indexOf(insertAt) == -1) {
  157. this._postProcessesTakenIndices.push(insertAt);
  158. }
  159. var result = insertAt + add;
  160. this._postProcesses[result] = postProcess;
  161. return result;
  162. };
  163. BABYLON.Camera.prototype.detachPostProcess = function (postProcess, atIndices) {
  164. var result = [];
  165. if (!atIndices) {
  166. var length = this._postProcesses.length;
  167. for (var i = 0; i < length; i++) {
  168. if (this._postProcesses[i] !== postProcess) {
  169. continue;
  170. }
  171. delete this._postProcesses[i];
  172. var index = this._postProcessesTakenIndices.indexOf(i);
  173. this._postProcessesTakenIndices.splice(index, 1);
  174. }
  175. }
  176. else {
  177. atIndices = (atIndices instanceof Array) ? atIndices : [atIndices];
  178. for (var i = 0; i < atIndices.length; i++) {
  179. var foundPostProcess = this._postProcesses[atIndices[i]];
  180. if (foundPostProcess !== postProcess) {
  181. result.push(i);
  182. continue;
  183. }
  184. delete this._postProcesses[atIndices[i]];
  185. var index = this._postProcessesTakenIndices.indexOf(atIndices[i]);
  186. this._postProcessesTakenIndices.splice(index, 1);
  187. }
  188. }
  189. return result;
  190. };
  191. BABYLON.Camera.prototype.getWorldMatrix = function () {
  192. if (!this._worldMatrix) {
  193. this._worldMatrix = BABYLON.Matrix.Identity();
  194. }
  195. var viewMatrix = this.getViewMatrix();
  196. viewMatrix.invertToRef(this._worldMatrix);
  197. return this._worldMatrix;
  198. };
  199. BABYLON.Camera.prototype._getViewMatrix = function () {
  200. return BABYLON.Matrix.Identity();
  201. };
  202. BABYLON.Camera.prototype.getViewMatrix = function () {
  203. this._computedViewMatrix = this._computeViewMatrix();
  204. if (!this.parent
  205. || !this.parent.getWorldMatrix
  206. || this.isSynchronized()) {
  207. return this._computedViewMatrix;
  208. }
  209. if (!this._worldMatrix) {
  210. this._worldMatrix = BABYLON.Matrix.Identity();
  211. }
  212. this._computedViewMatrix.invertToRef(this._worldMatrix);
  213. this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
  214. this._computedViewMatrix.invert();
  215. return this._computedViewMatrix;
  216. };
  217. BABYLON.Camera.prototype._computeViewMatrix = function (force) {
  218. if (!force && this._isSynchronizedViewMatrix()) {
  219. return this._computedViewMatrix;
  220. }
  221. this._currentRenderId = this._scene.getRenderId();
  222. this._computedViewMatrix = this._getViewMatrix();
  223. return this._computedViewMatrix;
  224. };
  225. BABYLON.Camera.prototype.getProjectionMatrix = function (force) {
  226. if (!force && this._isSynchronizedProjectionMatrix()) {
  227. return this._projectionMatrix;
  228. }
  229. var engine = this._scene.getEngine();
  230. if (this.mode === BABYLON.Camera.PERSPECTIVE_CAMERA) {
  231. BABYLON.Matrix.PerspectiveFovLHToRef(this.fov, engine.getAspectRatio(this), this.minZ, this.maxZ, this._projectionMatrix);
  232. return this._projectionMatrix;
  233. }
  234. var halfWidth = engine.getRenderWidth() / 2.0;
  235. var halfHeight = engine.getRenderHeight() / 2.0;
  236. BABYLON.Matrix.OrthoOffCenterLHToRef(this.orthoLeft || -halfWidth, this.orthoRight || halfWidth, this.orthoBottom || -halfHeight, this.orthoTop || halfHeight, this.minZ, this.maxZ, this._projectionMatrix);
  237. return this._projectionMatrix;
  238. };
  239. BABYLON.Camera.prototype.dispose = function () {
  240. // Remove from scene
  241. var index = this._scene.cameras.indexOf(this);
  242. this._scene.cameras.splice(index, 1);
  243. // Postprocesses
  244. for (var i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  245. this._postProcesses[this._postProcessesTakenIndices[i]].dispose(this);
  246. }
  247. };
  248. })();