TExcelTAD
Home › Shaders & Graphics Programming › One Dissolve, Three Engines

One Dissolve, Three Engines

The Dissolve lesson built the effect as a live shader. This lesson ports it to Unity (URP HLSL and Shader Graph), Unreal (Material Editor plus a Custom node) and Godot. Seeing one effect three ways shows what is the same everywhere (the math) and what differs (where the inputs come from and how a pixel is thrown away).

The math, once

A dissolve needs three things:

  1. A noise value n in 0..1 per pixel, usually from a tiling noise texture.
  2. A threshold amount in 0..1 that animates from 0 (fully visible) to 1 (gone). Pixels with n < amount are discarded.
  3. A thin glowing edge where n is just above amount: edge = 1 - smoothstep(amount, amount + width, n).

Here is that math as a pure function you can check by hand. It prints what happens to five pixels with different noise values as the threshold rises:

js · try it yourself
function smoothstep(e0, e1, x) {
  const t = Math.min(Math.max((x - e0) / (e1 - e0), 0), 1);
  return t * t * (3 - 2 * t);
}

function dissolvePixel(n, amount, width) {
  if (n < amount) return 'cut ';
  const edge = 1 - smoothstep(amount, amount + width, n);
  return edge > 0.01 ? `e${edge.toFixed(1)}` : 'body';
}

const noise = [0.05, 0.30, 0.42, 0.55, 0.90];
for (const amount of [0.0, 0.25, 0.4, 0.5, 0.95]) {
  const row = noise.map((n) => dissolvePixel(n, amount, 0.1)).join(' ');
  console.log(`amount ${amount.toFixed(2)}: ${row}`);
}
amount 0.00: e0.5 body body body body
amount 0.25: cut  e0.5 body body body
amount 0.40: cut  cut  e0.9 body body
amount 0.50: cut  cut  cut  e0.5 body
amount 0.95: cut  cut  cut  cut  cut

Each row moves the cut one pixel further, and the pixel right above the threshold glows. Notice the first row: at amount = 0 the darkest-noise pixel already glows (e0.5). Real shaders hide that by fading the edge in, for example multiplying it by step(0.001, amount). The live version uses the same structure, with procedural value noise standing in for the texture:

glsl · try it yourself
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }

float valueNoise(vec2 p) {
    vec2 i = floor(p), f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    return mix(mix(hash(i), hash(i + vec2(1, 0)), u.x),
               mix(hash(i + vec2(0, 1)), hash(i + vec2(1, 1)), u.x), u.y);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.y;
    float n = 0.6 * valueNoise(uv * 6.0) + 0.4 * valueNoise(uv * 13.0);   // the "noise texture"
    float amount = 0.5 + 0.5 * sin(iTime * 0.8);                          // animated threshold
    float width = 0.06;

    vec3 background = vec3(0.08, 0.09, 0.12);
    vec3 surface = mix(vec3(0.25, 0.45, 0.85), vec3(0.85, 0.9, 1.0), uv.y / 1.2);
    vec3 edgeColor = vec3(4.0, 1.6, 0.4);                                 // > 1: would bloom (HDR)

    if (n < amount) { fragColor = vec4(background, 1.0); return; }        // the "discard"
    float edge = (1.0 - smoothstep(amount, amount + width, n)) * step(0.001, amount);
    vec3 color = surface + edgeColor * edge;
    fragColor = vec4(min(color, vec3(1.0)), 1.0);
}

On a real mesh, a discarded pixel shows whatever is behind it, and it must also be removed from the shadow and depth passes, or the dissolved part still casts a shadow. That last point is where the engines differ most.

Unity URP (HLSL)

clip(x) discards the pixel when x < 0, so clip(n - _Amount) is the whole cut-out.

hlsl
Shader "Course/Dissolve"
{
    Properties
    {
        [MainColor] _BaseColor ("Base Color", Color) = (0.25, 0.45, 0.85, 1)
        _NoiseTex ("Noise", 2D) = "white" {}
        _Amount ("Amount", Range(0, 1)) = 0
        _EdgeWidth ("Edge Width", Range(0, 0.2)) = 0.05
        [HDR] _EdgeColor ("Edge Color", Color) = (4, 1.6, 0.4, 1)
    }

    SubShader
    {
        Tags { "RenderType" = "TransparentCutout" "Queue" = "AlphaTest" "RenderPipeline" = "UniversalPipeline" }

        Pass
        {
            Tags { "LightMode" = "UniversalForward" }
            Cull Off                                    // see the inside through the holes

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Attributes { float4 positionOS : POSITION; float2 uv : TEXCOORD0; };
            struct Varyings   { float4 positionHCS : SV_POSITION; float2 uv : TEXCOORD0; };

            TEXTURE2D(_NoiseTex);
            SAMPLER(sampler_NoiseTex);

            CBUFFER_START(UnityPerMaterial)
                float4 _NoiseTex_ST;
                half4  _BaseColor;
                half   _Amount;
                half   _EdgeWidth;
                half4  _EdgeColor;
            CBUFFER_END

            Varyings vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.uv = TRANSFORM_TEX(IN.uv, _NoiseTex);
                return OUT;
            }

            half4 frag(Varyings IN) : SV_Target
            {
                half n = SAMPLE_TEXTURE2D(_NoiseTex, sampler_NoiseTex, IN.uv).r;
                clip(n - _Amount);                                   // discard below the threshold
                half edge = (1.0 - smoothstep(_Amount, _Amount + _EdgeWidth, n)) * step(0.001, _Amount);
                return half4(_BaseColor.rgb + _EdgeColor.rgb * edge, 1.0);
            }
            ENDHLSL
        }
    }
}

