Shaders From Zero · Episode 10

Dissolve / Burn

Dissolve / Burn is Episode 10 of Shaders From Zero, a MasterAllArts free interactive lesson for artists learning creative 3D on the web.

Part of the MasterAllArts Creative 3D Roadmap .

Episode 10 · Shaders From Zero

Half the universe disappeared in Avengers: Infinity War. The shader behind that moment is a five-line discard, a noise lookup, and one animated threshold. By the end of this episode you have a shader you can drop on any mesh to make it dissolve, burn, or materialize into existence.

It's also the cleanest example of a pattern you'll use forever: sample noise, compare to a threshold, do something. Once you have it, "burn paper," "rust spreading," "ink soaking," "ice cracking" are all variations on the same five lines.

0 fps
IcosahedronGeometry · discard
Layer 1: base mesh, no dissolve. The starting point.

The five layers

  • Base color — solid sphere, sanity check.
  • Per-pixel noise sample — hash on the floor of world position. Each chunk of the surface has a stable random value.
  • Threshold discard — if (n < uCutoff) discard;. Pixels below the cutoff are skipped. Static cutoff = static holes.
  • Animated threshold — drive uCutoff with uTime. Now the dissolve sweeps.
  • Edge glow — pixels close to the threshold (within uEdgeWidth) render as hot orange. The detail that sells "burning," not "disappearing."

Layer 1 — Base sphere

The mesh we're about to destroy. Single tint, no math.

fragmentShader: /*glsl*/`
  uniform vec3 uBaseColor;
  void main() {
    gl_FragColor = vec4(uBaseColor, 1.0);
  }
`

Boring on purpose. Sanity-check the geometry, lighting, camera before adding the effect.

Layer 2 — Sample noise per pixel

The dissolve pattern needs to be stable on the mesh — the same point on the surface should always get the same noise value, regardless of camera angle. World position is the right input. Local position works too if your mesh doesn't transform.

varying vec3 vWorldPos;

float hash3(vec3 p) {
  return fract(sin(dot(p, vec3(127.1, 311.7, 74.7))) * 43758.5453);
}

void main() {
  float n = hash3(floor(vWorldPos * uNoiseScale));
  gl_FragColor = vec4(vec3(n), 1.0);          // visualize
}

Two details that matter:

  • floor(vWorldPos * uNoiseScale) snaps to a grid before hashing. This makes the noise CHUNKY instead of pixel-fine. Try uNoiseScale = 20.0 for medium chunks. Too small and the dissolve looks like fog; too large and it looks like pixel art.
  • Per-fragment noise vs per-vertex. Computing in the fragment shader is correct here — we want every pixel to make an independent stay/discard decision. Per-vertex would interpolate and give soft, blurry holes.

If you prefer smoother holes, swap hash3 for the value noise from Episode 05 (drop the floor()). The hash version gives the "shattered" look most people want for a dissolve.

Layer 3 — The discard cutoff

uniform float uCutoff;          // 0..1

void main() {
  float n = hash3(floor(vWorldPos * uNoiseScale));
  if (n < uCutoff) discard;
  gl_FragColor = vec4(uBaseColor, 1.0);
}

discard is a GLSL keyword that throws away the fragment entirely. No color is written. No depth is written. The pixel is simply absent.

With uCutoff = 0.5, every pixel with n < 0.5 is skipped — about half the surface. The result: a half-eaten mesh, in a stable pattern. Still no animation.

Power-user note: discard is a GPU branching operation. On mobile especially, it can hurt performance because the GPU can't early-out a tile if any fragment in it discards. For static dissolves on tight performance budgets, prefer rendering an alpha mask and blending. For one-shot dissolves, discard is fine.

Layer 4 — Animated threshold sweep

The dissolve effect only sells if it ANIMATES. We drive uCutoff from JavaScript:

