babylon.camera.js 11 KB

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