babylon.camera.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // Animations
  18. this.animations = [];
  19. // Postprocesses
  20. this.postProcesses = [];
  21. // Viewport
  22. this.viewport = new BABYLON.Viewport(0, 0, 1.0, 1.0);
  23. //Cache
  24. BABYLON.Camera.prototype._initCache.call(this);
  25. };
  26. BABYLON.Camera.prototype = Object.create(BABYLON.Node.prototype);
  27. // Statics
  28. BABYLON.Camera.PERSPECTIVE_CAMERA = 0;
  29. BABYLON.Camera.ORTHOGRAPHIC_CAMERA = 1;
  30. // Members
  31. BABYLON.Camera.prototype.orthoLeft = null;
  32. BABYLON.Camera.prototype.orthoRight = null;
  33. BABYLON.Camera.prototype.orthoBottom = null;
  34. BABYLON.Camera.prototype.orthoTop = null;
  35. BABYLON.Camera.prototype.fov = 0.8;
  36. BABYLON.Camera.prototype.minZ = 0.1;
  37. BABYLON.Camera.prototype.maxZ = 1000.0;
  38. BABYLON.Camera.prototype.inertia = 0.9;
  39. BABYLON.Camera.prototype.mode = BABYLON.Camera.PERSPECTIVE_CAMERA;
  40. // Properties
  41. BABYLON.Camera.prototype.getScene = function () {
  42. return this._scene;
  43. };
  44. //Cache
  45. BABYLON.Camera.prototype._initCache = function () {
  46. this._cache.position = this.position.clone();
  47. this._cache.upVector = this.upVector.clone();
  48. };
  49. BABYLON.Camera.prototype._updateCache = function (ignoreParentClass) {
  50. if(!ignoreParentClass)
  51. BABYLON.Node.prototype._updateCache.call(this);
  52. this._cache.position.copyFrom(this.position);
  53. this._cache.upVector.copyFrom(this.upVector);
  54. };
  55. // Synchronized
  56. BABYLON.Camera.prototype._isSynchronized = function () {
  57. if (!BABYLON.Node.prototype._isSynchronized.call(this))
  58. return false;
  59. return this._cache.position.equals(this.position)
  60. && this._cache.upVector.equals(this.upVector);
  61. };
  62. // Methods
  63. BABYLON.Camera.prototype.attachControl = function (canvas) {
  64. };
  65. BABYLON.Camera.prototype.detachControl = function (canvas) {
  66. };
  67. BABYLON.Camera.prototype._update = function () {
  68. };
  69. BABYLON.Camera.prototype._updateFromScene = function () {
  70. this.updateCache();
  71. this._update();
  72. };
  73. BABYLON.Camera.prototype.getWorldMatrix = function () {
  74. var viewMatrix = this.getViewMatrix();
  75. if (!this._worldMatrix) {
  76. this._worldMatrix = BABYLON.Matrix.Identity();
  77. }
  78. viewMatrix.invertToRef(this._worldMatrix);
  79. return this._worldMatrix;
  80. };
  81. BABYLON.Camera.prototype._getViewMatrix = function () {
  82. return BABYLON.Matrix.Identity();
  83. };
  84. BABYLON.Camera.prototype.getViewMatrix = function () {
  85. this._computedViewMatrix = this._computeViewMatrix();
  86. if(!this.parent
  87. || !this.parent.getWorldMatrix
  88. || (!this.hasNewParent() && this.parent.isSynchronized())) {
  89. return this._computedViewMatrix;
  90. }
  91. this._computedViewMatrix.invertToRef(this._worldMatrix);
  92. this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
  93. this._computedViewMatrix.invert();
  94. return this._computedViewMatrix;
  95. };
  96. BABYLON.Camera.prototype._computeViewMatrix = function (force) {
  97. if (!force && this.isSynchronized()) {
  98. return this._computedViewMatrix;
  99. }
  100. this._computedViewMatrix = this._getViewMatrix();
  101. return this._computedViewMatrix;
  102. };
  103. BABYLON.Camera.prototype.getProjectionMatrix = function () {
  104. if (!this._projectionMatrix) {
  105. this._projectionMatrix = new BABYLON.Matrix();
  106. }
  107. var engine = this._scene.getEngine();
  108. if (this.mode === BABYLON.Camera.PERSPECTIVE_CAMERA) {
  109. BABYLON.Matrix.PerspectiveFovLHToRef(this.fov, engine.getAspectRatio(), this.minZ, this.maxZ, this._projectionMatrix);
  110. return this._projectionMatrix;
  111. }
  112. var halfWidth = engine.getRenderWidth() / 2.0;
  113. var halfHeight = engine.getRenderHeight() / 2.0;
  114. BABYLON.Matrix.OrthoOffCenterLHToRef(this.orthoLeft || -halfWidth, this.orthoRight || halfWidth, this.orthoBottom || -halfHeight, this.orthoTop || halfHeight, this.minZ, this.maxZ, this._projectionMatrix);
  115. return this._projectionMatrix;
  116. };
  117. BABYLON.Camera.prototype.dispose = function () {
  118. // Remove from scene
  119. var index = this._scene.cameras.indexOf(this);
  120. this._scene.cameras.splice(index, 1);
  121. // Postprocesses
  122. while (this.postProcesses.length) {
  123. this.postProcesses[0].dispose();
  124. }
  125. };
  126. })();