babylon.camera.js 11 KB

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