Medium MeshAnimated

Marching cubes blobs

Shade a metaball mesh built by the marching-cubes algorithm: iridescent soap-bubble colors.

The target picture for the shader challenge "Marching cubes blobs"
Recreate this picture. You pass at a 99% match, then try to write it in fewer characters than the par.

About the Marching cubes blobs shader

Shade a metaball mesh built by the marching-cubes algorithm: iridescent soap-bubble colors. Vertex and fragment shaders on real 3D meshes: moving vertices, lighting, toon shading, holograms and dissolves, as used in Unity, Unreal and Godot.

How this challenge works

You write a fragment shader that draws the picture above, live in your browser. A match bar shows how close you are, and the score combines accuracy, code size (the par is 251 characters in Unity (Built-in)) and GPU speed. Work in HLSL, GLSL, WGSL, Metal or Godot and export the result when you are done.

What it is built from

About the mesh
Three.js MarchingCubes builds triangles from ? moving balls (Sebastian Lague has a great video on how the algorithm works). You only write the shading.
t
dot(i_normal, i_viewDir)
Color
0.5 + 0.5 * cos(? * (t + float3(0.0, 0.33, 0.67)))
Light
normalize(float3(?, ?, 0.5)) · i_normal, brightness ? + ? * diffuse

The exact numbers are left out (shown as ?), but every colour is given. Measure the rest with the pixel readout on both pictures and Diff.

How to make a marching cubes blobs shader

  1. Mesh normals come from the marching-cubes algorithm itself: smooth and ready to use.
  2. Cosine palette indexed by the view angle gives the iridescent bands.

Techniques used

  • A cosine palette: a smooth, repeating color gradient from one formula.
  • Diffuse lighting: brightness from the angle between the surface and the light.

Marching cubes blobs shader code, explained step by step

Try to build it yourself first, because that is how it sticks. When you want to check your work or you are stuck, this is a complete working solution cut into parts, with the idea behind each part. The parts are in HLSL (Unity and Unreal); the full code below comes in every language.

Part 1: Choose the colours

Inside float4 frag(float2 uv)

float t = dot(i_normal, i_viewDir);
float3 col = 0.5 + 0.5 * cos(6.28318 * (t + float3(0.0, 0.33, 0.67)));
float diff = max(dot(i_normal, normalize(float3(0.6, 0.8, 0.5))), 0.0);
  • The dot product of the surface normal and the light direction is the diffuse light: bright where the surface faces the light, dark where it faces away.
  • normalize() scales a direction to length 1, so only its direction matters.
  • A mesh shader receives the position, surface direction and view direction of each pixel (i_worldPos, i_normal, i_viewDir).
  • float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).

Part 2: Colour the pixel and return it

Still inside frag()

return float4(col * (0.4 + 0.6 * diff), 1.0);
  • The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).

The full code

HLSL Unity, Unreal
float4 frag(float2 uv)
{
    float t = dot(i_normal, i_viewDir);
    float3 col = 0.5 + 0.5 * cos(6.28318 * (t + float3(0.0, 0.33, 0.67)));
    float diff = max(dot(i_normal, normalize(float3(0.6, 0.8, 0.5))), 0.0);
    return float4(col * (0.4 + 0.6 * diff), 1.0);
}
GLSL three.js, Shadertoy, OpenGL
vec4 frag(vec2 uv)
{
    float t = dot(i_normal, i_viewDir);
    vec3 col = 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67)));
    float diff = max(dot(i_normal, normalize(vec3(0.6, 0.8, 0.5))), 0.0);
    return vec4(col * (0.4 + 0.6 * diff), 1.0);
}

Open it in the editor and change it

Solve it in your language

Unity (Built-in), Unity (URP), Unreal, Shadertoy / WebGL, OpenGL (LWJGL, raylib), three.js, GameMaker, LÖVE (Lua), Godot, Apple (iOS, macOS), WebGPU / Bevy.

Start coding

Questions about this shader

How do I make a marching cubes blobs shader?

Shade a metaball mesh built by the marching-cubes algorithm: iridescent soap-bubble colors. A good first step: mesh normals come from the marching-cubes algorithm itself: smooth and ready to use. In ShaderBattle you build it in the browser with a live match bar, so you see how close you are after every change.

What techniques does the marching cubes blobs shader use?

It is built from cosine palette, diffuse lighting. Each one is a small idea you can reuse in other effects.

Does this marching cubes blobs shader work in Unity, Unreal, Godot and Shadertoy?

Yes. The idea is the same in every shader language. ShaderBattle shows the code in HLSL (Unity, Unreal), GLSL (Shadertoy, three.js, OpenGL), WGSL (WebGPU), Metal and Godot, and exports a ready-to-paste version.