| 10 | import type {Effect, EffectContext, PostRenderOptions} from '../lib/effect'; |
| 11 | |
| 12 | export default class PostProcessEffect<ShaderPassT extends ShaderPass> implements Effect { |
| 13 | id: string; |
| 14 | props: ShaderPassT['props']; |
| 15 | module: ShaderPassT; |
| 16 | passes?: ScreenPass[]; |
| 17 | |
| 18 | constructor(module: ShaderPassT, props: ShaderPassT['props']) { |
| 19 | this.id = `${module.name}-pass`; |
| 20 | this.props = props; |
| 21 | initializeShaderModule(module); |
| 22 | this.module = module; |
| 23 | } |
| 24 | |
| 25 | setup({device}: EffectContext) { |
| 26 | this.passes = createPasses(device, this.module, this.id); |
| 27 | } |
| 28 | |
| 29 | setProps(props: ShaderPassT['props']) { |
| 30 | this.props = props; |
| 31 | } |
| 32 | |
| 33 | // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 34 | preRender(): void {} |
| 35 | |
| 36 | postRender(params: PostRenderOptions): Framebuffer { |
| 37 | const passes = this.passes!; |
| 38 | |
| 39 | const {target} = params; |
| 40 | let inputBuffer = params.inputBuffer; |
| 41 | let outputBuffer: Framebuffer | null = params.swapBuffer; |
| 42 | |
| 43 | for (let index = 0; index < passes.length; index++) { |
| 44 | const isLastPass = index === passes.length - 1; |
| 45 | const renderToTarget = target !== undefined && isLastPass; |
| 46 | if (renderToTarget) { |
| 47 | outputBuffer = target; |
| 48 | } |
| 49 | const clearCanvas = !renderToTarget || Boolean(params.clearCanvas); |
| 50 | const moduleProps = {}; |
| 51 | const uniforms = this.module.passes![index].uniforms; |
| 52 | moduleProps[this.module.name] = {...this.props, ...uniforms}; |
| 53 | passes[index].render({clearCanvas, inputBuffer, outputBuffer, moduleProps}); |
| 54 | |
| 55 | const switchBuffer = outputBuffer as Framebuffer; |
| 56 | outputBuffer = inputBuffer; |
| 57 | inputBuffer = switchBuffer; |
| 58 | } |
| 59 | return inputBuffer; |
| 60 | } |
| 61 | |
| 62 | cleanup(): void { |
| 63 | if (this.passes) { |
| 64 | for (const pass of this.passes) { |
| 65 | pass.delete(); |
| 66 | } |
| 67 | this.passes = undefined; |
| 68 | } |
| 69 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…