A flexible, layered post-processing system for PhaserJS 3. Apply fullscreen shaders to select scene elements.
- creates N+2 screen-sized textures for N effects
- this can potentially be reduced to 2 or 3 (no N) by calling
buffer.setPipeline()beforestage.draw(buffer)
- this can potentially be reduced to 2 or 3 (no N) by calling
- generating mipmaps for NP2 textures require a WebGL2 context
- the
exposureeffect requires WebGL2- the Phaser game must be created with the same WebGL2 context
textureLodGLSL function requires#version 300 esshaders- the fragment and vertex program version must match within the same layer (a
300 esvertex shader is provided)- mixing shader versions on different layers is allowed
- more effects
- support binding sampler to TEXTURE0 other than
stagebuffer (bound by draw call) - support writing to any target buffer, not just
stage
Tint-based pipeline for shader effects with texture caching, sampler binding, and other niceties.
Look up required textures from buffers and the scene's texture manager, and cache them with the uniform name to
which they will be bound.
Manages the buffers and pipelines for post-processing a particular scene. Methods match the scene lifecycle.
Create a new post-processing pipeline from data and bound to the scene.
Set up the post-processing pipelines and render textures, compiling shaders and caching textures.
Render a list of objects using the post-processing layers positioned for the camera.
Shut down the post-processing pipelines and clean up cached textures and WebGL resources.
The data structure used to initialize a PostProcessor instance describes the layers and effects they use. An
example is attached.
nameeffectslayersmipmapsscreen
The effect pipelines with shader source.
namefragment: fragment program sourcesamplers:name: uniform name as it appears in the fragment programsource: buffer (scene,stage) or texture name
vertex: vertex program source
A list of effects to be applied in order, with optional uniforms for each.
name: the effectnameuniforms:name: the uniform name within the shader programvalue: a float1 value
Update mipmaps for the scene and stage buffers each frame.
Note: This requires WebGL2 for NP2 framebuffers.
The screen size.
xy
Reduce color space to pseudo 8-bit.
Laplacian edge detection.
Invert colors per-channel.
Luminance-based grayscale.
Partial desaturation with overall brown tint, like old-fashioned photographs.
Gradiated desaturation with circular black edge.
Add the previos stage to the original scene.
TODO
Separable blur of 7 pixels using the kernel [0.125, 0.25, 0.5, 1.0, 0.5, 0.25, 0.125].
Offset pixels from the previous stage using a normal map from another texture.
TODO
Clamp scene colors between the high and low points, then expand that to fill the full [0.0, 1.0) texture range.
This effect can act as a low-pass/high-cut or high-pass/low-cut filter by setting the range:
- high pass:
uMin: desired cutoffuMax: 1.0
- low pass:
uMin: 0.0uMax: desired cutoff
Adjust scene luminance using a 4-point curve.
TODO
Adjust scene colors using a 2D tonemap texture, based on the biome tint/color palette effect from Crysis 1.
A very rudimentary and not at all production-suitable fake HDR effect.
Note: Requires WebGL2 and GLSL version 300 for the textureLod function and generating mipmaps on NP2 textures.
Most effects can be used on their own, but may not be useful. Including a few layers in the right order can produce much more complex effects.
- Clamp (high pass)
- high: 1.0
- low: 0.8
- Blur (repeat as needed)
- X
- Y
- Sum
This is not really much code, needs plenty of cleanup, and none of the shaders are original.
The postprocessor.ts and data.yml files are public domain, excluding the exposure effect's vertex shader,
which was copied from Phaser's source.