Repository navigation
feat(shaders): add LiquidGradient #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hunterbecton
merged 6 commits into
main
from
hunter/sha-223-build-the-liquidgradient-source
Oct 12, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1c7a8d0
docs: add UV warp to the glossary
hunterbecton fa89ece
feat(shaders): add SwirlGradient's bands folded along a still flow
hunterbecton d0ba48e
feat(shaders): drift SwirlGradient's flow with spinning noise arrows
hunterbecton b3bcec7
feat(shaders): turn SwirlGradient into a streaked liquid gradient
hunterbecton 42701f6
feat(shaders): rename SwirlGradient to LiquidGradient and bake its de…
hunterbecton 79aca4f
test(shaders): count LiquidGradient's pre-pass draws, and add its cha…
hunterbecton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@camp-dev/shaders': minor | ||
| --- | ||
|
|
||
| Add `LiquidGradient`, a Source that streaks bands of color along slow, curling currents, like paint stirred into liquid. Mount it inside a `ShaderScene`. `stops`, `colorSpace`, and `hueInterpolation` pick the colors, as they do on `LinearGradient`. `angle` and `repeat` lay the bands out before the current moves them, and `softness` sets how the colors meet, from hard bands at 0 to full blends at 1. `swirl` sets how far the current carries the colors, `scale` sets roughly how many eddies span the canvas's height, and `speed` sets how fast the current drifts. Each of those six props takes a static value or an animation signal. `seed` picks a different layout with the same look. A literal `speed={0}` holds the current still and lets the scene park. It works in Mode 1 only. | ||
|
|
||
| `LiquidGradient` draws the current, a noise field, and its palette into textures before the scene pass, and redraws each texture only when a prop it depends on changes. Where the renderer cannot draw into a half-float target, on WebGL2 without `EXT_color_buffer_float`, every read works its value out from the noise or the palette instead. The result looks the same and costs more GPU time. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { expect, test } from './fixtures'; | ||
| import { waitForShader } from './helpers'; | ||
|
|
||
| test('LiquidGradient — default story', async ({ page }) => { | ||
| await page.goto('/components/liquid-gradient?visualTest=1'); | ||
| await waitForShader(page); | ||
| await expect(page.locator('canvas').first()).toHaveScreenshot('liquid-gradient-default.png'); | ||
| }); |
Binary file added
BIN
+159 KB
...l/liquid-gradient.spec.ts-snapshots/liquid-gradient-default-chromium-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+159 KB
...al/liquid-gradient.spec.ts-snapshots/liquid-gradient-default-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| 'use client'; | ||
|
|
||
| /** | ||
| * LiquidGradient demo island: the interactive slice of the LiquidGradient | ||
| * page — control store, shader preview, and control panel for the bands, | ||
| * the current that streaks them, mixing, and the color stops. The shared | ||
| * components/[slug] template renders this between its static header and | ||
| * prose sections. | ||
| */ | ||
| import dynamic from 'next/dynamic'; | ||
| import { useMemo } from 'react'; | ||
|
|
||
| import { | ||
| COLOR_SPACE_OPTIONS, | ||
| ColorInput, | ||
| ControlPanel, | ||
| ControlsProvider, | ||
| createControlStore, | ||
| DemoLayout, | ||
| HUE_ARC_OPTIONS, | ||
| ListInput, | ||
| NumberInput, | ||
| Section, | ||
| SelectInput, | ||
| SliderInput, | ||
| useSnapshot, | ||
| } from '@/components/controls'; | ||
| import { DemoPoster } from '@/components/DemoPoster'; | ||
| import { createStop, newStopIndex } from '@/lib/stops'; | ||
| import { VisualTestPause } from '@/lib/visualTestHooks'; | ||
|
|
||
| import { INITIAL, MAX_STOPS, MIN_STOPS } from './params'; | ||
| import type { Params, Stop } from './params'; | ||
|
|
||
| const LiquidGradientScene = dynamic(() => import('./scene'), { ssr: false }); | ||
|
|
||
| function LiquidGradientDemo() { | ||
| const params = useSnapshot<Params>(); | ||
|
|
||
| return ( | ||
| <DemoPoster | ||
| alt="Liquid gradient shader preview: blue, violet, and purple streaked along curling currents" | ||
| src="/posters/liquid-gradient.jpg" | ||
| > | ||
| <LiquidGradientScene params={params}> | ||
| <VisualTestPause /> | ||
| </LiquidGradientScene> | ||
| </DemoPoster> | ||
| ); | ||
| } | ||
|
|
||
| function LiquidGradientControls() { | ||
| return ( | ||
| <ControlPanel> | ||
| <Section title="Bands"> | ||
| <SliderInput label="Angle" max={360} min={0} path="angle" step={1} /> | ||
| <SliderInput label="Repeat" max={8} min={1} path="repeat" step={0.1} /> | ||
| <SliderInput label="Softness" max={1} min={0} path="softness" step={0.01} /> | ||
| </Section> | ||
| <Section title="Flow"> | ||
| <SliderInput label="Swirl" max={2} min={0} path="swirl" step={0.01} /> | ||
| <SliderInput label="Scale" max={4} min={0.5} path="scale" step={0.1} /> | ||
| <SliderInput label="Speed" max={2} min={0} path="speed" step={0.01} /> | ||
| <SliderInput label="Seed" max={100} min={0} path="seed" step={1} /> | ||
| </Section> | ||
| <Section title="Mixing"> | ||
| <SelectInput label="Color space" options={COLOR_SPACE_OPTIONS} path="colorSpace" /> | ||
| <SelectInput label="Hue arc" options={HUE_ARC_OPTIONS} path="hueInterpolation" /> | ||
| </Section> | ||
| <ListInput<Stop> | ||
| createItem={createStop} | ||
| insertIndex={newStopIndex} | ||
| itemLabel="stop" | ||
| label="Color stops" | ||
| max={MAX_STOPS} | ||
| min={MIN_STOPS} | ||
| path="stops" | ||
| > | ||
| {() => ( | ||
| <> | ||
| <ColorInput label="Color" path="color" /> | ||
| <NumberInput | ||
| label="Position" | ||
| max={1} | ||
| min={0} | ||
| path="position" | ||
| scale={100} | ||
| step={0.01} | ||
| unit="%" | ||
| /> | ||
| </> | ||
| )} | ||
| </ListInput> | ||
| </ControlPanel> | ||
| ); | ||
| } | ||
|
|
||
| export function LiquidGradientIsland() { | ||
| const store = useMemo(() => createControlStore<Params>(INITIAL), []); | ||
|
|
||
| return ( | ||
| <ControlsProvider store={store}> | ||
| <DemoLayout controls={<LiquidGradientControls />}> | ||
| <div data-shader-demo> | ||
| <LiquidGradientDemo /> | ||
| </div> | ||
| </DemoLayout> | ||
| </ControlsProvider> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import type { ColorSpace, HueInterpolation } from '@camp-dev/shaders'; | ||
|
|
||
| import { paletteOklch } from '../../../lib/palette'; | ||
|
|
||
| export interface Stop { | ||
| color: string; | ||
| position: number; | ||
| } | ||
|
|
||
| export interface Params { | ||
| angle: number; | ||
| repeat: number; | ||
| softness: number; | ||
| swirl: number; | ||
| scale: number; | ||
| speed: number; | ||
| seed: number; | ||
| colorSpace: ColorSpace; | ||
| hueInterpolation: HueInterpolation; | ||
| stops: Stop[]; | ||
| } | ||
|
|
||
| export const MIN_STOPS = 1; | ||
| export const MAX_STOPS = 6; | ||
|
|
||
| // Values mirror the component defaults in liquid-gradient.tsx. Keep the two | ||
| // in sync. | ||
| export const INITIAL: Params = { | ||
| angle: 0, | ||
| repeat: 2, | ||
| softness: 1, | ||
| swirl: 0.9, | ||
| scale: 1.5, | ||
| speed: 0.15, | ||
| seed: 0, | ||
| colorSpace: 'oklab', | ||
| hueInterpolation: 'shorter', | ||
| stops: [ | ||
| { color: paletteOklch.blue[3], position: 0 }, | ||
| { color: paletteOklch.violet[6], position: 0.5 }, | ||
| { color: paletteOklch.purple[9], position: 1 }, | ||
| ], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| 'use client'; | ||
|
|
||
| import type { ReactNode } from 'react'; | ||
|
|
||
| import { LiquidGradient, ShaderScene } from '@camp-dev/shaders'; | ||
|
|
||
| import { INITIAL, type Params } from './params'; | ||
|
|
||
| export default function LiquidGradientScene({ | ||
| params = INITIAL, | ||
| children, | ||
| }: { | ||
| params?: Params; | ||
| children?: ReactNode; | ||
| } = {}) { | ||
| return ( | ||
| <ShaderScene> | ||
| <LiquidGradient | ||
| angle={params.angle} | ||
| colorSpace={params.colorSpace} | ||
| hueInterpolation={params.hueInterpolation} | ||
| repeat={params.repeat} | ||
| scale={params.scale} | ||
| seed={params.seed} | ||
| softness={params.softness} | ||
| speed={params.speed} | ||
| stops={params.stops} | ||
| swirl={params.swirl} | ||
| /> | ||
| {children} | ||
| </ShaderScene> | ||
| ); | ||
| } |
83 changes: 83 additions & 0 deletions
83
apps/docs/src/app/dev/liquid-gradient-benchmark/benchmark-scene.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| 'use client'; | ||
|
|
||
| // Benchmark for LiquidGradient with ShaderMonitor's GPU readout (SHA-223). | ||
| // `?size=card` renders it in the Reactive card's 312 by 309 CSS-pixel | ||
| // window, where it must stay under 0.4 ms at 2x; `?size=full` renders it at | ||
| // 1440 by 900. `?dpr=2` caps the canvas at two pixels per CSS pixel, and | ||
| // anything else at one. `?still` sets speed to 0, the Reactive card's | ||
| // parked case, while the scene keeps drawing. `?baseline` renders a | ||
| // LinearGradient instead: it reads about 0.12 ms in the card window at 2x | ||
| // on the M1 Max, so a higher reading means another tab or app is sharing | ||
| // the GPU and every reading beside it is inflated (the skewed-readout | ||
| // gotcha in docs/agents/tsl.md). The links are full page loads, so each | ||
| // measurement starts from a fresh renderer. | ||
| import { useState } from 'react'; | ||
|
|
||
| import { | ||
| LinearGradient, | ||
| LiquidGradient, | ||
| ShaderMonitor, | ||
| ShaderScene, | ||
| useStaticSceneHint, | ||
| } from '@camp-dev/shaders'; | ||
|
|
||
| const SIZES = { | ||
| card: { width: 312, height: 309 }, | ||
| full: { width: 1440, height: 900 }, | ||
| } as const; | ||
|
|
||
| type SizeName = keyof typeof SIZES; | ||
|
|
||
| interface Settings { | ||
| size: SizeName; | ||
| pixelRatio: number; | ||
| still: boolean; | ||
| baseline: boolean; | ||
| } | ||
|
|
||
| const readSettings = (): Settings => { | ||
| const search = new URLSearchParams(window.location.search); | ||
|
|
||
| return { | ||
| size: search.get('size') === 'full' ? 'full' : 'card', | ||
| pixelRatio: search.get('dpr') === '2' ? 2 : 1, | ||
| still: search.has('still'), | ||
| baseline: search.has('baseline'), | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Votes the scene animated, so a still gradient keeps drawing frames and the | ||
| * GPU readout averages many frames instead of settling on one. | ||
| */ | ||
| function KeepDrawing() { | ||
| useStaticSceneHint(false); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| export default function BenchmarkScene() { | ||
| const [{ size, pixelRatio, still, baseline }] = useState(readSettings); | ||
| const { width, height } = SIZES[size]; | ||
| const query = (overrides: Partial<Record<'size' | 'dpr', string>>) => | ||
| `?size=${overrides.size ?? size}&dpr=${overrides.dpr ?? pixelRatio}`; | ||
|
|
||
| return ( | ||
| <main style={{ padding: 24, background: '#000', color: '#fff', minHeight: '100vh' }}> | ||
| <p style={{ margin: '0 0 16px', font: '13px ui-monospace, monospace' }}> | ||
| {baseline ? 'LinearGradient baseline' : 'LiquidGradient'} | ||
| {still ? ' (still)' : ''} at {width} × {height} CSS px, pixel ratio capped at {pixelRatio}x.{' '} | ||
| <a href={query({ size: 'card' })}>card</a> · <a href={query({ size: 'full' })}>full</a> ·{' '} | ||
| <a href={query({ dpr: '1' })}>1x</a> · <a href={query({ dpr: '2' })}>2x</a> ·{' '} | ||
| <a href={`${query({})}&still`}>still</a> · <a href={`${query({})}&baseline`}>baseline</a> | ||
| </p> | ||
| <div style={{ position: 'relative', width, height }}> | ||
| <ShaderScene maxDPR={pixelRatio}> | ||
| {baseline ? <LinearGradient /> : <LiquidGradient speed={still ? 0 : undefined} />} | ||
| <KeepDrawing /> | ||
| <ShaderMonitor anchor="top-left" /> | ||
| </ShaderScene> | ||
| </div> | ||
| </main> | ||
| ); | ||
| } |
12 changes: 12 additions & 0 deletions
12
apps/docs/src/app/dev/liquid-gradient-benchmark/page.dev.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| 'use client'; | ||
|
|
||
| import dynamic from 'next/dynamic'; | ||
|
|
||
| // three/webgpu references `self` at module load and cannot SSR, so the | ||
| // benchmark scene is loaded client-only. Dev route: invisible to production | ||
| // builds. | ||
| const BenchmarkScene = dynamic(() => import('./benchmark-scene'), { ssr: false }); | ||
|
|
||
| export default function LiquidGradientBenchmarkPage() { | ||
| return <BenchmarkScene />; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.