diff --git a/src/entities/Bloom.ts b/src/entities/Bloom.ts index 4fbde30..2266b8e 100644 --- a/src/entities/Bloom.ts +++ b/src/entities/Bloom.ts @@ -1,6 +1,5 @@ import { BufferRenderTarget } from '../heck/BufferRenderTarget'; import { Entity } from '../heck/Entity'; -import { GLCatTexture } from '@fms-cat/glcat-ts'; import { Material } from '../heck/Material'; import { Quad } from '../heck/components/Quad'; import { RenderTarget } from '../heck/RenderTarget'; @@ -12,7 +11,7 @@ import bloomBlurFrag from '../shaders/bloom-blur.frag'; import bloomPostFrag from '../shaders/bloom-post.frag'; export interface BloomOptions { - input: GLCatTexture; + input: BufferRenderTarget; target: RenderTarget; } @@ -40,7 +39,7 @@ export class Bloom { quadVert, bloomPreFrag ); - materialBloomPre.addUniformTexture( 'sampler0', options.input ); + materialBloomPre.addUniformTexture( 'sampler0', options.input.texture ); this.entity.components.push( new Quad( { target: swap.o, @@ -92,7 +91,7 @@ export class Bloom { quadVert, bloomPostFrag ); - materialBloomPost.addUniformTexture( 'samplerDry', options.input ); + materialBloomPost.addUniformTexture( 'samplerDry', options.input.texture ); materialBloomPost.addUniformTexture( 'samplerWet', swap.i.texture ); this.entity.components.push( new Quad( { diff --git a/src/entities/Glitch.ts b/src/entities/Glitch.ts index e11721b..9b378c2 100644 --- a/src/entities/Glitch.ts +++ b/src/entities/Glitch.ts @@ -1,14 +1,16 @@ import { Entity } from '../heck/Entity'; -import { GLCatTexture } from '@fms-cat/glcat-ts'; import { Material } from '../heck/Material'; import { Quad } from '../heck/components/Quad'; import { RenderTarget } from '../heck/RenderTarget'; import quadVert from '../shaders/quad.vert'; import glitchFrag from '../shaders/glitch.frag'; import { Automaton } from '@fms-cat/automaton'; +import returnFrag from '../shaders/return.frag'; +import { BufferRenderTarget } from '../heck/BufferRenderTarget'; +import { Blit } from '../heck/components/Blit'; export interface GlitchOptions { - input: GLCatTexture; + input: BufferRenderTarget; target: RenderTarget; automaton: Automaton; } @@ -22,9 +24,34 @@ export class Glitch { public constructor( options: GlitchOptions ) { this.entity = new Entity(); + const entityBypass = new Entity(); + entityBypass.visible = false; + this.entity.children.push( entityBypass ); + + const entityMain = new Entity(); + entityMain.active = false; + entityMain.visible = false; + this.entity.children.push( entityMain ); + + // -- bypass ----------------------------------------------------------------------------------- + const materialReturn = new Material( + quadVert, + returnFrag, + ); + materialReturn.addUniformTexture( + 'sampler0', + options.input.texture, + ); + + entityBypass.components.push( new Blit( { + src: options.input, + dst: options.target, + name: 'Glitch/blitBypass', + } ) ); + // -- quad ------------------------------------------------------------------------------------- this.material = new Material( quadVert, glitchFrag ); - this.material.addUniformTexture( 'sampler0', options.input ); + this.material.addUniformTexture( 'sampler0', options.input.texture ); if ( module.hot ) { module.hot.accept( '../shaders/glitch.frag', () => { @@ -37,11 +64,14 @@ export class Glitch { material: this.material, name: process.env.DEV && 'Glitch/quad', } ); - this.entity.components.push( quad ); + entityMain.components.push( quad ); // -- update uniform --------------------------------------------------------------------------- options.automaton.auto( 'Glitch/amp', ( { value } ) => { this.material.addUniform( 'amp', '1f', value ); + + entityMain.active = 0.0 < value; + entityBypass.active = !entityMain.active; } ); } } diff --git a/src/entities/PixelSorter.ts b/src/entities/PixelSorter.ts index 07233d1..87b542f 100644 --- a/src/entities/PixelSorter.ts +++ b/src/entities/PixelSorter.ts @@ -1,5 +1,4 @@ import { Entity } from '../heck/Entity'; -import { GLCatTexture } from '@fms-cat/glcat-ts'; import { Material } from '../heck/Material'; import { Quad } from '../heck/components/Quad'; import { RenderTarget } from '../heck/RenderTarget'; @@ -10,10 +9,10 @@ import quadVert from '../shaders/quad.vert'; import { BufferRenderTarget } from '../heck/BufferRenderTarget'; import { Swap } from '@fms-cat/experimental'; import { Automaton } from '@fms-cat/automaton'; -import { gl } from '../heck/canvas'; +import { Blit } from '../heck/components/Blit'; export interface PixelSorterOptions { - input: GLCatTexture; + input: BufferRenderTarget; target: RenderTarget; automaton: Automaton; } @@ -61,12 +60,13 @@ export class PixelSorter { ); materialReturn.addUniformTexture( 'sampler0', - options.input, + options.input.texture, ); - entityBypass.components.push( new Quad( { - target: options.target, - material: materialReturn, + entityBypass.components.push( new Blit( { + src: options.input, + dst: options.target, + name: 'PixelSorter/blitBypass', } ) ); // -- calc index ------------------------------------------------------------------------------- @@ -83,7 +83,7 @@ export class PixelSorter { material.addUniform( 'mul', '1f', mul ); material.addUniformTexture( 'sampler0', - options.input, + options.input.texture, ); material.addUniformTexture( 'sampler1', @@ -118,7 +118,7 @@ export class PixelSorter { material.addUniform( 'comp', '1f', comp ); material.addUniformTexture( 'sampler0', - isFirst ? options.input : this.swapBuffer.o.texture, + ( isFirst ? options.input : this.swapBuffer.o ).texture, ); material.addUniformTexture( 'sampler1', diff --git a/src/entities/Post.ts b/src/entities/Post.ts index e3c8708..810829d 100644 --- a/src/entities/Post.ts +++ b/src/entities/Post.ts @@ -1,13 +1,13 @@ import { Entity } from '../heck/Entity'; -import { GLCatTexture } from '@fms-cat/glcat-ts'; import { Material } from '../heck/Material'; import { Quad } from '../heck/components/Quad'; import { RenderTarget } from '../heck/RenderTarget'; import postFrag from '../shaders/post.frag'; import quadVert from '../shaders/quad.vert'; +import { BufferRenderTarget } from '../heck/BufferRenderTarget'; export interface PostOptions { - input: GLCatTexture; + input: BufferRenderTarget; target: RenderTarget; } @@ -23,7 +23,7 @@ export class Post { quadVert, postFrag, ); - material.addUniformTexture( 'sampler0', options.input ); + material.addUniformTexture( 'sampler0', options.input.texture ); if ( process.env.DEV ) { if ( module.hot ) { diff --git a/src/entities/Return.ts b/src/entities/Return.ts index 1ca480a..72ccf59 100644 --- a/src/entities/Return.ts +++ b/src/entities/Return.ts @@ -1,13 +1,13 @@ import { Entity } from '../heck/Entity'; -import { GLCatTexture } from '@fms-cat/glcat-ts'; import { Material } from '../heck/Material'; import { Quad } from '../heck/components/Quad'; import { RenderTarget } from '../heck/RenderTarget'; import returnFrag from '../shaders/return.frag'; import quadVert from '../shaders/quad.vert'; +import { BufferRenderTarget } from '../heck/BufferRenderTarget'; export interface PostOptions { - input: GLCatTexture; + input: BufferRenderTarget; target: RenderTarget; } @@ -22,7 +22,7 @@ export class Return { quadVert, returnFrag, ); - material.addUniformTexture( 'sampler0', options.input ); + material.addUniformTexture( 'sampler0', options.input.texture ); this.entity.components.push( new Quad( { target: options.target, diff --git a/src/heck/components/Blit.ts b/src/heck/components/Blit.ts new file mode 100644 index 0000000..bb6e944 --- /dev/null +++ b/src/heck/components/Blit.ts @@ -0,0 +1,54 @@ +import { Component, ComponentOptions, ComponentUpdateEvent } from './Component'; +import { RenderTarget } from '../RenderTarget'; +import { gl } from '../canvas'; +import { BufferRenderTarget } from '../BufferRenderTarget'; + +export interface BlitOptions extends ComponentOptions { + src: BufferRenderTarget; + dst: RenderTarget; + srcRect?: [ number, number, number, number ]; + dstRect?: [ number, number, number, number ]; + mask?: GLenum; + filter?: GLenum; +} + +/** + * Blit. + */ +export class Blit extends Component { + public src: BufferRenderTarget; + public dst: RenderTarget; + public srcRect: [ number, number, number, number ]; + public dstRect: [ number, number, number, number ]; + public mask: GLenum; + public filter: GLenum; + + public constructor( options: BlitOptions ) { + super( options ); + + this.visible = false; + + this.src = options.src; + this.dst = options.dst; + this.srcRect = options.srcRect ?? [ 0, 0, this.src.width, this.src.height ]; + this.dstRect = options.dstRect ?? [ 0, 0, this.dst.width, this.dst.height ]; + this.mask = options.mask ?? gl.COLOR_BUFFER_BIT; + this.filter = options.filter ?? gl.NEAREST; + } + + protected __updateImpl(): void { + gl.bindFramebuffer( gl.READ_FRAMEBUFFER, this.src.framebuffer.raw ); + if ( this.dst instanceof BufferRenderTarget ) { + gl.bindFramebuffer( gl.DRAW_FRAMEBUFFER, this.dst.framebuffer.raw ); + } else { + gl.bindFramebuffer( gl.DRAW_FRAMEBUFFER, null ); + } + + gl.blitFramebuffer( + ...this.srcRect, + ...this.dstRect, + this.mask, + this.filter, + ); + } +} diff --git a/src/main.ts b/src/main.ts index 4e63bcc..0ee3b52 100644 --- a/src/main.ts +++ b/src/main.ts @@ -312,14 +312,14 @@ dog.root.children.push( camera.entity ); swap.swap(); const bloom = new Bloom( { - input: swap.i.texture, + input: swap.i, target: swap.o } ); dog.root.children.push( bloom.entity ); swap.swap(); const glitch = new Glitch( { - input: swap.i.texture, + input: swap.i, target: swap.o, automaton, } ); @@ -327,7 +327,7 @@ dog.root.children.push( glitch.entity ); swap.swap(); const pixelSorter = new PixelSorter( { - input: swap.i.texture, + input: swap.i, target: swap.o, automaton, } ); @@ -335,7 +335,7 @@ dog.root.children.push( pixelSorter.entity ); swap.swap(); const post = new Post( { - input: swap.i.texture, + input: swap.i, target: canvasRenderTarget } ); dog.root.children.push( post.entity );