Custom visual effects with GLSL shaders for web games. A tested library of 2D fragment effects (hit flash, outline, dissolve, CRT, chromatic aberration, pixelate, palette swap, wave distortion, desaturate, hurt vignette), how to run them as post-processing or per sprite in raw WebGL2, Pixi filters, Phaser pipelines and three.js ShaderMaterial / passes, uniforms and time, premultiplied alpha, pr...
Download skill (.zip) Files on GitHubShader development
Library: assets/shaders.js, 10 effects compiled and rendered on a real GPU (check_gpu_render: 15/15 checks, including
output pixels). Runner for raw WebGL2: webgl-rendering’s assets/gl.js.
import { FULLSCREEN_VERT, EFFECTS, DEFAULTS } from "./core/shaders.js";
import { createGL, fullscreenPass, renderTarget, texture } from "./core/gl.js";
const crt = fullscreenPass(gl, FULLSCREEN_VERT, EFFECTS.crt);
// render the game into a target, then: crt.draw(sceneTarget.tex, { uStrength: 0.8 }); // uTex, uRes, uTime are set for you
| Effect | Uniforms | Use |
|---|---|---|
flash |
uAmount, uColor | Hit flash on sprites |
outline |
uColor, uTexel | Selection, interactables |
dissolve |
uProgress | Death, teleport |
crt |
uStrength | Retro filter (whole screen) |
chroma |
uAmount (px) | Big hit pulse |
pixelate |
uSize | Transitions, retro |
palette |
uPal[4] | Game Boy / limited palettes |
wave |
uAmp, uFreq | Water, heat |
desaturate |
uAmount | Pause, game over, memories |
hurt |
uHurt | Red vignette when damaged |
In engines
- Pixi v8:
new Filter({ glProgram: GlProgram.from({ vertex, fragment }), resources: { … } }). Pixi’s uniforms use its own naming (uTexture,vTextureCoord), so rename in the effect body. - Phaser 3: a
PostFXPipelinesubclass with the fragment source; set uniforms inonPreRender. - three.js:
ShaderPass(EffectComposer) for post effects,ShaderMaterial/onBeforeCompilefor materials (ps1-retro-graphics shows the pattern).
Rules
- Never test shaders in the sandbox browser (no GPU). Use the viewer’s preview or
check_gpu_render, and read pixels or view the screenshot. Compile errors:gl.jsthrows with the failing line. - WebGL2 /
#version 300 es,precision mediump float(fast on phones). Usehighponly for positions or big coordinates. - Premultiplied alpha: if blending with
ONE, ONE_MINUS_SRC_ALPHA, outputrgb * a. (The outline effect had exactly this bug in testing: a yellow background. Fixed.) - Avoid heavy loops and many texture reads per pixel on phones; full-screen effects at half resolution when possible.
- Time-based effects use
uTime(seconds), so they’re frame-rate independent. Pause them with the game. - Offer a toggle for strong effects (CRT, chroma, flashing) for comfort and accessibility.