Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.
JavaScript 3d3d-assets3d-modelingagent-skillsagent-toolsai-agentsasset-pipelinecligame-assetsglbgltfmcpmcp-servermodel-context-protocolprocedural-generationprocedural-modelingtext-to-3dthreejstypescript
fix(site): hold the viewer's ground grid still and keep slim spires in frame (#336)
Matthew Kissinger31e19d3a0dce
diff --git a/site/scripts/sitemap-lastmod.mjs b/site/scripts/sitemap-lastmod.mjsindex 7dc67873..984cdf13 100644--- a/site/scripts/sitemap-lastmod.mjs+++ b/site/scripts/sitemap-lastmod.mjs@@ -9,7 +9,11 @@* those files is the page's lastmod. A file with no commit yet (an uncommitted addition) counts as changed at HEAD.* The shared chrome every page carries (`SITE_CHROME`: the base layout's header, footer and share card, and the* site constants with the release label) is not followed: Google asks for the last *significant* change to a page, and- * a navigation link or version label changing on all pages at once would otherwise redate the whole sitemap.+ * a navigation link or version label changing on all pages at once would otherwise redate the whole sitemap. The 3D+ * viewer is left out for the same reason: fixing it changes no asset page's content. That covers its component, the+ * runtime behind its button and what only they import (island-session, viewer-retry, viewer-framing, vehicle-paint and+ * review-rig-three), none of which is in a page's crawled HTML beyond the viewer's own controls, and none of which may+ * import a data record: a test holds that.* Files read at build time from untracked inputs (the restored example archive, generated sources) are pinned by* tracked records, which are the files that carry the date.*/@@ -23,7 +27,7 @@ const SITE = resolve(dirname(fileURLToPath(import.meta.url)), '..');const EXTENSIONS = ['', '.ts', '.tsx', '.mjs', '.js', '.astro', '.json'];const SOURCE = /\.(?:astro|[cm]?[jt]sx?|json)$/;/** Repository paths of the site-wide chrome that no page's lastmod follows (see above). */-export const SITE_CHROME = ['site/src/layouts/BaseLayout.astro', 'site/src/lib/config.ts'];+export const SITE_CHROME = ['site/src/layouts/BaseLayout.astro', 'site/src/lib/config.ts', 'site/src/components/AssetViewer.astro', 'site/src/components/AssetViewerRuntime.tsx'];const IMPORT = /(?:^|[\s;])(?:import|export)\s+(?:[^'"`;]*?\sfrom\s*)?['"]([^'"]+)['"]|\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g;/** The Astro page pattern for a file under src/pages: static segments, `[name]` (one segment) or `[...name]` (any). */diff --git a/site/scripts/sitemap-lastmod.test.ts b/site/scripts/sitemap-lastmod.test.tsindex 7efa82bb..b111b1a6 100644--- a/site/scripts/sitemap-lastmod.test.ts+++ b/site/scripts/sitemap-lastmod.test.ts@@ -1,8 +1,8 @@import { expect, test } from 'bun:test';import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';-import { join } from 'node:path';-import { commitDates, pageForRoute, sitemapLastmod, sourceClosure } from './sitemap-lastmod.mjs';+import { join, resolve, sep } from 'node:path';+import { commitDates, pageForRoute, SITE_CHROME, sitemapLastmod, sourceClosure } from './sitemap-lastmod.mjs';const pages = ['index.astro', '404.astro', 'docs/index.astro', 'docs/[slug].astro', 'gallery/[slug].astro', 'gallery/archive/index.astro', 'gallery/archive/[slug].astro', 'packs/troy/[...page].astro', 'scenes/farm.astro'];@@ -47,6 +47,34 @@ test('commit dates keep the newest commit per path from git log order', () => {expect(Object.fromEntries(dates)).toEqual({ 'site/a.json': '2026-10-25T22:07:37-04:00', 'docs/install.md': '2026-10-25T22:07:37-04:00', 'site/b.ts': '2026-09-01T10:00:00Z' });});+test('nothing skipped as site chrome can carry a page\'s content', () => {+ const site = resolve(import.meta.dir, '..');+ const skipped = SITE_CHROME.map((path) => resolve(site, '..', path)).filter((file) => file.includes(`${sep}components${sep}`));+ expect(skipped.length).toBeGreaterThan(0);+ const reached = skipped.flatMap((file) => sourceClosure(file));+ // Everything the viewer reaches is behind a click; a data record imported here would stop dating the pages it changes.+ expect(reached.filter((file) => file.includes(`${sep}src${sep}data${sep}`))).toEqual([]);+ for (const name of ['AssetViewer.astro', 'island-session.ts', 'viewer-retry.ts', 'AssetViewerRuntime.tsx', 'viewer-framing.ts', 'vehicle-paint.ts', 'review-rig-three.ts']) expect(reached.some((file) => file.endsWith(`${sep}${name}`))).toBe(true);+});++test('a change to the 3D viewer redates no page', () => {+ const root = mkdtempSync(join(tmpdir(), 'kiln-lastmod-'));+ try {+ const site = join(root, 'site');+ for (const directory of ['src/pages', 'src/components', 'src/data']) mkdirSync(join(site, directory), { recursive: true });+ writeFileSync(join(site, 'src/pages/asset.astro'), "import AssetViewer from '../components/AssetViewer.astro';\nimport record from '../data/asset.json';\n");+ writeFileSync(join(site, 'src/components/AssetViewer.astro'), 'const { mountAssetViewer } = await import("./AssetViewerRuntime");\n');+ writeFileSync(join(site, 'src/components/AssetViewerRuntime.tsx'), "import { frameAsset } from './viewer-framing';\n");+ writeFileSync(join(site, 'src/components/viewer-framing.ts'), '');+ writeFileSync(join(site, 'src/data/asset.json'), '{}');+ const log = '\0' + '2026-11-06T18:00:00Z\n\nsite/src/components/AssetViewer.astro\nsite/src/components/AssetViewerRuntime.tsx\nsite/src/components/viewer-framing.ts\n\0' + '2026-10-28T12:00:00Z\n\nsite/src/data/asset.json\nsite/src/pages/asset.astro\n';+ const git = (args: string[]) => (args[0] === 'rev-parse' ? root : args[0] === 'log' && args[1] === '-1' ? '2026-11-07T00:00:00Z' : log);+ expect(sitemapLastmod({ site, git })('/asset/')).toBe('2026-10-28T12:00:00Z');+ } finally {+ rmSync(root, { recursive: true, force: true });+ }+});+test('a page is dated by its newest source but not the site chrome, a doc by its Markdown too, an uncommitted file at HEAD', () => {const root = mkdtempSync(join(tmpdir(), 'kiln-lastmod-'));try {diff --git a/site/scripts/viewer-framing.test.ts b/site/scripts/viewer-framing.test.tsindex a5372cdc..9129ac61 100644--- a/site/scripts/viewer-framing.test.ts+++ b/site/scripts/viewer-framing.test.ts@@ -1,6 +1,54 @@import { expect, test } from 'bun:test';import * as THREE from 'three';-import { AZIMUTH, frameAsset, orbitDirection } from '../src/components/viewer-framing';+import { AZIMUTH, frameAsset, groundGrid, meshTops, orbitDirection, wantsMeshTops } from '../src/components/viewer-framing';++test('a very large model offers each mesh\'s top, so a slim spire stays in the frame', () => {+ const body = new THREE.Mesh(new THREE.BoxGeometry(100, 40, 40));+ body.position.set(0, 20, 0);+ const spire = new THREE.Mesh(new THREE.ConeGeometry(2, 60, 8));+ spire.position.set(0, 70, 0);+ const root = new THREE.Group();+ root.add(body, spire);+ root.updateWorldMatrix(true, true);+ // A coarse stride over the vertices lands on the body and steps over the spire altogether.+ const sampled: THREE.Vector3[] = [];+ const position = body.geometry.getAttribute('position');+ for (let i = 0; i < position.count; i++) sampled.push(new THREE.Vector3().fromBufferAttribute(position, i).applyMatrix4(body.matrixWorld));+ const center = new THREE.Vector3(0, 50, 0);+ const tipInFrame = (points: THREE.Vector3[]) => {+ const framed = frameAsset(points, center, 100, 34, 2);+ const camera = new THREE.PerspectiveCamera(34, 2, 0.1, 5000);+ camera.position.copy(framed.target).addScaledVector(orbitDirection(AZIMUTH), framed.distance * 1.04);+ camera.lookAt(framed.target);+ camera.updateMatrixWorld();+ return new THREE.Vector3(0, 100, 0).project(camera).y < 1;+ };+ expect(tipInFrame(sampled)).toBe(false);+ const tops = meshTops([body, spire]);+ expect(tops.some((point) => point.y === 100)).toBe(true);+ expect(tipInFrame([...sampled, ...tops])).toBe(true);+});++test('only a model past a million vertices offers its mesh tops', () => {+ expect(wantsMeshTops(80_069)).toBe(false);+ expect(wantsMeshTops(1_000_000)).toBe(false);+ expect(wantsMeshTops(2_489_344)).toBe(true);+});++// One unit in the last place of a 32-bit float, which is what the grid's shader has.+const precisionAt = (value: number) => 2 ** (Math.floor(Math.log2(value)) - 23);++test('the ground grid\'s plane is just big enough for its fade and travels with the camera', () => {+ // Reach and cell size both scale with the radius, so one radius stands for all of them.+ const radius = 1042;+ const grid = groundGrid(radius);+ // The fade hides everything further than this from the camera's spot on the ground.+ expect(grid.args[0] / 2).toBeGreaterThanOrEqual(grid.fadeDistance);+ // The orbit limit is 14 radii, so this is the largest coordinate the shader sees.+ const reach = radius * 14 + grid.args[0] / 2;+ expect(precisionAt(reach)).toBeLessThan(grid.cellSize / 1000);+ expect(grid.followCamera).toBe(true);+});for (const aspect of [0.59, 1, 2.4]) {test(`frames a building around the vertical center at aspect ${aspect}`, () => {diff --git a/site/src/components/AssetViewerRuntime.tsx b/site/src/components/AssetViewerRuntime.tsxindex 0f9d60c1..249610a5 100644--- a/site/src/components/AssetViewerRuntime.tsx+++ b/site/src/components/AssetViewerRuntime.tsx@@ -13,7 +13,14 @@ import {reviewToneMapping,toneMappingChoice,} from '../lib/review-rig-three';-import { AZIMUTH, frameAsset, orbitDirection } from './viewer-framing';+import {+ AZIMUTH,+ frameAsset,+ groundGrid,+ meshTops,+ orbitDirection,+ wantsMeshTops,+} from './viewer-framing';import { clearingOnError } from './viewer-retry';import { createVehiclePaint, PAINT_SWATCHES } from './vehicle-paint';@@ -65,6 +72,7 @@ function surfacePoints(root: THREE.Object3D, budget = 4000): THREE.Vector3[] {points.push(v.fromBufferAttribute(pos, i).applyMatrix4(mesh.matrixWorld).clone());}}+ if (wantsMeshTops(total)) points.push(...meshTops(meshes));return points;}@@ -383,15 +391,10 @@ function Viewer({ name, modelUrl, onError, paintAssetId }: ViewerProps) {/>{grid && (<Grid- args={[radius * 10, radius * 10]}- cellSize={radius / 5}+ {...groundGrid(radius)}cellColor="#9ba4b0"- sectionSize={radius}sectionColor="#7b8492"- fadeDistance={radius * 9}fadeStrength={1.4}- infiniteGrid- followCamera={false}/>)}</Canvas>diff --git a/site/src/components/viewer-framing.ts b/site/src/components/viewer-framing.tsindex e6c465ed..aebdd333 100644--- a/site/src/components/viewer-framing.ts+++ b/site/src/components/viewer-framing.ts@@ -13,6 +13,45 @@ export const orbitDirection = (azimuth: number) =>Math.sin(azimuth) * Math.cos(ELEVATION),);+/**+ * The ground grid's size, from the asset's radius.+ *+ * A plane just big enough for the fade, carried along under the camera, rather+ * than drei's `infiniteGrid`. That option multiplies the plane by 1 + fadeDistance,+ * and the fade grows with the asset, so the plane grows with the square of the+ * radius: about 310 km out from a cathedral 83 m in radius, and tens of thousands+ * of kilometres from the Golden Gate Bridge. The shader's 32-bit coordinates can't+ * hold a line still at that scale, so the grid broke into stair-steps and stripes+ * that jumped as the camera moved. `followCamera` keeps the lines fixed in the+ * world while the plane stays small.+ */+export function groundGrid(radius: number) {+ const fadeDistance = radius * 9;+ return {+ args: [radius * 20, radius * 20] as [number, number],+ cellSize: radius / 5,+ sectionSize: radius,+ fadeDistance,+ followCamera: true,+ };+}++/** Past this many vertices, the surface sample below is coarse enough to step over a slim part altogether. */+const MESH_TOPS_ABOVE_VERTICES = 1_000_000;+export const wantsMeshTops = (vertices: number) => vertices > MESH_TOPS_ABOVE_VERTICES;++/**+ * The top of each mesh, added to the sample when framing a very large model. At one+ * vertex in hundreds the sample can miss a spire entirely, and it then sticks out of+ * the frame. Smaller models are framed from their vertices alone, as before.+ */+export function meshTops(meshes: readonly THREE.Object3D[]) {+ return meshes.map((mesh) => {+ const box = new THREE.Box3().setFromObject(mesh);+ return new THREE.Vector3((box.min.x + box.max.x) / 2, box.max.y, (box.min.z + box.max.z) / 2);+ });+}+const AZIMUTH_SAMPLES = 16;/**