Ask Rift AI
Theme: Smoke
Layout: Contained
Background: Field
Search or ask anythingAsk Rift AI
Theme: Smoke
Layout: Contained
Background: Field
Colour mode: System

Shaders

A scene of your own, in your theme’s colours

Shader scene paints a WebGL2 scene behind a page. Pick one the library ships, or write your own in GLSL and pass it the same way. Yours gets everything the library’s get: the theme’s colours, the finishing passes, light mode, reduced motion and a fallback when WebGL2 cannot run.

Three ways in

The scene prop takes an id or a scene. Bringing your own is the same line of code as picking one.

tsx
import { ShaderScene } from 'rift-ds/components/ShaderScene/ShaderScene';
import { ember } from './ember';

// One the library ships, by id.
<ShaderScene scene="horizon" />

// One of your own, the same way.
<ShaderScene scene={ember} />

Or start from a working file. The shader scene block writes a scene and a page that stages it with a slider for each setting, and both are yours to change.

bash
npx rift-ds init block shader-scene

A scene

A scene is three things: the colours it reads, its settings, and the GLSL that paints it. This is the one the block writes, a bed of light that rises in slow tongues.

ts
import type { ShaderSceneSource } from 'rift-ds/components/ShaderScene/ShaderScene';

export const ember: ShaderSceneSource = {
  mode: 'dark',
  colours: ['--color-ambient-4', '--color-ambient-5'],
  params: [
    { key: 'glow', label: 'Glow', min: 0, max: 2, step: 0.01, default: 1 },
    { key: 'height', label: 'Height', min: 0.1, max: 1.5, step: 0.01, default: 0.8 },
    { key: 'scale', label: 'Scale', min: 0.5, max: 6, step: 0.05, default: 2.4 },
  ],
  glsl: `
vec3 scene(vec2 uv, float t) {
  // Square the space up, so the noise is not stretched on a wide stage.
  float aspect = u_size.x / u_size.y;
  vec2 p = vec2((uv.x - 0.5) * aspect, uv.y);

  // How high the light reaches in this column: slow noise, drifting.
  float reach = u_height * (0.35 + 1.1 * fbm(vec2(p.x * u_scale + t * 0.05, t * 0.08)));

  // Brightest at the floor and gone at its reach, with a soft top edge.
  float body = smoothstep(reach, 0.0, uv.y);
  body *= body;

  // Finer noise rising through it lifts the second colour out as embers.
  float fine = fbm(vec2(p.x, uv.y) * u_scale * 5.0 - vec2(0.0, t * 0.35));
  float embers = smoothstep(0.5, 0.85, fine) * body;

  // Add the light to the ground: the fades and light mode depend on it.
  vec3 light = (u_col[0] * body * 1.2 + u_col[1] * embers * 0.5) * u_glow;
  return u_ground + light;
}
`,
};

Keep it in a file of its own or write it inline. The scene is compared by what it says, so an inline object compiles once and again only when it changes.

Try it

That scene, running. Each slider is one of the settings it declares.

Placing it

The scene fills its positioned ancestor and nothing else, so the ancestor decides where it sits and clips it. It is scenery: it takes no pointer events and is hidden from assistive tech, and content goes over it.

tsx
<div style={{ position: 'relative', overflow: 'hidden', height: 360 }}>
  <ShaderScene scene={ember} params={{ glow: 1.4 }} />
  <h1 style={{ position: 'relative' }}>Your content, over the scene</h1>
</div>

What your GLSL gets

Write one function, vec3 scene(vec2 uv, float t), in GLSL ES 3.00 (WebGL2). Returns the pixel's colour in linear RGB over the ground. uv runs 0 to 1 on both axes from the bottom left, with no aspect correction; t is the scene's clock in seconds. Everything in these two tables is declared before your code runs, so you call it by name and import nothing.

The uniforms every scene is handed
UniformTypeWhat it holds
u_size
vec2
The drawing buffer's size in pixels. Divide its x by its y for the aspect ratio.
u_time
float
The scene's clock in seconds, the same value as t. It advances with speed and holds still under reduced motion.
u_ground
vec3
The ground the scene paints over, in linear RGB: the page ground, or a near-black of its own for a dark scene on a light page. Add light to it and return the sum.
u_page
vec3
The page ground itself, which the fades melt into.
u_lightMode
float
1 when a dark scene is being laid onto a light page as colour and ink, otherwise 0.
u_ratio
float
Drawing-buffer pixels per CSS pixel. Multiply a size meant in CSS pixels by it.
u_col
vec3[]
The scene's colours in linear RGB, in the order its colours list names them. Unused slots are black.
u_glyphs
sampler2D
The scene's character ramp as one strip, a cell per character, white on black. Read its red channel. Only for a scene that sets glyphs.
u_glyphCount
float
How many characters the ramp holds. Only for a scene that sets glyphs.
The GLSL functions every scene can call
FunctionWhat it gives
uint ihash(uvec2 p)
An integer hash with no visible pattern, the base of the others.
float rnd(ivec2 p)
A random number from 0 to 1 for an integer cell.
float vnoise(vec2 p)
Smooth value noise from 0 to 1.
float fbm(vec2 p)
Four octaves of value noise, for cloud, smoke and water.
float lobe(float d, float above, float below)
A soft falloff around zero with one width above it and another below, for a glow with a hard side and a soft one.

Return the ground plus your light, u_ground + light, in linear colour. The fades, the grain and light mode all work from that sum.

Colours

A scene never holds a colour. It names custom properties in colours, and they arrive in the GLSL as u_col[0], u_col[1] and so on, in that order. Name the ambient roles, --color-ambient-1 onward, and the scene follows the theme, the colour mode and any preset. A custom property of your own works too, set anywhere above the scene.

Colours are read where the scene sits and read again when the theme changes on the root element. One with no value falls back to the text colour, and a development warning names it.

Settings

Each entry in params becomes uniform float u_<key>, declared for you. Do not declare it again in your GLSL. Set values through the params prop, which merges over the defaults, so passing one key changes one thing and compiles nothing. A key must be a GLSL identifier and must not repeat a name from the table above.

The label, range and step are there for a host that draws controls. For sliders, write to a ref the scene reads every frame, so a drag renders nothing:

tsx
const values = useRef<ShaderSceneValues>({
  speed: 1.6,
  scene: shaderSceneDefaults(ember.params),
  passes: Object.fromEntries(
    SHADER_PASSES.map((pass) => [pass.id, { on: pass.on, params: shaderSceneDefaults(pass.params) }])
  ),
});

<ShaderScene scene={ember} liveRef={values} />
<Slider label="Glow" min={0} max={2} step={0.01} defaultValue={1}
  onValueChange={(glow) => { values.current.scene.glow = glow; }} />

Finishing passes

The same passes run over your scene as over the library’s, each switched and tuned through the passes prop. The fades melt the scene into the page at its top and bottom edges; turn them off for a scene that fills a framed stage.

The finishing passes
PassWhat it doesBy defaultSettings
scrim
Scrim
Off
amount
vignette
Vignette
Off
amount, size
fadeTop
Fade the top
On
start, softness
fadeBottom
Fade the bottom
On
from, to
grain
Film grain
On
amount

Light and dark

Most scenes only add light, which reads on a dark ground and washes out on a light one. Say so with mode: 'dark': on a light page the scene computes its light against a near-black ground of its own, and that light is laid onto the page as a pale tint.

A scene that paints both modes itself leaves mode out and reads the ground to tell which it is on:

glsl
// 0 on a dark ground, 1 on a light one.
float day = smoothstep(0.1, 0.45, dot(u_ground, vec3(0.2126, 0.7152, 0.0722)));
vec3 water = mix(u_col[0], u_col[1], day);

Printing type

Give a scene a ramp of characters in glyphs, thin to heavy, and it is drawn into a texture in the theme’s code face and handed over as u_glyphs. Type and other fine detail want more than one buffer pixel per CSS pixel, so raise pixelRatio to 2 for those scenes and scale anything sized in CSS pixels by u_ratio. Soft light does not need it, and every extra pixel is paid for each frame.

When it does not compile

A scene that fails to compile paints nothing, reports unavailable and hands the compiler’s log to onError. Line numbers count from the first line of your own glsl, written 1:line. The log reaches the console as well.

tsx
const [status, setStatus] = useState<ShaderSceneStatus>('pending');
const [log, setLog] = useState<string>();

<ShaderScene scene={ember} onStatusChange={setStatus} onError={setLog} />

// A mistake on the scene's second line arrives marked 1:2,
// string 1 (your GLSL), line 2.

In development, the mistakes a compiler does not explain are warned about before it runs: a key that is not an identifier, a uniform declared twice, too many colours and a colour with no value.

Keeping it cheap

The component holds every scene to the same budget: thirty frames a second, one buffer pixel per CSS pixel unless the scene asks for more, and a loop that stops when the scene scrolls out of view. Under reduced motion it holds a still frame that still answers its settings. What is left to you is the cost of one pixel, so count your noise calls: fbm is four lookups, and a scene that calls it a dozen times fills a wide band slowly on a laptop.

Where WebGL2 cannot run, the scene stays empty and whatever sits under it shows, so lay a fallback beneath it or drive one from onStatusChange.

With an agent

npx rift-ds init installs a skill that teaches a coding agent this page: the contract, the colour roles and the checks to run. Ask it for a scene by describing the picture. See Get started for the skills and the MCP endpoint.