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
feat(kiln/primitives): billboard + imposter primitives
Matt Kissinger733b17b8e099
diff --git a/__tests__/billboard-primitives.test.ts b/__tests__/billboard-primitives.test.tsnew file mode 100644index 00000000..6e92e1ad--- /dev/null+++ b/__tests__/billboard-primitives.test.ts@@ -0,0 +1,97 @@+/**+ * Billboard primitive tests — foliageCardGeo / crossedQuadsGeo / octaGridPlane.+ */++import { describe, expect, test } from 'bun:test';+import * as THREE from 'three';++import {+ crossedQuadsGeo,+ foliageCardGeo,+ octaGridPlane,+} from '../primitives';++function bbox(g: THREE.BufferGeometry): { min: THREE.Vector3; max: THREE.Vector3 } {+ g.computeBoundingBox();+ return { min: g.boundingBox!.min, max: g.boundingBox!.max };+}++describe('foliageCardGeo', () => {+ test('yPivot=0 sits the quad above the ground plane', () => {+ const g = foliageCardGeo({ width: 2, height: 4, yPivot: 0 });+ const { min, max } = bbox(g);+ expect(min.y).toBeCloseTo(0, 5);+ expect(max.y).toBeCloseTo(4, 5);+ });++ test('yPivot=0.5 centers the quad on origin', () => {+ const g = foliageCardGeo({ width: 2, height: 4, yPivot: 0.5 });+ const { min, max } = bbox(g);+ expect(min.y).toBeCloseTo(-2, 5);+ expect(max.y).toBeCloseTo(2, 5);+ });++ test('defaults to a 1×1 card with bottom pivot', () => {+ const g = foliageCardGeo();+ const { min, max } = bbox(g);+ expect(min.y).toBeCloseTo(0, 5);+ expect(max.y).toBeCloseTo(1, 5);+ expect(max.x - min.x).toBeCloseTo(1, 5);+ });+});++describe('crossedQuadsGeo', () => {+ test('planes=2 yields 2 planes × 2 tris = 4 triangles', () => {+ const g = crossedQuadsGeo({ width: 1, height: 1, planes: 2 });+ const tris = (g.index!.count / 3);+ expect(tris).toBe(4);+ });++ test('planes=3 yields 6 triangles', () => {+ const g = crossedQuadsGeo({ width: 1, height: 1, planes: 3 });+ const tris = (g.index!.count / 3);+ expect(tris).toBe(6);+ });++ test('has position, normal, uv attributes', () => {+ const g = crossedQuadsGeo({ width: 1, height: 1, planes: 2 });+ expect(g.attributes.position).toBeTruthy();+ expect(g.attributes.normal).toBeTruthy();+ expect(g.attributes.uv).toBeTruthy();+ });+});++describe('octaGridPlane', () => {+ test('UVs cover 1/tilesX × 1/tilesY top-left rect', () => {+ const g = octaGridPlane({ tilesX: 4, tilesY: 4, width: 1, height: 1 });+ const uv = g.attributes.uv!;+ let minU = Infinity,+ maxU = -Infinity,+ minV = Infinity,+ maxV = -Infinity;+ for (let i = 0; i < uv.count; i++) {+ minU = Math.min(minU, uv.getX(i));+ maxU = Math.max(maxU, uv.getX(i));+ minV = Math.min(minV, uv.getY(i));+ maxV = Math.max(maxV, uv.getY(i));+ }+ expect(minU).toBeCloseTo(0, 5);+ expect(maxU).toBeCloseTo(0.25, 5);+ // Top-left tile — V spans [0.75, 1.0] with top row convention.+ expect(minV).toBeCloseTo(0.75, 5);+ expect(maxV).toBeCloseTo(1.0, 5);+ });++ test('tilesX=8 tilesY=4 → quarter-height, eighth-width UV rect', () => {+ const g = octaGridPlane({ tilesX: 8, tilesY: 4, width: 1, height: 1 });+ const uv = g.attributes.uv!;+ let maxU = -Infinity;+ let minV = Infinity;+ for (let i = 0; i < uv.count; i++) {+ maxU = Math.max(maxU, uv.getX(i));+ minV = Math.min(minV, uv.getY(i));+ }+ expect(maxU).toBeCloseTo(1 / 8, 5);+ expect(minV).toBeCloseTo(1 - 1 / 4, 5);+ });+});diff --git a/index.ts b/index.tsindex 2224a1df..dbf6f45b 100644--- a/index.ts+++ b/index.ts@@ -87,6 +87,9 @@ export {createWingPair,beamBetween,createLadder,+ foliageCardGeo,+ crossedQuadsGeo,+ octaGridPlane,gameMaterial,basicMaterial,glassMaterial,diff --git a/list-primitives.ts b/list-primitives.tsindex 414c1cdb..c95014ef 100644--- a/list-primitives.ts+++ b/list-primitives.ts@@ -262,6 +262,36 @@ const PRIMITIVES: PrimitiveSpec[] = [example:"const star = decalBox(0.18, 0.18, 0.01);\ncreatePart('Mesh_StarPort', star, gameMaterial(0xc61f2a), { position: [0.4, 0.6, 0.41], parent: fuselage });",},+ {+ name: 'foliageCardGeo',+ signature: 'foliageCardGeo(opts?: { width?, height?, yPivot?: 0..1 })',+ returns: 'THREE.PlaneGeometry',+ category: 'geometry',+ description:+ 'Single-quad foliage card with a configurable Y pivot. yPivot=0 plants the quad on the ground. Pair with an alpha-tested material and a leaf/plant sprite.',+ example:+ "const quad = foliageCardGeo({ width: 4, height: 6, yPivot: 0 });\ncreatePart('Mesh_Fern', quad, gameMaterial(0x3a6b2e), { parent: root });",+ },+ {+ name: 'crossedQuadsGeo',+ signature: 'crossedQuadsGeo(opts?: { width?, height?, planes?: 2 | 3, yPivot? })',+ returns: 'THREE.BufferGeometry',+ category: 'geometry',+ description:+ 'Cross-billboard bush primitive: 2 or 3 planes intersecting along the Y axis. Reads as a dense plant from any angle, cheaper than real geometry.',+ example:+ "const bush = crossedQuadsGeo({ width: 2, height: 2, planes: 3 });\ncreatePart('Mesh_Bush', bush, gameMaterial(0x2f4a1a), { parent: root });",+ },+ {+ name: 'octaGridPlane',+ signature: 'octaGridPlane({ tilesX, tilesY, width?, height?, yPivot? })',+ returns: 'THREE.PlaneGeometry',+ category: 'geometry',+ description:+ 'Atlas-ready billboard quad. UVs are pre-scaled to cover one tile of a tilesX×tilesY atlas; the consumer shader adds per-instance tile offsets at draw time.',+ example:+ "const card = octaGridPlane({ tilesX: 4, tilesY: 4, width: 6, height: 6 });",+ },{name: 'wingGeo',signature:diff --git a/primitives.ts b/primitives.tsindex ccb4d312..15a93db2 100644--- a/primitives.ts+++ b/primitives.ts@@ -254,6 +254,150 @@ export function decalBox(return new THREE.BoxGeometry(width, height, d);}+// =============================================================================+// Billboard / imposter primitives+// =============================================================================++export interface FoliageCardOptions {+ /** Quad width in world units. */+ width?: number;+ /** Quad height in world units. */+ height?: number;+ /**+ * Vertical pivot as a fraction of height: 0 = bottom, 0.5 = center, 1 = top.+ * Foliage usually wants 0 so the card plants on the ground.+ */+ yPivot?: number;+}++/**+ * Double-sided quad for foliage / vegetation cards. Y-up facing +Z by default.+ * The pivot is placed at `yPivot * height` below the quad center — set 0 to+ * have the card root on the ground plane.+ *+ * Use with an alpha-tested material for leaves and billboard plants.+ */+export function foliageCardGeo(opts: FoliageCardOptions = {}): THREE.PlaneGeometry {+ const w = opts.width ?? 1;+ const h = opts.height ?? 1;+ const yPivot = opts.yPivot ?? 0;+ const geom = new THREE.PlaneGeometry(w, h);+ // Shift so the pivot offset becomes the local origin.+ geom.translate(0, h * (0.5 - yPivot), 0);+ return geom;+}++export interface CrossedQuadsOptions {+ width?: number;+ height?: number;+ /** Number of intersecting planes. 2 = X-cross, 3 = triple-star. Default 2. */+ planes?: 2 | 3;+ /** Y pivot fraction of height (see foliageCardGeo). Default 0. */+ yPivot?: number;+}++/**+ * Two or three planes intersecting along the Y axis — the classic "bush" or+ * "cross-billboard" near-field vegetation primitive. Cheaper than real+ * geometry, denser-looking than a single foliage card from any angle.+ */+export function crossedQuadsGeo(opts: CrossedQuadsOptions = {}): THREE.BufferGeometry {+ const w = opts.width ?? 1;+ const h = opts.height ?? 1;+ const planes = opts.planes ?? 2;+ const yPivot = opts.yPivot ?? 0;++ const geometries: THREE.PlaneGeometry[] = [];+ for (let i = 0; i < planes; i++) {+ const angle = (i / planes) * Math.PI;+ const quad = new THREE.PlaneGeometry(w, h);+ quad.translate(0, h * (0.5 - yPivot), 0);+ quad.rotateY(angle);+ geometries.push(quad);+ }++ // Merge into one BufferGeometry — deterministic + cheaper at draw time.+ const out = new THREE.BufferGeometry();+ const posCount = geometries.reduce(+ (sum, g) => sum + (g.attributes.position?.count ?? 0),+ 0,+ );+ const positions = new Float32Array(posCount * 3);+ const normals = new Float32Array(posCount * 3);+ const uvs = new Float32Array(posCount * 2);+ const indices: number[] = [];+ let vOffset = 0;+ let iOffset = 0;+ for (const g of geometries) {+ const p = g.attributes.position!;+ const n = g.attributes.normal!;+ const uv = g.attributes.uv!;+ for (let i = 0; i < p.count; i++) {+ positions[(vOffset + i) * 3] = p.getX(i);+ positions[(vOffset + i) * 3 + 1] = p.getY(i);+ positions[(vOffset + i) * 3 + 2] = p.getZ(i);+ normals[(vOffset + i) * 3] = n.getX(i);+ normals[(vOffset + i) * 3 + 1] = n.getY(i);+ normals[(vOffset + i) * 3 + 2] = n.getZ(i);+ uvs[(vOffset + i) * 2] = uv.getX(i);+ uvs[(vOffset + i) * 2 + 1] = uv.getY(i);+ }+ const idx = g.index!;+ for (let i = 0; i < idx.count; i++) {+ indices.push(idx.getX(i) + vOffset);+ }+ vOffset += p.count;+ iOffset += idx.count;+ }+ out.setAttribute('position', new THREE.BufferAttribute(positions, 3));+ out.setAttribute('normal', new THREE.BufferAttribute(normals, 3));+ out.setAttribute('uv', new THREE.BufferAttribute(uvs, 2));+ out.setIndex(indices);+ return out;+}++export interface OctaGridPlaneOptions {+ /** Atlas tile grid X. */+ tilesX: number;+ /** Atlas tile grid Y. */+ tilesY: number;+ /** Quad width in world units. Default 1. */+ width?: number;+ /** Quad height in world units. Default 1. */+ height?: number;+ /** 0 = bottom pivot, 0.5 = center, 1 = top. Default 0. */+ yPivot?: number;+}++/**+ * Atlas-ready billboard quad — same geometry as foliageCardGeo but the UV+ * rect is [0, 1/tilesX]×[1 - 1/tilesY, 1] so a shader using viewDir->tileIdx+ * can offset the UVs per-instance to sample the right imposter tile.+ *+ * The returned geometry has its UVs pre-scaled to cover exactly one tile.+ * The shader (TIJ billboard material) is expected to add (tileX/tilesX,+ * tileY/tilesY) to each vertex's UV at draw time.+ */+export function octaGridPlane(opts: OctaGridPlaneOptions): THREE.PlaneGeometry {+ const w = opts.width ?? 1;+ const h = opts.height ?? 1;+ const yPivot = opts.yPivot ?? 0;+ const geom = new THREE.PlaneGeometry(w, h);+ geom.translate(0, h * (0.5 - yPivot), 0);++ // Scale UVs so the quad covers one tile-sized rect in the top-left corner.+ const uv = geom.attributes.uv!;+ const du = 1 / opts.tilesX;+ const dv = 1 / opts.tilesY;+ for (let i = 0; i < uv.count; i++) {+ uv.setX(i, uv.getX(i) * du);+ // Top row — start at 1 - dv so the default samples the top-left tile.+ uv.setY(i, 1 - dv + uv.getY(i) * dv);+ }+ uv.needsUpdate = true;+ return geom;+}+export interface WingGeometryOptions {span?: number;rootChord?: number;@@ -881,6 +1025,9 @@ export function buildSandboxGlobals(torusGeo: wrap('torusGeo', torusGeo),planeGeo: wrap('planeGeo', planeGeo),decalBox: wrap('decalBox', decalBox),+ foliageCardGeo: wrap('foliageCardGeo', foliageCardGeo),+ crossedQuadsGeo: wrap('crossedQuadsGeo', crossedQuadsGeo),+ octaGridPlane: wrap('octaGridPlane', octaGridPlane),wingGeo: wrap('wingGeo', wingGeo),createWingPair: wrap('createWingPair', createWingPair),beamBetween: wrap('beamBetween', beamBetween),