Skip to content

armgit / viz3d

Public Imported from GitHub
0

An AI agent skill that turns any subject into a 3D film or app

Add make-pixel-splats: pixel art from code-built gaussian splats in 67js

Nguyen Xuan Ana39f98542a27
diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json
index 89596bd..a2e8e06 100644
--- a/.claude-plugin/marketplace.json
+++ b/.claude-plugin/marketplace.json
@@ -20,6 +20,11 @@
"name": "build-house-from-plan",
"source": "./plugins/build-house-from-plan",
"description": "Turn a 2D house plan into a photoreal 3D house, checked against the drawing"
+ },
+ {
+ "name": "make-pixel-splats",
+ "source": "./plugins/make-pixel-splats",
+ "description": "Make pixel art scenes out of gaussian splats written by code, rendered in 67js"
}
]
}
diff --git a/README.md b/README.md
index 22be210..10682fe 100644
--- a/README.md
+++ b/README.md
@@ -325,6 +325,18 @@ Python through [uv](https://docs.astral.sh/uv/), Blender 5.x and a GPU.
The plans behind the picture are public domain. They are Historic American Buildings Survey and Historic American
Engineering Record sheets from the Library of Congress, and a USDA plan. Furniture includes CC BY models listed in the skill's example.
+**make-pixel-splats** makes pixel art scenes out of gaussian splats that a script writes, with no 3D model. Each
+shape is a signed distance function. Every surface voxel becomes one round splat with sun, shadow and ambient light
+baked into its colour. [67js](https://github.com/codepawl/67.js) draws the splats at a low size, then a post pass adds
+glow, haze, a small palette and ordered dither. It comes with three haunted medieval scenes, three cozy islands, eased
+motion helpers and a headless renderer for stills and films. It needs Node 20, Chrome and npm.
+
+![A haunted chapel, a witch's swamp and the Reaper at a gallows, all pixel art made of gaussian splats](docs/images/pixel-splats.jpg)
+
+```text
+/plugin install make-pixel-splats@codepawl
+```
+
## Contributing
Faults you saw, new looks, example topics and harness adapters are welcome. Read
diff --git a/docs/images/pixel-splats.jpg b/docs/images/pixel-splats.jpg
new file mode 100644
index 0000000..b9b9b7a
Binary files /dev/null and b/docs/images/pixel-splats.jpg differ
diff --git a/plugins/make-pixel-splats/.claude-plugin/plugin.json b/plugins/make-pixel-splats/.claude-plugin/plugin.json
new file mode 100644
index 0000000..49b0912
--- /dev/null
+++ b/plugins/make-pixel-splats/.claude-plugin/plugin.json
@@ -0,0 +1,20 @@
+{
+ "name": "make-pixel-splats",
+ "version": "0.1.0",
+ "description": "Make pixel art scenes out of gaussian splats written by code, with no 3D model: SDF shapes, baked light, 67js, palette and dither, with haunted medieval examples and a film renderer.",
+ "author": {
+ "name": "CodePawl",
+ "url": "https://codepawl.com"
+ },
+ "homepage": "https://github.com/codepawl/viz3d",
+ "repository": "https://github.com/codepawl/viz3d",
+ "license": "MIT",
+ "keywords": [
+ "3d",
+ "pixel-art",
+ "gaussian-splats",
+ "67js",
+ "voxel",
+ "sdf"
+ ]
+}
diff --git a/plugins/make-pixel-splats/skills/make-pixel-splats/SKILL.md b/plugins/make-pixel-splats/skills/make-pixel-splats/SKILL.md
new file mode 100644
index 0000000..7ca726d
--- /dev/null
+++ b/plugins/make-pixel-splats/skills/make-pixel-splats/SKILL.md
@@ -0,0 +1,61 @@
+---
+name: make-pixel-splats
+description: Make pixel art scenes out of gaussian splats written by code, with no 3D model, rendered in 67js. Shapes come from signed distance functions, every surface voxel becomes one round splat with sun, shadow and ambient light baked into its colour, and a post pass adds glow, haze, a small palette and ordered dither before a nearest-neighbour upscale. Includes three haunted medieval scenes, three cozy islands, eased motion helpers, a headless frame renderer and a 24 s film plan cut on lightning. Use for pixel art splats, voxel splat dioramas, code-built gaussian splat scenes, scenes like a haunted crag or a witch swamp.
+license: MIT
+---
+
+# Pixel art from code-built gaussian splats
+
+No mesh and no capture go in. A script writes every splat: position, round scale and a colour that already holds its
+light. 67js draws them at a low internal size, then the page turns the frame into pixel art.
+
+## Set up
+
+1. Copy `{{SKILL_DIR}}/scripts/` to `{{WORK_DIR}}/pixel-splats/` and run `npm install` there. It installs
+ `@codepawlhq/67js`, which brings `playwright-core`. Rendering needs Chrome installed.
+2. Stills: `node render.mjs plans/stills.mjs out/stills`. Film frames: `node render.mjs plans/haunted-film.mjs out/film`
+ (`FILM_FPS`, `FILM_SIZE`, `FILM_UNTIL` set rate, output size and a cut-off), then
+ `ffmpeg -framerate 30 -i out/film/frame_%04d.png -c:v libx264 -pix_fmt yuv420p film.mp4`.
+3. A frame takes 0.3 to 1 s on a mid-range GPU at 60k to 80k splats.
+
+## Files
+
+| File | Holds |
+|---|---|
+| `voxel-splats.js` | SDF shapes (sphere, box, capsule, cone, torus, floating island), `voxelizeModel`, `placeSplats`, `packSplatData` |
+| `motion.js` | easing, walk-then-hold steps, struck pendulum that rings down, smooth wobble |
+| `page.js` | scene setup, mist and wisps, assemble and dissolve, camera, glow halos, haze, palette and Bayer dither |
+| `medieval.js`, `medieval2.js` | haunted chapel crag, witch swamp, gallows with the Reaper |
+| `scenes.js` | lighthouse, snail meadow, koi pond, and `SCENE_BUILDERS`, the registry `render.mjs` names scenes from |
+| `plans/` | a stills plan and the 24 s film plan with eased keyframed cameras |
+
+## Make a scene
+
+1. Write a builder `(spacing) => scene` like the ones in `medieval2.js` and add it to `SCENE_BUILDERS`.
+2. Each static part is a model `{ distance, material, bounds }` passed to `voxelizeModel(model, spacing, light,
+ { shadowModel })`. `material(x, y, z, nx, ny, nz)` returns `[r, g, b, emission, alpha]`. Give every part the whole
+ scene as `shadowModel` so parts shadow each other.
+3. Moving parts are voxelized once around their own pivot and moved each frame with `placeSplats(list, { x, y, z,
+ heading, scale, deform })`. Small things (bats, bubbles, ash, eyes) are point lists built in `dynamic(time)`.
+4. The scene object also carries `palette`, `background`, `camera`, `mist`, `haze`, `glows(time)` and
+ `contactShadows(time)`. Read [references/method.md](references/method.md) for the fields and the numbers that worked.
+5. Render one still in each look, read it, fix, and only then render a film.
+
+## Looks
+
+- A: internal 216 px, spacing 0.02, dither 0.12. Chunky, good on bright scenes, muddy on dark ones.
+- C: internal 360 px, spacing 0.015, dither 0.06, `paletteSteps: 3`. Holds detail at night. The haunted films use it.
+- Reveal: raise `internalSize` to 1080 and set `style.palette` to `'none'` to show the single round splats.
+
+## Rules that came from mistakes
+
+- Never mutate cached static lists in a frame. Copy before scaling, or splats shrink a little every frame.
+- Keep mist alpha near 0.02 to 0.04 and its splats low and outside the subject, or it buries the scene.
+- Keep haze soften at 1 px at any size, or the high resolution reveal turns to blur.
+- Dark scenes need moonlit sun colour near white and an ambient near 0.4. A green ambient makes green soup.
+- A face in a dark hood needs a pale, slightly emissive skull and small eye splats with a tight glow, or it reads as a
+ red blob.
+- A thin blade swung in the plane that faces away from the camera reads as a bar. Swing it in the plane facing the
+ viewer.
+- Linear motion looks dead: ease every move, hold between moves, let swings ring down, and key the camera.
+- Lightning is many filled steps with a soft wide twin, plus a sky flash and a glow on the ground, not a dotted line.
diff --git a/plugins/make-pixel-splats/skills/make-pixel-splats/references/method.md b/plugins/make-pixel-splats/skills/make-pixel-splats/references/method.md
new file mode 100644
index 0000000..eb3c1e3
--- /dev/null
+++ b/plugins/make-pixel-splats/skills/make-pixel-splats/references/method.md
@@ -0,0 +1,55 @@
+# Method and scene fields
+
+Reading map: [Pipeline](#pipeline), [Scene object](#scene-object), [Frame options](#frame-options),
+[Film plans](#film-plans).
+
+## Pipeline
+
+1. **Shapes.** Each part is a signed distance function built from the helpers in `voxel-splats.js`. Combine with
+ `Math.min` (union), `Math.max(a, -b)` (cut) and `smoothUnion`. Add `valueNoise` to a distance for rough rock.
+2. **Voxelize.** `voxelizeModel` walks a grid of `spacing` inside `bounds` and keeps cells on the surface. Each cell
+ becomes one round splat of radius `spacing * splatScale` (0.62 by default).
+3. **Bake light.** For each splat it computes the normal from the distance field, a sun term, a soft shadow by
+ marching toward the sun through `shadowModel`, ambient occlusion and a rim term. The result goes into the colour.
+ Splats are drawn unlit, so the light never changes after this step. `material` can return an emission above 0 to
+ keep a colour bright in shadow (candles, windows, a skull in a hood).
+4. **Draw.** `packSplatData` packs lists into the 67js splat data (`positions`, RGBA `colors`, linear `scales`,
+ `rotations` with w first). `createGaussianSplats` and `attachGaussianSplats` from
+ `@codepawlhq/67js/addons/gaussian-splats` put them in a `Renderer67` with a transparent background.
+5. **Post.** `page.js` reads the canvas back and composites, in order: background gradient, stars, moon with halo,
+ cloud band, lightning flash, glow halos, then haze (bloom over a threshold, lift toward a haze colour, a mist band
+ near the bottom, vignette, a 1 px soften), then the palette with a 4x4 Bayer dither, then a nearest-neighbour
+ upscale to the output size.
+
+## Scene object
+
+| Field | Meaning |
+|---|---|
+| `palette`, `paletteExtra` | colours as `[r, g, b]` 0..1, and extra hex strings merged in at setup |
+| `background` | `{ inner, outer, stars, moon: { x, y, radius, color, halo }, clouds }`, moon position in 0..1 of the frame |
+| `camera` | `{ target, distance, height, startAngle }` |
+| `mist` | `{ color, alpha, low, high, wispColor, wispHeight, trails }`: 320 faint splats and 7 glowing wisps |
+| `haze` | `{ color, lift, bloom, bloomThreshold, soften, mistTop, mistStrength, vignette }` |
+| `staticLists` | voxelized parts that never move |
+| `dynamic(time)` | lists made or placed this frame |
+| `glows(time)` | `{ position, color, radius, strength }` halos drawn in screen space |
+| `contactShadows(time)` | `{ x, z, radius, strength }` darkening on the ground under moving things |
+
+Numbers that worked on night scenes: haze lift 0.08 to 0.15, bloom 0.8 to 0.9, bloomThreshold 0.55 to 0.6,
+mistStrength 0.08 to 0.1, vignette 1.1 to 1.2, mist alpha 0.02 to 0.035.
+
+## Frame options
+
+`window.renderFrame({ time, internalSize, outputSize, style, camera, assemble, dissolve, fadeToBlack, flash })`.
+
+- `style`: `{ dither, paletteSteps, splatScale, palette: 'none' }`.
+- `camera`: `{ angleOffset, zoom, orbitSpeed, heightOffset, targetOffset }` on top of the scene camera. Set
+ `orbitSpeed: 0` and drive the rest from eased keyframes.
+- `assemble` 0..1: splats fall into place. `dissolve` 0..1.25: splats fly apart as ash. `flash` 0..1: lightning.
+
+## Film plans
+
+A plan module exports `frames`, each `{ file, scene, spacing, options }`. `plans/haunted-film.mjs` shows shots as
+functions of local time, cuts placed on flashes, a push in that raises `internalSize` for the reveal, and a camera
+jolt that decays after an impact. Export event times from the scene (`REAPER_TIMING`, `GALLOWS_BOLTS`) so cuts,
+flashes and sound land on them.
diff --git a/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/index.html b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/index.html
new file mode 100644
index 0000000..62cf879
--- /dev/null
+++ b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/index.html
@@ -0,0 +1,5 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>Pixel splats</title>
+<style>body { margin: 0; background: #000; }</style>
+<script type="module" src="/page.js"></script>
diff --git a/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/medieval.js b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/medieval.js
new file mode 100644
index 0000000..39a2428
--- /dev/null
+++ b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/medieval.js
@@ -0,0 +1,432 @@
+// A haunted medieval crag: ruined chapel with a bell tower, graveyard, dead tree, a hooded monk with a lantern,
+// crows round the tower, a ghost rising from a grave, falling ash and lantern embers.
+import {
+ hashNoise, valueNoise, sphereDistance, ellipsoidDistance, boxDistance, cylinderDistance, taperedCylinderDistance,
+ capsuleDistance, islandDistance, voxelizeModel, placeSplats,
+} from './voxel-splats.js';
+import { easeOutCubic, easeInOutCubic, stepAndHold, struckSwing, wobble } from './motion.js';
+
+const hex = value => [1, 3, 5].map(start => parseInt(value.slice(start, start + 2), 16) / 255);
+
+function makeList(points) {
+ const count = points.length;
+ const positions = new Float32Array(count * 3);
+ const colors = new Float32Array(count * 4);
+ const scales = new Float32Array(count);
+ points.forEach((point, index) => {
+ positions.set([point.x, point.y, point.z], index * 3);
+ colors.set([point.color[0], point.color[1], point.color[2], point.alpha ?? 1], index * 4);
+ scales[index] = point.scale;
+ });
+ return { count, positions, colors, scales };
+}
+
+function pyramidDistance(x, y, z, centerX, bottomY, centerZ, halfWidth, height) {
+ const amount = Math.min(Math.max((y - bottomY) / height, 0), 1);
+ const half = halfWidth * (1 - amount);
+ const side = (Math.max(Math.abs(x - centerX), Math.abs(z - centerZ)) - half) * 0.9;
+ return Math.max(side, bottomY - y, y - bottomY - height);
+}
+
+// A pointed (gothic) arch opening on a plane: across is the coordinate along the wall, up is height.
+function gothicArch(across, up, halfWidth, springY) {
+ if (up < springY) return Math.max(Math.abs(across) - halfWidth, -up);
+ const leftArc = Math.hypot(across + halfWidth * 0.6, up - springY) - halfWidth * 1.6;
+ const rightArc = Math.hypot(across - halfWidth * 0.6, up - springY) - halfWidth * 1.6;
+ return Math.max(leftArc, rightArc);
+}
+
+const GRAVES = [
+ { x: 0.42, z: 0.42, lean: 0.12, kind: 'stone' }, { x: 0.62, z: 0.3, lean: -0.18, kind: 'cross' },
+ { x: 0.25, z: 0.62, lean: 0.25, kind: 'stone' }, { x: 0.68, z: 0.58, lean: 0.05, kind: 'stone' },
+ { x: 0.05, z: 0.72, lean: -0.22, kind: 'cross' }, { x: 0.48, z: 0.75, lean: 0.3, kind: 'stone' },
+ { x: -0.2, z: 0.7, lean: 0.1, kind: 'stone' },
+];
+const GHOST_GRAVE = GRAVES[2];
+
+function graveDistance(x, y, z, grave) {
+ const cosine = Math.cos(grave.lean);
+ const sine = Math.sin(grave.lean);
+ const localX = x - grave.x;
+ const localZ = z - grave.z;
+ const tiltedX = localX * cosine - y * sine;
+ const tiltedY = localX * sine + y * cosine;
+ if (grave.kind === 'cross') {
+ return Math.min(
+ boxDistance(tiltedX, tiltedY, localZ, 0, 0.1, 0, 0.014, 0.11, 0.014),
+ boxDistance(tiltedX, tiltedY, localZ, 0, 0.15, 0, 0.055, 0.014, 0.014));
+ }
+ const slab = boxDistance(tiltedX, tiltedY, localZ, 0, 0.07, 0, 0.055, 0.07, 0.018, 0.006);
+ const top = Math.max(Math.hypot(tiltedX, tiltedY - 0.14) - 0.055, Math.abs(localZ) - 0.018);
+ const mound = ellipsoidDistance(x, y, z, grave.x + 0.06, -0.02, grave.z + 0.08, 0.1, 0.035, 0.14);
+ return Math.min(slab, top, mound);
+}
+
+export function buildMedieval(spacing) {
+ const palette = ['#06060c', '#0e0e1a', '#181a2a', '#262a3e', '#3a3e54', '#5a5e74', '#8a8c9e', '#c4c4cc',
+ '#1e2a22', '#34463a', '#56664e', '#3e2a26', '#ff9a3a', '#ffd88a', '#7ef2d0', '#c8fff0'].map(hex);
+ const light = { sun: [-0.35, 0.6, 0.75], sunColor: [0.85, 0.92, 1.15], ambientColor: [0.42, 0.44, 0.6], rimColor: [0.45, 0.6, 0.9], shadowSoftness: 7 };
+ const stone = hex('#6a6c7c');
+ const stoneDark = hex('#44465a');
+ const moss = hex('#4a5a46');
+
+ const cragDistance = (x, y, z) => {
+ const path = Math.abs(Math.hypot(x - 0.55, z - 0.5) - 0.3) < 0.05 ? 0.012 : 0;
+ return islandDistance(x, y + path, z, 1.08, 1.25, 0, 0.11);
+ };
+ const chapelCenter = [0.12, 0.06];
+ const chapelDistance = (x, y, z) => {
+ const localX = x - chapelCenter[0];
+ const localZ = z - chapelCenter[1];
+ const outer = boxDistance(localX, y, localZ, 0, 0.25, 0, 0.32, 0.25, 0.19);
+ const inner = boxDistance(localX, y, localZ, 0, 0.3, 0, 0.27, 0.32, 0.14);
+ let walls = Math.max(outer, -inner);
+ const gable = Math.max(boxDistance(localX, y, localZ, 0.295, 0.62, 0, 0.025, 0.14, 0.19), Math.abs(localZ) * 0.95 + (y - 0.5) - 0.17);
+ walls = Math.min(walls, gable);
+ const ruinTop = 0.4 + valueNoise(x * 9, 0, z * 9) * 0.16 + (localX < -0.05 ? -0.08 : 0);
+ const isGableEnd = localX > 0.26;
+ if (!isGableEnd) walls = Math.max(walls, y - ruinTop);
+ const frontWindow = gothicArch(localZ, y - 0.3, 0.06, 0.38 - 0.3);
+ const door = gothicArch(localZ, y - 0.0, 0.055, 0.16);
+ const sideWindows = Math.min(gothicArch(localX - 0.12, y - 0.18, 0.04, 0.12), gothicArch(localX + 0.12, y - 0.18, 0.04, 0.12));
+ if (localX > 0.2) walls = Math.max(walls, -Math.max(frontWindow, -(localX - 0.2)), -Math.max(door, -(localX - 0.2)));
+ if (Math.abs(localZ) > 0.12) walls = Math.max(walls, -sideWindows);
+ const buttresses = Math.min(
+ boxDistance(localX, y, localZ, 0.12, 0.13, 0.21, 0.025, 0.13, 0.03),
+ boxDistance(localX, y, localZ, -0.12, 0.13, 0.21, 0.025, 0.13, 0.03),
+ boxDistance(localX, y, localZ, 0.12, 0.13, -0.21, 0.025, 0.13, 0.03));
+ const rubble = sphereDistance(x, y, z, chapelCenter[0] - 0.36, 0.0, chapelCenter[1] + 0.22, 0.07) + (valueNoise(x * 30, y * 30, z * 30) - 0.5) * 0.04;
+ return Math.min(walls, buttresses, rubble);
+ };
+ const towerCenter = [-0.32, -0.2];
+ const towerDistance = (x, y, z) => {
+ const localX = x - towerCenter[0];
+ const localZ = z - towerCenter[1];
+ let tower = boxDistance(localX, y, localZ, 0, 0.47, 0, 0.11, 0.47, 0.11);
+ const hollow = boxDistance(localX, y, localZ, 0, 0.78, 0, 0.085, 0.12, 0.085);
+ tower = Math.max(tower, -hollow);
+ const belfryX = gothicArch(localZ, y - 0.68, 0.055, 0.1);
+ const belfryZ = gothicArch(localX, y - 0.68, 0.055, 0.1);
+ tower = Math.max(tower, -belfryX, -belfryZ);
+ const slit = Math.max(Math.abs(localZ) - 0.012, Math.abs(y - 0.42) - 0.05);
+ tower = Math.max(tower, -slit);
+ const cornice = boxDistance(localX, y, localZ, 0, 0.95, 0, 0.13, 0.02, 0.13);
+ const spire = pyramidDistance(x, y, z, towerCenter[0], 0.97, towerCenter[1], 0.12, 0.42);
+ const finial = Math.min(
+ capsuleDistance(x, y, z, towerCenter[0], 1.38, towerCenter[1], towerCenter[0], 1.5, towerCenter[1], 0.008),
+ capsuleDistance(x, y, z, towerCenter[0] - 0.03, 1.46, towerCenter[1], towerCenter[0] + 0.03, 1.46, towerCenter[1], 0.008));
+ return Math.min(tower, cornice, spire, finial);
+ };
+ const treeBranches = (() => {
+ const branches = [];
+ const grow = (startX, startY, startZ, angle, tilt, length, radius, depth) => {
+ const endX = startX + Math.cos(angle) * Math.sin(tilt) * length;
+ const endY = startY + Math.cos(tilt) * length;
+ const endZ = startZ + Math.sin(angle) * Math.sin(tilt) * length;
+ branches.push([startX, startY, startZ, endX, endY, endZ, radius]);
+ if (depth === 0) return;
+ for (let child = 0; child < 2; child++) {
+ const random = hashNoise(depth * 7 + child, startX * 13, startZ * 11);
+ grow(endX, endY, endZ, angle + (child ? 1 : -1) * (0.6 + random * 0.9), tilt + 0.25 + random * 0.35, length * 0.68, radius * 0.62, depth - 1);
+ }
+ };
+ grow(0.66, 0, -0.42, 0.5, 0.18, 0.32, 0.032, 4);
+ return branches;
+ })();
+ const treeDistance = (x, y, z) => {
+ if (x < 0.2 || x > 1.15 || y > 1.1 || z < -0.95 || z > 0.1) return 0.2;
+ let distance = Infinity;
+ for (const [startX, startY, startZ, endX, endY, endZ, radius] of treeBranches) {
+ distance = Math.min(distance, capsuleDistance(x, y, z, startX, startY, startZ, endX, endY, endZ, radius));
+ }
+ return distance;
+ };
+ const graveyardDistance = (x, y, z) => {
+ let distance = Infinity;
+ for (const grave of GRAVES) {
+ if (Math.abs(x - grave.x) > 0.25 || Math.abs(z - grave.z) > 0.3) continue;
+ distance = Math.min(distance, graveDistance(x, y, z, grave));
+ }
+ const post = capsuleDistance(x, y, z, 0.86, 0, 0.12, 0.86, 0.36, 0.12, 0.012);
+ const arm = capsuleDistance(x, y, z, 0.86, 0.36, 0.12, 0.8, 0.36, 0.12, 0.008);
+ const lantern = boxDistance(x, y, z, 0.8, 0.3, 0.12, 0.022, 0.03, 0.022, 0.004);
+ return Math.min(distance, post, arm, lantern);
+ };
+ const sceneDistance = (x, y, z) => Math.min(cragDistance(x, y, z), chapelDistance(x, y, z), towerDistance(x, y, z), treeDistance(x, y, z), graveyardDistance(x, y, z));
+ const shadowModel = { distance: sceneDistance };
+
+ const crag = voxelizeModel({
+ distance: cragDistance,
+ bounds: [-1.15, -1.3, -1.15, 1.15, 0.08, 1.15],
+ material: (x, y, z, normalX, normalY) => {
+ const onPath = Math.abs(Math.hypot(x - 0.55, z - 0.5) - 0.3) < 0.05;
+ if (y > -0.06 && normalY > 0.4) {
+ if (onPath) return [...hex('#5a5048'), 0];
+ const grass = valueNoise(x * 9, 2, z * 9);
+ return [...(grass > 0.62 ? hex('#56664e') : grass > 0.3 ? hex('#3e4e3e') : hex('#2e3a30')), 0];
+ }
+ const strata = valueNoise(x * 3, y * 11, z * 3);
+ return [...(strata > 0.6 ? hex('#5a5e74') : strata > 0.32 ? hex('#3a3e54') : hex('#262a3e')), 0];
+ },
+ }, spacing, light, { shadowModel });
+ const stoneMaterial = (x, y, z) => {
+ const brickRow = Math.floor(y / 0.035);
+ const brickColumn = Math.floor((x + z + (brickRow % 2) * 0.03) / 0.06);
+ const mortar = (y / 0.035) % 1 < 0.14;
+ const tone = hashNoise(brickRow, brickColumn, 3.3);
+ if (y < 0.12 && valueNoise(x * 18, y * 18, z * 18) > 0.55) return [...moss, 0];
+ if (mortar) return [...stoneDark, 0];
+ return [...(tone > 0.66 ? hex('#7c7e8e') : tone > 0.3 ? stone : hex('#585a6c')), 0];
+ };
+ const chapel = voxelizeModel({
+ distance: chapelDistance,
+ bounds: [-0.3, -0.02, -0.18, 0.5, 0.82, 0.3],
+ material: stoneMaterial,
+ }, spacing * 0.8, light, { shadowModel });
+ const tower = voxelizeModel({
+ distance: towerDistance,
+ bounds: [-0.47, -0.02, -0.35, -0.17, 1.52, -0.05],
+ material: (x, y, z) => (y > 0.97 ? [...(valueNoise(x * 40, y * 40, z * 40) > 0.5 ? hex('#3a3e54') : hex('#2a2c40')), 0] : stoneMaterial(x, y, z)),
+ }, spacing * 0.8, light, { shadowModel });
+ const tree = voxelizeModel({
+ distance: treeDistance,
+ bounds: [0.3, -0.02, -0.85, 1.1, 1.0, 0.05],
+ material: (x, y, z) => [...(valueNoise(x * 30, y * 30, z * 30) > 0.5 ? hex('#3e2a26') : hex('#2a1e1e')), 0],
+ }, spacing * 0.7, light, { shadowModel });
+ const graveyard = voxelizeModel({
+ distance: graveyardDistance,
+ bounds: [-0.35, -0.06, -0.02, 0.95, 0.42, 0.95],
+ material: (x, y, z) => {
+ if (Math.abs(x - 0.8) < 0.03 && Math.abs(y - 0.3) < 0.035 && Math.abs(z - 0.12) < 0.03) return [...hex('#ffb24a'), 1];
+ if (Math.abs(x - 0.86) < 0.02 && z > 0.08 && z < 0.16) return [...hex('#2a2a30'), 0];
+ if (y < 0.0) return [...hex('#34302c'), 0];
+ const lichen = valueNoise(x * 35, y * 35, z * 35);
+ return [...(lichen > 0.68 ? moss : lichen > 0.3 ? hex('#7a7c8a') : hex('#5c5e6e')), 0];
+ },
+ }, spacing * 0.65, light, { shadowModel });
+
+ // Interior glow: candles behind the front window and door, drawn as emissive splats inside the nave.
+ const candlePoints = [];
+ for (let candle = 0; candle < 9; candle++) {
+ const candleX = chapelCenter[0] + 0.05 + (candle % 3) * 0.06;
+ const candleZ = chapelCenter[1] - 0.08 + Math.floor(candle / 3) * 0.08;
+ for (let piece = 0; piece < 4; piece++) candlePoints.push({ x: candleX, y: 0.02 + piece * 0.012, z: candleZ, color: hex('#e8e0d0'), scale: 0.008, candle: -1 });
+ candlePoints.push({ x: candleX, y: 0.075, z: candleZ, color: hex('#ffd88a'), scale: 0.01, candle });
+ }
+
+ const monk = voxelizeModel({
+ distance: (x, y, z) => {
+ const robe = taperedCylinderDistance(x, y, z, 0, 0, 0, 0.065, 0.035, 0.2);
+ const hood = Math.min(sphereDistance(x, y, z, 0, 0.23, 0, 0.045), taperedCylinderDistance(x, y, z, -0.012, 0.23, 0, 0.04, 0.0, 0.07));
+ const arm = capsuleDistance(x, y, z, 0.0, 0.17, 0.035, 0.06, 0.12, 0.05, 0.016);
+ const staff = capsuleDistance(x, y, z, 0.05, 0.0, -0.05, 0.05, 0.3, -0.05, 0.007);
+ return Math.min(robe, hood, arm, staff);
+ },
+ bounds: [-0.08, -0.02, -0.08, 0.1, 0.32, 0.08],
+ material: (x, y, z) => {
+ if (Math.abs(x - 0.05) < 0.012 && z < -0.04) return [...hex('#3e2a26'), 0];
+ if (y > 0.2 && x > 0.025 && Math.abs(z) < 0.025) return [...hex('#06060c'), 0];
+ return [...(y < 0.03 ? hex('#3a3448') : hex('#4e4660')), 0];
+ },
+ }, spacing * 0.5, light);
+ const monkPlace = time => {
+ const step = stepAndHold(time, 3.4, 0.62);
+ const angle = step.progress * 0.75 + 2.4;
+ const x = 0.55 + Math.cos(angle) * 0.3;
+ const z = 0.5 + Math.sin(angle) * 0.3;
+ const lookAround = step.moving ? 0 : Math.sin((time % 3.4) * 2.2) * 0.5 * Math.sin(Math.PI * Math.min(((time % 3.4) - 3.4 * 0.62) / (3.4 * 0.38), 1));
+ return { x, z, heading: -angle + Math.PI / 2 + Math.PI + lookAround, angle, moving: step.moving };
+ };
+ const monkLantern = time => {
+ const place = monkPlace(time);
+ const sway = (place.moving ? Math.sin(time * 5.2) * 0.012 : Math.sin(time * 1.4) * 0.005);
+ const cosine = Math.cos(place.heading);
+ const sine = Math.sin(place.heading);
+ const localX = (0.075 + sway) * 1.6;
+ const localZ = 0.055 * 1.6;
+ return [place.x + localX * cosine + localZ * sine, 0.16 + Math.abs(Math.sin(time * 2.6)) * 0.01, place.z - localX * sine + localZ * cosine];
+ };
+
+ const bell = voxelizeModel({
+ distance: (x, y, z) => Math.max(taperedCylinderDistance(x, y, z, 0, -0.07, 0, 0.05, 0.028, 0.07), -taperedCylinderDistance(x, y, z, 0, -0.08, 0, 0.04, 0.018, 0.065)),
+ bounds: [-0.06, -0.08, -0.06, 0.06, 0.01, 0.06],
+ material: () => [...hex('#8a6a3a'), 0.25],
+ }, spacing * 0.5, light);
+
+ const crowPoints = (phase, flap) => {
+ const points = [];
+ for (let body = 0; body < 5; body++) points.push([body * 0.012 - 0.024, 0, 0, 0.011]);
+ points.push([0.034, 0.004, 0, 0.008]);
+ for (let side = -1; side <= 1; side += 2) {
+ for (let feather = 1; feather <= 5; feather++) {
+ const reach = feather * 0.014;
+ points.push([-0.004 - feather * 0.002, Math.sin(flap) * reach * 0.9, side * Math.cos(flap) * reach, 0.008]);
+ }
+ }
+ return points;
+ };
+
+ const ashCount = 160;
+ const emberCount = 12;
+ return {
+ name: 'medieval',
+ mist: { color: hex('#8a92b0'), alpha: 0.03, low: -0.25, high: 0.15, wispColor: hex('#7ef2d0'), wispHeight: 0.32, trails: true },
+ haze: { color: hex('#1a1c2e'), lift: 0.15, bloom: 0.85, bloomThreshold: 0.58, soften: 0.3, mistTop: 0.6, mistStrength: 0.1, vignette: 1.1 },
+ paletteExtra: ['#4e5672', '#7a84a2', '#a8b0c8', '#2a4a44', '#4e8a7a', '#ffbe66'],
+ palette,
+ background: { inner: hex('#20243c'), outer: hex('#04040a'), stars: true, moon: { x: 0.84, y: 0.13, radius: 0.045, color: hex('#cdd6e4'), halo: hex('#5a6a96') }, clouds: hex('#2a2e46') },
+ camera: { target: [0.05, 0.5, 0.1], distance: 4.4, height: 1.25, startAngle: 0.35 },
+ staticLists: [crag, chapel, tower, tree, graveyard],
+ dynamic(time) {
+ const lists = [];
+ const candleList = makeList(candlePoints.map(point => point.candle < 0 ? point : {
+ ...point, y: point.y + Math.sin(time * 13 + point.candle) * 0.003, scale: point.scale * (0.85 + 0.3 * hashNoise(Math.floor(time * 12), point.candle, 1)),
+ }));
+ lists.push(candleList);
+ const place = monkPlace(time);
+ lists.push(placeSplats(monk, { x: place.x, y: -0.005 + (place.moving ? Math.abs(Math.sin(time * 5.2)) * 0.008 : 0), z: place.z, heading: place.heading, scale: 1.6 }));
+ // Two red eyes inside the hood.
+ const eyes = [];
+ for (const side of [-1, 1]) {
+ const localX = 0.036 * 1.6;
+ const localZ = side * 0.012 * 1.6;
+ const cosine = Math.cos(place.heading);
+ const sine = Math.sin(place.heading);
+ eyes.push({ x: place.x + localX * cosine + localZ * sine, y: 0.232 * 1.6, z: place.z - localX * sine + localZ * cosine, color: hex('#ff2a1a'), scale: 0.0075 });
+ }
+ // Something watches from the belfry: eye pairs that blink and sometimes go dark.
+ for (const [faceX, faceZ, seed] of [[0.1, 0, 1], [0, 0.1, 2], [-0.1, 0, 3]]) {
+ const open = Math.sin(time * 0.8 + seed * 2.1) > -0.3 && (time * 3 + seed) % 2.7 > 0.12;
+ if (!open) continue;
+ for (const side of [-1, 1]) {
+ eyes.push({
+ x: towerCenter[0] + faceX * 0.6 + (faceZ ? side * 0.018 : 0), y: 0.735 + wobble(time, seed) * 0.004,
+ z: towerCenter[1] + faceZ * 0.6 + (faceX ? side * 0.018 : 0), color: hex('#ff3a20'), scale: 0.007,
+ });
+ }
+ }
+ lists.push(makeList(eyes));
+ const lantern = monkLantern(time);
+ lists.push(makeList([{ x: lantern[0], y: lantern[1], z: lantern[2], color: hex('#ffd88a'), scale: 0.02 }]));
+ const swing = struckSwing(time, 4.5, 0.55, 3.4, 0.55, 1.2);
+ lists.push(placeSplats(bell, {
+ x: towerCenter[0], y: 0.84, z: towerCenter[1], heading: 0,
+ deform: (x, y, z) => [x * Math.cos(swing) - y * Math.sin(swing), x * Math.sin(swing) + y * Math.cos(swing), z],
+ }));
+ // Crows circling the spire.
+ const crows = [];
+ for (let crow = 0; crow < 6; crow++) {
+ const phase = crow * 1.13;
+ const angle = time * (0.45 + crow * 0.04) + phase + Math.sin(time * 0.6 + phase) * 0.6;
+ const radius = 0.22 + crow * 0.05;
+ const centerX = towerCenter[0] + Math.cos(angle) * radius;
+ const centerZ = towerCenter[1] + Math.sin(angle) * radius;
+ const dive = Math.pow(Math.max(0, Math.sin(time * 0.45 + phase * 2)), 8) * 0.35;
+ const centerY = 1.05 + wobble(time * 0.6, phase) * 0.08 + crow * 0.05 - dive;
+ const heading = -angle - Math.PI / 2;
+ const gliding = Math.sin(time * 0.9 + phase * 3) > 0.25 || dive > 0.05;
+ const flap = gliding ? 0.35 + Math.sin(time * 2 + phase) * 0.05 : Math.sin(time * 11 + phase * 5) * 0.95;
+ for (const [localX, localY, localZ, scale] of crowPoints(phase, flap)) {
+ crows.push({
+ x: centerX + (localX * Math.cos(heading) + localZ * Math.sin(heading)) * 2.3,
+ y: centerY + localY * 2.3,
+ z: centerZ + (-localX * Math.sin(heading) + localZ * Math.cos(heading)) * 2.3,
+ color: hex('#141420'), scale: scale * 2,
+ });
+ }
+ }
+ lists.push(makeList(crows));
+ // A ghost rising out of a grave, swaying, then thinning out.
+ const ghostAge = ((time - 3.5) % 11) / 4.5;
+ if (ghostAge > 0 && ghostAge < 1) {
+ const ghost = [];
+ const rise = easeOutCubic(ghostAge * 2.2) * 0.3 + easeInOutCubic((ghostAge - 0.5) * 2) * 0.35;
+ const strength = Math.sin(Math.min(ghostAge, 1) * Math.PI);
+ const baseX = GHOST_GRAVE.x + 0.06;
+ const baseZ = GHOST_GRAVE.z + 0.06;
+ const sway = height => Math.sin(time * 2.2 - height * 12) * 0.035 * (1 - height / 0.36);
+ for (let index = 0; index < 420; index++) {
+ const pick = hashNoise(index, 9.1, 0.3);
+ const around = hashNoise(index, 3.3, 4.4) * Math.PI * 2;
+ const radial = Math.sqrt(hashNoise(index, 5.5, 6.6));
+ let x;
+ let y;
+ let z;
+ let tone = hex('#7ef2d0');
+ if (pick < 0.18) {
+ x = baseX + Math.cos(around) * 0.034 * radial;
+ y = rise + 0.32 + Math.sin(around * 1.7) * 0.034 * radial;
+ z = baseZ + Math.sin(around) * 0.034 * radial;
+ tone = hex('#c8fff0');
+ } else if (pick < 0.36) {
+ const side = pick < 0.27 ? -1 : 1;
+ const along = hashNoise(index, 7.7, 8.8);
+ // Arms reach forward toward the viewer and droop into long claws.
+ x = baseX + side * (0.028 + along * 0.035) + Math.sin(time * 3 + side) * 0.008 * along;
+ y = rise + 0.27 - along * along * 0.06 + Math.sin(time * 2.4 + along * 4) * 0.01;
+ z = baseZ + along * 0.11 + (hashNoise(index, 1, 9) - 0.5) * 0.012;
+ } else {
+ const height = hashNoise(index, 1.1, 2.2) * 0.3;
+ const width = 0.006 + (height / 0.3) ** 0.7 * 0.045;
+ x = baseX + Math.cos(around) * width * radial + sway(height);
+ y = rise - 0.04 + height;
+ z = baseZ + Math.sin(around) * width * radial * 0.8;
+ if (height < 0.08) tone = hex('#4e8a7a');
+ }
+ ghost.push({ x, y, z, color: tone, alpha: 0.3 * strength, scale: 0.011 });
+ }
+ // Hollow eyes and a long gaping mouth.
+ const face = [[-0.013, 0.33, 0.012], [0.013, 0.33, 0.012], [0, 0.302, 0.011], [0, 0.29, 0.012], [0, 0.278, 0.01]];
+ for (const [offsetX, offsetY, size] of face) {
+ ghost.push({ x: baseX + offsetX, y: rise + offsetY, z: baseZ + 0.03, color: hex('#020406'), alpha: 0.95 * strength, scale: size });
+ }
+ lists.push(makeList(ghost));
+ }
+ // Ash falling through the scene and embers rising from the monk's lantern.
+ const ash = [];
+ for (let flake = 0; flake < ashCount; flake++) {
+ const fall = (time * (0.05 + hashNoise(flake, 1, 1) * 0.05) + hashNoise(flake, 2, 2)) % 1;
+ const angle = hashNoise(flake, 3, 3) * Math.PI * 2;
+ const radius = Math.sqrt(hashNoise(flake, 4, 4)) * 1.5;
+ ash.push({
+ x: Math.cos(angle) * radius + Math.sin(time * 0.6 + flake) * 0.05,
+ y: 1.7 - fall * 2.2,
+ z: Math.sin(angle) * radius + Math.cos(time * 0.5 + flake) * 0.05,
+ color: hex('#c8b4a0'), alpha: 0.6 * Math.min(fall * 6, 1) * Math.min((1 - fall) * 6, 1), scale: 0.01,
+ });
+ }
+ for (let ember = 0; ember < emberCount; ember++) {
+ const age = (time * 0.6 + ember / emberCount) % 1;
+ const origin = monkLantern(time - age / 0.6);
+ ash.push({
+ x: origin[0] + Math.sin(ember * 2 + age * 6) * 0.03 * age,
+ y: origin[1] + age * 0.35,
+ z: origin[2] + Math.cos(ember * 3 + age * 5) * 0.03 * age,
+ color: hex('#ff9a3a'), alpha: 0.9 * (1 - age), scale: 0.006,
+ });
+ }
+ lists.push(makeList(ash));
+ return lists;
+ },
+ glows(time) {
+ const lantern = monkLantern(time);
+ const flicker = 0.85 + 0.15 * hashNoise(Math.floor(time * 10), 1, 1);
+ const glows = [
+ { position: lantern, color: hex('#ffb24a'), radius: 0.24, strength: 0.55 * flicker },
+ { position: [0.8, 0.3, 0.12], color: hex('#ffb24a'), radius: 0.2, strength: 0.42 * flicker },
+ { position: [chapelCenter[0] + 0.3, 0.3, chapelCenter[1]], color: hex('#ff9a3a'), radius: 0.34, strength: 0.36 * flicker },
+ { position: [chapelCenter[0] + 0.3, 0.08, chapelCenter[1]], color: hex('#ff9a3a'), radius: 0.3, strength: 0.42 * flicker },
+ ];
+ const ghostAge = ((time - 3.5) % 11) / 4.5;
+ if (ghostAge > 0 && ghostAge < 1) {
+ const rise = easeOutCubic(ghostAge * 2.2) * 0.3 + easeInOutCubic((ghostAge - 0.5) * 2) * 0.35;
+ glows.push({ position: [GHOST_GRAVE.x + 0.06, rise + 0.25, GHOST_GRAVE.z + 0.06], color: hex('#7ef2d0'), radius: 0.26, strength: 0.25 * Math.sin(ghostAge * Math.PI) });
+ }
+ return glows;
+ },
+ contactShadows(time) {
+ const place = monkPlace(time);
+ return [{ x: place.x, z: place.z, radius: 0.09, strength: 0.4 }];
+ },
+ };
+}
diff --git a/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/medieval2.js b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/medieval2.js
new file mode 100644
index 0000000..787ca09
--- /dev/null
+++ b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/medieval2.js
@@ -0,0 +1,592 @@
+// Two more haunted medieval scenes: a witch's hut in a swamp, and a gallows crossroads where the Reaper waits.
+import {
+ hashNoise, valueNoise, sphereDistance, ellipsoidDistance, boxDistance, cylinderDistance, taperedCylinderDistance,
+ capsuleDistance, torusDistance, smoothUnion, islandDistance, voxelizeModel, placeSplats,
+} from './voxel-splats.js';
+import { clamp01, easeInCubic, easeOutBack, easeInOutCubic, struckSwing, wobble } from './motion.js';
+
+const hex = value => [1, 3, 5].map(start => parseInt(value.slice(start, start + 2), 16) / 255);
+
+function makeList(points) {
+ const count = points.length;
+ const positions = new Float32Array(count * 3);
+ const colors = new Float32Array(count * 4);
+ const scales = new Float32Array(count);
+ points.forEach((point, index) => {
+ positions.set([point.x, point.y, point.z], index * 3);
+ colors.set([point.color[0], point.color[1], point.color[2], point.alpha ?? 1], index * 4);
+ scales[index] = point.scale;
+ });
+ return { count, positions, colors, scales };
+}
+
+function pyramidDistance(x, y, z, centerX, bottomY, centerZ, halfWidth, height) {
+ const amount = Math.min(Math.max((y - bottomY) / height, 0), 1);
+ const half = halfWidth * (1 - amount);
+ const side = (Math.max(Math.abs(x - centerX), Math.abs(z - centerZ)) - half) * 0.85;
+ return Math.max(side, bottomY - y, y - bottomY - height);
+}
+
+function gnarledTree(rootX, rootZ, seed, size) {
+ const branches = [];
+ const grow = (startX, startY, startZ, angle, tilt, length, radius, depth) => {
+ const endX = startX + Math.cos(angle) * Math.sin(tilt) * length;
+ const endY = startY + Math.cos(tilt) * length;
+ const endZ = startZ + Math.sin(angle) * Math.sin(tilt) * length;
+ branches.push([startX, startY, startZ, endX, endY, endZ, radius]);
+ if (depth === 0) return;
+ for (let child = 0; child < 2; child++) {
+ const random = hashNoise(depth * 7 + child + seed * 13, startX * 13, startZ * 11);
+ grow(endX, endY, endZ, angle + (child ? 1 : -1) * (0.5 + random * 1.1), tilt + 0.3 + random * 0.4, length * 0.7, radius * 0.6, depth - 1);
+ }
+ };
+ grow(rootX, 0, rootZ, seed * 2.1, 0.25, 0.3 * size, 0.035 * size, 4);
+ const tips = branches.filter(branch => branch[6] < 0.006 * size + 0.002).map(branch => [branch[3], branch[4], branch[5]]);
+ const distance = (x, y, z) => {
+ let best = Infinity;
+ for (const [startX, startY, startZ, endX, endY, endZ, radius] of branches) {
+ best = Math.min(best, capsuleDistance(x, y, z, startX, startY, startZ, endX, endY, endZ, radius));
+ }
+ return best;
+ };
+ return { distance, tips, branches };
+}
+
+// Points of a flapping bat or crow around its own origin, facing +x.
+function wingedPoints(flap, span, color) {
+ const points = [];
+ for (let body = 0; body < 3; body++) points.push({ x: body * 0.01 - 0.01, y: 0, z: 0, scale: 0.009 * span });
+ for (let side = -1; side <= 1; side += 2) {
+ for (let feather = 1; feather <= 5; feather++) {
+ const reach = feather * 0.013 * span;
+ points.push({ x: -0.004 - feather * 0.003 * span + (feather === 5 ? 0.01 : 0), y: Math.sin(flap) * reach, z: side * Math.cos(flap) * reach, scale: 0.007 * span });
+ }
+ }
+ return points.map(point => ({ ...point, color }));
+}
+
+function placePoints(points, centerX, centerY, centerZ, heading, size = 1) {
+ const cosine = Math.cos(heading);
+ const sine = Math.sin(heading);
+ return points.map(point => ({
+ ...point,
+ x: centerX + (point.x * cosine + point.z * sine) * size,
+ y: centerY + point.y * size,
+ z: centerZ + (-point.x * sine + point.z * cosine) * size,
+ scale: point.scale * size,
+ }));
+}
+
+// ---------------------------------------------------------------------------------------------------------------
+// The witch's swamp.
+// ---------------------------------------------------------------------------------------------------------------
+export function buildSwamp(spacing) {
+ const palette = ['#050806', '#0c140e', '#16241a', '#24382a', '#3a5440', '#5a7a54', '#8aa070', '#c8d0a8',
+ '#2a1e18', '#4a3424', '#6e5034', '#3cff7a', '#a8ffb8', '#ff3a20', '#ff9a3a', '#d8d0c0'].map(hex);
+ const light = { sun: [0.4, 0.6, 0.7], sunColor: [0.95, 1.05, 1.1], ambientColor: [0.42, 0.46, 0.5], rimColor: [0.4, 0.75, 0.55], shadowSoftness: 7 };
+ const poolCenter = [-0.2, 0.15];
+ const poolRadius = 0.4;
+ const hutCenter = [0.36, -0.32];
+ const cauldronCenter = [0.22, 0.42];
+ const groundDistance = (x, y, z) => {
+ const island = islandDistance(x, y, z, 1.06, 1.15, 0, 0.12);
+ const poolCut = cylinderDistance(x, y, z, poolCenter[0], -0.1, poolCenter[1], poolRadius + (valueNoise(x * 8, 0, z * 8) - 0.5) * 0.08, 0.3);
+ return Math.max(island, -poolCut);
+ };
+ const crookedX = (x, y) => x - (y - 0.12) * 0.18;
+ const hutDistance = (x, y, z) => {
+ const leanX = crookedX(x, y);
+ let distance = Infinity;
+ for (const [offsetX, offsetZ] of [[-0.13, -0.11], [0.13, -0.11], [-0.13, 0.11], [0.13, 0.11]]) {
+ distance = Math.min(distance, capsuleDistance(x, y, z, hutCenter[0] + offsetX, -0.02, hutCenter[1] + offsetZ, hutCenter[0] + offsetX + 0.01, 0.13, hutCenter[1] + offsetZ, 0.016));
+ }
+ const floor = boxDistance(x, y, z, hutCenter[0], 0.13, hutCenter[1], 0.19, 0.015, 0.16);
+ const walls = boxDistance(leanX, y, z, hutCenter[0], 0.25, hutCenter[1], 0.16, 0.11, 0.13, 0.01);
+ const roof = pyramidDistance(leanX - (y - 0.36) * 0.12, y, z, hutCenter[0], 0.35, hutCenter[1], 0.23, 0.34);
+ const chimney = capsuleDistance(x, y, z, hutCenter[0] - 0.1, 0.3, hutCenter[1] - 0.06, hutCenter[0] - 0.16, 0.66, hutCenter[1] - 0.04, 0.026);
+ const steps = boxDistance(x, y, z, hutCenter[0] + 0.12, 0.06, hutCenter[1] + 0.22, 0.05, 0.012, 0.06);
+ return Math.min(distance, floor, walls, roof, chimney, steps);
+ };
+ const cauldronDistance = (x, y, z) => {
+ const bowl = Math.max(sphereDistance(x, y, z, cauldronCenter[0], 0.11, cauldronCenter[1], 0.1), y - 0.17);
+ const hollow = Math.max(sphereDistance(x, y, z, cauldronCenter[0], 0.11, cauldronCenter[1], 0.082), y - 0.2);
+ const rim = torusDistance(x, y, z, cauldronCenter[0], 0.17, cauldronCenter[1], 0.08, 0.012);
+ let legs = Infinity;
+ for (let leg = 0; leg < 3; leg++) {
+ const angle = leg * 2.094;
+ legs = Math.min(legs, capsuleDistance(x, y, z, cauldronCenter[0] + Math.cos(angle) * 0.06, 0.04, cauldronCenter[1] + Math.sin(angle) * 0.06, cauldronCenter[0] + Math.cos(angle) * 0.09, -0.01, cauldronCenter[1] + Math.sin(angle) * 0.09, 0.01));
+ }
+ let logs = Infinity;
+ for (let log = 0; log < 4; log++) {
+ const angle = log * 1.57 + 0.4;
+ logs = Math.min(logs, capsuleDistance(x, y, z, cauldronCenter[0] + Math.cos(angle) * 0.13, 0.0, cauldronCenter[1] + Math.sin(angle) * 0.13, cauldronCenter[0] + Math.cos(angle) * 0.02, 0.025, cauldronCenter[1] + Math.sin(angle) * 0.02, 0.014));
+ }
+ const liquid = Math.max(sphereDistance(x, y, z, cauldronCenter[0], 0.11, cauldronCenter[1], 0.085), y - 0.155, 0.13 - y);
+ return Math.min(Math.max(bowl, -hollow), rim, legs, logs, liquid);
+ };
+ const leftTree = gnarledTree(-0.66, -0.36, 1, 1.25);
+ const rightTree = gnarledTree(0.72, 0.36, 2, 1.0);
+ const reedsDistance = (x, y, z) => {
+ if (Math.hypot(x - poolCenter[0], z - poolCenter[1]) > poolRadius + 0.14) return 0.1;
+ let distance = Infinity;
+ for (let reed = 0; reed < 26; reed++) {
+ const angle = reed * 0.61 + hashNoise(reed, 1, 1);
+ const radius = poolRadius - 0.03 + hashNoise(reed, 2, 2) * 0.12;
+ const baseX = poolCenter[0] + Math.cos(angle) * radius;
+ const baseZ = poolCenter[1] + Math.sin(angle) * radius;
+ const height = 0.12 + hashNoise(reed, 3, 3) * 0.14;
+ distance = Math.min(distance, capsuleDistance(x, y, z, baseX, -0.05, baseZ, baseX + 0.02, height, baseZ, 0.006));
+ if (reed % 3 === 0) distance = Math.min(distance, capsuleDistance(x, y, z, baseX + 0.018, height - 0.04, baseZ, baseX + 0.02, height, baseZ, 0.013));
+ }
+ for (const [skullX, skullZ] of [[0.4, 0.62], [0.05, 0.6], [0.52, 0.18]]) {
+ const skull = sphereDistance(x, y, z, skullX, 0.035, skullZ, 0.035);
+ const jaw = boxDistance(x, y, z, skullX + 0.012, 0.01, skullZ, 0.022, 0.012, 0.022);
+ distance = Math.min(distance, smoothUnion(skull, jaw, 0.01));
+ }
+ return distance;
+ };
+ const sceneDistance = (x, y, z) => Math.min(groundDistance(x, y, z), hutDistance(x, y, z), cauldronDistance(x, y, z), leftTree.distance(x, y, z), rightTree.distance(x, y, z));
+ const shadowModel = { distance: sceneDistance };
+ const ground = voxelizeModel({
+ distance: groundDistance,
+ bounds: [-1.12, -1.2, -1.12, 1.12, 0.08, 1.12],
+ material: (x, y, z, normalX, normalY) => {
+ const nearPool = Math.hypot(x - poolCenter[0], z - poolCenter[1]) < poolRadius + 0.06;
+ if (nearPool) return [...hex('#2a1e18'), 0];
+ if (y > -0.06 && normalY > 0.4) {
+ const mud = valueNoise(x * 7, 1, z * 7);
+ return [...(mud > 0.62 ? hex('#4a5a34') : mud > 0.35 ? hex('#34402a') : hex('#3a2c20')), 0];
+ }
+ const strata = valueNoise(x * 3, y * 10, z * 3);
+ return [...(strata > 0.55 ? hex('#4a3424') : hex('#2a1e18')), 0];
+ },
+ }, spacing, light, { shadowModel });
+ const hut = voxelizeModel({
+ distance: hutDistance,
+ bounds: [0.05, -0.04, -0.6, 0.66, 0.72, -0.02],
+ material: (x, y, z) => {
+ const leanX = crookedX(x, y);
+ if (y > 0.35 && !(Math.hypot(x - hutCenter[0] + 0.13, z - hutCenter[1] + 0.05) < 0.05 && y > 0.4)) {
+ const thatch = valueNoise(x * 30, y * 50, z * 30);
+ return [...(thatch > 0.55 ? hex('#6e5034') : thatch > 0.3 ? hex('#4a3a22') : hex('#2e2416')), 0];
+ }
+ if (y > 0.3) return [...hex('#3a3430'), 0];
+ const windowSpot = Math.abs(y - 0.27) < 0.035 && Math.abs(leanX - hutCenter[0]) < 0.045 && z > hutCenter[1] + 0.11;
+ if (windowSpot) return [...hex('#3cff7a'), 1];
+ const doorSpot = Math.abs(z - hutCenter[1] - 0.05) < 0.04 && leanX > hutCenter[0] + 0.14 && y < 0.31;
+ if (doorSpot) return [...hex('#120c08'), 0];
+ const plank = Math.floor(leanX * 60 + z * 60) % 2;
+ return [...(plank ? hex('#6e5034') : hex('#4a3424')), 0];
+ },
+ }, spacing * 0.75, light, { shadowModel });
+ const cauldron = voxelizeModel({
+ distance: cauldronDistance,
+ bounds: [cauldronCenter[0] - 0.16, -0.03, cauldronCenter[1] - 0.16, cauldronCenter[0] + 0.16, 0.2, cauldronCenter[1] + 0.16],
+ material: (x, y, z) => {
+ if (y > 0.13 && y < 0.158 && Math.hypot(x - cauldronCenter[0], z - cauldronCenter[1]) < 0.084) return [...hex('#3cff7a'), 1];
+ if (y < 0.035 && Math.hypot(x - cauldronCenter[0], z - cauldronCenter[1]) > 0.04) return [...hex('#3a281c'), 0];
+ if (y < 0.035) return [...hex('#ff9a3a'), 1];
+ return [...hex('#1c1c20'), 0];
+ },
+ }, spacing * 0.6, light, { shadowModel });
+ const trees = voxelizeModel({
+ distance: (x, y, z) => Math.min(leftTree.distance(x, y, z), rightTree.distance(x, y, z)),
+ bounds: [-1.1, -0.02, -0.95, 1.12, 1.15, 0.95],
+ material: (x, y, z) => [...(valueNoise(x * 30, y * 30, z * 30) > 0.5 ? hex('#2e2620') : hex('#1c1612')), 0],
+ }, spacing * 0.75, light, { shadowModel });
+ const reeds = voxelizeModel({
+ distance: reedsDistance,
+ bounds: [-0.75, -0.06, -0.4, 0.65, 0.3, 0.72],
+ material: (x, y) => (y < 0.06 && y > 0.0 ? [...hex('#d8d0c0'), 0] : [...hex('#3a5440'), 0]),
+ }, spacing * 0.6, light, { shadowModel });
+
+ const charmAnchors = [...leftTree.tips.filter((tip, index) => index % 4 === 1).slice(0, 3), ...rightTree.tips.filter((tip, index) => index % 4 === 2).slice(0, 2)];
+ const waterGrid = [];
+ const waterSpacing = spacing * 1.3;
+ for (let x = -poolRadius; x <= poolRadius; x += waterSpacing) {
+ for (let z = -poolRadius; z <= poolRadius; z += waterSpacing) {
+ if (Math.hypot(x, z) < poolRadius - 0.01) waterGrid.push([poolCenter[0] + x, poolCenter[1] + z]);
+ }
+ }
+ const eyePairs = [[-0.95, 0.05, -0.3], [0.2, 0.0, -0.98], [-0.5, 0.0, 0.85], [0.95, 0.0, -0.2], [-0.25, 0.42, -0.7]];
+ return {
+ name: 'swamp',
+ mist: { color: hex('#7a8a90'), alpha: 0.022, low: -0.25, high: 0.08, wispColor: hex('#3cff7a'), wispHeight: 0.28, trails: true },
+ haze: { color: hex('#10141a'), lift: 0.08, bloom: 0.8, bloomThreshold: 0.6, soften: 0.25, mistTop: 0.65, mistStrength: 0.08, vignette: 1.15 },
+ paletteExtra: ['#2e4834', '#4a6a4e', '#7a9a7a', '#1a3a24', '#7affa0', '#5a3a28'],
+ palette,
+ background: { inner: hex('#1a2622'), outer: hex('#030505'), stars: true, moon: { x: 0.8, y: 0.15, radius: 0.05, color: hex('#d8e8c8'), halo: hex('#3a6a4a') }, clouds: hex('#1e2a26') },
+ camera: { target: [0.05, 0.32, 0.05], distance: 4.3, height: 1.2, startAngle: 0.7 },
+ staticLists: [ground, hut, cauldron, trees, reeds],
+ dynamic(time) {
+ const lists = [];
+ const water = waterGrid.map(([x, z]) => {
+ const ripple = Math.sin(x * 14 + time * 1.2) * Math.sin(z * 12 - time) * 0.5 + 0.5;
+ return { x, y: -0.05 + ripple * 0.004, z, color: ripple > 0.85 ? hex('#8aa070') : ripple > 0.55 ? hex('#3a5440') : hex('#24382a'), alpha: 0.9, scale: waterSpacing * 0.62 };
+ });
+ // Bubbles in the pool and in the cauldron that swell and pop.
+ const bubbles = [];
+ for (let bubble = 0; bubble < 22; bubble++) {
+ const inCauldron = bubble < 12;
+ const period = 1.1 + hashNoise(bubble, 1, 5) * 1.4;
+ const age = ((time + hashNoise(bubble, 2, 5) * period) % period) / period;
+ const angle = hashNoise(bubble, 3, Math.floor((time + hashNoise(bubble, 2, 5) * period) / period)) * Math.PI * 2;
+ const radius = Math.sqrt(hashNoise(bubble, 4, Math.floor(time / period))) * (inCauldron ? 0.07 : poolRadius * 0.85);
+ const center = inCauldron ? cauldronCenter : poolCenter;
+ const size = easeInCubic(age) * (inCauldron ? 0.022 : 0.03);
+ if (age > 0.92) continue;
+ bubbles.push({
+ x: center[0] + Math.cos(angle) * radius, y: (inCauldron ? 0.158 : -0.045) + size * 0.5, z: center[1] + Math.sin(angle) * radius,
+ color: inCauldron ? hex('#a8ffb8') : hex('#5a7a54'), alpha: inCauldron ? 0.9 : 0.7, scale: size,
+ });
+ }
+ // Green fumes from the cauldron and the chimney.
+ const fumes = [];
+ for (let puff = 0; puff < 16; puff++) {
+ const fromChimney = puff >= 10;
+ const period = fromChimney ? 3.2 : 2.4;
+ const age = ((time / period) + puff / (fromChimney ? 6 : 10)) % 1;
+ const originX = fromChimney ? hutCenter[0] - 0.16 : cauldronCenter[0];
+ const originZ = fromChimney ? hutCenter[1] - 0.04 : cauldronCenter[1];
+ const originY = fromChimney ? 0.68 : 0.17;
+ fumes.push({
+ x: originX + wobble(time + puff, puff) * 0.05 * age + age * 0.12,
+ y: originY + easeInOutCubic(age) * 0.55,
+ z: originZ + wobble(time * 0.7, puff + 9) * 0.05 * age,
+ color: fromChimney ? hex('#5a6a5a') : hex('#3cff7a'), alpha: (fromChimney ? 0.35 : 0.3) * (1 - age) * Math.min(age * 6, 1), scale: 0.03 + age * 0.07,
+ });
+ }
+ // Flames licking the cauldron.
+ const flames = [];
+ for (let flame = 0; flame < 14; flame++) {
+ const age = ((time * 2.2) + flame / 14) % 1;
+ const angle = flame * 2.4;
+ flames.push({
+ x: cauldronCenter[0] + Math.cos(angle) * 0.06 * (1 - age), y: 0.01 + age * 0.09, z: cauldronCenter[1] + Math.sin(angle) * 0.06 * (1 - age),
+ color: age < 0.4 ? hex('#ffd88a') : hex('#ff6a2a'), alpha: 1 - age, scale: 0.022 * (1 - age * 0.6),
+ });
+ }
+ // Bone charms on ropes swinging in gusts.
+ const charms = [];
+ charmAnchors.forEach(([anchorX, anchorY, anchorZ], charmIndex) => {
+ const gust = 0.25 * wobble(time * 0.8, charmIndex) + struckSwing(time, 5.5, 0.35, 2.6, 0.7, charmIndex * 0.7);
+ const ropeLength = 0.12 + charmIndex % 2 * 0.06;
+ for (let knot = 0; knot <= 8; knot++) {
+ const along = knot / 8 * ropeLength;
+ charms.push({ x: anchorX + Math.sin(gust) * along, y: anchorY - Math.cos(gust) * along, z: anchorZ, color: hex('#2a1e18'), scale: 0.004 });
+ }
+ const endX = anchorX + Math.sin(gust) * ropeLength;
+ const endY = anchorY - Math.cos(gust) * ropeLength;
+ const doll = [[0, -0.01], [0, -0.03], [0, -0.05], [-0.02, -0.025], [0.02, -0.025], [-0.012, -0.065], [0.012, -0.065], [0, 0.006]];
+ for (const [offsetX, offsetY] of doll) {
+ charms.push({ x: endX + offsetX * Math.cos(gust) - offsetY * Math.sin(gust) * 0, y: endY + offsetY, z: anchorZ, color: hex('#d8d0c0'), scale: 0.008 });
+ }
+ });
+ // Bats flying in jerky loops.
+ const bats = [];
+ for (let bat = 0; bat < 8; bat++) {
+ const phase = bat * 1.9;
+ const angle = time * (0.9 + hashNoise(bat, 1, 1) * 0.6) + phase + wobble(time * 2.2, phase) * 0.8;
+ const radius = 0.55 + hashNoise(bat, 2, 2) * 0.5 + wobble(time * 1.7, phase + 3) * 0.12;
+ const centerY = 0.75 + wobble(time * 2.5, phase + 5) * 0.15 + hashNoise(bat, 3, 3) * 0.3;
+ const flap = Math.sin(time * 22 + phase) * 1.1;
+ bats.push(...placePoints(wingedPoints(flap, 0.9, hex('#06060a')), Math.cos(angle) * radius, centerY, Math.sin(angle) * radius, -angle - Math.PI / 2, 1.5));
+ }
+ // Red eyes in the dark around the edge that blink.
+ const eyes = [];
+ eyePairs.forEach(([eyeX, eyeY, eyeZ], pair) => {
+ const open = Math.sin(time * 0.7 + pair * 2.3) > -0.2 && (time * 2.3 + pair * 0.37) % 3.1 > 0.14;
+ if (!open) return;
+ const sideX = -eyeZ * 0.03;
+ const sideZ = eyeX * 0.03;
+ for (const side of [-1, 1]) {
+ eyes.push({ x: eyeX + sideX * side * 1.3, y: eyeY + 0.05, z: eyeZ + sideZ * side * 1.3, color: hex('#ff3a20'), scale: 0.016 });
+ }
+ });
+ lists.push(makeList(water), makeList(bubbles), makeList(fumes), makeList(flames), makeList(charms), makeList(bats), makeList(eyes));
+ return lists;
+ },
+ glows(time) {
+ const flicker = 0.8 + 0.2 * hashNoise(Math.floor(time * 12), 4, 4);
+ return [
+ { position: [cauldronCenter[0], 0.2, cauldronCenter[1]], color: hex('#3cff7a'), radius: 0.42, strength: 0.55 * flicker },
+ { position: [cauldronCenter[0], 0.03, cauldronCenter[1]], color: hex('#ff8a3a'), radius: 0.22, strength: 0.45 * flicker },
+ { position: [hutCenter[0], 0.27, hutCenter[1] + 0.14], color: hex('#3cff7a'), radius: 0.2, strength: 0.35 * flicker },
+ ...eyePairs.filter((pair, index) => Math.sin(time * 0.7 + index * 2.3) > -0.2).map(([eyeX, eyeY, eyeZ]) => ({ position: [eyeX, eyeY + 0.05, eyeZ], color: hex('#ff3a20'), radius: 0.09, strength: 0.4 })),
+ ];
+ },
+ contactShadows() {
+ return [];
+ },
+ };
+}
+
+// ---------------------------------------------------------------------------------------------------------------
+// The gallows crossroads and the Reaper.
+// ---------------------------------------------------------------------------------------------------------------
+export const REAPER_TIMING = { eyes: 4.4, raise: 5.0, slash: 6.0 };
+export const GALLOWS_BOLTS = [1.1, 6.05];
+
+export function buildGallows(spacing) {
+ const palette = ['#050508', '#0c0c14', '#16161f', '#24242f', '#383846', '#565666', '#7e7e8c', '#b8b8c2',
+ '#2a1e18', '#4a3424', '#6e1018', '#a8202a', '#ff3a20', '#e8e4d8', '#c8d4ff', '#8a96c8'].map(hex);
+ const light = { sun: [-0.5, 0.6, 0.6], sunColor: [0.75, 0.82, 1.1], ambientColor: [0.32, 0.33, 0.44], rimColor: [0.45, 0.5, 0.8], shadowSoftness: 7 };
+ const hill = (x, z) => 0.22 * Math.exp(-(x * x + z * z) / 0.3);
+ const gibbetBase = [-0.22, -0.12];
+ const gibbetTop = 1.12;
+ const armEnd = [0.2, gibbetBase[1]];
+ const groundDistance = (x, y, z) => islandDistance(x, y - hill(x, z), z, 1.08, 1.2, 0, 0.12) * 0.85;
+ const baseY = hill(gibbetBase[0], gibbetBase[1]);
+ const gibbetDistance = (x, y, z) => {
+ const post = boxDistance(x, y, z, gibbetBase[0], (gibbetTop + baseY) / 2, gibbetBase[1], 0.025, (gibbetTop - baseY) / 2 + 0.02, 0.025);
+ const arm = boxDistance(x, y, z, (gibbetBase[0] + armEnd[0]) / 2, gibbetTop, gibbetBase[1], (armEnd[0] - gibbetBase[0]) / 2 + 0.02, 0.02, 0.02);
+ const brace = capsuleDistance(x, y, z, gibbetBase[0], gibbetTop - 0.22, gibbetBase[1], gibbetBase[0] + 0.2, gibbetTop, gibbetBase[1], 0.013);
+ const footing = Math.max(sphereDistance(x, y, z, gibbetBase[0], baseY, gibbetBase[1], 0.09) + (valueNoise(x * 30, y * 30, z * 30) - 0.5) * 0.03, -(y - baseY + 0.05));
+ return Math.min(post, arm, brace, footing);
+ };
+ const pikes = [[0.55, 0.45], [0.7, -0.1], [-0.6, 0.45], [0.15, 0.78]];
+ const propsDistance = (x, y, z) => {
+ let distance = Infinity;
+ for (const [pikeX, pikeZ] of pikes) {
+ const groundY = hill(pikeX, pikeZ);
+ distance = Math.min(distance, capsuleDistance(x, y, z, pikeX, groundY - 0.02, pikeZ, pikeX + 0.01, groundY + 0.34, pikeZ, 0.01));
+ const skull = sphereDistance(x, y, z, pikeX + 0.01, groundY + 0.38, pikeZ, 0.042);
+ const jaw = boxDistance(x, y, z, pikeX + 0.02, groundY + 0.345, pikeZ, 0.026, 0.014, 0.026);
+ distance = Math.min(distance, smoothUnion(skull, jaw, 0.012));
+ }
+ const signGround = hill(-0.55, -0.45);
+ const signPost = capsuleDistance(x, y, z, -0.55, signGround - 0.02, -0.45, -0.52, signGround + 0.4, -0.45, 0.013);
+ const signBoard = boxDistance(x, y, z, -0.47, signGround + 0.33, -0.45, 0.09, 0.025, 0.008, 0.004);
+ const signBoardLow = boxDistance(x, y, z, -0.6, signGround + 0.25, -0.43, 0.008, 0.022, 0.08, 0.004);
+ const cartWheel = Math.max(torusDistance(x, y, z, 0.42, hill(0.42, -0.5) + 0.02, -0.5, 0.1, 0.013), Math.abs(y - hill(0.42, -0.5) - 0.02) - 0.02);
+ return Math.min(distance, signPost, signBoard, signBoardLow, cartWheel);
+ };
+ const sceneDistance = (x, y, z) => Math.min(groundDistance(x, y, z), gibbetDistance(x, y, z), propsDistance(x, y, z));
+ const shadowModel = { distance: sceneDistance };
+ const ground = voxelizeModel({
+ distance: groundDistance,
+ bounds: [-1.14, -1.15, -1.14, 1.14, 0.3, 1.14],
+ material: (x, y, z, normalX, normalY) => {
+ const groundY = hill(x, z);
+ const onPath = (Math.abs(x - z * 0.3) < 0.07 || Math.abs(z + x * 0.2) < 0.06) && y > groundY - 0.05;
+ if (y > groundY - 0.06 && normalY > 0.4) {
+ if (onPath) return [...(valueNoise(x * 30, 1, z * 30) > 0.5 ? hex('#4a3a2e') : hex('#3a2c22')), 0];
+ const grass = valueNoise(x * 9, 3, z * 9);
+ return [...(grass > 0.6 ? hex('#5a5a4a') : grass > 0.3 ? hex('#44443a') : hex('#34322c')), 0];
+ }
+ const strata = valueNoise(x * 3, y * 10, z * 3);
+ return [...(strata > 0.6 ? hex('#565666') : strata > 0.3 ? hex('#383846') : hex('#24242f')), 0];
+ },
+ }, spacing, light, { shadowModel });
+ const gibbet = voxelizeModel({
+ distance: gibbetDistance,
+ bounds: [-0.33, 0.05, -0.23, 0.25, 1.16, -0.01],
+ material: (x, y, z) => (y < baseY + 0.05 && Math.hypot(x - gibbetBase[0], z - gibbetBase[1]) > 0.03
+ ? [...hex('#565666'), 0]
+ : [...(valueNoise(x * 6, y * 60, z * 6) > 0.5 ? hex('#4a3424') : hex('#2a1e18')), 0]),
+ }, spacing * 0.7, light, { shadowModel });
+ const props = voxelizeModel({
+ distance: propsDistance,
+ bounds: [-0.72, -0.05, -0.65, 0.8, 0.72, 0.85],
+ material: (x, y, z) => {
+ for (const [pikeX, pikeZ] of pikes) {
+ const groundY = hill(pikeX, pikeZ);
+ if (Math.hypot(x - pikeX - 0.01, y - groundY - 0.38, z - pikeZ) < 0.05 || (Math.abs(y - groundY - 0.345) < 0.02 && Math.hypot(x - pikeX, z - pikeZ) < 0.04)) {
+ const socket = Math.abs(y - groundY - 0.385) < 0.012 && Math.abs(Math.atan2(z - pikeZ, x - pikeX - 0.01)) > 0.0 && valueNoise(x * 90, y * 90, z * 90) > 0.62;
+ return [...(socket ? hex('#0c0c14') : hex('#d8d2c2')), 0];
+ }
+ }
+ return [...(valueNoise(x * 40, y * 40, z * 40) > 0.5 ? hex('#4a3424') : hex('#2a1e18')), 0];
+ },
+ }, spacing * 0.6, light, { shadowModel });
+
+ // Cage hanging from the arm, built around its hinge at the origin.
+ const cage = voxelizeModel({
+ distance: (x, y, z) => {
+ const chain = capsuleDistance(x, y, z, 0, 0, 0, 0, -0.1, 0, 0.006);
+ const top = torusDistance(x, y, z, 0, -0.11, 0, 0.07, 0.007);
+ const bottom = torusDistance(x, y, z, 0, -0.4, 0, 0.07, 0.007);
+ let bars = Infinity;
+ for (let bar = 0; bar < 8; bar++) {
+ const angle = bar * Math.PI / 4;
+ bars = Math.min(bars, capsuleDistance(x, y, z, Math.cos(angle) * 0.07, -0.11, Math.sin(angle) * 0.07, Math.cos(angle) * 0.075, -0.4, Math.sin(angle) * 0.075, 0.005));
+ }
+ const skull = sphereDistance(x, y, z, 0.0, -0.17, 0, 0.034);
+ const spine = capsuleDistance(x, y, z, 0, -0.2, 0, 0.005, -0.33, 0, 0.008);
+ let ribs = Infinity;
+ for (let rib = 0; rib < 4; rib++) ribs = Math.min(ribs, Math.max(torusDistance(x, y, z, 0.0, -0.23 - rib * 0.024, 0, 0.032 - rib * 0.002, 0.004), -x));
+ const armLeft = capsuleDistance(x, y, z, 0, -0.22, 0.03, 0.03, -0.32, 0.045, 0.007);
+ const armRight = capsuleDistance(x, y, z, 0, -0.22, -0.03, 0.04, -0.3, -0.045, 0.007);
+ const legs = Math.min(capsuleDistance(x, y, z, 0, -0.33, 0.015, 0.02, -0.39, 0.025, 0.008), capsuleDistance(x, y, z, 0, -0.33, -0.015, 0.03, -0.38, -0.03, 0.008));
+ return Math.min(chain, top, bottom, bars, skull, spine, ribs, armLeft, armRight, legs);
+ },
+ bounds: [-0.09, -0.42, -0.09, 0.09, 0.01, 0.09],
+ material: (x, y, z) => {
+ const inside = Math.hypot(x, z) < 0.06 && y < -0.13;
+ if (inside) {
+ const socket = y > -0.18 && y < -0.16 && x > 0.022 && Math.abs(Math.abs(z) - 0.012) < 0.008;
+ return [...(socket ? hex('#050508') : hex('#d8d2c2')), 0];
+ }
+ return [...hex('#2a2a32'), 0];
+ },
+ }, spacing * 0.45, light);
+
+ // The Reaper, facing +x, origin on the ground.
+ const reaper = voxelizeModel({
+ distance: (x, y, z) => {
+ const tatter = (valueNoise(x * 40, y * 10, z * 40) - 0.5) * 0.03 + Math.max(0.04 - y, 0) * (valueNoise(Math.atan2(z, x) * 6, 0, 1) > 0.5 ? 1 : 0) * 1.5;
+ const robe = taperedCylinderDistance(x, y, z, 0, 0, 0, 0.12, 0.06, 0.46) + tatter;
+ const shoulders = ellipsoidDistance(x, y, z, 0, 0.44, 0, 0.07, 0.05, 0.1);
+ const hood = Math.min(ellipsoidDistance(x, y, z, -0.005, 0.53, 0, 0.065, 0.075, 0.06), taperedCylinderDistance(x, y, z, -0.03, 0.55, 0, 0.05, 0.0, 0.11));
+ const face = ellipsoidDistance(x, y, z, 0.05, 0.52, 0, 0.03, 0.05, 0.04);
+ const armBack = capsuleDistance(x, y, z, 0, 0.43, -0.08, 0.06, 0.34, -0.1, 0.028);
+ const skull = ellipsoidDistance(x, y, z, 0.028, 0.515, 0, 0.036, 0.043, 0.032);
+ return Math.min(Math.max(smoothUnion(Math.min(robe, shoulders), hood, 0.02), -face), armBack, skull);
+ },
+ bounds: [-0.15, -0.02, -0.15, 0.15, 0.68, 0.15],
+ material: (x, y, z) => {
+ const insideSkull = Math.hypot((x - 0.028) / 0.036, (y - 0.515) / 0.043, z / 0.032) < 1.2;
+ if (insideSkull) {
+ const socket = y > 0.515 && y < 0.54 && Math.abs(Math.abs(z) - 0.014) < 0.009 && x > 0.04;
+ const teeth = y < 0.49 && x > 0.045 && Math.floor(z * 160) % 2 === 0;
+ return [...(socket ? hex('#050508') : teeth ? hex('#8a8478') : hex('#e8e2d2')), socket ? 0 : 0.3];
+ }
+ if (y > 0.47 && x > 0.035) return [...hex('#050508'), 0];
+ return [...(y < 0.06 ? hex('#16161f') : hex('#24242f')), 0];
+ },
+ }, spacing * 0.6, light);
+ // Scythe built around the hand that holds it at the origin: staff along +y, blade at the top curving to -x... +x.
+ const scythe = voxelizeModel({
+ distance: (x, y, z) => {
+ const staff = capsuleDistance(x, y, z, 0, -0.3, 0, 0.02, 0.4, 0, 0.011);
+ let blade = Infinity;
+ for (let segment = 0; segment < 7; segment++) {
+ const from = segment / 7;
+ const to = (segment + 1) / 7;
+ const point = amount => [0.02 + Math.sin(amount * 1.6) * 0.36, 0.4 + Math.sin(amount * 3.0) * 0.07 - amount * 0.1, 0];
+ const [startX, startY] = point(from);
+ const [endX, endY] = point(to);
+ blade = Math.min(blade, capsuleDistance(x, y, z, startX, startY, 0, endX, endY, 0, 0.016 * (1 - from * 0.8)));
+ }
+ return Math.min(staff, Math.max(blade, Math.abs(z) - 0.006));
+ },
+ bounds: [-0.04, -0.32, -0.04, 0.42, 0.5, 0.04],
+ material: (x, y) => (y > 0.3 && x > 0.04 ? [...hex('#d8dce8'), 0.35] : [...hex('#2a1e18'), 0]),
+ }, spacing * 0.45, light);
+
+ const reaperSpot = [0.32, 0.22];
+ const reaperGround = hill(reaperSpot[0], reaperSpot[1]);
+ const startAngle = 0.95;
+ const reaperHeading = startAngle - Math.PI / 2 + 0.25;
+ const scytheAngle = time => {
+ const { raise, slash } = REAPER_TIMING;
+ const rest = -0.15;
+ const raised = 1.25;
+ const struck = -1.1;
+ if (time < raise) return rest + Math.sin(time * 0.8) * 0.04;
+ if (time < slash) return rest + (raised - rest) * easeInOutCubic((time - raise) / (slash - raise - 0.15));
+ return raised + (struck - raised) * easeOutBack((time - slash) / 0.28, 1.2);
+ };
+ return {
+ name: 'gallows',
+ mist: { color: hex('#7a7e96'), alpha: 0.035, low: -0.2, high: 0.2, wispColor: hex('#c8d4ff'), wispHeight: 0.35, trails: true },
+ haze: { color: hex('#12121c'), lift: 0.14, bloom: 0.85, bloomThreshold: 0.58, soften: 0.3, mistTop: 0.6, mistStrength: 0.1, vignette: 1.2 },
+ paletteExtra: ['#4a4a5a', '#6a6a7c', '#9a9aaa', '#3a1018', '#d84040', '#d8d2c2'],
+ palette,
+ background: { inner: hex('#1a1a28'), outer: hex('#030306'), stars: true, moon: { x: 0.2, y: 0.12, radius: 0.04, color: hex('#e0c8c0'), halo: hex('#6a3a40') }, clouds: hex('#22222e') },
+ camera: { target: [0.05, 0.5, 0.05], distance: 4.3, height: 1.15, startAngle },
+ staticLists: [ground, gibbet, props],
+ dynamic(time) {
+ const lists = [];
+ const swing = 0.22 * wobble(time * 0.7, 3) + struckSwing(time, 7, 0.3, 2.2, 0.35, 0.8);
+ lists.push(placeSplats(cage, {
+ x: armEnd[0] - 0.03, y: gibbetTop - 0.02, z: armEnd[1],
+ deform: (x, y, z) => [x * Math.cos(swing) - y * Math.sin(swing), x * Math.sin(swing) + y * Math.cos(swing), z + Math.sin(time * 0.9) * 0.01 * -y],
+ }));
+ // The Reaper hovers, leans in as he raises the scythe.
+ const hover = 0.02 + Math.sin(time * 1.3) * 0.012;
+ const lean = easeInOutCubic((time - REAPER_TIMING.raise) / 1.0) * 0.12;
+ const reaperY = reaperGround + hover;
+ lists.push(placeSplats(reaper, {
+ x: reaperSpot[0], y: reaperY, z: reaperSpot[1], heading: reaperHeading,
+ deform: (x, y, z) => [x + y * lean * 0.6 + Math.sin(time * 2 + y * 9) * 0.006 * Math.max(0.1 - y, 0) * 10, y, z + Math.sin(time * 1.7 + y * 12) * 0.008 * Math.max(0.12 - y, 0) * 8],
+ }));
+ const handLocal = [0.08, 0.36, 0.1];
+ const angle = scytheAngle(time);
+ const cosine = Math.cos(reaperHeading);
+ const sine = Math.sin(reaperHeading);
+ const handWorld = [reaperSpot[0] + handLocal[0] * cosine + handLocal[2] * sine, reaperY + handLocal[1], reaperSpot[1] - handLocal[0] * sine + handLocal[2] * cosine];
+ lists.push(placeSplats(scythe, {
+ x: handWorld[0], y: handWorld[1], z: handWorld[2], heading: reaperHeading,
+ deform: (x, y, z) => [z, x * Math.sin(angle) + y * Math.cos(angle), -(x * Math.cos(angle) - y * Math.sin(angle))],
+ }));
+ // Eyes ignite.
+ const eyeGlow = clamp01((time - REAPER_TIMING.eyes) / 0.5);
+ if (eyeGlow > 0) {
+ const eyes = [];
+ for (const side of [-1, 1]) {
+ const localX = 0.066;
+ const localZ = side * 0.014;
+ eyes.push({ x: reaperSpot[0] + localX * cosine + localZ * sine, y: reaperY + 0.527, z: reaperSpot[1] - localX * sine + localZ * cosine, color: hex('#ff3a20'), alpha: eyeGlow, scale: 0.0065 });
+ }
+ lists.push(makeList(eyes));
+ }
+ // Ravens on the arm, pecking.
+ const ravens = [];
+ for (let raven = 0; raven < 3; raven++) {
+ const ravenX = gibbetBase[0] + 0.06 + raven * 0.09;
+ const peck = Math.max(0, Math.sin(time * 2.4 + raven * 2)) ** 6 * 0.02;
+ for (const [offsetX, offsetY, scale] of [[0, 0.04, 0.022], [0.018, 0.05, 0.018], [0.036, 0.062 - peck, 0.014], [0.048, 0.06 - peck, 0.008], [-0.03, 0.032, 0.014]]) {
+ ravens.push({ x: ravenX + offsetX, y: gibbetTop + offsetY, z: gibbetBase[1], color: hex('#0a0a10'), scale });
+ }
+ }
+ lists.push(makeList(ravens));
+ // A lightning bolt in the back when the sky flashes.
+ const bolt = [];
+ for (const boltTime of GALLOWS_BOLTS) {
+ const age = time - boltTime;
+ if (age < 0 || age > 0.22) continue;
+ const fade = 1 - age / 0.22;
+ const strike = (startX, startY, startZ, steps, stepLength, width, seed) => {
+ let boltX = startX;
+ let boltY = startY;
+ let boltZ = startZ;
+ for (let step = 0; step < steps; step++) {
+ const nextX = boltX + (hashNoise(step, boltTime, seed) - 0.5) * stepLength * 2.2;
+ const nextY = boltY - stepLength;
+ const nextZ = boltZ + (hashNoise(step, boltTime, seed + 1) - 0.5) * stepLength;
+ for (let fill = 0; fill < 4; fill++) {
+ const amount = fill / 4;
+ const x = boltX + (nextX - boltX) * amount;
+ const y = boltY + (nextY - boltY) * amount;
+ const z = boltZ + (nextZ - boltZ) * amount;
+ bolt.push({ x, y, z, color: hex('#ffffff'), alpha: fade, scale: width });
+ bolt.push({ x, y, z, color: hex('#8a96ff'), alpha: fade * 0.35, scale: width * 3 });
+ }
+ if (seed < 10 && step % 7 === 3) strike(nextX, nextY, nextZ, 6, stepLength * 0.8, width * 0.6, seed + 10 + step);
+ boltX = nextX;
+ boltY = nextY;
+ boltZ = nextZ;
+ }
+ };
+ strike(-0.75, 2.4, -1.3, 34, 0.075, 0.016, 1);
+ }
+ if (bolt.length) lists.push(makeList(bolt));
+ return lists;
+ },
+ glows(time) {
+ const glows = [];
+ const eyeGlow = clamp01((time - REAPER_TIMING.eyes) / 0.5);
+ if (eyeGlow > 0) glows.push({ position: [reaperSpot[0], reaperGround + 0.55, reaperSpot[1]], color: hex('#ff3a20'), radius: 0.07, strength: 0.16 * eyeGlow * (0.85 + 0.15 * Math.sin(time * 9)) });
+ for (const boltTime of GALLOWS_BOLTS) {
+ const age = time - boltTime;
+ if (age >= 0 && age < 0.25) glows.push({ position: [-0.75, 1.3, -1.3], color: hex('#c8d4ff'), radius: 1.5, strength: 0.8 * (1 - age / 0.25) });
+ }
+ return glows;
+ },
+ contactShadows(time) {
+ return [{ x: reaperSpot[0], z: reaperSpot[1], radius: 0.18, strength: 0.4 }];
+ },
+ reaperFace: () => [reaperSpot[0], reaperGround + 0.55, reaperSpot[1]],
+ };
+}
diff --git a/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/motion.js b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/motion.js
new file mode 100644
index 0000000..30d3112
--- /dev/null
+++ b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/motion.js
@@ -0,0 +1,31 @@
+// Timing helpers so motion has rhythm: easing, walk-then-pause steps, struck-and-decaying swings, smooth noise.
+export const clamp01 = value => Math.min(Math.max(value, 0), 1);
+export const easeInOutCubic = value => {
+ const amount = clamp01(value);
+ return amount < 0.5 ? 4 * amount * amount * amount : 1 - Math.pow(-2 * amount + 2, 3) / 2;
+};
+export const easeOutCubic = value => 1 - Math.pow(1 - clamp01(value), 3);
+export const easeInCubic = value => Math.pow(clamp01(value), 3);
+export const easeOutBack = (value, overshoot = 1.7) => {
+ const amount = clamp01(value) - 1;
+ return 1 + (overshoot + 1) * amount * amount * amount + overshoot * amount * amount;
+};
+
+// Moves for `moveShare` of each period with an ease in and out, then holds still: returns the progress and whether moving.
+export function stepAndHold(time, period, moveShare) {
+ const cycles = time / period;
+ const whole = Math.floor(cycles);
+ const within = (cycles - whole) / moveShare;
+ return { progress: whole + easeInOutCubic(within), moving: within > 0 && within < 1, within: clamp01(within) };
+}
+
+// A pendulum struck every `period` seconds that rings down.
+export function struckSwing(time, period, amplitude, frequency, damping, offset = 0) {
+ const since = ((time + offset) % period + period) % period;
+ return amplitude * Math.exp(-since * damping) * Math.sin(since * frequency);
+}
+
+// Smooth 1D noise from a few sines, in about -1..1.
+export function wobble(time, seed) {
+ return (Math.sin(time * 0.71 + seed * 3.1) * 0.5 + Math.sin(time * 1.37 + seed * 1.7) * 0.3 + Math.sin(time * 2.83 + seed * 5.3) * 0.2);
+}
diff --git a/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/package.json b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/package.json
new file mode 100644
index 0000000..83841be
--- /dev/null
+++ b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/package.json
@@ -0,0 +1,8 @@
+{
+ "name": "pixel-splats",
+ "private": true,
+ "type": "module",
+ "dependencies": {
+ "@codepawlhq/67js": "^0.19.0"
+ }
+}
diff --git a/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/page.js b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/page.js
new file mode 100644
index 0000000..fd5c00e
--- /dev/null
+++ b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/page.js
@@ -0,0 +1,404 @@
+// Page side of the renderer: builds a scene, draws its splats with 67js at a low internal size, then turns the frame
+// into pixel art (palette, ordered dither, glow halos) and scales it up with nearest-neighbour sampling.
+import { Renderer67 } from '/node_modules/@codepawlhq/67js/build/67.module.js';
+import { attachGaussianSplats, createGaussianSplats } from '/node_modules/@codepawlhq/67js/build/gaussian-splats.js';
+import { packSplatData, hashNoise, valueNoise } from './voxel-splats.js';
+import { SCENE_BUILDERS } from './scenes.js';
+
+const BAYER = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5].map(value => (value + 0.5) / 16);
+
+const state = { scene: null, sceneKey: '', renderer: null, rendererSize: 0, canvas: null };
+
+function getRenderer(size) {
+ if (state.renderer && state.rendererSize === size) return state.renderer;
+ if (state.renderer) state.renderer.dispose();
+ state.canvas?.remove();
+ const canvas = document.createElement('canvas');
+ canvas.style.width = `${size}px`;
+ canvas.style.height = `${size}px`;
+ document.body.appendChild(canvas);
+ state.canvas = canvas;
+ state.renderer = new Renderer67(canvas, [], { ground: false, transparent: true, pixelRatio: 1, samples: 1, memoryBudgetMiB: 512 });
+ state.rendererSize = size;
+ return state.renderer;
+}
+
+window.setupScene = function setupScene(sceneName, spacing) {
+ const key = `${sceneName}:${spacing}`;
+ if (state.sceneKey === key) return state.scene.splatCount;
+ const started = performance.now();
+ const scene = SCENE_BUILDERS[sceneName](spacing);
+ const toColor = value => [1, 3, 5].map(start => parseInt(value.slice(start, start + 2), 16) / 255);
+ if (scene.paletteExtra) scene.palette = [...scene.palette, ...scene.paletteExtra.map(toColor)];
+ let minimumY = Infinity;
+ let maximumY = -Infinity;
+ for (const list of scene.staticLists) {
+ for (let index = 0; index < list.count; index++) {
+ const y = list.positions[index * 3 + 1];
+ minimumY = Math.min(minimumY, y);
+ maximumY = Math.max(maximumY, y);
+ }
+ }
+ scene.heightRange = [minimumY, maximumY];
+ scene.splatCount = scene.staticLists.reduce((sum, list) => sum + list.count, 0);
+ scene.buildMilliseconds = performance.now() - started;
+ state.scene = scene;
+ state.sceneKey = key;
+ return scene.splatCount;
+};
+
+// Mist made of big faint splats drifting around the island, and a few wandering wisp lights.
+function atmosphereSplats(scene, time, fade) {
+ const mist = scene.mist;
+ if (!mist || fade <= 0) return { lists: [], glows: [] };
+ const mistCount = 320;
+ const positions = new Float32Array(mistCount * 3);
+ const colors = new Float32Array(mistCount * 4);
+ const scales = new Float32Array(mistCount);
+ for (let index = 0; index < mistCount; index++) {
+ const radiusRandom = hashNoise(index, 1.3, 7.1);
+ const radius = 0.75 + radiusRandom * 1.3;
+ const speed = (0.03 + hashNoise(index, 2.2, 3.3) * 0.06) * (hashNoise(index, 9, 9) > 0.5 ? 1 : -1);
+ const angle = hashNoise(index, 4.4, 1.1) * Math.PI * 2 + time * speed;
+ const height = mist.low + hashNoise(index, 5.5, 2.2) * (mist.high - mist.low) + Math.sin(time * 0.3 + index) * 0.04;
+ positions.set([Math.cos(angle) * radius, height, Math.sin(angle) * radius], index * 3);
+ const breathe = 0.6 + 0.4 * Math.sin(time * 0.4 + index * 1.7);
+ colors.set([...mist.color, mist.alpha * breathe * fade * (0.5 + hashNoise(index, 6, 6))], index * 4);
+ scales[index] = 0.08 + hashNoise(index, 7.7, 8.8) * 0.16;
+ }
+ const wispCount = 7;
+ const trailLength = mist.trails ? 10 : 1;
+ const wispPositions = new Float32Array(wispCount * trailLength * 3);
+ const wispColors = new Float32Array(wispCount * trailLength * 4);
+ const wispScales = new Float32Array(wispCount * trailLength);
+ const glows = [];
+ const wispAt = (phase, sampleTime) => [
+ Math.sin(sampleTime * 0.13 + phase) * 1.1 + Math.sin(sampleTime * 0.31 + phase * 2) * 0.25,
+ mist.wispHeight + Math.sin(sampleTime * 0.5 + phase) * 0.18,
+ Math.cos(sampleTime * 0.11 + phase * 1.3) * 1.1 + Math.cos(sampleTime * 0.27 + phase) * 0.25,
+ ];
+ for (let wisp = 0; wisp < wispCount; wisp++) {
+ const phase = hashNoise(wisp, 3.1, 4.7) * 100;
+ const flicker = (0.55 + 0.45 * Math.sin(time * 1.7 + phase * 3)) * fade;
+ for (let step = 0; step < trailLength; step++) {
+ const slot = wisp * trailLength + step;
+ wispPositions.set(wispAt(phase, time - step * 0.12), slot * 3);
+ const tail = 1 - step / trailLength;
+ wispColors.set([...mist.wispColor, flicker * (step === 0 ? 1 : 0.45 * tail)], slot * 4);
+ wispScales[slot] = step === 0 ? 0.016 : 0.012 * tail + 0.004;
+ }
+ glows.push({ position: wispAt(phase, time), color: mist.wispColor, radius: 0.13, strength: 0.22 * flicker });
+ }
+ const wispTotal = wispCount * trailLength;
+ return {
+ lists: [{ count: mistCount, positions, colors, scales }, { count: wispTotal, positions: wispPositions, colors: wispColors, scales: wispScales }],
+ glows,
+ };
+}
+
+const easeOutBack = amount => 1 + 2.2 * Math.pow(amount - 1, 3) + 1.2 * Math.pow(amount - 1, 2);
+
+// Moves, fades and grows static splats for the build-up and break-apart moments, and darkens ground under movers.
+function animateStatic(scene, time, { assemble = 1, dissolve = 0 }) {
+ const shadows = scene.contactShadows(time);
+ const [minimumY, maximumY] = scene.heightRange;
+ const range = maximumY - minimumY;
+ return scene.staticLists.map((list, listIndex) => {
+ if (assemble >= 1 && dissolve <= 0 && shadows.length === 0) return list;
+ const positions = new Float32Array(list.positions);
+ const colors = new Float32Array(list.colors);
+ const scales = new Float32Array(list.scales);
+ for (let index = 0; index < list.count; index++) {
+ const x = positions[index * 3];
+ const y = positions[index * 3 + 1];
+ const z = positions[index * 3 + 2];
+ for (const shadow of shadows) {
+ const distance = Math.hypot(x - shadow.x, z - shadow.z);
+ if (distance < shadow.radius && y > -0.08 && y < 0.06) {
+ const darken = 1 - shadow.strength * Math.pow(1 - distance / shadow.radius, 0.7);
+ colors[index * 4] *= darken;
+ colors[index * 4 + 1] *= darken;
+ colors[index * 4 + 2] *= darken * 1.04;
+ }
+ }
+ const random = hashNoise(index * 0.37 + listIndex * 13.1, x * 3.1, z * 2.7);
+ const randomSecond = hashNoise(index * 0.11, listIndex + 4.2, y * 5.3);
+ if (assemble < 1) {
+ const delay = ((y - minimumY) / range) * 0.55 + random * 0.2;
+ const progress = Math.min(Math.max((assemble - delay) / 0.25, 0), 1);
+ const settle = easeOutBack(progress);
+ const scatterAngle = randomSecond * Math.PI * 2;
+ const lift = (1 - settle) * (1.6 + random * 1.2);
+ positions[index * 3] = x + Math.cos(scatterAngle) * (1 - settle) * 0.9;
+ positions[index * 3 + 1] = y + lift;
+ positions[index * 3 + 2] = z + Math.sin(scatterAngle) * (1 - settle) * 0.9;
+ colors[index * 4 + 3] *= progress > 0 ? Math.min(progress * 3, 1) : 0;
+ scales[index] *= 1 + (1 - progress) * 0.5;
+ }
+ if (dissolve > 0) {
+ const delay = random * 0.45 + ((maximumY - y) / range) * 0.25;
+ const progress = Math.min(Math.max((dissolve - delay) / 0.35, 0), 1);
+ const push = progress * progress;
+ const outward = Math.hypot(x, z) || 1;
+ positions[index * 3] = x + (x / outward) * push * 1.3 + Math.cos(randomSecond * 6.28) * push * 0.5;
+ positions[index * 3 + 1] = y + push * (1.2 + random * 1.5);
+ positions[index * 3 + 2] = z + (z / outward) * push * 1.3 + Math.sin(randomSecond * 6.28) * push * 0.5;
+ colors[index * 4 + 3] *= 1 - progress;
+ scales[index] *= 1 + progress * 2.5;
+ }
+ }
+ return { count: list.count, positions, colors, scales };
+ });
+}
+
+function cameraFor(scene, time, { angleOffset = 0, zoom = 1, orbitSpeed = 0.12, heightOffset = 0, targetOffset = [0, 0, 0] }) {
+ const { target, distance, height, startAngle } = scene.camera;
+ const angle = startAngle + time * orbitSpeed + angleOffset;
+ const finalTarget = [target[0] + targetOffset[0], target[1] + targetOffset[1], target[2] + targetOffset[2]];
+ return {
+ eye: [finalTarget[0] + Math.sin(angle) * distance * zoom, finalTarget[1] + (height + heightOffset) * zoom, finalTarget[2] + Math.cos(angle) * distance * zoom],
+ target: finalTarget,
+ };
+}
+
+function project(renderer, point, size) {
+ const view = renderer.view;
+ const projection = renderer.projection;
+ const viewSpace = [0, 1, 2, 3].map(row => view[row] * point[0] + view[4 + row] * point[1] + view[8 + row] * point[2] + view[12 + row]);
+ const clip = [0, 1, 2, 3].map(row => projection[row] * viewSpace[0] + projection[4 + row] * viewSpace[1] + projection[8 + row] * viewSpace[2] + projection[12 + row] * viewSpace[3]);
+ if (clip[3] <= 0) return null;
+ return {
+ x: (clip[0] / clip[3] * 0.5 + 0.5) * size,
+ y: (1 - (clip[1] / clip[3] * 0.5 + 0.5)) * size,
+ pixelsPerUnit: projection[5] * size * 0.5 / clip[3],
+ };
+}
+
+function boxBlur(source, size, radius) {
+ const horizontal = new Float32Array(source.length);
+ const result = new Float32Array(source.length);
+ const passes = [[source, horizontal, 1, 0], [horizontal, result, 0, 1]];
+ for (const [from, to, stepX, stepY] of passes) {
+ for (let row = 0; row < size; row++) {
+ for (let column = 0; column < size; column++) {
+ const sum = [0, 0, 0];
+ let count = 0;
+ for (let offset = -radius; offset <= radius; offset++) {
+ const sampleX = column + offset * stepX;
+ const sampleY = row + offset * stepY;
+ if (sampleX < 0 || sampleY < 0 || sampleX >= size || sampleY >= size) continue;
+ const index = (sampleY * size + sampleX) * 3;
+ sum[0] += from[index];
+ sum[1] += from[index + 1];
+ sum[2] += from[index + 2];
+ count++;
+ }
+ const target = (row * size + column) * 3;
+ to[target] = sum[0] / count;
+ to[target + 1] = sum[1] / count;
+ to[target + 2] = sum[2] / count;
+ }
+ }
+ }
+ return result;
+}
+
+// Soft bloom on the bright parts, lifted blacks towards the fog colour, a low mist band and a vignette.
+function applyDreamyHaze(buffer, size, haze) {
+ if (!haze) return;
+ const bright = new Float32Array(buffer.length);
+ for (let index = 0; index < buffer.length; index++) bright[index] = Math.max(buffer[index] - haze.bloomThreshold, 0);
+ const radius = Math.max(2, Math.round(size / 36));
+ const bloom = boxBlur(boxBlur(bright, size, radius), size, radius);
+ const softened = boxBlur(buffer, size, 1);
+ for (let row = 0; row < size; row++) {
+ for (let column = 0; column < size; column++) {
+ const index = (row * size + column) * 3;
+ const vertical = row / size;
+ const mistBand = Math.max(0, Math.min(1, (vertical - haze.mistTop) / 0.35)) * haze.mistStrength;
+ const edge = Math.hypot(column / size - 0.5, row / size - 0.5);
+ const vignette = 1 - Math.max(edge - 0.32, 0) * haze.vignette;
+ for (let channel = 0; channel < 3; channel++) {
+ let value = buffer[index + channel] * (1 - haze.soften) + softened[index + channel] * haze.soften;
+ value += bloom[index + channel] * haze.bloom;
+ value = value * (1 - haze.lift) + haze.color[channel] * haze.lift;
+ value = value * (1 - mistBand) + haze.color[channel] * 1.4 * mistBand;
+ buffer[index + channel] = value * vignette;
+ }
+ }
+ }
+}
+
+function expandPalette(palette, steps) {
+ if (steps <= 1) return palette;
+ const expanded = [...palette];
+ for (let first = 0; first < palette.length; first++) {
+ for (let second = first + 1; second < palette.length; second++) {
+ const distance = Math.hypot(...palette[first].map((value, channel) => value - palette[second][channel]));
+ if (distance > 0.32) continue;
+ for (let step = 1; step < steps; step++) {
+ const amount = step / steps;
+ expanded.push(palette[first].map((value, channel) => value + (palette[second][channel] - value) * amount));
+ }
+ }
+ }
+ return expanded;
+}
+
+function postProcess(scene, renderer, time, internalSize, style, outputSize, fadeToBlack, flash = 0) {
+ const reader = document.createElement('canvas');
+ reader.width = internalSize;
+ reader.height = internalSize;
+ const readerContext = reader.getContext('2d', { willReadFrequently: true });
+ readerContext.drawImage(state.canvas, 0, 0, internalSize, internalSize);
+ const image = readerContext.getImageData(0, 0, internalSize, internalSize);
+ const pixels = image.data;
+ const glows = [...scene.glows(time), ...(scene.extraGlows ?? [])].map(glow => {
+ const screen = project(renderer, glow.position, internalSize);
+ return screen && { ...glow, screenX: screen.x, screenY: screen.y, radiusPixels: glow.radius * screen.pixelsPerUnit };
+ }).filter(Boolean);
+ const palette = style.palette === 'none' ? null : expandPalette(scene.palette, style.paletteSteps ?? 1);
+ const { inner, outer, stars, moon, clouds } = scene.background;
+ const pixelScale = internalSize / 216;
+ const outlineColor = scene.palette[0].map(value => value * 0.6);
+ const isOutline = new Uint8Array(internalSize * internalSize);
+ if (style.outline) {
+ const solid = index => pixels[index * 4 + 3] > 150;
+ for (let row = 1; row < internalSize - 1; row++) {
+ for (let column = 1; column < internalSize - 1; column++) {
+ const index = row * internalSize + column;
+ if (pixels[index * 4 + 3] > 90) continue;
+ if (solid(index - 1) || solid(index + 1) || solid(index - internalSize) || solid(index + internalSize)) isOutline[index] = 1;
+ }
+ }
+ }
+ const buffer = new Float32Array(internalSize * internalSize * 3);
+ for (let row = 0; row < internalSize; row++) {
+ for (let column = 0; column < internalSize; column++) {
+ const pixelIndex = row * internalSize + column;
+ const offset = pixelIndex * 4;
+ const alpha = pixels[offset + 3] / 255;
+ const centerDistance = Math.min(Math.hypot(column / internalSize - 0.5, row / internalSize - 0.42) * 1.6, 1);
+ const color = [0, 1, 2].map(channel => {
+ const background = inner[channel] + (outer[channel] - inner[channel]) * centerDistance;
+ return (pixels[offset + channel] / 255) * alpha + background * (1 - alpha);
+ });
+ if (stars && alpha < 0.2) {
+ const starCellX = Math.floor(column / pixelScale);
+ const starCellY = Math.floor(row / pixelScale);
+ const star = hashNoise(starCellX * 1.3, starCellY * 2.1, 9.7);
+ if (star > 0.993) {
+ const twinkle = 0.5 + 0.5 * Math.sin(time * 3 + star * 400);
+ for (let channel = 0; channel < 3; channel++) color[channel] += 0.4 * twinkle;
+ }
+ }
+ if (moon && alpha < 0.5) {
+ const moonDistance = Math.hypot(column / internalSize - moon.x, row / internalSize - moon.y);
+ const cover = 1 - alpha * 2;
+ if (moonDistance < moon.radius) {
+ const crater = valueNoise(column / internalSize * 40, row / internalSize * 40, 1) > 0.62 ? 0.82 : 1;
+ for (let channel = 0; channel < 3; channel++) color[channel] = color[channel] * (1 - cover) + moon.color[channel] * crater * cover;
+ } else {
+ const halo = Math.pow(Math.max(1 - (moonDistance - moon.radius) / (moon.radius * 3.2), 0), 2) * 0.45 * cover;
+ for (let channel = 0; channel < 3; channel++) color[channel] += moon.halo[channel] * halo;
+ }
+ }
+ if (clouds && alpha < 0.6) {
+ const cloudX = column / internalSize * 3 + time * 0.035;
+ const cloudY = row / internalSize * 7;
+ const density = valueNoise(cloudX, cloudY, 3) * 0.65 + valueNoise(cloudX * 2.3, cloudY * 2.3, 5) * 0.35;
+ const band = Math.max(0, 1 - Math.abs(row / internalSize - 0.24) * 3.2);
+ const amount = Math.max(density - 0.5, 0) * 2.2 * band * (1 - alpha);
+ for (let channel = 0; channel < 3; channel++) color[channel] = color[channel] * (1 - amount * 0.8) + clouds[channel] * amount * 0.8;
+ }
+ if (flash > 0) {
+ for (let channel = 0; channel < 3; channel++) color[channel] = color[channel] * (1 + flash * 1.4) + flash * [0.16, 0.18, 0.26][channel] * (1 - alpha * 0.5);
+ }
+ for (const glow of glows) {
+ const distance = Math.hypot(column - glow.screenX, row - glow.screenY);
+ if (distance < glow.radiusPixels) {
+ const falloff = Math.pow(1 - distance / glow.radiusPixels, 2) * glow.strength;
+ for (let channel = 0; channel < 3; channel++) color[channel] += glow.color[channel] * falloff;
+ }
+ }
+ if (isOutline[pixelIndex]) for (let channel = 0; channel < 3; channel++) color[channel] = outlineColor[channel];
+ buffer.set(color, pixelIndex * 3);
+ }
+ }
+ applyDreamyHaze(buffer, internalSize, scene.haze ?? null);
+ for (let row = 0; row < internalSize; row++) {
+ for (let column = 0; column < internalSize; column++) {
+ const pixelIndex = row * internalSize + column;
+ const offset = pixelIndex * 4;
+ const color = [buffer[pixelIndex * 3], buffer[pixelIndex * 3 + 1], buffer[pixelIndex * 3 + 2]];
+ if (fadeToBlack > 0) for (let channel = 0; channel < 3; channel++) color[channel] *= 1 - fadeToBlack;
+ if (palette) {
+ const threshold = BAYER[(row % 4) * 4 + (column % 4)] - 0.5;
+ const ditheredColor = color.map(value => value + threshold * style.dither);
+ let best = palette[0];
+ let bestDistance = Infinity;
+ for (const entry of palette) {
+ const red = ditheredColor[0] - entry[0];
+ const green = ditheredColor[1] - entry[1];
+ const blue = ditheredColor[2] - entry[2];
+ const distance = red * red * 0.3 + green * green * 0.59 + blue * blue * 0.11;
+ if (distance < bestDistance) {
+ bestDistance = distance;
+ best = entry;
+ }
+ }
+ for (let channel = 0; channel < 3; channel++) pixels[offset + channel] = Math.round(best[channel] * 255);
+ } else {
+ for (let channel = 0; channel < 3; channel++) pixels[offset + channel] = Math.round(Math.min(Math.max(color[channel], 0), 1) * 255);
+ }
+ pixels[offset + 3] = 255;
+ }
+ }
+ readerContext.putImageData(image, 0, 0);
+ const output = document.createElement('canvas');
+ output.width = outputSize;
+ output.height = outputSize;
+ const outputContext = output.getContext('2d');
+ outputContext.imageSmoothingEnabled = false;
+ outputContext.drawImage(reader, 0, 0, outputSize, outputSize);
+ return output.toDataURL('image/png');
+}
+
+// options: { time, internalSize, outputSize, style: { palette, dither, paletteSteps, splatScale }, camera: {...},
+// assemble, dissolve, fadeToBlack }
+window.renderFrame = function renderFrame(options) {
+ const scene = state.scene;
+ const time = options.time;
+ const staticLists = animateStatic(scene, time, options);
+ const dynamicLists = scene.dynamic(time);
+ if ((options.dissolve ?? 0) > 0) {
+ for (const list of dynamicLists) {
+ const colors = new Float32Array(list.colors);
+ for (let index = 0; index < list.count; index++) colors[index * 4 + 3] *= Math.max(1 - options.dissolve * 2.5, 0);
+ list.colors = colors;
+ }
+ }
+ if ((options.assemble ?? 1) < 1) {
+ for (const list of dynamicLists) {
+ const colors = new Float32Array(list.colors);
+ for (let index = 0; index < list.count; index++) colors[index * 4 + 3] *= Math.max((options.assemble - 0.75) * 4, 0);
+ list.colors = colors;
+ }
+ }
+ const fade = Math.max(1 - (options.dissolve ?? 0) * 1.5, 0) * Math.min(Math.max(((options.assemble ?? 1) - 0.4) * 2, 0), 1);
+ const atmosphere = atmosphereSplats(scene, time, fade);
+ const lists = [...staticLists, ...dynamicLists, ...atmosphere.lists];
+ scene.extraGlows = atmosphere.glows;
+ const splatScale = options.style.splatScale ?? 1;
+ const scaledLists = splatScale === 1 ? lists : lists.map(list => ({ ...list, scales: list.scales.map(value => value * splatScale) }));
+ const data = packSplatData(scaledLists.filter(list => list.count > 0));
+ const renderer = getRenderer(options.internalSize);
+ const splats = createGaussianSplats(data, { maximumCount: 3_000_000 });
+ const detach = attachGaussianSplats(renderer, splats);
+ renderer.render(cameraFor(scene, time, options.camera ?? {}));
+ const url = postProcess(scene, renderer, time, options.internalSize, options.style, options.outputSize, options.fadeToBlack ?? 0, options.flash ?? 0);
+ detach();
+ return { url, splatCount: data.count };
+};
+
+window.pageReady = true;
diff --git a/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/plans/haunted-film.mjs b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/plans/haunted-film.mjs
new file mode 100644
index 0000000..fb0e838
--- /dev/null
+++ b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/plans/haunted-film.mjs
@@ -0,0 +1,118 @@
+// 24 s haunted medieval triptych in look C, cut on lightning:
+// chapel crag (splats rain in, ghost rises) -> flash -> witch's swamp (push into the cauldron until single splats
+// show) -> flash -> gallows crossroads (the Reaper's eyes ignite, he raises the scythe and slashes, the bolt hits,
+// everything breaks into ash and goes black).
+// Camera moves are eased keyframes with a little handheld drift, never a constant orbit.
+// Environment: FILM_FPS, FILM_SIZE (output pixels), FILM_UNTIL (seconds).
+import { REAPER_TIMING, GALLOWS_BOLTS } from '../medieval2.js';
+
+const framesPerSecond = Number(process.env.FILM_FPS ?? 30);
+const outputSize = Number(process.env.FILM_SIZE ?? 1080);
+const untilSeconds = Number(process.env.FILM_UNTIL ?? 99);
+const internalBase = 360;
+const style = { dither: 0.06, paletteSteps: 3 };
+
+const clamp = value => Math.min(Math.max(value, 0), 1);
+const easeInOut = value => {
+ const amount = clamp(value);
+ return amount < 0.5 ? 4 * amount ** 3 : 1 - (-2 * amount + 2) ** 3 / 2;
+};
+const easeOut = value => 1 - (1 - clamp(value)) ** 3;
+const pulse = (time, at, width) => Math.max(0, 1 - Math.abs(time - at) / width);
+const mix = (from, to, amount) => from + (to - from) * amount;
+const drift = (time, seed) => Math.sin(time * 0.9 + seed) * 0.6 + Math.sin(time * 2.3 + seed * 2) * 0.4;
+
+const CUT_TO_SWAMP = 8.15;
+const CUT_TO_GALLOWS = 15.6;
+export const DURATION = 24;
+
+function chapelShot(localTime) {
+ const move = easeInOut(localTime / 8.2);
+ return {
+ scene: 'medieval', sceneTime: localTime,
+ assemble: clamp(localTime / 2.8),
+ camera: {
+ orbitSpeed: 0,
+ angleOffset: mix(-0.55, 0.35, move) + drift(localTime, 1) * 0.01,
+ zoom: mix(1.15, 0.97, move),
+ heightOffset: mix(0.25, -0.1, move) + drift(localTime, 2) * 0.008,
+ targetOffset: [0, mix(0, 0.05, move), 0],
+ },
+ };
+}
+
+function swampShot(localTime) {
+ // Settle in from a low close angle, hold, then push into the cauldron while the resolution climbs.
+ const settle = easeOut(localTime / 2.2);
+ const push = easeInOut((localTime - 4.3) / 2.6);
+ const reveal = easeInOut((localTime - 4.9) / 1.6);
+ const cauldron = [0.22, 0.16, 0.42];
+ const target = [0.05, 0.32, 0.05];
+ return {
+ scene: 'swamp', sceneTime: localTime + 1.5,
+ reveal,
+ camera: {
+ orbitSpeed: 0,
+ angleOffset: mix(0.55, 0.0, settle) + mix(0, 0.35, push) + drift(localTime, 4) * 0.012,
+ zoom: mix(0.78, 0.95, settle) * mix(1, 0.32, push),
+ heightOffset: mix(-0.5, -0.05, settle) + mix(0, -0.55, push) + drift(localTime, 5) * 0.01,
+ targetOffset: [(cauldron[0] - target[0]) * push, (cauldron[1] - target[1]) * push, (cauldron[2] - target[2]) * push],
+ },
+ };
+}
+
+function gallowsShot(localTime) {
+ const { eyes, slash } = REAPER_TIMING;
+ const approach = easeInOut(localTime / (slash - 0.2));
+ const jolt = Math.exp(-Math.max(localTime - slash, 0) * 9) * (localTime > slash ? 1 : 0);
+ const pullBack = easeInOut((localTime - slash - 0.5) / 1.6);
+ const reaperHead = [0.32, 0.6, 0.22];
+ const target = [0.05, 0.5, 0.05];
+ const toward = clamp(approach - pullBack);
+ const holdBreath = eyes < localTime ? 0 : drift(localTime, 7) * 0.01;
+ return {
+ scene: 'gallows', sceneTime: localTime,
+ dissolve: easeInOut((localTime - slash - 0.35) / 2.1) * 1.1,
+ fadeToBlack: easeInOut((localTime - (DURATION - CUT_TO_GALLOWS) + 0.9) / 0.8),
+ camera: {
+ orbitSpeed: 0,
+ angleOffset: mix(-0.35, 0.15, approach) + holdBreath + Math.sin(localTime * 60) * 0.02 * jolt,
+ zoom: mix(1.02, 0.5, toward),
+ heightOffset: mix(0.2, -0.6, toward) + Math.sin(localTime * 47) * 0.03 * jolt,
+ targetOffset: [(reaperHead[0] - target[0]) * toward * 0.8, (reaperHead[1] - target[1]) * toward * 0.6, (reaperHead[2] - target[2]) * toward * 0.8],
+ },
+ };
+}
+
+function flashAt(time) {
+ const strikes = [
+ [CUT_TO_SWAMP - 0.05, 1], [CUT_TO_SWAMP + 0.18, 0.55],
+ [CUT_TO_GALLOWS - 0.05, 1], [CUT_TO_GALLOWS + 0.2, 0.5],
+ ...GALLOWS_BOLTS.map(boltTime => [CUT_TO_GALLOWS + boltTime, 0.9]),
+ ];
+ return Math.max(0, ...strikes.map(([at, strength]) => strength * pulse(time, at, 0.12)));
+}
+
+export const frames = [];
+const totalFrames = Math.round(DURATION * framesPerSecond);
+for (let frameIndex = 0; frameIndex < totalFrames; frameIndex++) {
+ const time = frameIndex / framesPerSecond;
+ if (time >= untilSeconds) break;
+ const shot = time < CUT_TO_SWAMP ? chapelShot(time) : time < CUT_TO_GALLOWS ? swampShot(time - CUT_TO_SWAMP) : gallowsShot(time - CUT_TO_GALLOWS);
+ const reveal = shot.reveal ?? 0;
+ const internalSize = Math.round((internalBase + (1080 - internalBase) * reveal) / 8) * 8;
+ frames.push({
+ file: `frame_${String(frameIndex).padStart(4, '0')}.png`, scene: shot.scene, spacing: 0.015,
+ options: {
+ time: shot.sceneTime,
+ outputSize,
+ internalSize,
+ style: reveal > 0.75 ? { palette: 'none' } : style,
+ assemble: shot.assemble ?? 1,
+ dissolve: shot.dissolve ?? 0,
+ fadeToBlack: shot.fadeToBlack ?? 0,
+ flash: flashAt(time),
+ camera: shot.camera,
+ },
+ });
+}
diff --git a/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/plans/stills.mjs b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/plans/stills.mjs
new file mode 100644
index 0000000..655aac9
--- /dev/null
+++ b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/plans/stills.mjs
@@ -0,0 +1,12 @@
+// One still of each scene in look A (216 px) and look C (360 px). Run: node render.mjs plans/stills.mjs out/stills
+const looks = {
+ a: { internalSize: 216, spacing: 0.02, style: { dither: 0.12 } },
+ c: { internalSize: 360, spacing: 0.015, style: { dither: 0.06, paletteSteps: 3 } },
+};
+export const frames = [];
+for (const scene of ['medieval', 'swamp', 'gallows', 'lighthouse', 'snail', 'koi']) {
+ for (const [name, look] of Object.entries(looks)) {
+ frames.push({ file: `${scene}_${name}.png`, scene, spacing: look.spacing,
+ options: { time: 5, internalSize: look.internalSize, outputSize: 1080, style: look.style, camera: { orbitSpeed: 0 } } });
+ }
+}
diff --git a/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/render.mjs b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/render.mjs
new file mode 100644
index 0000000..5758bc9
--- /dev/null
+++ b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/render.mjs
@@ -0,0 +1,49 @@
+// Renders frames of the pixel splat scenes in headless Chrome on the GPU.
+// Usage: node render.mjs <plan.mjs> <output folder> (run from the folder that holds this file and node_modules)
+// The plan module exports `frames`: an array of { file, scene, spacing, options } (options go to window.renderFrame).
+import { createServer } from 'node:http';
+import { readFile, writeFile, mkdir } from 'node:fs/promises';
+import { extname, join, resolve } from 'node:path';
+import { pathToFileURL } from 'node:url';
+import { chromium } from 'playwright-core';
+
+const projectRoot = import.meta.dirname;
+const [planPath, outputFolder] = process.argv.slice(2);
+const { frames } = await import(pathToFileURL(resolve(planPath)).href);
+await mkdir(outputFolder, { recursive: true });
+
+const contentTypes = { '.html': 'text/html', '.js': 'text/javascript', '.mjs': 'text/javascript', '.wasm': 'application/wasm', '.json': 'application/json' };
+const server = createServer(async (request, response) => {
+ const requested = decodeURIComponent(new URL(request.url, 'http://localhost').pathname);
+ const path = requested === '/' ? '/index.html' : requested;
+ try {
+ const file = await readFile(join(projectRoot, path));
+ response.writeHead(200, { 'Content-Type': contentTypes[extname(path)] ?? 'application/octet-stream' }).end(file);
+ } catch {
+ response.writeHead(404).end();
+ }
+});
+await new Promise(done => server.listen(0, '127.0.0.1', done));
+
+const browser = await chromium.launch({
+ channel: 'chrome',
+ // Draw on the real GPU. Software rendering works but is several times slower.
+ args: [...(process.platform === 'win32' ? ['--use-angle=d3d11'] : []), '--enable-gpu', '--ignore-gpu-blocklist', '--disable-gpu-sandbox'],
+});
+const page = await browser.newPage({ viewport: { width: 1200, height: 1200 } });
+page.on('pageerror', error => console.error('page error:', error.message));
+page.on('console', message => { if (message.type() === 'error') console.error('console:', message.text()); });
+await page.goto(`http://127.0.0.1:${server.address().port}/`);
+await page.waitForFunction(() => window.pageReady === true);
+
+const started = Date.now();
+for (const [frameIndex, frame] of frames.entries()) {
+ const count = await page.evaluate(({ scene, spacing }) => window.setupScene(scene, spacing), frame);
+ const result = await page.evaluate(options => window.renderFrame(options), frame.options);
+ await writeFile(join(outputFolder, frame.file), Buffer.from(result.url.split(',')[1], 'base64'));
+ if (frameIndex % 30 === 0 || frames.length < 20) {
+ console.log(`${frame.file} scene=${frame.scene} static=${count} drawn=${result.splatCount} elapsed=${((Date.now() - started) / 1000).toFixed(1)}s`);
+ }
+}
+await browser.close();
+server.close();
diff --git a/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/scenes.js b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/scenes.js
new file mode 100644
index 0000000..f5e28a4
--- /dev/null
+++ b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/scenes.js
@@ -0,0 +1,563 @@
+// Three original pixel-art splat dioramas. Each scene builds its static splats once and adds moving splats per frame.
+import { buildMedieval } from './medieval.js';
+import { buildSwamp, buildGallows } from './medieval2.js';
+import {
+ hashNoise, valueNoise, sphereDistance, ellipsoidDistance, boxDistance, cylinderDistance, taperedCylinderDistance,
+ capsuleDistance, smoothUnion, islandDistance, voxelizeModel, placeSplats,
+} from './voxel-splats.js';
+
+const hex = value => [parseInt(value.slice(1, 3), 16) / 255, parseInt(value.slice(3, 5), 16) / 255, parseInt(value.slice(5, 7), 16) / 255];
+
+function makeList(points) {
+ const count = points.length;
+ const positions = new Float32Array(count * 3);
+ const colors = new Float32Array(count * 4);
+ const scales = new Float32Array(count);
+ points.forEach((point, index) => {
+ positions.set([point.x, point.y, point.z], index * 3);
+ colors.set([point.color[0], point.color[1], point.color[2], point.alpha ?? 1], index * 4);
+ scales[index] = point.scale;
+ });
+ return { count, positions, colors, scales };
+}
+
+// ---------------------------------------------------------------------------------------------------------------
+// Lighthouse on a rock in a disc of night sea.
+// ---------------------------------------------------------------------------------------------------------------
+function buildLighthouse(spacing) {
+ const palette = ['#070a14', '#0f1528', '#1a2544', '#263a66', '#36578a', '#5684b0', '#9cc8dc', '#e6f2ee',
+ '#2a2430', '#4a3e48', '#76666e', '#7a2230', '#c23a44', '#f2c75c', '#fff2c0', '#e07a34'].map(hex);
+ const colors = {
+ rockDark: hex('#3a3040'), rock: hex('#5a4c58'), rockLight: hex('#86727c'), white: hex('#e8eef0'), red: hex('#c23a44'),
+ darkMetal: hex('#262636'), lamp: hex('#fff2c0'), roof: hex('#8a2634'), wall: hex('#d8d0c8'), wood: hex('#6a4a3a'),
+ seaBody: hex('#1a2a52'), window: hex('#f2c75c'),
+ };
+ const light = { sun: [-0.5, 0.8, 0.45], sunColor: [0.75, 0.82, 1.0], ambientColor: [0.32, 0.38, 0.58], rimColor: [0.3, 0.45, 0.8], shadowSoftness: 6 };
+ const rockDistance = (x, y, z) => {
+ const mound = ellipsoidDistance(x, y, z, 0.05, -0.15, 0.0, 0.62, 0.5, 0.52);
+ const ledge = ellipsoidDistance(x, y, z, 0.45, -0.12, 0.35, 0.32, 0.26, 0.3);
+ const noise = (valueNoise(x * 7, y * 7, z * 7) - 0.5) * 0.09 + (valueNoise(x * 19, y * 19, z * 19) - 0.5) * 0.03;
+ return Math.max(smoothUnion(mound, ledge, 0.12) + noise, -y - 0.45);
+ };
+ const towerDistance = (x, y, z) => {
+ const tower = taperedCylinderDistance(x, y, z, -0.05, 0.2, -0.02, 0.17, 0.115, 0.95);
+ const gallery = cylinderDistance(x, y, z, -0.05, 1.13, -0.02, 0.16, 0.035);
+ const lampRoom = cylinderDistance(x, y, z, -0.05, 1.165, -0.02, 0.085, 0.13);
+ const roof = taperedCylinderDistance(x, y, z, -0.05, 1.29, -0.02, 0.12, 0.0, 0.13);
+ const ball = sphereDistance(x, y, z, -0.05, 1.43, -0.02, 0.025);
+ return Math.min(tower, gallery, lampRoom, roof, ball);
+ };
+ const cottageDistance = (x, y, z) => {
+ const walls = boxDistance(x, y, z, 0.38, 0.27, 0.22, 0.13, 0.09, 0.1, 0.01);
+ const roofLocalY = y - 0.36;
+ const roof = Math.max(boxDistance(x, y, z, 0.38, 0.42, 0.22, 0.155, 0.08, 0.12), Math.abs(z - 0.22) * 0.8 + roofLocalY * 1.0 - 0.1);
+ const chimney = boxDistance(x, y, z, 0.46, 0.47, 0.17, 0.022, 0.06, 0.022);
+ return Math.min(walls, Math.max(roof, -roofLocalY), chimney);
+ };
+ const seaBodyDistance = (x, y, z) => cylinderDistance(x, y, z, 0, -0.42, 0, 1.25, 0.4);
+ const sceneDistance = (x, y, z) => Math.min(rockDistance(x, y, z), towerDistance(x, y, z), cottageDistance(x, y, z));
+
+ const rockModel = {
+ distance: rockDistance,
+ bounds: [-0.75, -0.46, -0.65, 0.85, 0.42, 0.75],
+ material: (x, y, z, normalX, normalY) => {
+ if (normalY > 0.75 && y > 0.12) return [...hex('#3f5a3a'), 0];
+ const band = valueNoise(x * 3, y * 14, z * 3);
+ return band > 0.62 ? [...colors.rockLight, 0] : band > 0.35 ? [...colors.rock, 0] : [...colors.rockDark, 0];
+ },
+ };
+ const towerModel = {
+ distance: towerDistance,
+ bounds: [-0.27, 0.15, -0.24, 0.17, 1.48, 0.2],
+ material: (x, y, z) => {
+ if (y > 1.43 - 0.03 && y < 1.47) return [...colors.darkMetal, 0];
+ if (y > 1.29) return [...colors.roof, 0];
+ if (y > 1.165) {
+ const radial = Math.hypot(x + 0.05, z + 0.02);
+ if (radial < 0.095) {
+ const mullion = Math.abs(Math.sin(Math.atan2(z + 0.02, x + 0.05) * 4)) < 0.2;
+ return mullion ? [...colors.darkMetal, 0] : [...colors.lamp, 1];
+ }
+ return [...colors.darkMetal, 0];
+ }
+ if (y > 1.12) return [...colors.darkMetal, 0];
+ const doorway = y < 0.38 && Math.abs(x + 0.05 - 0.0) < 0.05 && z > 0.05;
+ if (doorway) return [...colors.wood, 0];
+ const windowSpot = Math.abs(y - 0.72) < 0.04 && Math.abs(x + 0.05) < 0.03 && z > 0.05;
+ if (windowSpot) return [...colors.window, 0.8];
+ const band = Math.floor((y - 0.2) / 0.19) % 2 === 0;
+ return band ? [...colors.white, 0] : [...colors.red, 0];
+ },
+ };
+ const cottageModel = {
+ distance: cottageDistance,
+ bounds: [0.2, 0.15, 0.06, 0.56, 0.56, 0.38],
+ material: (x, y, z) => {
+ if (y > 0.36 && !(Math.abs(x - 0.46) < 0.03 && Math.abs(z - 0.17) < 0.03)) return [...colors.roof, 0];
+ if (y > 0.36) return [...colors.rock, 0];
+ const windowSpot = Math.abs(y - 0.28) < 0.03 && Math.abs(x - 0.33) < 0.03 && z > 0.3;
+ if (windowSpot) return [...colors.window, 1];
+ return [...colors.wall, 0];
+ },
+ };
+ const seaBodyModel = {
+ distance: seaBodyDistance,
+ bounds: [-1.28, -0.45, -1.28, 1.28, 0.0, 1.28],
+ material: (x, y) => {
+ const depth = -y;
+ if (depth > 0.4) return [...hex('#0f1528'), 0];
+ return depth > 0.22 ? [...hex('#1a2544'), 0.25] : [...hex('#263a66'), 0.25];
+ },
+ };
+ const shadowModel = { distance: sceneDistance };
+ const staticLists = [rockModel, towerModel, cottageModel].map(model => voxelizeModel(model, spacing, light, { shadowModel }));
+ const seaWall = voxelizeModel(seaBodyModel, spacing * 1.4, light, { shadowModel: { distance: seaBodyDistance } });
+ // Keep only the side wall and floor of the sea body; the top is the moving water.
+ const wallKeep = [];
+ for (let index = 0; index < seaWall.count; index++) if (seaWall.positions[index * 3 + 1] < -0.04) wallKeep.push(index);
+ staticLists.push(subsetList(seaWall, wallKeep));
+
+ const boat = voxelizeModel({
+ distance: (x, y, z) => {
+ const hull = Math.max(ellipsoidDistance(x, y, z, 0, 0, 0, 0.17, 0.08, 0.07), y - 0.02, -(y + 0.06));
+ const mast = capsuleDistance(x, y, z, 0.01, 0, 0, 0.01, 0.24, 0, 0.008);
+ const sail = Math.max(boxDistance(x, y, z, -0.04, 0.13, 0, 0.05, 0.09, 0.006), x - 0.0 - (0.22 - y) * 0.4);
+ return Math.min(hull, mast, sail);
+ },
+ bounds: [-0.2, -0.08, -0.1, 0.2, 0.27, 0.1],
+ material: (x, y) => (y > 0.025 ? [...colors.wall, 0] : [...hex('#8a4a32'), 0]),
+ }, spacing * 0.8, light);
+
+ const waterSpacing = spacing * 1.35;
+ const waterGrid = [];
+ for (let x = -1.24; x <= 1.24; x += waterSpacing) {
+ for (let z = -1.24; z <= 1.24; z += waterSpacing) {
+ if (Math.hypot(x, z) > 1.24) continue;
+ if (rockDistance(x, 0, z) < -0.02) continue;
+ waterGrid.push([x + (hashNoise(x * 9, 1, z * 9) - 0.5) * waterSpacing * 0.4, z + (hashNoise(x * 9, 2, z * 9) - 0.5) * waterSpacing * 0.4]);
+ }
+ }
+ const lampPosition = [-0.05, 1.23, -0.02];
+ const boatPath = time => ({ x: 0.72 + Math.sin(time * 0.3) * 0.05, z: -0.42, heading: 0.6 + Math.sin(time * 0.5) * 0.08 });
+ const waveHeight = (x, z, time) => 0.022 * Math.sin(x * 5.5 + time * 1.6) + 0.016 * Math.sin(z * 7.1 - time * 1.2 + x * 2) + 0.008 * Math.sin((x + z) * 13 + time * 2.4);
+
+ return {
+ name: 'lighthouse',
+ mist: { color: hex('#7f9db4'), alpha: 0.05, low: -0.08, high: 0.3, wispColor: hex('#c8f0ff'), wispHeight: 0.35 },
+ haze: { color: hex('#22314c'), lift: 0.14, bloom: 0.8, bloomThreshold: 0.45, soften: 0.35, mistTop: 0.55, mistStrength: 0.22, vignette: 1.1 },
+ paletteExtra: ['#3a4c6a', '#5a7290', '#8aa4bc', '#b4cad8'],
+ palette,
+ background: { inner: hex('#1a2448'), outer: hex('#05070f'), stars: true },
+ camera: { target: [0, 0.45, 0], distance: 5.3, height: 1.7, startAngle: 0.5 },
+ staticLists,
+ dynamic(time) {
+ const lists = [];
+ const waterPoints = waterGrid.map(([x, z]) => {
+ const height = waveHeight(x, z, time);
+ const shoreDistance = rockDistance(x, 0.0, z);
+ const foam = height > 0.025 || shoreDistance < 0.07 + 0.03 * Math.sin(time * 2 + x * 8) || Math.hypot(x, z) > 1.2;
+ const lampSide = Math.max(0, Math.cos(Math.atan2(z - lampPosition[2], x - lampPosition[0]) - time * 0.9)) ** 24;
+ const base = foam ? hex('#9cc8dc') : height > 0.008 ? hex('#36578a') : hex('#263a66');
+ const color = base.map((value, channel) => value + lampSide * [0.45, 0.35, 0.1][channel] * (1 - Math.min(Math.hypot(x, z) / 1.3, 1)));
+ return { x, y: height - 0.01, z, color, scale: waterSpacing * 0.68 };
+ });
+ lists.push(makeList(waterPoints));
+ const boatPlace = boatPath(time);
+ lists.push(placeSplats(boat, { x: boatPlace.x, y: waveHeight(boatPlace.x, boatPlace.z, time) + 0.02, z: boatPlace.z, heading: boatPlace.heading,
+ deform: (x, y, z) => [x, y + x * 0.25 * Math.sin(time * 1.6 + 0.8), z] }));
+ // Two beams turning around the lamp.
+ const beamPoints = [];
+ for (let beam = 0; beam < 2; beam++) {
+ const angle = time * 0.9 + beam * Math.PI;
+ for (let step = 0; step < 70; step++) {
+ const along = 0.12 + step * 0.03;
+ const width = 0.02 + along * 0.11;
+ for (let across = 0; across < 3; across++) {
+ const sideways = (across - 1) * width * 0.8 + (hashNoise(step, across, beam) - 0.5) * width * 0.5;
+ const x = lampPosition[0] + Math.cos(angle) * along - Math.sin(angle) * sideways;
+ const z = lampPosition[2] + Math.sin(angle) * along + Math.cos(angle) * sideways;
+ const y = lampPosition[1] - along * 0.04 + (hashNoise(step, across, beam + 7) - 0.5) * width * 0.6;
+ beamPoints.push({ x, y, z, color: hex('#fff2c0'), alpha: 0.075 * (1 - along / 2.2) * Math.min(along / 0.3, 1), scale: width * 0.6 });
+ }
+ }
+ }
+ lists.push(makeList(beamPoints));
+ return lists;
+ },
+ glows(time) {
+ return [
+ { position: lampPosition, color: hex('#ffd878'), radius: 0.55, strength: 0.65 },
+ { position: [0.33, 0.28, 0.33], color: hex('#f2a85c'), radius: 0.16, strength: 0.4 },
+ ];
+ },
+ contactShadows() {
+ return [];
+ },
+ };
+}
+
+// ---------------------------------------------------------------------------------------------------------------
+// A snail carrying a tiny house across a floating meadow at dusk.
+// ---------------------------------------------------------------------------------------------------------------
+function buildSnail(spacing) {
+ const palette = ['#140f1e', '#251a34', '#3a2a4a', '#2f4a34', '#4f7a3c', '#8cba52', '#d8e88a', '#5e3e2e',
+ '#94603c', '#d29a5e', '#a8503a', '#e8d6b0', '#c2607e', '#f4ece0', '#7e6e94', '#ffcc66'].map(hex);
+ const light = { sun: [0.6, 0.55, 0.4], sunColor: [1.15, 0.9, 0.68], ambientColor: [0.58, 0.5, 0.7], rimColor: [0.6, 0.35, 0.5], shadowSoftness: 6 };
+ const meadowDistance = (x, y, z) => {
+ const tuft = (valueNoise(x * 11, 0, z * 11) - 0.6) * 0.07;
+ return islandDistance(x, y - Math.max(tuft, 0), z, 1.02, 0.95, 0, 0.08);
+ };
+ const decorDistance = (x, y, z) => {
+ let distance = Infinity;
+ for (const [mushroomX, mushroomZ, size] of [[-0.55, 0.35, 1], [-0.45, 0.5, 0.7], [0.55, -0.55, 0.85]]) {
+ const stem = capsuleDistance(x, y, z, mushroomX, 0, mushroomZ, mushroomX, 0.14 * size, mushroomZ, 0.035 * size);
+ const cap = Math.max(ellipsoidDistance(x, y, z, mushroomX, 0.15 * size, mushroomZ, 0.11 * size, 0.08 * size, 0.11 * size), -(y - 0.13 * size));
+ distance = Math.min(distance, stem, cap);
+ }
+ const stone = ellipsoidDistance(x, y, z, 0.35, 0.02, 0.62, 0.13, 0.09, 0.1) + (valueNoise(x * 20, y * 20, z * 20) - 0.5) * 0.02;
+ distance = Math.min(distance, stone);
+ for (let flower = 0; flower < 9; flower++) {
+ const angle = flower * 2.39;
+ const radius = 0.25 + (flower % 4) * 0.17;
+ const flowerX = Math.cos(angle) * radius;
+ const flowerZ = Math.sin(angle) * radius;
+ const stem = capsuleDistance(x, y, z, flowerX, 0, flowerZ, flowerX, 0.07, flowerZ, 0.008);
+ const head = sphereDistance(x, y, z, flowerX, 0.08, flowerZ, 0.022);
+ distance = Math.min(distance, stem, head);
+ }
+ return distance;
+ };
+ const sceneDistance = (x, y, z) => Math.min(meadowDistance(x, y, z), decorDistance(x, y, z));
+ const meadow = voxelizeModel({
+ distance: meadowDistance,
+ bounds: [-1.08, -0.98, -1.08, 1.08, 0.1, 1.08],
+ material: (x, y, z, normalX, normalY) => {
+ if (y > -0.05 && normalY > 0.35) {
+ const shade = valueNoise(x * 8, 3, z * 8);
+ return [...(shade > 0.6 ? hex('#8cba52') : shade > 0.3 ? hex('#5f9a44') : hex('#4f7a3c')), 0];
+ }
+ if (y > -0.12) return [...hex('#4f7a3c'), 0];
+ const strata = valueNoise(x * 2, y * 9, z * 2);
+ return [...(strata > 0.55 ? hex('#94603c') : strata > 0.3 ? hex('#6e4a34') : hex('#4e3628')), 0];
+ },
+ }, spacing, light, { shadowModel: { distance: sceneDistance } });
+ const decor = voxelizeModel({
+ distance: decorDistance,
+ bounds: [-0.7, -0.02, -0.75, 0.75, 0.24, 0.75],
+ material: (x, y, z) => {
+ if (Math.hypot(x - 0.35, z - 0.62) < 0.16 && y < 0.12) return [...hex('#7e6e94'), 0];
+ for (let flower = 0; flower < 9; flower++) {
+ const angle = flower * 2.39;
+ const radius = 0.25 + (flower % 4) * 0.17;
+ if (Math.hypot(x - Math.cos(angle) * radius, z - Math.sin(angle) * radius) < 0.03) {
+ if (y > 0.06) return [...(flower % 3 === 0 ? hex('#f4ece0') : flower % 3 === 1 ? hex('#ffcc66') : hex('#c2607e')), 0];
+ return [...hex('#4f7a3c'), 0];
+ }
+ }
+ if (y > 0.1 * 0.7 && valueNoise(x * 40, y * 40, z * 40) > 0.72) return [...hex('#f4ece0'), 0];
+ if (y > 0.09) return [...hex('#c2607e'), 0];
+ return [...hex('#e8d6b0'), 0];
+ },
+ }, spacing * 0.8, light, { shadowModel: { distance: sceneDistance } });
+
+ // The snail, built facing +x around its own origin on the ground.
+ const shellCenter = [-0.04, 0.2, 0];
+ const snailDistance = (x, y, z) => {
+ const foot = smoothUnion(
+ ellipsoidDistance(x, y, z, 0.02, 0.04, 0, 0.3, 0.05, 0.08),
+ capsuleDistance(x, y, z, 0.18, 0.05, 0, 0.27, 0.14, 0, 0.055), 0.05);
+ const eyeLeft = capsuleDistance(x, y, z, 0.27, 0.16, 0.025, 0.3, 0.27, 0.05, 0.011);
+ const eyeRight = capsuleDistance(x, y, z, 0.27, 0.16, -0.025, 0.31, 0.26, -0.05, 0.011);
+ const eyeBallLeft = sphereDistance(x, y, z, 0.3, 0.275, 0.05, 0.022);
+ const eyeBallRight = sphereDistance(x, y, z, 0.31, 0.265, -0.05, 0.022);
+ const shell = ellipsoidDistance(x, y, z, shellCenter[0], shellCenter[1], shellCenter[2], 0.17, 0.16, 0.12);
+ const houseWalls = boxDistance(x, y, z, -0.04, 0.39, 0, 0.065, 0.05, 0.05, 0.006);
+ const roof = Math.max(boxDistance(x, y, z, -0.04, 0.47, 0, 0.08, 0.045, 0.065), Math.abs(z) * 0.9 + (y - 0.44) - 0.055, -(y - 0.44));
+ const chimney = boxDistance(x, y, z, 0.0, 0.5, -0.025, 0.012, 0.035, 0.012);
+ return Math.min(foot, eyeLeft, eyeRight, eyeBallLeft, eyeBallRight, shell, houseWalls, roof, chimney);
+ };
+ const snail = voxelizeModel({
+ distance: snailDistance,
+ bounds: [-0.3, -0.02, -0.16, 0.36, 0.56, 0.16],
+ material: (x, y, z) => {
+ const shellDistance = ellipsoidDistance(x, y, z, shellCenter[0], shellCenter[1], shellCenter[2], 0.17, 0.16, 0.12);
+ if (y > 0.335) {
+ if (y > 0.44 && !(Math.abs(x) < 0.02 && z < -0.01 && y > 0.46)) return [...hex('#a8503a'), 0];
+ if (y > 0.44) return [...hex('#5e3e2e'), 0];
+ const windowSpot = Math.abs(y - 0.395) < 0.018 && Math.abs(x + 0.04) < 0.02 && Math.abs(z) > 0.04;
+ if (windowSpot) return [...hex('#ffcc66'), 1];
+ const doorSpot = x > 0.02 && Math.abs(z) < 0.02 && y < 0.41;
+ if (doorSpot) return [...hex('#5e3e2e'), 0];
+ return [...hex('#e8d6b0'), 0];
+ }
+ if (shellDistance < 0.02) {
+ const spiral = Math.atan2(y - shellCenter[1], x - shellCenter[0]) + Math.hypot(x - shellCenter[0], y - shellCenter[1]) * 26;
+ const stripe = Math.sin(spiral);
+ return [...(stripe > 0.45 ? hex('#d29a5e') : stripe > -0.3 ? hex('#a8503a') : hex('#5e3e2e')), 0];
+ }
+ if (y > 0.25) return [...hex('#2a1e2a'), 0];
+ return [...hex('#c8b4a0'), 0];
+ },
+ }, spacing * 0.75, light);
+
+ const snailScale = 1.55;
+ const pathRadius = 0.45;
+ const snailPlace = time => {
+ const angle = -time * 0.16 + 0.6;
+ return { x: Math.cos(angle) * pathRadius, z: Math.sin(angle) * pathRadius, heading: angle + Math.PI };
+ };
+ const chimneyWorld = time => {
+ const place = snailPlace(time);
+ const cosine = Math.cos(place.heading);
+ const sine = Math.sin(place.heading);
+ const localX = 0.0;
+ const localZ = -0.025;
+ return [place.x + (localX * cosine + localZ * sine) * snailScale, 0.53 * snailScale, place.z + (-localX * sine + localZ * cosine) * snailScale];
+ };
+ return {
+ name: 'snail',
+ mist: { color: hex('#c4aed4'), alpha: 0.045, low: -0.1, high: 0.25, wispColor: hex('#ffe8a8'), wispHeight: 0.3 },
+ haze: { color: hex('#3a2c4c'), lift: 0.16, bloom: 0.75, bloomThreshold: 0.5, soften: 0.35, mistTop: 0.55, mistStrength: 0.25, vignette: 1.1 },
+ paletteExtra: ['#5a4870', '#86709c', '#b29cc4', '#d8c8e2'],
+ palette,
+ background: { inner: hex('#3a2a4a'), outer: hex('#120c1a'), stars: false },
+ camera: { target: [0, 0.12, 0], distance: 3.9, height: 1.5, startAngle: 2.2 },
+ staticLists: [meadow, decor],
+ dynamic(time) {
+ const place = snailPlace(time);
+ const lists = [placeSplats(snail, {
+ x: place.x, y: 0.0, z: place.z, heading: place.heading, scale: snailScale,
+ deform: (x, y, z) => {
+ const stretch = 1 + 0.06 * Math.sin(time * 3.2 - x * 6);
+ let movedX = x * (y < 0.09 ? stretch : 1);
+ let movedY = y;
+ if (x > 0.22 && y > 0.15) {
+ movedX += 0.012 * Math.sin(time * 2.1 + z * 30);
+ movedY += 0.01 * Math.sin(time * 2.7 + z * 20);
+ }
+ if (y > 0.13 && x < 0.2) movedY += 0.006 * Math.sin(time * 3.2);
+ return [movedX, movedY, z];
+ },
+ })];
+ const smoke = [];
+ const chimney = chimneyWorld(time);
+ for (let puff = 0; puff < 9; puff++) {
+ const age = ((time * 0.4 + puff / 9) % 1);
+ const birthTime = time - age / 0.4;
+ const origin = chimneyWorld(birthTime);
+ const drift = age * 0.5;
+ smoke.push({
+ x: origin[0] + drift * 0.3 + Math.sin(puff * 3 + age * 5) * 0.04,
+ y: chimney[1] + age * 0.6,
+ z: origin[2] + drift * 0.15,
+ color: hex('#d8cfe2'), alpha: 0.55 * (1 - age) * Math.min(age * 8, 1), scale: 0.03 + age * 0.06,
+ });
+ }
+ lists.push(makeList(smoke));
+ const fireflies = [];
+ for (let fly = 0; fly < 10; fly++) {
+ const phase = fly * 1.7;
+ fireflies.push({
+ x: Math.cos(time * 0.4 + phase) * (0.55 + 0.25 * Math.sin(phase)),
+ y: 0.25 + 0.12 * Math.sin(time * 1.3 + phase * 2),
+ z: Math.sin(time * 0.35 + phase) * (0.6 + 0.2 * Math.cos(phase)),
+ color: hex('#ffeaa0'), alpha: 0.6 + 0.4 * Math.sin(time * 4 + phase), scale: 0.014,
+ });
+ }
+ lists.push(makeList(fireflies));
+ return lists;
+ },
+ glows(time) {
+ const place = snailPlace(time);
+ const glows = [{ position: [place.x, 0.4 * snailScale, place.z], color: hex('#ffcc66'), radius: 0.25, strength: 0.2 }];
+ for (let fly = 0; fly < 10; fly++) {
+ const phase = fly * 1.7;
+ glows.push({
+ position: [Math.cos(time * 0.4 + phase) * (0.55 + 0.25 * Math.sin(phase)), 0.25 + 0.12 * Math.sin(time * 1.3 + phase * 2), Math.sin(time * 0.35 + phase) * (0.6 + 0.2 * Math.cos(phase))],
+ color: hex('#ffe08a'), radius: 0.07, strength: 0.3 * (0.6 + 0.4 * Math.sin(time * 4 + phase)),
+ });
+ }
+ return glows;
+ },
+ contactShadows(time) {
+ const place = snailPlace(time);
+ return [{ x: place.x, z: place.z, radius: 0.36, strength: 0.5 }];
+ },
+ };
+}
+
+// ---------------------------------------------------------------------------------------------------------------
+// A koi pond island with a blossom tree and a stone lantern.
+// ---------------------------------------------------------------------------------------------------------------
+function buildKoi(spacing) {
+ const palette = ['#0b1418', '#132a32', '#1c4650', '#2a6e72', '#5cb2a4', '#d6f4e8', '#f29ab2', '#c0587a',
+ '#fbd2dc', '#3e302e', '#6e5650', '#355e36', '#74a454', '#ee7426', '#fff4e4', '#ffd070'].map(hex);
+ const light = { sun: [0.45, 0.85, -0.35], sunColor: [1.05, 0.95, 0.88], ambientColor: [0.55, 0.6, 0.66], rimColor: [0.6, 0.4, 0.5], shadowSoftness: 6 };
+ const pondRadius = 0.56;
+ const pondCenter = [0.12, 0.12];
+ const islandWithPond = (x, y, z) => {
+ const island = islandDistance(x, y, z, 1.05, 0.95, 0, 0.08);
+ const pond = cylinderDistance(x, y, z, pondCenter[0], -0.14, pondCenter[1], pondRadius, 0.3);
+ return Math.max(island, -pond);
+ };
+ const treeDistance = (x, y, z) => {
+ const trunk = Math.min(
+ capsuleDistance(x, y, z, -0.6, 0, -0.45, -0.55, 0.38, -0.42, 0.055),
+ capsuleDistance(x, y, z, -0.55, 0.38, -0.42, -0.38, 0.62, -0.3, 0.035),
+ capsuleDistance(x, y, z, -0.55, 0.36, -0.42, -0.7, 0.62, -0.5, 0.03));
+ const canopyNoise = (valueNoise(x * 9, y * 9, z * 9) - 0.5) * 0.1;
+ const canopy = Math.min(
+ ellipsoidDistance(x, y, z, -0.38, 0.72, -0.3, 0.3, 0.2, 0.26),
+ ellipsoidDistance(x, y, z, -0.72, 0.7, -0.52, 0.24, 0.18, 0.22),
+ ellipsoidDistance(x, y, z, -0.55, 0.86, -0.42, 0.24, 0.16, 0.22)) + canopyNoise;
+ return Math.min(trunk, canopy);
+ };
+ const lanternDistance = (x, y, z) => {
+ const lanternX = 0.68;
+ const lanternZ = -0.5;
+ return Math.min(
+ cylinderDistance(x, y, z, lanternX, 0, lanternZ, 0.07, 0.04),
+ cylinderDistance(x, y, z, lanternX, 0.04, lanternZ, 0.03, 0.17),
+ boxDistance(x, y, z, lanternX, 0.25, lanternZ, 0.07, 0.04, 0.07, 0.008),
+ Math.max(taperedCylinderDistance(x, y, z, lanternX, 0.29, lanternZ, 0.12, 0.02, 0.09), -(y - 0.29)),
+ sphereDistance(x, y, z, lanternX, 0.39, lanternZ, 0.02));
+ };
+ const sceneDistance = (x, y, z) => Math.min(islandWithPond(x, y, z), treeDistance(x, y, z), lanternDistance(x, y, z));
+ const island = voxelizeModel({
+ distance: islandWithPond,
+ bounds: [-1.1, -0.98, -1.1, 1.1, 0.1, 1.1],
+ material: (x, y, z, normalX, normalY) => {
+ const insidePond = Math.hypot(x - pondCenter[0], z - pondCenter[1]) < pondRadius + 0.02;
+ if (insidePond) return [...(valueNoise(x * 25, 1, z * 25) > 0.5 ? hex('#6e5650') : hex('#3e302e')), 0];
+ if (y > -0.05 && normalY > 0.35) {
+ const moss = valueNoise(x * 8, 5, z * 8);
+ const pebble = Math.abs(Math.hypot(x - pondCenter[0], z - pondCenter[1]) - pondRadius - 0.05) < 0.04 && valueNoise(x * 30, 1, z * 30) > 0.45;
+ if (pebble) return [...hex('#b8b0aa'), 0];
+ return [...(moss > 0.55 ? hex('#74a454') : hex('#4f8240')), 0];
+ }
+ const strata = valueNoise(x * 2, y * 9, z * 2);
+ return [...(strata > 0.5 ? hex('#6e5650') : hex('#3e302e')), 0];
+ },
+ }, spacing, light, { shadowModel: { distance: sceneDistance } });
+ const tree = voxelizeModel({
+ distance: treeDistance,
+ bounds: [-1.0, -0.02, -0.82, -0.05, 1.06, -0.04],
+ material: (x, y, z) => {
+ const trunk = Math.min(
+ capsuleDistance(x, y, z, -0.6, 0, -0.45, -0.55, 0.38, -0.42, 0.06),
+ capsuleDistance(x, y, z, -0.55, 0.38, -0.42, -0.38, 0.62, -0.3, 0.04),
+ capsuleDistance(x, y, z, -0.55, 0.36, -0.42, -0.7, 0.62, -0.5, 0.035));
+ if (trunk < 0.012) return [...hex('#4a3634'), 0];
+ const blossom = valueNoise(x * 22, y * 22, z * 22);
+ return [...(blossom > 0.62 ? hex('#fbd2dc') : blossom > 0.32 ? hex('#f29ab2') : hex('#d06888')), 0.35];
+ },
+ }, spacing * 0.9, light, { shadowModel: { distance: sceneDistance } });
+ const lantern = voxelizeModel({
+ distance: lanternDistance,
+ bounds: [0.5, -0.02, -0.68, 0.86, 0.43, -0.32],
+ material: (x, y) => {
+ if (Math.abs(y - 0.25) < 0.03) return [...hex('#ffd070'), 1];
+ return [...(valueNoise(x * 30, y * 30, 0) > 0.5 ? hex('#a8a4a0') : hex('#8a8686')), 0];
+ },
+ }, spacing * 0.8, light, { shadowModel: { distance: sceneDistance } });
+
+ const koi = [0, 1, 2].map(fish => voxelizeModel({
+ distance: (x, y, z) => {
+ const body = ellipsoidDistance(x, y, z, 0, 0, 0, 0.11, 0.035, 0.04);
+ const tail = Math.max(ellipsoidDistance(x, y, z, -0.13, 0, 0, 0.045, 0.012, 0.045), x + 0.09);
+ const fins = Math.max(ellipsoidDistance(x, y, z, 0.03, -0.01, 0, 0.03, 0.008, 0.07), -(Math.abs(z) - 0.03));
+ return Math.min(body, tail, fins);
+ },
+ bounds: [-0.19, -0.05, -0.08, 0.13, 0.05, 0.08],
+ material: (x, y, z) => {
+ const patch = valueNoise(x * 30 + fish * 7, y * 30, z * 30 + fish * 3);
+ if (fish === 1) return [...(patch > 0.55 ? hex('#ee7426') : hex('#fff4e4')), 0.15];
+ if (fish === 2) return [...(patch > 0.6 ? hex('#202020') : patch > 0.35 ? hex('#ee7426') : hex('#fff4e4')), 0.15];
+ return [...(patch > 0.3 ? hex('#ee7426') : hex('#fff4e4')), 0.15];
+ },
+ }, spacing * 0.55, { ...light, sun: [0, 1, 0] }));
+
+ const waterSpacing = spacing * 1.3;
+ const waterGrid = [];
+ for (let x = -pondRadius; x <= pondRadius; x += waterSpacing) {
+ for (let z = -pondRadius; z <= pondRadius; z += waterSpacing) {
+ if (Math.hypot(x, z) > pondRadius - 0.01) continue;
+ waterGrid.push([pondCenter[0] + x, pondCenter[1] + z]);
+ }
+ }
+ const fishPlace = (fish, time) => {
+ const radius = 0.22 + fish * 0.09;
+ const speed = 0.55 - fish * 0.1;
+ const angle = time * speed * (fish === 1 ? -1 : 1) + fish * 2.1;
+ return { x: pondCenter[0] + Math.cos(angle) * radius, z: pondCenter[1] + Math.sin(angle) * radius, heading: fish === 1 ? -angle + Math.PI / 2 * -1 : -angle - Math.PI / 2 };
+ };
+ const petalAt = (petal, time) => {
+ const period = 5.5;
+ const age = ((time + petal * 0.61) % period) / period;
+ const startX = -0.55 + (hashNoise(petal, 1, 0) - 0.5) * 0.7;
+ const startZ = -0.4 + (hashNoise(petal, 2, 0) - 0.5) * 0.6;
+ const fallHeight = 0.75 - age * 0.82;
+ return {
+ x: startX + age * 0.6 + Math.sin(age * 9 + petal) * 0.06,
+ y: Math.max(fallHeight, 0.0),
+ z: startZ + age * 0.45 + Math.cos(age * 7 + petal) * 0.05,
+ color: petal % 2 ? hex('#fbd2dc') : hex('#f29ab2'), scale: 0.014,
+ };
+ };
+ return {
+ name: 'koi',
+ mist: { color: hex('#a8d4cc'), alpha: 0.045, low: -0.1, high: 0.3, wispColor: hex('#ffd6e4'), wispHeight: 0.4 },
+ haze: { color: hex('#1e3a40'), lift: 0.15, bloom: 0.75, bloomThreshold: 0.5, soften: 0.35, mistTop: 0.55, mistStrength: 0.22, vignette: 1.1 },
+ paletteExtra: ['#2e5458', '#4e7a7a', '#7eaaa4', '#b6d6ce'],
+ palette,
+ background: { inner: hex('#1c3a40'), outer: hex('#060d10'), stars: false },
+ camera: { target: [0, 0.18, 0], distance: 4.5, height: 2.1, startAngle: 4.0 },
+ staticLists: [island, tree, lantern],
+ dynamic(time) {
+ const lists = [];
+ koi.forEach((fishModel, fish) => {
+ const place = fishPlace(fish, time);
+ lists.push(placeSplats(fishModel, {
+ x: place.x, y: -0.075, z: place.z, heading: place.heading, scale: 1.45,
+ deform: (x, y, z) => [x, y, z + (x < 0 ? Math.sin(time * 7 + fish) * x * x * 2.4 : 0)],
+ }));
+ });
+ const waterPoints = waterGrid.map(([x, z]) => {
+ let ripple = 0;
+ for (let fish = 0; fish < 3; fish++) {
+ const place = fishPlace(fish, time);
+ const distance = Math.hypot(x - place.x, z - place.z);
+ ripple += Math.max(0, 1 - distance / 0.12) * 0.5;
+ }
+ const wave = Math.sin(x * 9 + time * 1.4) * Math.sin(z * 8 - time * 1.1);
+ const color = wave + ripple > 0.75 ? hex('#5cb2a4') : wave > -0.2 ? hex('#2a6e72') : hex('#1c4650');
+ return { x, y: -0.035 + wave * 0.004, z, color, alpha: 0.26, scale: waterSpacing * 0.6 };
+ });
+ lists.push(makeList(waterPoints));
+ const petals = [];
+ for (let petal = 0; petal < 26; petal++) petals.push(petalAt(petal, time));
+ lists.push(makeList(petals));
+ return lists;
+ },
+ glows() {
+ return [{ position: [0.68, 0.25, -0.5], color: hex('#ffd070'), radius: 0.22, strength: 0.45 }];
+ },
+ contactShadows() {
+ return [];
+ },
+ };
+}
+
+function subsetList(list, indices) {
+ const positions = new Float32Array(indices.length * 3);
+ const colors = new Float32Array(indices.length * 4);
+ const scales = new Float32Array(indices.length);
+ indices.forEach((source, target) => {
+ positions.set(list.positions.subarray(source * 3, source * 3 + 3), target * 3);
+ colors.set(list.colors.subarray(source * 4, source * 4 + 4), target * 4);
+ scales[target] = list.scales[source];
+ });
+ return { count: indices.length, positions, colors, scales };
+}
+
+export const SCENE_BUILDERS = { lighthouse: buildLighthouse, snail: buildSnail, koi: buildKoi, medieval: buildMedieval, swamp: buildSwamp, gallows: buildGallows };
diff --git a/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/voxel-splats.js b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/voxel-splats.js
new file mode 100644
index 0000000..b62a406
--- /dev/null
+++ b/plugins/make-pixel-splats/skills/make-pixel-splats/scripts/voxel-splats.js
@@ -0,0 +1,233 @@
+// Turns signed distance models into gaussian splats: every surface voxel of the model becomes one round splat whose
+// colour carries baked light (sun, soft shadow, ambient occlusion, rim). Runs in the browser page.
+
+export function hashNoise(x, y, z) {
+ let value = Math.sin(x * 127.1 + y * 311.7 + z * 74.7) * 43758.5453;
+ return value - Math.floor(value);
+}
+
+export function valueNoise(x, y, z) {
+ const cellX = Math.floor(x);
+ const cellY = Math.floor(y);
+ const cellZ = Math.floor(z);
+ const fractionX = x - cellX;
+ const fractionY = y - cellY;
+ const fractionZ = z - cellZ;
+ const smoothX = fractionX * fractionX * (3 - 2 * fractionX);
+ const smoothY = fractionY * fractionY * (3 - 2 * fractionY);
+ const smoothZ = fractionZ * fractionZ * (3 - 2 * fractionZ);
+ const lerp = (from, to, amount) => from + (to - from) * amount;
+ const corner = (offsetX, offsetY, offsetZ) => hashNoise(cellX + offsetX, cellY + offsetY, cellZ + offsetZ);
+ const bottom = lerp(lerp(corner(0, 0, 0), corner(1, 0, 0), smoothX), lerp(corner(0, 1, 0), corner(1, 1, 0), smoothX), smoothY);
+ const top = lerp(lerp(corner(0, 0, 1), corner(1, 0, 1), smoothX), lerp(corner(0, 1, 1), corner(1, 1, 1), smoothX), smoothY);
+ return lerp(bottom, top, smoothZ);
+}
+
+// Distance helpers. Every one takes the point first.
+export function sphereDistance(x, y, z, centerX, centerY, centerZ, radius) {
+ return Math.hypot(x - centerX, y - centerY, z - centerZ) - radius;
+}
+
+export function ellipsoidDistance(x, y, z, centerX, centerY, centerZ, radiusX, radiusY, radiusZ) {
+ const scaledX = (x - centerX) / radiusX;
+ const scaledY = (y - centerY) / radiusY;
+ const scaledZ = (z - centerZ) / radiusZ;
+ const length = Math.hypot(scaledX, scaledY, scaledZ);
+ return (length - 1) * Math.min(radiusX, radiusY, radiusZ);
+}
+
+export function boxDistance(x, y, z, centerX, centerY, centerZ, halfX, halfY, halfZ, rounding = 0) {
+ const qx = Math.abs(x - centerX) - halfX + rounding;
+ const qy = Math.abs(y - centerY) - halfY + rounding;
+ const qz = Math.abs(z - centerZ) - halfZ + rounding;
+ const outside = Math.hypot(Math.max(qx, 0), Math.max(qy, 0), Math.max(qz, 0));
+ return outside + Math.min(Math.max(qx, qy, qz), 0) - rounding;
+}
+
+export function cylinderDistance(x, y, z, centerX, bottomY, centerZ, radius, height) {
+ const radial = Math.hypot(x - centerX, z - centerZ) - radius;
+ const vertical = Math.abs(y - (bottomY + height / 2)) - height / 2;
+ return Math.min(Math.max(radial, vertical), 0) + Math.hypot(Math.max(radial, 0), Math.max(vertical, 0));
+}
+
+// A cone standing on bottomY: radius goes from bottomRadius to topRadius over height.
+export function taperedCylinderDistance(x, y, z, centerX, bottomY, centerZ, bottomRadius, topRadius, height) {
+ const amount = Math.min(Math.max((y - bottomY) / height, 0), 1);
+ const radius = bottomRadius + (topRadius - bottomRadius) * amount;
+ const radial = (Math.hypot(x - centerX, z - centerZ) - radius) * Math.cos(Math.atan2(Math.abs(bottomRadius - topRadius), height));
+ const vertical = Math.abs(y - (bottomY + height / 2)) - height / 2;
+ return Math.max(radial, vertical);
+}
+
+export function capsuleDistance(x, y, z, startX, startY, startZ, endX, endY, endZ, radius) {
+ const segmentX = endX - startX;
+ const segmentY = endY - startY;
+ const segmentZ = endZ - startZ;
+ const pointX = x - startX;
+ const pointY = y - startY;
+ const pointZ = z - startZ;
+ const lengthSquared = segmentX * segmentX + segmentY * segmentY + segmentZ * segmentZ;
+ const amount = Math.min(Math.max((pointX * segmentX + pointY * segmentY + pointZ * segmentZ) / lengthSquared, 0), 1);
+ return Math.hypot(pointX - segmentX * amount, pointY - segmentY * amount, pointZ - segmentZ * amount) - radius;
+}
+
+export function torusDistance(x, y, z, centerX, centerY, centerZ, majorRadius, minorRadius) {
+ const ring = Math.hypot(x - centerX, z - centerZ) - majorRadius;
+ return Math.hypot(ring, y - centerY) - minorRadius;
+}
+
+export function smoothUnion(first, second, softness) {
+ const blend = Math.min(Math.max(0.5 + 0.5 * (second - first) / softness, 0), 1);
+ return second + (first - second) * blend - softness * blend * (1 - blend);
+}
+
+// A floating island: flat top at topY, tapering to a point below, with noisy rock sides.
+export function islandDistance(x, y, z, radius, depth, topY = 0, roughness = 0.07) {
+ const radial = Math.hypot(x, z);
+ const heightBelowTop = topY - y;
+ const coneRadius = radius * Math.max(1 - Math.pow(Math.max(heightBelowTop, 0) / depth, 1.4), 0);
+ const side = (radial - coneRadius) * 0.7;
+ const top = y - topY;
+ const noise = (valueNoise(x * 6, y * 6, z * 6) - 0.5) * roughness * 2 + (valueNoise(x * 15, y * 15, z * 15) - 0.5) * roughness * 0.6;
+ return Math.max(side, top) + (heightBelowTop > 0.04 ? noise : noise * 0.15);
+}
+
+// A model is { distance(x, y, z), material(x, y, z) -> [r, g, b, emission?], bounds: [minX, minY, minZ, maxX, maxY, maxZ] }.
+// Light is { sun: [x, y, z] (towards the sun), sunColor, ambientColor, rimColor, shadowSoftness }.
+export function voxelizeModel(model, spacing, light, { shadowModel = model, splatScale = 0.62 } = {}) {
+ const [minX, minY, minZ, maxX, maxY, maxZ] = model.bounds;
+ const countX = Math.ceil((maxX - minX) / spacing) + 1;
+ const countY = Math.ceil((maxY - minY) / spacing) + 1;
+ const countZ = Math.ceil((maxZ - minZ) / spacing) + 1;
+ const field = new Float32Array(countX * countY * countZ);
+ const indexOf = (indexX, indexY, indexZ) => (indexZ * countY + indexY) * countX + indexX;
+ for (let indexZ = 0; indexZ < countZ; indexZ++) {
+ for (let indexY = 0; indexY < countY; indexY++) {
+ for (let indexX = 0; indexX < countX; indexX++) {
+ field[indexOf(indexX, indexY, indexZ)] = model.distance(minX + indexX * spacing, minY + indexY * spacing, minZ + indexZ * spacing);
+ }
+ }
+ }
+ const positions = [];
+ const colors = [];
+ const sunLength = Math.hypot(...light.sun);
+ const sunX = light.sun[0] / sunLength;
+ const sunY = light.sun[1] / sunLength;
+ const sunZ = light.sun[2] / sunLength;
+ for (let indexZ = 1; indexZ < countZ - 1; indexZ++) {
+ for (let indexY = 1; indexY < countY - 1; indexY++) {
+ for (let indexX = 1; indexX < countX - 1; indexX++) {
+ if (field[indexOf(indexX, indexY, indexZ)] > 0) continue;
+ const exposed = field[indexOf(indexX + 1, indexY, indexZ)] > 0 || field[indexOf(indexX - 1, indexY, indexZ)] > 0
+ || field[indexOf(indexX, indexY + 1, indexZ)] > 0 || field[indexOf(indexX, indexY - 1, indexZ)] > 0
+ || field[indexOf(indexX, indexY, indexZ + 1)] > 0 || field[indexOf(indexX, indexY, indexZ - 1)] > 0;
+ if (!exposed) continue;
+ const x = minX + indexX * spacing;
+ const y = minY + indexY * spacing;
+ const z = minZ + indexZ * spacing;
+ let normalX = field[indexOf(indexX + 1, indexY, indexZ)] - field[indexOf(indexX - 1, indexY, indexZ)];
+ let normalY = field[indexOf(indexX, indexY + 1, indexZ)] - field[indexOf(indexX, indexY - 1, indexZ)];
+ let normalZ = field[indexOf(indexX, indexY, indexZ + 1)] - field[indexOf(indexX, indexY, indexZ - 1)];
+ const normalLength = Math.hypot(normalX, normalY, normalZ) || 1;
+ normalX /= normalLength;
+ normalY /= normalLength;
+ normalZ /= normalLength;
+ const surface = model.material(x, y, z, normalX, normalY, normalZ);
+ const shaded = shadePoint(shadowModel, x, y, z, normalX, normalY, normalZ, surface, light, sunX, sunY, sunZ, spacing);
+ positions.push(x, y, z);
+ colors.push(shaded[0], shaded[1], shaded[2], surface[4] ?? 1);
+ }
+ }
+ }
+ const count = positions.length / 3;
+ const scales = new Float32Array(count).fill(spacing * splatScale);
+ return { count, positions: new Float32Array(positions), colors: new Float32Array(colors), scales };
+}
+
+function shadePoint(shadowModel, x, y, z, normalX, normalY, normalZ, surface, light, sunX, sunY, sunZ, spacing) {
+ const emission = surface[3] ?? 0;
+ const facing = Math.max(normalX * sunX + normalY * sunY + normalZ * sunZ, 0);
+ let shadow = 1;
+ if (facing > 0) {
+ let travelled = spacing * 2.5;
+ const startX = x + normalX * spacing * 1.2;
+ const startY = y + normalY * spacing * 1.2;
+ const startZ = z + normalZ * spacing * 1.2;
+ for (let step = 0; step < 40 && travelled < 3; step++) {
+ const distance = shadowModel.distance(startX + sunX * travelled, startY + sunY * travelled, startZ + sunZ * travelled);
+ if (distance < spacing * 0.3) {
+ shadow = 0;
+ break;
+ }
+ shadow = Math.min(shadow, light.shadowSoftness * distance / travelled);
+ travelled += Math.max(distance, spacing * 0.5);
+ }
+ shadow = Math.min(Math.max(shadow, 0), 1);
+ }
+ let occlusion = 0;
+ for (let sample = 1; sample <= 4; sample++) {
+ const reach = spacing * 1.6 * sample;
+ const distance = shadowModel.distance(x + normalX * reach, y + normalY * reach, z + normalZ * reach);
+ occlusion += Math.max(reach - distance, 0) / reach / sample;
+ }
+ const ambient = Math.max(1 - occlusion * 0.9, 0.25) * (0.6 + 0.4 * (normalY * 0.5 + 0.5));
+ const rim = Math.pow(Math.max(1 - Math.abs(normalY), 0), 3) * 0.12;
+ const result = [0, 0, 0];
+ for (let channel = 0; channel < 3; channel++) {
+ const lit = surface[channel] * (light.ambientColor[channel] * ambient + light.sunColor[channel] * facing * shadow) + light.rimColor[channel] * rim;
+ result[channel] = lit * (1 - emission) + surface[channel] * emission * 1.3;
+ }
+ return result;
+}
+
+// Concatenate splat lists ({ count, positions, colors (0..1 floats, rgba), scales }) into one 67js splat data object.
+export function packSplatData(lists) {
+ let total = 0;
+ for (const list of lists) total += list.count;
+ const positions = new Float32Array(total * 3);
+ const colors = new Uint8Array(total * 4);
+ const scales = new Float32Array(total * 3);
+ const rotations = new Float32Array(total * 4);
+ const minimum = [Infinity, Infinity, Infinity];
+ const maximum = [-Infinity, -Infinity, -Infinity];
+ let offset = 0;
+ for (const list of lists) {
+ for (let index = 0; index < list.count; index++) {
+ const target = offset + index;
+ for (let axis = 0; axis < 3; axis++) {
+ const value = list.positions[index * 3 + axis];
+ positions[target * 3 + axis] = value;
+ if (value < minimum[axis]) minimum[axis] = value;
+ if (value > maximum[axis]) maximum[axis] = value;
+ scales[target * 3 + axis] = list.scales[index];
+ }
+ for (let channel = 0; channel < 4; channel++) {
+ colors[target * 4 + channel] = Math.round(Math.min(Math.max(list.colors[index * 4 + channel], 0), 1) * 255);
+ }
+ rotations[target * 4] = 1;
+ }
+ offset += list.count;
+ }
+ return { count: total, positions, colors, scales, rotations, bounds: { minimum, maximum } };
+}
+
+// Rigid transform of a local splat list: rotate about Y by heading, then translate.
+export function placeSplats(list, { x = 0, y = 0, z = 0, heading = 0, scale = 1, deform = null }) {
+ const positions = new Float32Array(list.count * 3);
+ const cosine = Math.cos(heading);
+ const sine = Math.sin(heading);
+ for (let index = 0; index < list.count; index++) {
+ let localX = list.positions[index * 3];
+ let localY = list.positions[index * 3 + 1];
+ let localZ = list.positions[index * 3 + 2];
+ if (deform) [localX, localY, localZ] = deform(localX, localY, localZ, index);
+ localX *= scale;
+ localY *= scale;
+ localZ *= scale;
+ positions[index * 3] = x + localX * cosine + localZ * sine;
+ positions[index * 3 + 1] = y + localY;
+ positions[index * 3 + 2] = z - localX * sine + localZ * cosine;
+ }
+ const scales = scale === 1 ? list.scales : list.scales.map(value => value * scale);
+ return { count: list.count, positions, colors: list.colors, scales };
+}