Skip to content

Postprocessing shaders

zeganstyl edited this page Sep 18, 2020 · 1 revision

You need to render your scene to frame buffer, and then you can pass image data to shader.

val fb = SimpleFrameBuffer()
fb.render {
	// render scene...
}

Now, we can create shader and pass to it texture from frame buffer.

To create postprocessing shaders you can use PostShader. It already has vertex shader, so you need to write only fragment shader.

val shaderCode = """
put here fragment shader code...
"""

val shader = PostShader(fragCode = shaderCode)
val texUnit = 1
shader["tex"] = texUnit

By default PostShader uses "uv" as name of screen coordinates (you can specify any name).

Fragment shader code:

varying vec2 uv;
uniform sampler2D tex;

void main() {
	gl_FragColor = vec4(uv, 1.0, 1.0);
}

ScreenQuad creates mesh that covers entire screen and renders that mesh with shader to frame buffer. So you can take any texture and render it on this mesh.

val screenQuad = ScreenQuad()

// in main loop
fb.texture.bind(texUnit)

// null for frame buffer means, that output will be to main frame buffer
screenQuad.render(shader, null)

Take a look into post package

Clone this wiki locally