BrdfLutGenerator.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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._framebuffer = undefined;
  16. this._colorTexture = undefined;
  17. this._drawCommand = undefined;
  18. }
  19. Object.defineProperties(BrdfLutGenerator.prototype, {
  20. colorTexture: {
  21. get: function () {
  22. return this._colorTexture;
  23. },
  24. },
  25. });
  26. function createCommand(generator, context) {
  27. var framebuffer = generator._framebuffer;
  28. var drawCommand = context.createViewportQuadCommand(BrdfLutGeneratorFS, {
  29. framebuffer: framebuffer,
  30. renderState: RenderState.fromCache({
  31. viewport: new BoundingRectangle(0.0, 0.0, 256.0, 256.0),
  32. }),
  33. });
  34. generator._drawCommand = drawCommand;
  35. }
  36. function createFramebuffer(generator, context) {
  37. var 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. generator._colorTexture = colorTexture;
  46. var framebuffer = new Framebuffer({
  47. context: context,
  48. colorTextures: [colorTexture],
  49. destroyAttachments: false,
  50. });
  51. generator._framebuffer = framebuffer;
  52. }
  53. BrdfLutGenerator.prototype.update = function (frameState) {
  54. if (!defined(this._colorTexture)) {
  55. var context = frameState.context;
  56. createFramebuffer(this, context);
  57. createCommand(this, context);
  58. this._drawCommand.execute(context);
  59. this._framebuffer = this._framebuffer && this._framebuffer.destroy();
  60. this._drawCommand.shaderProgram =
  61. this._drawCommand.shaderProgram &&
  62. this._drawCommand.shaderProgram.destroy();
  63. }
  64. };
  65. BrdfLutGenerator.prototype.isDestroyed = function () {
  66. return false;
  67. };
  68. BrdfLutGenerator.prototype.destroy = function () {
  69. this._colorTexture = this._colorTexture && this._colorTexture.destroy();
  70. return destroyObject(this);
  71. };
  72. export default BrdfLutGenerator;