Skip to content

Custom shaders

shader() makes a filter from your own fragment shader. Use it for what the built-in filters can’t do: waves, ripples, dissolves, pixelation, outlines.

import { shader, brightness } from '@fabiplay/engine'
const wave = shader({
fragment: `
uniform float uAmp;
void main() {
vec2 uv = vUv + vec2(sin(vUv.y * 60.0 + uTime * 5.0) * uAmp, 0.0);
gl_FragColor = texture2D(uTexture, uv);
}`,
uniforms: { uAmp: 0.004 },
padding: 6,
})
flag.filters = engine.supports.shaders ? [wave] : [brightness(1.1)]

The fragment is GLSL ES 1.00 (the language both WebGL 1 and 2 accept). Write main() and declare your own uniforms. The engine puts this before your code (don’t declare these again):

precision highp float; // mediump where highp is missing
uniform sampler2D uTexture; // the node as drawn so far (premultiplied alpha)
varying vec2 vUv; // this pixel's place in uTexture, 0–1, (0, 0) bottom-left
uniform vec2 uResolution; // uTexture's size in pixels (the canvas's)
uniform float uTime; // engine.time, seconds: stops on pause and breaks
uniform vec4 uRect; // the node's area in uTexture (x0, y0, x1, y1), 0–1
  • Premultiplied alpha in and out: texture2D(uTexture, …) gives premultiplied colours; write premultiplied colours to gl_FragColor. To change colours, divide by alpha first, multiply after.
  • The engine applies the node’s alpha after your shader; the clip and mask apply too.
  • One pixel of the screen is one pixel of uTexture: to read a neighbour, vUv + vec2(1.0, 0.0) / uResolution.
  • Reading outside the node’s area gives transparent pixels. If your effect moves pixels outward (a wave, a shadow), give padding (stage units) so that area is processed too.
  • Error line numbers are your code’s lines (the engine’s lines don’t count).

uniforms takes numbers (float) and arrays of 2–4 numbers (vec2–vec4). Change them any time, without allocating:

wave.uniforms.uAmp = 0.01 // a float
ripple.uniforms.uCenter[0] = hand.x / engine.stage.width // change a vector in place
engine.tween(wave.uniforms, { uAmp: 0 }, 0.5) // tween a float

Arrays become Float32Arrays when the shader is made: change their items, don’t replace them with plain arrays every frame. int, bool and mat uniforms are not supported (use floats). A uniform the compiler optimised away (unused) is ignored.

uTime is game time: shaders freeze with the game on pause. For time that keeps running, pass your own uniform.

  • A shader is compiled once per distinct source: making the same shader for 20 nodes compiles it once (each filter keeps its own uniforms).
  • If a WebGL engine is running when you call shader(), the source is compiled right away and a compile error throws, with the compiler’s log:
    shader(): the fragment does not compile:
    ERROR: 0:3: 'uAmpp' : undeclared identifier
  • Made before the engine (at import time), the shader is compiled at its first use. An error is then logged once on the console, and the filter is skipped (counted in engine.stats.filtersSkipped); it is never compiled again.
  • After a lost WebGL context, shaders are compiled again by themselves.

Your game runs sandboxed in an iframe and the shader is your own code, so the engine adds no sandbox. GLSL ES 1.00 loops have constant bounds, so a shader always ends, but a very heavy one can make the GPU so slow that the browser resets it: the engine then waits for the context and, after 3 seconds, carries on in Canvas 2D (without shaders).

  • A shader costs one pass over the node’s area: cheap on a sprite, expensive on a full-screen background on a TV.
  • Keep texture reads per pixel low (a 9-tap blur is 9 reads). Test on ?tier=low.
  • Engines keep up to 64 distinct shaders compiled; past that the one used least recently is dropped (and compiled again if used).