TExcelTAD

Dissolve

An enemy dies and burns away from the edges. A portal eats through a wall. A character teleports in, assembled from glowing fragments. All of these are a dissolve shader: one of the most common effects in games, and a perfect example of how a single noise texture and one threshold can do a lot.

The idea in one line

Every pixel of the object gets a fixed random-looking value n (from noise). A uniform amount goes from 0 to 1 over time. Pixels whose noise is below amount are thrown away; the rest are drawn:

glsl
if (n < amount) discard;

discard tells the GPU to write nothing for this fragment: no color, no depth. Because the noise is smooth, the holes grow as connected, organic blobs instead of random dots.

glsl · try it yourself
float hash12(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * 0.1031);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
}
float noise(vec2 p) {
    vec2 i = floor(p), f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    return mix(mix(hash12(i), hash12(i + vec2(1, 0)), u.x),
               mix(hash12(i + vec2(0, 1)), hash12(i + vec2(1, 1)), u.x), u.y);
}
float fbm(vec2 p) {
    float s = 0.0, a = 0.5;
    for (int i = 0; i < 4; i++) { s += a * noise(p); p *= 2.0; a *= 0.5; }
    return s / 0.9375;                                    // normalize to about 0..1
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    vec2 p = fragCoord / iResolution.y;
    float amount = 0.5 + 0.5 * sin(iTime);                // 0..1 and back

    vec3 background = mix(vec3(0.1), vec3(0.2), step(0.5, fract((p.x + p.y) * 8.0)));
    vec3 picture = mix(vec3(0.2, 0.4, 0.9), vec3(0.9, 0.5, 0.2), uv.x);   // "the object"

    float n = fbm(p * 5.0);
    vec3 color = n < amount ? background : picture;       // the 2D stand-in for discard
    fragColor = vec4(color, 1.0);
}

In a full-screen 2D shader there is nothing behind the pixel to reveal, so the example picks the background color itself. On a mesh, discard lets the real scene behind show through.

The burning edge

A plain cut looks like holes in paper. The effect becomes "fire" when the pixels just above the threshold glow. Measure how close each pixel is to being dissolved, n - amount, and color a thin band:

glsl · try it yourself
float hash12(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * 0.1031);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
}
float noise(vec2 p) {
    vec2 i = floor(p), f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    return mix(mix(hash12(i), hash12(i + vec2(1, 0)), u.x),
               mix(hash12(i + vec2(0, 1)), hash12(i + vec2(1, 1)), u.x), u.y);
}
float fbm(vec2 p) {
    float s = 0.0, a = 0.5;
    for (int i = 0; i < 4; i++) { s += a * noise(p); p *= 2.0; a *= 0.5; }
    return s / 0.9375;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    vec2 p = fragCoord / iResolution.y;
    float amount = fract(iTime * 0.25) * 1.2 - 0.1;       // loops from -0.1 to 1.1
    float edgeWidth = 0.06;

    vec3 background = vec3(0.05, 0.05, 0.08);
    vec3 picture = mix(vec3(0.75), vec3(0.4), step(0.5, fract(uv.y * 6.0)));

    float n = fbm(p * 4.0);
    float d = n - amount;                                  // < 0: gone, small > 0: burning
    vec3 color;
    if (d < 0.0) {
        color = background;
    } else {
        float edge = 1.0 - smoothstep(0.0, edgeWidth, d);  // 1 at the cut, 0 further in
        vec3 fire = mix(vec3(1.0, 0.25, 0.0), vec3(1.0, 0.9, 0.4), edge);
        color = mix(picture, fire * 2.0, edge);            // values > 1 read as "hot" in HDR/bloom
    }
    fragColor = vec4(color, 1.0);
}

Starting amount slightly below 0 and ending slightly above 1 matters: fbm never reaches exactly 0 or 1, so a 0..1 range leaves a few stubborn pixels at the start and the end.

A directional dissolve

Pure noise dissolves everywhere at once. For "burn from the feet up" or "wipe from left to right", add a gradient to the noise. The cut line then moves in that direction, still ragged from the noise:

glsl · try it yourself
float hash12(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * 0.1031);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
}
float noise(vec2 p) {
    vec2 i = floor(p), f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    return mix(mix(hash12(i), hash12(i + vec2(1, 0)), u.x),
               mix(hash12(i + vec2(0, 1)), hash12(i + vec2(1, 1)), u.x), u.y);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    vec2 p = fragCoord / iResolution.y;
    float amount = fract(iTime * 0.3) * 1.4 - 0.2;

    float ragged = noise(p * 8.0) * 0.3;                  // how ragged the edge is
    float n = uv.y * 0.7 + ragged;                         // bottom dissolves first
    float d = n - amount;

    vec3 color = vec3(0.04, 0.06, 0.1);
    if (d > 0.0) {
        float edge = 1.0 - smoothstep(0.0, 0.04, d);
        color = mix(vec3(0.2, 0.7, 0.5), vec3(0.4, 1.0, 1.6), edge);   // teleport-blue edge
    }
    fragColor = vec4(color, 1.0);
}

In 3D, the gradient usually comes from the object's world or object-space position (position.y for bottom-up) instead of UVs, so the effect ignores UV seams.

Production notes

Concern What to do
alpha clipping vs discard engines expose the same thing as Alpha Clip (Unity URP, Shader Graph Alpha Clip Threshold), Opacity Mask with the Masked blend mode (Unreal), or ALPHA_SCISSOR_THRESHOLD (Godot)
shadows the shadow pass must clip with the same noise, or the dissolved object still casts a full shadow. Engine alpha-clip settings handle this; custom passes must copy the logic
performance discard disables some early depth-test optimizations on many GPUs. Fine for a few dying enemies, not for every surface in the level
noise source in production, sample a tiling noise texture instead of computing fbm: one fetch instead of dozens of instructions
driving it animate amount from gameplay code (e.g. 0 → 1 over 1.5 s after death), never from global time
glow output the edge as emissive with values above 1 so bloom picks it up

The One Dissolve, Three Engines lesson writes this exact effect in Unity, Unreal and Godot.

Test yourself

What does discard do in a fragment shader?

  1. Writes black
  2. Writes nothing for that fragment: no color and no depth
  3. Ends the whole draw call
  4. Skips the vertex shader

How is the glowing burn edge found?

  1. With a second texture
  2. From pixels whose noise is only slightly above the threshold (small n - amount)
  3. With a blur
  4. From the normal

Why can a dissolving object still cast a full shadow in a custom shader?

  1. Shadows ignore materials
  2. The shadow-caster pass doesn't apply the same clip, so it renders the whole mesh
  3. discard only works in shadows
  4. Bloom adds the shadow