babylon.camera.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.Camera = function (name, position, scene) {
  4. this.name = name;
  5. this.id = name;
  6. this.position = position;
  7. this.upVector = BABYLON.Vector3.Up();
  8. this._childrenFlag = true;
  9. this._scene = scene;
  10. scene.cameras.push(this);
  11. if (!scene.activeCamera) {
  12. scene.activeCamera = this;
  13. }
  14. this._computedViewMatrix = BABYLON.Matrix.Identity();
  15. this._currentRenderId = -1;
  16. // Animations
  17. this.animations = [];
  18. // Postprocesses
  19. this.postProcesses = [];
  20. };
  21. BABYLON.Camera.prototype = Object.create(BABYLON.Node.prototype);
  22. // Statics
  23. BABYLON.Camera.PERSPECTIVE_CAMERA = 0;
  24. BABYLON.Camera.ORTHOGRAPHIC_CAMERA = 1;
  25. // Members
  26. BABYLON.Camera.prototype.fov = 0.8;
  27. BABYLON.Camera.prototype.orthoLeft = null;
  28. BABYLON.Camera.prototype.orthoRight = null;
  29. BABYLON.Camera.prototype.orthoBottom = null;
  30. BABYLON.Camera.prototype.orthoTop = null;
  31. BABYLON.Camera.prototype.fov = 0.8;
  32. BABYLON.Camera.prototype.minZ = 0.1;
  33. BABYLON.Camera.prototype.maxZ = 1000.0;
  34. BABYLON.Camera.prototype.inertia = 0.9;
  35. BABYLON.Camera.prototype.mode = BABYLON.Camera.PERSPECTIVE_CAMERA;
  36. // Properties
  37. BABYLON.Camera.prototype.getScene = function () {
  38. return this._scene;
  39. };
  40. // Methods
  41. BABYLON.Camera.prototype.attachControl = function (canvas) {
  42. };
  43. BABYLON.Camera.prototype.detachControl = function (canvas) {
  44. };
  45. BABYLON.Camera.prototype._update = function () {
  46. };
  47. BABYLON.Camera.prototype.getWorldMatrix = function () {
  48. var viewMatrix = this.getViewMatrix();
  49. if (!this._worldMatrix) {
  50. this._worldMatrix = BABYLON.Matrix.Identity();
  51. }
  52. viewMatrix.invertToRef(this._worldMatrix);
  53. return this._worldMatrix;
  54. };
  55. BABYLON.Camera.prototype._getViewMatrix = function () {
  56. return BABYLON.Matrix.Identity();
  57. };
  58. BABYLON.Camera.prototype.getViewMatrix = function () {
  59. if (this._currentRenderId == this._scene.getRenderId()) {
  60. return this._computedViewMatrix;
  61. }
  62. this._computedViewMatrix = this._getViewMatrix();
  63. this._currentRenderId = this._scene.getRenderId();
  64. if (this.parent && this.parent.getWorldMatrix) {
  65. if (!this._worldMatrix) {
  66. this._worldMatrix = BABYLON.Matrix.Identity();
  67. }
  68. this._computedViewMatrix.invertToRef(this._worldMatrix);
  69. this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
  70. this._computedViewMatrix.invert();
  71. return this._computedViewMatrix;
  72. }
  73. return this._computedViewMatrix;
  74. };
  75. BABYLON.Camera.prototype.getProjectionMatrix = function () {
  76. if (!this._projectionMatrix) {
  77. this._projectionMatrix = new BABYLON.Matrix();
  78. }
  79. var engine = this._scene.getEngine();
  80. if (this.mode === BABYLON.Camera.PERSPECTIVE_CAMERA) {
  81. BABYLON.Matrix.PerspectiveFovLHToRef(this.fov, engine.getAspectRatio(), this.minZ, this.maxZ, this._projectionMatrix);
  82. return this._projectionMatrix;
  83. }
  84. var halfWidth = engine.getRenderWidth() / 2.0;
  85. var halfHeight = engine.getRenderHeight() / 2.0;
  86. BABYLON.Matrix.OrthoOffCenterLHToRef(this.orthoLeft || -halfWidth, this.orthoRight || halfWidth, this.orthoBottom || -halfHeight, this.orthoTop || halfHeight, this.minZ, this.maxZ, this._projectionMatrix);
  87. return this._projectionMatrix;
  88. };
  89. BABYLON.Camera.prototype.dispose = function () {
  90. // Remove from scene
  91. var index = this._scene.cameras.indexOf(this);
  92. this._scene.cameras.splice(index, 1);
  93. // Postprocesses
  94. while (this.postProcesses.length) {
  95. this.postProcesses[0].dispose();
  96. }
  97. };
  98. })();