BrdfLutGenerator.js 3.2 KB

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