Skip to content

Instantly share code, notes, and snippets.

@roboshoes
Created March 20, 2017 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roboshoes/58015c17c41a072db6c8ae8d44de61c9 to your computer and use it in GitHub Desktop.
Save roboshoes/58015c17c41a072db6c8ae8d44de61c9 to your computer and use it in GitHub Desktop.
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