babylon.camera.js 7.7 KB

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