babylon.camera.js 3.9 KB

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