Animate it from C#: material.SetFloat("_Amount", t); (better: cache Shader.PropertyToID("_Amount")). With [HDR] on the edge color and Bloom in a URP Volume, the edge glows.

For shadows and depth to match, the shader needs ShadowCaster and DepthOnly passes that sample the noise and call clip too. That is the boring part of hand-written URP shaders, and the strongest reason to use Shader Graph for this effect:

  1. Create a URP > Lit Shader Graph. In the Graph Inspector's Graph Settings, enable Alpha Clipping.
  2. Add Blackboard properties Amount (Float, slider 0–1), EdgeWidth (Float), EdgeColor (Color, mode HDR) and NoiseTex (Texture2D), or use a Simple Noise node instead of a texture.
  3. Wire the noise into Alpha and Amount into Alpha Clip Threshold. Shader Graph discards where Alpha < threshold, in every pass, shadows included.
  4. Build the edge: Add (Amount + EdgeWidth) → Smoothstep (Edge1 = Amount, Edge2 = that sum, In = noise) → One Minus → Multiply by EdgeColor → Emission.

Unreal Engine 5

  1. Create a material, set Blend Mode to Masked (and tick Two Sided if you want to see inside).
  2. Add a Texture Sample with a noise texture (or the Noise node, which is more expensive) and a Scalar Parameter Amount.
  3. Subtract Amount from the noise and wire the result into Opacity Mask. Pixels whose mask is below the Opacity Mask Clip Value (a material setting, 0.3333 by default) are discarded, so set that value to 0 for a clean n - Amount < 0 test, or keep it and add an offset.
  4. Build the edge in a Custom node (Output Type CMOT Float 3; inputs Noise, Amount, Width, EdgeColor) and wire it into Emissive Color:
hlsl
float edge = 1.0 - smoothstep(Amount, Amount + Width, Noise);
edge *= step(0.001, Amount);
return EdgeColor * edge;
  1. Create a Material Instance; animate Amount from Blueprint or C++ with a Material Instance Dynamic (SetScalarParameterValue(TEXT("Amount"), Value)), or from a Timeline.

Masked materials are respected by Unreal's shadow and depth passes automatically. With Nanite meshes, masked materials are supported in UE 5.1 and later, but they cost more than opaque ones; check the current Nanite docs for your version.

Godot 4

Two options: an explicit discard, or Godot's alpha scissor, which gives the same cut-out and keeps shadows consistent.

gdshader
shader_type spatial;
render_mode cull_disabled;

uniform vec4 base_color : source_color = vec4(0.25, 0.45, 0.85, 1.0);
uniform sampler2D noise_tex : hint_default_white;
uniform float amount : hint_range(0.0, 1.0) = 0.0;
uniform float edge_width : hint_range(0.0, 0.2) = 0.05;
uniform vec4 edge_color : source_color = vec4(1.0, 0.4, 0.1, 1.0);
uniform float edge_energy = 4.0;

void fragment() {
    float n = texture(noise_tex, UV).r;
    ALBEDO = base_color.rgb;
    ALPHA = n;
    ALPHA_SCISSOR_THRESHOLD = amount;          // pixels with ALPHA < amount are discarded

    float edge = (1.0 - smoothstep(amount, amount + edge_width, n)) * step(0.001, amount);
    EMISSION = edge_color.rgb * edge * edge_energy;
}

For noise_tex, create a NoiseTexture2D resource in the Inspector and give it a FastNoiseLite noise. Animate from GDScript with material.set_shader_parameter("amount", value) or a Tween. With Glow enabled in the WorldEnvironment, EMISSION blooms.

Side by side

Unity URP Unreal 5 Godot 4
Noise SAMPLE_TEXTURE2D or Simple Noise node Texture Sample or Noise node texture(noise_tex, UV), NoiseTexture2D
Cut-out clip(n - _Amount) / Alpha Clipping Blend Mode Masked + Opacity Mask ALPHA_SCISSOR_THRESHOLD or discard
Glow HDR color → Emission, Bloom volume Emissive Color, Bloom post-process EMISSION, Glow in WorldEnvironment
Shadows match needs ShadowCaster pass (automatic in Shader Graph) automatic automatic
Animate SetFloat on the material Material Instance Dynamic set_shader_parameter

The step(0.001, amount) trick, the HDR edge and the "shadows must clip too" rule are the same in every engine. That is the pattern of this whole section: learn the effect once in plain GLSL, then learn each engine's plumbing.

Test yourself

In HLSL, what does clip(n - _Amount) do?

  1. Clamps n to _Amount
  2. Discards the pixel when n - _Amount is negative
  3. Sets alpha to n - _Amount
  4. Clips the vertex to the screen

A dissolving character's holes are visible, but its shadow is still whole. What is the likely cause in a hand-written URP shader?

  1. Bloom is off
  2. The ShadowCaster pass doesn't apply the same clip
  3. The noise texture is sRGB
  4. The Queue tag is wrong

In Godot, which built-in gives an alpha-tested cut-out without writing discard?

  1. ALPHA_SCISSOR_THRESHOLD
  2. RIM
  3. NORMAL_MAP
  4. LIGHT_VERTEX