babylon.camera.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. // Viewport
  21. this.viewport = new BABYLON.Viewport(0, 0, 1.0, 1.0);
  22. //Cache
  23. BABYLON.Camera.prototype._initCache.call(this);
  24. };
  25. BABYLON.Camera.prototype = Object.create(BABYLON.Node.prototype);
  26. // Statics
  27. BABYLON.Camera.PERSPECTIVE_CAMERA = 0;
  28. BABYLON.Camera.ORTHOGRAPHIC_CAMERA = 1;
  29. // Members
  30. BABYLON.Camera.prototype.orthoLeft = null;
  31. BABYLON.Camera.prototype.orthoRight = null;
  32. BABYLON.Camera.prototype.orthoBottom = null;
  33. BABYLON.Camera.prototype.orthoTop = null;
  34. BABYLON.Camera.prototype.fov = 0.8;
  35. BABYLON.Camera.prototype.minZ = 0.1;
  36. BABYLON.Camera.prototype.maxZ = 1000.0;
  37. BABYLON.Camera.prototype.inertia = 0.9;
  38. BABYLON.Camera.prototype.mode = BABYLON.Camera.PERSPECTIVE_CAMERA;
  39. // Properties
  40. BABYLON.Camera.prototype.getScene = function () {
  41. return this._scene;
  42. };
  43. // Methods
  44. //Cache
  45. BABYLON.Camera.prototype._initCache = function () {
  46. this._cache.position = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  47. this._cache.upVector = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  48. this._cache.mode = undefined;
  49. this._cache.minZ = undefined;
  50. this._cache.maxZ = undefined;
  51. this._cache.fov = undefined;
  52. this._cache.aspectRatio = undefined;
  53. this._cache.orthoLeft = undefined;
  54. this._cache.orthoRight = undefined;
  55. this._cache.orthoBottom = undefined;
  56. this._cache.orthoTop = undefined;
  57. this._cache.renderWidth = undefined;
  58. this._cache.renderHeight = undefined;
  59. };
  60. BABYLON.Camera.prototype._updateCache = function (ignoreParentClass) {
  61. if (!ignoreParentClass) {
  62. BABYLON.Node.prototype._updateCache.call(this);
  63. }
  64. var engine = this._scene.getEngine();
  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. this._cache.aspectRatio = engine.getAspectRatio();
  72. this._cache.orthoLeft = this.orthoLeft;
  73. this._cache.orthoRight = this.orthoRight;
  74. this._cache.orthoBottom = this.orthoBottom;
  75. this._cache.orthoTop = this.orthoTop;
  76. this._cache.renderWidth = engine.getRenderWidth();
  77. this._cache.renderHeight = engine.getRenderHeight();
  78. };
  79. BABYLON.Camera.prototype._updateFromScene = function () {
  80. this.updateCache();
  81. this._update();
  82. };
  83. // Synchronized
  84. BABYLON.Camera.prototype._isSynchronized = function () {
  85. return this._isSynchronizedViewMatrix() && this._isSynchronizedProjectionMatrix();
  86. };
  87. BABYLON.Camera.prototype._isSynchronizedViewMatrix = function () {
  88. if (!BABYLON.Node.prototype._isSynchronized.call(this))
  89. return false;
  90. return this._cache.position.equals(this.position)
  91. && this._cache.upVector.equals(this.upVector)
  92. && this.isSynchronizedWithParent();
  93. };
  94. BABYLON.Camera.prototype._isSynchronizedProjectionMatrix = function () {
  95. var check = this._cache.mode === this.mode
  96. && this._cache.minZ === this.minZ
  97. && this._cache.maxZ === this.maxZ;
  98. var engine = this._scene.getEngine();
  99. if (check) {
  100. if (this.mode === BABYLON.Camera.PERSPECTIVE_CAMERA) {
  101. check = this._cache.fov === this.fov
  102. && this._cache.aspectRatio === engine.getAspectRatio();
  103. }
  104. else {
  105. check = this._cache.orthoLeft === this.orthoLeft
  106. && this._cache.orthoRight === this.orthoRight
  107. && this._cache.orthoBottom === this.orthoBottom
  108. && this._cache.orthoTop === this.orthoTop
  109. && this._cache.renderWidth === engine.getRenderWidth()
  110. && this._cache.renderHeight === engine.getRenderHeight();
  111. }
  112. }
  113. return check;
  114. };
  115. // Controls
  116. BABYLON.Camera.prototype.attachControl = function (canvas) {
  117. };
  118. BABYLON.Camera.prototype.detachControl = function (canvas) {
  119. };
  120. BABYLON.Camera.prototype._update = function () {
  121. };
  122. BABYLON.Camera.prototype.getWorldMatrix = function () {
  123. if (!this._worldMatrix) {
  124. this._worldMatrix = BABYLON.Matrix.Identity();
  125. }
  126. var viewMatrix = this.getViewMatrix();
  127. viewMatrix.invertToRef(this._worldMatrix);
  128. return this._worldMatrix;
  129. };
  130. BABYLON.Camera.prototype._getViewMatrix = function () {
  131. return BABYLON.Matrix.Identity();
  132. };
  133. BABYLON.Camera.prototype.getViewMatrix = function () {
  134. this._computedViewMatrix = this._computeViewMatrix();
  135. if (!this.parent
  136. || !this.parent.getWorldMatrix
  137. || (!this.hasNewParent() && this.parent.isSynchronized())) {
  138. return this._computedViewMatrix;
  139. }
  140. if (!this._worldMatrix) {
  141. this._worldMatrix = BABYLON.Matrix.Identity();
  142. }
  143. this._computedViewMatrix.invertToRef(this._worldMatrix);
  144. this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
  145. this._computedViewMatrix.invert();
  146. return this._computedViewMatrix;
  147. };
  148. BABYLON.Camera.prototype._computeViewMatrix = function (force) {
  149. if (!force && this._isSynchronizedViewMatrix()) {
  150. return this._computedViewMatrix;
  151. }
  152. this._syncChildFlag();
  153. this._computedViewMatrix = this._getViewMatrix();
  154. return this._computedViewMatrix;
  155. };
  156. BABYLON.Camera.prototype.getProjectionMatrix = function (force) {
  157. if (!force && this._isSynchronizedProjectionMatrix()) {
  158. return this._projectionMatrix;
  159. }
  160. var engine = this._scene.getEngine();
  161. if (this.mode === BABYLON.Camera.PERSPECTIVE_CAMERA) {
  162. BABYLON.Matrix.PerspectiveFovLHToRef(this.fov, engine.getAspectRatio(), this.minZ, this.maxZ, this._projectionMatrix);
  163. return this._projectionMatrix;
  164. }
  165. var halfWidth = engine.getRenderWidth() / 2.0;
  166. var halfHeight = engine.getRenderHeight() / 2.0;
  167. BABYLON.Matrix.OrthoOffCenterLHToRef(this.orthoLeft || -halfWidth, this.orthoRight || halfWidth, this.orthoBottom || -halfHeight, this.orthoTop || halfHeight, this.minZ, this.maxZ, this._projectionMatrix);
  168. return this._projectionMatrix;
  169. };
  170. BABYLON.Camera.prototype.dispose = function () {
  171. // Remove from scene
  172. var index = this._scene.cameras.indexOf(this);
  173. this._scene.cameras.splice(index, 1);
  174. // Postprocesses
  175. while (this.postProcesses.length) {
  176. this.postProcesses[0].dispose();
  177. }
  178. };
  179. })();