babylon.camera.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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(-Infinity, -Infinity,-Infinity);
  48. this._cache.upVector = new BABYLON.Vector3(-Infinity, -Infinity,-Infinity);
  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. this._cache.aspectRatio = engine.getAspectRatio();
  71. this._cache.orthoLeft = this.orthoLeft;
  72. this._cache.orthoRight = this.orthoRight;
  73. this._cache.orthoBottom = this.orthoBottom;
  74. this._cache.orthoTop = this.orthoTop;
  75. this._cache.renderWidth = engine.getRenderWidth()
  76. this._cache.renderHeight = engine.getRenderHeight();
  77. };
  78. // Synchronized
  79. BABYLON.Camera.prototype._isSynchronized = function () {
  80. return this._isSynchronizedViewMatrix() && this._isSynchronizedProjectionMatrix();
  81. };
  82. BABYLON.Camera.prototype._isSynchronizedViewMatrix = function () {
  83. if (!BABYLON.Node.prototype._isSynchronized.call(this))
  84. return false;
  85. return this._cache.position.equals(this.position)
  86. && this._cache.upVector.equals(this.upVector);
  87. };
  88. BABYLON.Camera.prototype._isSynchronizedProjectionMatrix = function () {
  89. var r = this._cache.mode === this.mode
  90. && this._cache.minZ === this.minZ
  91. && this._cache.maxZ === this.maxZ;
  92. if (r) {
  93. if (this.mode === BABYLON.Camera.PERSPECTIVE_CAMERA) {
  94. r = r & this._cache.fov === this.fov
  95. && this._cache.aspectRatio === engine.getAspectRatio();
  96. }
  97. else {
  98. r = r & this._cache.orthoLeft === this.orthoLeft
  99. && this._cache.orthoRight === this.orthoRight
  100. && this._cache.orthoBottom === this.orthoBottom
  101. && this._cache.orthoTop === this.orthoTop
  102. && this._cache.renderWidth === engine.getRenderWidth()
  103. && this._cache.renderHeight === engine.getRenderHeight();
  104. }
  105. }
  106. return r;
  107. };
  108. // Methods
  109. BABYLON.Camera.prototype.attachControl = function (canvas) {
  110. };
  111. BABYLON.Camera.prototype.detachControl = function (canvas) {
  112. };
  113. BABYLON.Camera.prototype._update = function () {
  114. };
  115. BABYLON.Camera.prototype._updateFromScene = function () {
  116. this.updateCache();
  117. this._update();
  118. };
  119. BABYLON.Camera.prototype.getWorldMatrix = function () {
  120. var viewMatrix = this.getViewMatrix();
  121. if (!this._worldMatrix) {
  122. this._worldMatrix = BABYLON.Matrix.Identity();
  123. }
  124. viewMatrix.invertToRef(this._worldMatrix);
  125. return this._worldMatrix;
  126. };
  127. BABYLON.Camera.prototype._getViewMatrix = function () {
  128. return BABYLON.Matrix.Identity();
  129. };
  130. BABYLON.Camera.prototype.getViewMatrix = function () {
  131. this._computedViewMatrix = this._computeViewMatrix();
  132. if(!this.parent
  133. || !this.parent.getWorldMatrix
  134. || (!this.hasNewParent() && this.parent.isSynchronized())) {
  135. return this._computedViewMatrix;
  136. }
  137. this._computedViewMatrix.invertToRef(this._worldMatrix);
  138. this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
  139. this._computedViewMatrix.invert();
  140. return this._computedViewMatrix;
  141. };
  142. BABYLON.Camera.prototype._computeViewMatrix = function (force) {
  143. if (!force && this._isSynchronizedViewMatrix()) {
  144. return this._computedViewMatrix;
  145. }
  146. this._computedViewMatrix = this._getViewMatrix();
  147. return this._computedViewMatrix;
  148. };
  149. BABYLON.Camera.prototype.getProjectionMatrix = function (force) {
  150. if(!force && this._isSynchronizedProjectionMatrix()) {
  151. return this._projectionMatrix;
  152. }
  153. var engine = this._scene.getEngine();
  154. if (this.mode === BABYLON.Camera.PERSPECTIVE_CAMERA) {
  155. BABYLON.Matrix.PerspectiveFovLHToRef(this.fov, engine.getAspectRatio(), this.minZ, this.maxZ, this._projectionMatrix);
  156. return this._projectionMatrix;
  157. }
  158. var halfWidth = engine.getRenderWidth() / 2.0;
  159. var halfHeight = engine.getRenderHeight() / 2.0;
  160. BABYLON.Matrix.OrthoOffCenterLHToRef(this.orthoLeft || -halfWidth, this.orthoRight || halfWidth, this.orthoBottom || -halfHeight, this.orthoTop || halfHeight, this.minZ, this.maxZ, this._projectionMatrix);
  161. return this._projectionMatrix;
  162. };
  163. BABYLON.Camera.prototype.dispose = function () {
  164. // Remove from scene
  165. var index = this._scene.cameras.indexOf(this);
  166. this._scene.cameras.splice(index, 1);
  167. // Postprocesses
  168. while (this.postProcesses.length) {
  169. this.postProcesses[0].dispose();
  170. }
  171. };
  172. })();