babylon.camera.js 11 KB

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