packages/core/src/features/textures/lib/gray-scott.ts
This is the source snapshot used to build these API details. View this revision on GitHub.
1 import { validateSeed } from "./palette.js";
2 /** Curated immutable Gray–Scott feed/kill pairs. Appearance also depends on seed and elapsed steps. @see {@link ReactionDiffusionOptions} */
3 export const grayScottPresets = Object.freeze({
4 /** Existing isolated spots. See {@link grayScottPresets}. */
5 spots: Object.freeze({ feed: 0.0367, kill: 0.0649 }),
6 /** Existing connected stripes. See {@link grayScottPresets}. */
7 stripes: Object.freeze({ feed: 0.029, kill: 0.057 }),
8 /** Branching coral-like fronts. See {@link grayScottPresets}. */
9 coral: Object.freeze({ feed: 0.0545, kill: 0.062 }),
10 /** Elongated worm-like structures. See {@link grayScottPresets}. */
11 worms: Object.freeze({ feed: 0.078, kill: 0.061 }),
12 /** Dense connected labyrinths. See {@link grayScottPresets}. */
13 labyrinths: Object.freeze({ feed: 0.037, kill: 0.06 }),
14 /** Growing and dividing spots in suitable seeded conditions. See {@link grayScottPresets}. */
15 mitosis: Object.freeze({ feed: 0.028, kill: 0.062 }),
16 });
17 /** Named configurations of the same Gray–Scott solver and message contract. @see {@link grayScottPresets} */
18 export type ReactionDiffusionPreset = keyof typeof grayScottPresets;
19 /** Seed, chemistry, and initialization controls for Gray–Scott textures. @see {@link grayScottPresets} */
20 export interface ReactionDiffusionOptions {
21 /** Unsigned 32-bit initialization seed; default 42. See {@link ReactionDiffusionOptions}. */
22 readonly seed?: number;
23 /** Curated feed/kill configuration; default spots. See {@link ReactionDiffusionPreset}. */
24 readonly preset?: ReactionDiffusionPreset;
25 /** Feed-rate override, finite 0–0.1. Changes reset the field. See {@link ReactionDiffusionOptions}. */
26 readonly feed?: number;
27 /** Kill-rate override, finite 0–0.1. Changes reset the field. See {@link ReactionDiffusionOptions}. */
28 readonly kill?: number;
29 /** Initial numerical steps, integer 0–4096; default 0. Runs once per initialization. See {@link ReactionDiffusionOptions}. */
30 readonly warmupSteps?: number;
31 }
32 export function grayScottConfig(options: ReactionDiffusionOptions) {
33 const preset = options.preset ?? "spots";
34 if (!Object.hasOwn(grayScottPresets, preset))
35 throw new TypeError("Unknown reaction-diffusion preset.");
36 const feed = options.feed ?? grayScottPresets[preset].feed;
37 const kill = options.kill ?? grayScottPresets[preset].kill;
38 if (!(
39 Number.isFinite(feed) &&
40 feed >= 0 &&
41 feed <= 0.1 &&
42 Number.isFinite(kill) &&
43 kill >= 0 &&
44 kill <= 0.1
45 ))
46 throw new RangeError(
47 "Gray–Scott feed and kill must be finite values in [0, 0.1].",
48 );
49 const warmupSteps = options.warmupSteps ?? 0;
50 if (!Number.isInteger(warmupSteps) || warmupSteps < 0 || warmupSteps > 4096)
51 throw new RangeError(
52 "Gray–Scott warmupSteps must be an integer in [0, 4096].",
53 );
54 return { seed: validateSeed(options.seed), preset, feed, kill, warmupSteps };
55 }
56
Documentation version
Section titled “Documentation version”Documentation built with @pibbl/core 0.0.2, revision a1dcf8e. ALPHA — NOT FOR PRODUCTION USE.