SceneFramebuffer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import Color from "../Core/Color.js";
  2. import destroyObject from "../Core/destroyObject.js";
  3. import ClearCommand from "../Renderer/ClearCommand.js";
  4. import FramebufferManager from "../Renderer/FramebufferManager.js";
  5. import PixelDatatype from "../Renderer/PixelDatatype.js";
  6. /**
  7. * @private
  8. */
  9. function SceneFramebuffer() {
  10. this._numSamples = 1;
  11. this._colorFramebuffer = new FramebufferManager({
  12. depthStencil: true,
  13. supportsDepthTexture: true,
  14. });
  15. this._idFramebuffer = new FramebufferManager({
  16. depthStencil: true,
  17. supportsDepthTexture: true,
  18. });
  19. this._idClearColor = new Color(0.0, 0.0, 0.0, 0.0);
  20. this._clearCommand = new ClearCommand({
  21. color: new Color(0.0, 0.0, 0.0, 0.0),
  22. depth: 1.0,
  23. owner: this,
  24. });
  25. }
  26. function destroyResources(post) {
  27. post._colorFramebuffer.destroy();
  28. post._idFramebuffer.destroy();
  29. }
  30. Object.defineProperties(SceneFramebuffer.prototype, {
  31. framebuffer: {
  32. get: function () {
  33. return this._colorFramebuffer.framebuffer;
  34. },
  35. },
  36. idFramebuffer: {
  37. get: function () {
  38. return this._idFramebuffer.framebuffer;
  39. },
  40. },
  41. depthStencilTexture: {
  42. get: function () {
  43. return this._colorFramebuffer.getDepthStencilTexture();
  44. },
  45. },
  46. });
  47. SceneFramebuffer.prototype.update = function (
  48. context,
  49. viewport,
  50. hdr,
  51. numSamples
  52. ) {
  53. const width = viewport.width;
  54. const height = viewport.height;
  55. const pixelDatatype = hdr
  56. ? context.halfFloatingPointTexture
  57. ? PixelDatatype.HALF_FLOAT
  58. : PixelDatatype.FLOAT
  59. : PixelDatatype.UNSIGNED_BYTE;
  60. this._numSamples = numSamples;
  61. this._colorFramebuffer.update(
  62. context,
  63. width,
  64. height,
  65. numSamples,
  66. pixelDatatype
  67. );
  68. this._idFramebuffer.update(context, width, height);
  69. };
  70. SceneFramebuffer.prototype.clear = function (context, passState, clearColor) {
  71. Color.clone(clearColor, this._clearCommand.color);
  72. Color.clone(this._idClearColor, this._clearCommand.color);
  73. this._colorFramebuffer.clear(context, this._clearCommand, passState);
  74. this._idFramebuffer.clear(context, this._clearCommand, passState);
  75. };
  76. SceneFramebuffer.prototype.getFramebuffer = function () {
  77. return this._colorFramebuffer.framebuffer;
  78. };
  79. SceneFramebuffer.prototype.getIdFramebuffer = function () {
  80. return this._idFramebuffer.framebuffer;
  81. };
  82. SceneFramebuffer.prototype.prepareColorTextures = function (context) {
  83. if (this._numSamples > 1) {
  84. this._colorFramebuffer.prepareTextures(context);
  85. }
  86. };
  87. SceneFramebuffer.prototype.isDestroyed = function () {
  88. return false;
  89. };
  90. SceneFramebuffer.prototype.destroy = function () {
  91. destroyResources(this);
  92. return destroyObject(this);
  93. };
  94. export default SceneFramebuffer;