babylon.camera.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.orthoLeft = null;
  29. BABYLON.Camera.prototype.orthoRight = null;
  30. BABYLON.Camera.prototype.orthoBottom = null;
  31. BABYLON.Camera.prototype.orthoTop = null;
  32. BABYLON.Camera.prototype.fov = 0.8;
  33. BABYLON.Camera.prototype.minZ = 0.1;
  34. BABYLON.Camera.prototype.maxZ = 1000.0;
  35. BABYLON.Camera.prototype.inertia = 0.9;
  36. BABYLON.Camera.prototype.mode = BABYLON.Camera.PERSPECTIVE_CAMERA;
  37. // Properties
  38. BABYLON.Camera.prototype.getScene = function () {
  39. return this._scene;
  40. };
  41. // Methods
  42. BABYLON.Camera.prototype.attachControl = function (canvas) {
  43. };
  44. BABYLON.Camera.prototype.detachControl = function (canvas) {
  45. };
  46. BABYLON.Camera.prototype._update = function () {
  47. };
  48. BABYLON.Camera.prototype.getWorldMatrix = function () {
  49. var viewMatrix = this.getViewMatrix();
  50. if (!this._worldMatrix) {
  51. this._worldMatrix = BABYLON.Matrix.Identity();
  52. }
  53. viewMatrix.invertToRef(this._worldMatrix);
  54. return this._worldMatrix;
  55. };
  56. BABYLON.Camera.prototype._getViewMatrix = function () {
  57. return BABYLON.Matrix.Identity();
  58. };
  59. BABYLON.Camera.prototype.getViewMatrix = function () {
  60. this._computedViewMatrix = this._getViewMatrix();
  61. if (this.parent && this.parent.getWorldMatrix) {
  62. if (!this._worldMatrix) {
  63. this._worldMatrix = BABYLON.Matrix.Identity();
  64. }
  65. this._computedViewMatrix.invertToRef(this._worldMatrix);
  66. this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
  67. this._computedViewMatrix.invert();
  68. return this._computedViewMatrix;
  69. }
  70. return this._computedViewMatrix;
  71. };
  72. BABYLON.Camera.prototype.getProjectionMatrix = function () {
  73. if (!this._projectionMatrix) {
  74. this._projectionMatrix = new BABYLON.Matrix();
  75. }
  76. var engine = this._scene.getEngine();
  77. if (this.mode === BABYLON.Camera.PERSPECTIVE_CAMERA) {
  78. BABYLON.Matrix.PerspectiveFovLHToRef(this.fov, engine.getAspectRatio(), this.minZ, this.maxZ, this._projectionMatrix);
  79. return this._projectionMatrix;
  80. }
  81. var halfWidth = engine.getRenderWidth() / 2.0;
  82. var halfHeight = engine.getRenderHeight() / 2.0;
  83. BABYLON.Matrix.OrthoOffCenterLHToRef(this.orthoLeft || -halfWidth, this.orthoRight || halfWidth, this.orthoBottom || -halfHeight, this.orthoTop || halfHeight, this.minZ, this.maxZ, this._projectionMatrix);
  84. return this._projectionMatrix;
  85. };
  86. BABYLON.Camera.prototype.dispose = function () {
  87. // Remove from scene
  88. var index = this._scene.cameras.indexOf(this);
  89. this._scene.cameras.splice(index, 1);
  90. // Postprocesses
  91. while (this.postProcesses.length) {
  92. this.postProcesses[0].dispose();
  93. }
  94. };
  95. })();