material.uniforms.uCutoff.value = 0.5 + 0.5 * Math.sin(t * 0.4);
// or for a one-shot:
material.uniforms.uCutoff.value = Math.min(1.0, t * 0.2);

The sine version oscillates between fully solid and fully dissolved — looks like the mesh is "phasing." The one-shot version does it once over a few seconds — looks like a "snap." Both useful in different contexts.

For the demo we use the sine version so the reader can see the dissolve continuously cycle.

Layer 5 — Edge glow (the finishing move)

The single detail that turns "vanishing" into "burning." Pixels that are close to the threshold render as hot orange instead of the base color.

uniform float uCutoff;
uniform float uEdgeWidth;        // e.g., 0.05

void main() {
  float n = hash3(floor(vWorldPos * uNoiseScale));

  // Pixels that are well below the threshold: skip.
  if (n < uCutoff - uEdgeWidth) discard;

  // Pixels close to the threshold: interpolate from edge color to base color.
  float edge = smoothstep(uCutoff - uEdgeWidth, uCutoff, n);
  vec3 col = mix(vec3(1.0, 0.4, 0.0), uBaseColor, edge);

  gl_FragColor = vec4(col, 1.0);
}

Read the logic top to bottom:

  • n < uCutoff - uEdgeWidth: well below threshold → discard.
  • uCutoff - uEdgeWidth <= n < uCutoff: edge zone → smoothstep gives a value from 0 (edge color) to 1 (base color). Mixed.
  • n >= uCutoff: above threshold → full base color (edge = 1).

The hot edge color matters. vec3(1.0, 0.4, 0.0) is orange — burning paper. Switch to vec3(0.2, 1.0, 0.8) for "materializing into cyan light." Different feel, same five lines.

Putting it together

// FINAL FRAGMENT SHADER
varying vec3 vWorldPos;
uniform float uTime;
uniform float uCutoff;
uniform float uNoiseScale;
uniform float uEdgeWidth;
uniform vec3  uBaseColor;
uniform vec3  uEdgeColor;

float hash3(vec3 p) {
  return fract(sin(dot(p, vec3(127.1, 311.7, 74.7))) * 43758.5453);
}

void main() {
  float n = hash3(floor(vWorldPos * uNoiseScale));
  if (n < uCutoff - uEdgeWidth) discard;
  float edge = smoothstep(uCutoff - uEdgeWidth, uCutoff, n);
  vec3 col = mix(uEdgeColor, uBaseColor, edge);
  gl_FragColor = vec4(col, 1.0);
}

Set material.side = THREE.DoubleSide so the inside of the partially-dissolved mesh is visible (otherwise you see through the holes into the background, which sometimes works, sometimes looks like a bug).

Common pitfalls

  • Used position instead of vWorldPos. The dissolve pattern rotates with the mesh. Looks weird. Pass world position from the vertex shader and use that.
  • Noise scale mistuned. Too small → fog. Too large → giant pixel art. 20.0 is a good starting point for a unit sphere.
  • Edge color too desaturated. Looks like noise, not fire. Push to pure orange/red. Or pure cyan for "materializing."
  • Forgot side: DoubleSide. Holes show empty space instead of the inner surface. Usually you want both sides rendered.
  • Confused threshold direction. If you want the mesh to dissolve FROM solid TO gone, uCutoff increases from 0 to 1. Reverse if you want materialize.

Exercises

  1. Try a cyan edge color (vec3(0.2, 1.0, 0.9)) instead of orange. The effect now reads as "materializing into a force field" instead of "burning."
  2. Replace the hash with value noise from Episode 05. Compare. Hash → shattered look. Value noise → goopy, lava-like dissolve.
  3. Add a small vertex displacement during the dissolve. In the vertex shader: p += normal * (1.0 - uCutoff) * 0.1 * hash(position);. The mesh swells as it dissolves. Looks like it's blowing apart.

What's next

Episode 11 — Water Surface. Gerstner waves, depth-based color, fake sky reflection, foam at the peaks. Vertex shader heavy.