Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A short implementation of a shader pass based on (mostly stolen from) https://github.com/stackgl/gl-particles
import drawTriangle from "a-big-triangle";
import createShader from "gl-shader";
import createFBO from "gl-fbo";
const vertex = [
"precision mediump float;",
"attribute vec2 position;",
"void main() {",
" gl_Position = vec4( position, 1, 1 );",
"}"
].join('\n')
export class ShaderPass {
constructor( gl, size, source ) {
this.gl = gl;
this.size = size;
this.logic = createShader( gl, vertex, source );
this.previous = createFBO( gl, size, size, { float: true } );
this.current = createFBO( gl, size, size, { float: true } );
}
step( update ) {
this.gl.disable( this.gl.BLEND );
this.current.bind();
this.gl.viewport( 0, 0, this.size, this.size );
this.logic.bind();
this.logic.uniforms.resolution = [ this.size, this.size ];
this.logic.uniforms.data = this.previous.color[ 0 ].bind( 0 );
if ( update ) update( this.logic.uniforms );
drawTriangle( this.gl );
this.gl.bindFramebuffer( this.gl.FRAMEBUFFER, null );
const temp = this.previous;
this.previous = this.current;
this.current = temp;
this.gl.enable( this.gl.BLEND );
}
getTexture() {
return this.previous.color[ 0 ];
}
dispose() {
this.logic.dispose();
this.previous.dispose();
this.current.dispose();
this.logic = this.previous = this.current = undefined;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment