Hard Coding adventuresAnimated
Fluid particles: the density field
How a particle fluid simulation sees the world: every particle spreads its mass over a small radius (a smoothing kernel), and the density at a point is the sum of nearby particles. Draw that density as a blue glow.
About the Fluid particles: the density field shader
How a particle fluid simulation sees the world: every particle spreads its mass over a small radius (a smoothing kernel), and the density at a point is the sum of nearby particles. Draw that density as a blue glow. Experiments in the spirit of well-known open-source graphics projects: a software rasterizer, a ray tracer, procedural planets, fluid particles and more, rebuilt as pixel shaders.
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 675 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
- Grid
uses floor- Particle
uses pos, sin, hash, cos- Kernel
a plain value or condition- Density
uses pixel- Colors
uses lerp, saturate · colours float3(0.02, 0.05, 0.15), float3(0.1, 0.5, 0.9), float3(0.95, 0.98, 1.0)
Only the building blocks and the colours are listed. Work out how they fit together, then measure the numbers from the target.
How to make a fluid particles: the density field shader
- Only the 9 nearest cells can contain a particle that reaches this pixel, so two small nested loops from -1 to 1 are enough.
- Compute each neighbour particle position with the same formula, then the distance from g to it.
- A kernel that falls smoothly to zero at the radius (here squared) means density changes smoothly as particles move.
Techniques used
- A hash function: a cheap way to get a repeatable random number from a position.
- Sine waves: smooth oscillation for waves, pulses and motion over time.
- lerp (mix): blend between two colors or values.
Fluid particles: the density field 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: Helper: hash()
float hash(float2 p)
{
return frac(sin(dot(p, float2(127.1, 311.7))) * 43758.5453);
} - A hash function. Shaders have no random(), so this turns a position into a pseudo-random number between 0 and 1. The same input always gives the same output, which is what makes noise repeatable.
Part 2: Set up the coordinates
Inside float4 frag(float2 uv)
float2 g = uv * float2(7.0, 5.25);
float2 cell = floor(g);
float dens = 0.0; - floor() picks the cell a point is in and frac() is the position inside that cell. Together they repeat a pattern.
Part 3: The main loop
Still inside frag()
for (int j = -1; j <= 1; j++)
{
for (int i = -1; i <= 1; i++)
{
float2 c = cell + float2(float(i), float(j));
float2 pos = c + 0.5 + 0.35 * float2(sin(time * 0.8 + 6.2832 * hash(c + 1.0)), cos(time * 0.6 + 6.2832 * hash(c + 5.0)));
float d = length(g - pos);
float w = max(1.0 - d / 1.1, 0.0);
dens += w * w;
}
} - The loop runs 3 times. Each pass is one step of the repeated work; the browser caps loops, so keep the count modest.
- A hash turns a position into a repeatable pseudo-random number, so every cell can get its own random value.
- Putting time inside sin or cos makes the value swing back and forth, which is what animates the effect.
- time is the number of seconds since the shader started, so anything that uses it moves.
Part 4: Colour the pixel and return it
Still inside frag()
float3 col = lerp(float3(0.02, 0.05, 0.15), float3(0.1, 0.5, 0.9), saturate(dens));
col = lerp(col, float3(0.95, 0.98, 1.0), saturate(dens - 1.0));
return float4(col, 1.0); - lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
- saturate() clamps a value into the 0 to 1 range.
- float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).
- The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).
The full code
HLSL Unity, Unreal
float hash(float2 p)
{
return frac(sin(dot(p, float2(127.1, 311.7))) * 43758.5453);
}
float4 frag(float2 uv)
{
float2 g = uv * float2(7.0, 5.25);
float2 cell = floor(g);
float dens = 0.0;
for (int j = -1; j <= 1; j++)
{
for (int i = -1; i <= 1; i++)
{
float2 c = cell + float2(float(i), float(j));
float2 pos = c + 0.5 + 0.35 * float2(sin(time * 0.8 + 6.2832 * hash(c + 1.0)), cos(time * 0.6 + 6.2832 * hash(c + 5.0)));
float d = length(g - pos);
float w = max(1.0 - d / 1.1, 0.0);
dens += w * w;
}
}
float3 col = lerp(float3(0.02, 0.05, 0.15), float3(0.1, 0.5, 0.9), saturate(dens));
col = lerp(col, float3(0.95, 0.98, 1.0), saturate(dens - 1.0));
return float4(col, 1.0);
} GLSL Shadertoy, three.js, OpenGL
float hash(vec2 p)
{
return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}
vec4 frag(vec2 uv)
{
vec2 g = uv * vec2(7.0, 5.25);
vec2 cell = floor(g);
float dens = 0.0;
for (int j = -1; j <= 1; j++)
{
for (int i = -1; i <= 1; i++)
{
vec2 c = cell + vec2(float(i), float(j));
vec2 pos = c + 0.5 + 0.35 * vec2(sin(time * 0.8 + 6.2832 * hash(c + 1.0)), cos(time * 0.6 + 6.2832 * hash(c + 5.0)));
float d = length(g - pos);
float w = max(1.0 - d / 1.1, 0.0);
dens += w * w;
}
}
vec3 col = mix(vec3(0.02, 0.05, 0.15), vec3(0.1, 0.5, 0.9), clamp(dens, 0.0, 1.0));
col = mix(col, vec3(0.95, 0.98, 1.0), clamp(dens - 1.0, 0.0, 1.0));
return vec4(col, 1.0);
} WGSL WebGPU, Bevy
fn hash(p: vec2f) -> f32
{
return fract(sin(dot(p, vec2f(127.1, 311.7))) * 43758.5453);
}
fn frag(uv: vec2f) -> vec4f
{
let g = uv * vec2f(7.0, 5.25);
let cell = floor(g);
var dens = 0.0;
for (var j = -1; j <= 1; j++)
{
for (var i = -1; i <= 1; i++)
{
let c = cell + vec2f(f32(i), f32(j));
let pos = c + 0.5 + 0.35 * vec2f(sin(time * 0.8 + 6.2832 * hash(c + 1.0)), cos(time * 0.6 + 6.2832 * hash(c + 5.0)));
let d = length(g - pos);
let w = max(1.0 - d / 1.1, 0.0);
dens += w * w;
}
}
var col = mix(vec3f(0.02, 0.05, 0.15), vec3f(0.1, 0.5, 0.9), saturate(dens));
col = mix(col, vec3f(0.95, 0.98, 1.0), saturate(dens - 1.0));
return vec4f(col, 1.0);
} Godot Godot 4 shading language
shader_type canvas_item;
float hash(vec2 p)
{
return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}
vec4 frag(vec2 uv)
{
vec2 g = uv * vec2(7.0, 5.25);
vec2 cell = floor(g);
float dens = 0.0;
for (int j = -1; j <= 1; j++)
{
for (int i = -1; i <= 1; i++)
{
vec2 c = cell + vec2(float(i), float(j));
vec2 pos = c + 0.5 + 0.35 * vec2(sin(TIME * 0.8 + 6.2832 * hash(c + 1.0)), cos(TIME * 0.6 + 6.2832 * hash(c + 5.0)));
float d = length(g - pos);
float w = max(1.0 - d / 1.1, 0.0);
dens += w * w;
}
}
vec3 col = mix(vec3(0.02, 0.05, 0.15), vec3(0.1, 0.5, 0.9), clamp(dens, 0.0, 1.0));
col = mix(col, vec3(0.95, 0.98, 1.0), clamp(dens - 1.0, 0.0, 1.0));
return vec4(col, 1.0);
}
void fragment()
{
// Godot's UV starts at the top-left, the lessons use bottom-left
COLOR = frag(vec2(UV.x, 1.0 - UV.y));
} Metal iOS, macOS
float hash(float2 p)
{
return fract(sin(dot(p, float2(127.1, 311.7))) * 43758.5453);
}
float4 frag(float2 uv)
{
float2 g = uv * float2(7.0, 5.25);
float2 cell = floor(g);
float dens = 0.0;
for (int j = -1; j <= 1; j++)
{
for (int i = -1; i <= 1; i++)
{
float2 c = cell + float2(float(i), float(j));
float2 pos = c + 0.5 + 0.35 * float2(sin(time * 0.8 + 6.2832 * hash(c + 1.0)), cos(time * 0.6 + 6.2832 * hash(c + 5.0)));
float d = length(g - pos);
float w = max(1.0 - d / 1.1, 0.0);
dens += w * w;
}
}
float3 col = mix(float3(0.02, 0.05, 0.15), float3(0.1, 0.5, 0.9), saturate(dens));
col = mix(col, float3(0.95, 0.98, 1.0), saturate(dens - 1.0));
return float4(col, 1.0);
} Helper functions provided
The toolbox has: hash(). Add them above your frag function with one click.
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 codingQuestions about this shader
How do I make a fluid particles: the density field shader?
How a particle fluid simulation sees the world: every particle spreads its mass over a small radius (a smoothing kernel), and the density at a point is the sum of nearby particles. A good first step: only the 9 nearest cells can contain a particle that reaches this pixel, so two small nested loops from -1 to 1 are enough. 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 fluid particles: the density field shader use?
It is built from hash function, sine waves, lerp (mix). Each one is a small idea you can reuse in other effects.
Does this fluid particles: the density field 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.