Created
March 20, 2017 02:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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