Mesh Gradient
Animated mesh gradient with distortion
Quick Start
Loading...
Source
Loading...
Documentation
mesh-gradient
Animated mesh gradient with organic distortion and swirl. Renders up to 10 color spots that drift and blend, producing the flowing gradient effect seen on modern landing pages.
Quick Start
import { createMeshGradient } from './gpu-modules/mesh-gradient/mesh-gradient';
const gradient = createMeshGradient(device, { format: 'rgba8unorm' });
// Each frame
gradient.render(targetTexture, {
time: elapsed,
colors: [
[0.9, 0.2, 0.3, 1],
[0.2, 0.5, 0.9, 1],
[0.1, 0.8, 0.4, 1]
],
distortion: 0.15
});
gradient.destroy();
API
createMeshGradient(device, options?)
Creates a mesh gradient renderer.
device—GPUDeviceoptions.format— Output texture format. Default'rgba8unorm'.
Returns { render, destroy }.
render(target, params?)
Renders the gradient into a GPUTexture.
target— TheGPUTextureto render into.params.time— Animation time in seconds.params.colors— Array of[r, g, b, a]colors (0–1). Up to 10.params.distortion— Organic wave distortion strength. 0–1, default0.15.params.swirl— Vortex swirl strength. 0–1, default0.
How to Modify
- Change the number of color spots: The shader supports up to 10 via the
MAX_COLORSconstant. To increase, change the uniform buffer size and thearray<vec4<f32>, N>in the shader. - Adjust color spot movement: Edit
get_position()in the shader. The default uses sinusoidal paths — change the frequencies or add noise for more organic motion. - Control blending sharpness: The
pow(dist, 3.5)exponent in the fragment shader controls how sharply colors separate. Higher values = harder edges between color regions. - Use as a background: Render to a texture and sample it in your scene, or render directly to the canvas.
Further Reading
Rationale
Mesh gradients (also called "aurora gradients" or "blob gradients") are everywhere in modern design — Apple, Linear, Stripe, Vercel. The effect is simple to describe (blended color spots that drift organically) but non-trivial to implement well on the GPU with smooth blending, distortion, and grain.
This module gives you a single-file implementation you can drop into any WebGPU project. Useful for backgrounds, hero sections, loading screens, or generative art.
Original Source
- paper-design/shaders — The shader this module is ported from, released under MIT license. https://github.com/paper-design/shaders
How It Works
The shader places N color spots at animated positions (sinusoidal paths). For each pixel:
- Compute the inverse-distance weight to every color spot (
1 / dist^3.5). - Blend all colors by their weights (weighted average).
- Optionally apply organic distortion (sinusoidal UV warping) and vortex swirl (rotational warp that increases toward edges).
- Optionally overlay film grain using value noise.
The inverse-distance weighting is the same principle used in Shepard's interpolation (IDW), a technique from spatial statistics.
Further Learning
- Shepard's Method (Inverse Distance Weighting) — The mathematical foundation for the color blending approach. https://en.wikipedia.org/wiki/Inverse_distance_weighting
- The Book of Shaders: 2D Noise — Covers value noise and hash functions used in the grain generation. https://thebookofshaders.com/11/