Mesh Gradient

Animated mesh gradient with distortion

Initializing WebGPU...

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.

  • deviceGPUDevice
  • options.format — Output texture format. Default 'rgba8unorm'.

Returns { render, destroy }.

render(target, params?)

Renders the gradient into a GPUTexture.

  • target — The GPUTexture to 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, default 0.15.
  • params.swirl — Vortex swirl strength. 0–1, default 0.

How to Modify

  • Change the number of color spots: The shader supports up to 10 via the MAX_COLORS constant. To increase, change the uniform buffer size and the array<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

How It Works

The shader places N color spots at animated positions (sinusoidal paths). For each pixel:

  1. Compute the inverse-distance weight to every color spot (1 / dist^3.5).
  2. Blend all colors by their weights (weighted average).
  3. Optionally apply organic distortion (sinusoidal UV warping) and vortex swirl (rotational warp that increases toward edges).
  4. 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