BrdfLutGenerator.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import BoundingRectangle from "../Core/BoundingRectangle.js";
  2. import defined from "../Core/defined.js";
  3. import destroyObject from "../Core/destroyObject.js";
  4. import PixelFormat from "../Core/PixelFormat.js";
  5. import Framebuffer from "../Renderer/Framebuffer.js";
  6. import PixelDatatype from "../Renderer/PixelDatatype.js";
  7. import RenderState from "../Renderer/RenderState.js";
  8. import Sampler from "../Renderer/Sampler.js";
  9. import Texture from "../Renderer/Texture.js";
  10. import BrdfLutGeneratorFS from "../Shaders/BrdfLutGeneratorFS.js";
  11. /**
  12. * @private
  13. */
  14. function BrdfLutGenerator() {
  15. this._colorTexture = undefined;
  16. this._drawCommand = undefined;
  17. }
  18. Object.defineProperties(BrdfLutGenerator.prototype, {
  19. colorTexture: {
  20. get: function () {
  21. return this._colorTexture;
  22. },
  23. },
  24. });
  25. function createCommand(generator, context, framebuffer) {
  26. const drawCommand = context.createViewportQuadCommand(BrdfLutGeneratorFS, {
  27. framebuffer: framebuffer,
  28. renderState: RenderState.fromCache({
  29. viewport: new BoundingRectangle(0.0, 0.0, 256.0, 256.0),
  30. }),
  31. });
  32. generator._drawCommand = drawCommand;
  33. }
  34. BrdfLutGenerator.prototype.update = function (frameState) {
  35. if (!defined(this._colorTexture)) {
  36. const context = frameState.context;
  37. const colorTexture = new Texture({
  38. context: context,
  39. width: 256,
  40. height: 256,
  41. pixelFormat: PixelFormat.RGBA,
  42. pixelDatatype: PixelDatatype.UNSIGNED_BYTE,
  43. sampler: Sampler.NEAREST,
  44. });
  45. this._colorTexture = colorTexture;
  46. const framebuffer = new Framebuffer({
  47. context: context,
  48. colorTextures: [colorTexture],
  49. destroyAttachments: false,
  50. });
  51. createCommand(this, context, framebuffer);
  52. this._drawCommand.execute(context);
  53. framebuffer.destroy();
  54. this._drawCommand.shaderProgram =
  55. this._drawCommand.shaderProgram &&
  56. this._drawCommand.shaderProgram.destroy();
  57. }
  58. };
  59. BrdfLutGenerator.prototype.isDestroyed = function () {
  60. return false;
  61. };
  62. BrdfLutGenerator.prototype.destroy = function () {
  63. this._colorTexture = this._colorTexture && this._colorTexture.destroy();
  64. return destroyObject(this);
  65. };
  66. export default BrdfLutGenerator;