Skip to content

armgit / kiln

Public Imported from GitHub
0

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.

fix(uv): give cylindrical projectUV poles their triangle's angle (#178)

Matt Kissingercada2722df7f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bc875655..b8705fce 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -80,6 +80,18 @@ does not publish a package.
`PROFILE_BEVEL_COLLAPSE` still names an over-large bevel. The generic rejection now says
that `kiln_validate` checks syntax and static rules but cannot see execution-time
failures, and points to reproducing them with `kiln_render`.
+- Map cylindrical `projectUV` poles and name profile solid failures
+ ([#178](https://github.com/instruktlabs/kiln/issues/178)). A bevelled `revolveProfile`
+ whose outline touches the axis ends in sloped triangles meeting on it; cylindrical
+ `projectUV` gave that pole no defined angle, so the mesh smeared U or, at some segment
+ counts, threw and rejected the whole program. A corner on the axis now takes the mean U of
+ the other corners of its triangle; cone apexes are treated the same way. Separately, an `extrudeProfile` or
+ `revolveProfile` outline that cannot become a solid (for example out-of-scale coordinates,
+ or holes that consume it) now rejects with the closed cause `PROFILE_SOLID_FAILED` and
+ engine-written repair advice instead of the bare generic rejection; no library message
+ crosses the boundary, and `PROFILE_BEVEL_COLLAPSE` still names an over-large bevel. The
+ generic rejection now says that `kiln_validate` checks syntax and static rules but cannot
+ see execution-time failures, and points to reproducing them with `kiln_render`.
## 1.4.0 (2026-10-23)
diff --git a/src/__tests__/uv-projection.test.ts b/src/__tests__/uv-projection.test.ts
index 0185b660..1c6f0a31 100644
--- a/src/__tests__/uv-projection.test.ts
+++ b/src/__tests__/uv-projection.test.ts
@@ -3,6 +3,9 @@ import * as THREE from 'three';
import { NodeIO } from '@gltf-transform/core';
import { renderSceneToGLB } from '../render';
import { projectUV } from '../uv-project';
+import { revolveProfile } from '../profile';
+import { evaluateEvaluatorRequestV2 } from '../evaluator/handler';
+import { createEvaluatorRequestV2 } from '../evaluator/protocol';
test('planar projection is frame-covariant, owned, and replaces unrelated existing UVs', () => {
const source = new THREE.PlaneGeometry(2, 3);
@@ -272,3 +275,73 @@ test('metric cylindrical projection keeps the full-wrap seam on a whole tile', (
);
expect(Math.max(...halfU)).toBeCloseTo((Math.PI * 0.4) / 0.3, 5);
});
+
+/** Per-triangle U spans of cylindrical side triangles, after checking every UV is finite. */
+function sideUSpans(projected: THREE.BufferGeometry): number[] {
+ const uv = projected.getAttribute('uv');
+ const p = projected.getAttribute('position');
+ const spans: number[] = [];
+ for (let i = 0; i < uv.count; i++) {
+ expect(Number.isFinite(uv.getX(i))).toBe(true);
+ expect(Number.isFinite(uv.getY(i))).toBe(true);
+ }
+ for (let i = 0; i < p.count; i += 3) {
+ const y = [p.getY(i), p.getY(i + 1), p.getY(i + 2)];
+ if (Math.max(...y) - Math.min(...y) < 1e-6) continue;
+ const u = [uv.getX(i), uv.getX(i + 1), uv.getX(i + 2)];
+ spans.push(Math.max(...u) - Math.min(...u));
+ }
+ return spans;
+}
+
+test('cylindrical poles on the axis take their triangle angle (#178)', async () => {
+ // Reduced from the grotto dogfood: a bevelled revolve whose outline touches x = 0 ends in
+ // a fan of sloped triangles meeting on the axis. At 14 and 18 segments the pole's
+ // undefined angle threw "more than half a revolution"; at other counts it smeared U.
+ for (const segments of [12, 14, 16, 18, 24]) {
+ const geometry = await revolveProfile(
+ [
+ [0, 0],
+ [0.2, 0],
+ [0.2, 1],
+ [0, 1],
+ ],
+ { segments, bevel: 0.01 },
+ );
+ const projected = projectUV(geometry, { projection: 'cylindrical' });
+ const spans = sideUSpans(projected);
+ expect(spans.length).toBeGreaterThan(0);
+ for (const span of spans) expect(span).toBeLessThanOrEqual(1.5 / segments);
+ const uv = projected.getAttribute('uv');
+ for (let i = 0; i < uv.count; i++) {
+ expect(uv.getX(i)).toBeGreaterThanOrEqual(0);
+ expect(uv.getX(i)).toBeLessThanOrEqual(1 + 1 / segments);
+ }
+ }
+ // Without a bevel, a cone apex is the same pole: one vertex per side triangle on the axis.
+ const cone = projectUV(new THREE.ConeGeometry(1, 2, 12), { projection: 'cylindrical' });
+ for (const span of sideUSpans(cone)) expect(span).toBeLessThanOrEqual(1.5 / 12);
+});
+
+test('a cylindrical triangle lying entirely on the axis gets one consistent finite U', () => {
+ const source = new THREE.BufferGeometry();
+ source.setAttribute(
+ 'position',
+ new THREE.Float32BufferAttribute([0, 0, 0, 0, 1, 0, 0, 2, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0], 3),
+ );
+ const uv = projectUV(source, { projection: 'cylindrical', caps: 'side' }).getAttribute('uv');
+ for (let i = 0; i < uv.count; i++) expect(Number.isFinite(uv.getX(i))).toBe(true);
+ expect(uv.getX(1)).toBe(uv.getX(0));
+ expect(uv.getX(2)).toBe(uv.getX(0));
+});
+
+test('a grotto-shaped bevelled column with cylindrical UVs renders through the worker', async () => {
+ const column =
+ '[[0,0.06],[0.24,0.06],[0.26,0.24],[0.18,0.48],[0.16,0.66],[0.12,0.96],[0.11,1.38],[0.15,1.52],[0,1.52]]';
+ const code = `async function build(){const root=createRoot('Beacon');createPart('Column',projectUV(await revolveProfile(${column},{segments:18,bevel:0.01}),{projection:'cylindrical'}),gameMaterial(0x888888),{parent:root});return root;}`;
+ const wire = JSON.parse(
+ await evaluateEvaluatorRequestV2(createEvaluatorRequestV2({ requestId: 'column', code }).json),
+ );
+ expect(wire.error).toBeUndefined();
+ expect(wire.ok).toBe(true);
+}, 20000);
diff --git a/src/discovery/helper-contracts.ts b/src/discovery/helper-contracts.ts
index b0fa226a..85abe37a 100644
--- a/src/discovery/helper-contracts.ts
+++ b/src/discovery/helper-contracts.ts
@@ -2667,7 +2667,7 @@ define(
'By default planar/box axes and cylindrical height normalize each geometry extent to 0..1, so texel density changes with part size. metersPerUnit (positive length) instead maps one UV unit to that length measured from the frame origin: parts of any size tile at the same density, and parts sharing a frame keep the same phase. A full cylindrical wrap rounds its circumference at the largest radius to whole tiles so the seam stays continuous; a partial angularRange uses the exact arc length.',
'Output is non-indexed. Weld with mergeVertices afterwards where the projection is continuous (one planar face, cylindrical sides); it keeps corners split where UVs or normals differ, such as box faces and the cylindrical seam.',
'projection is required. frame uses origin/rotation. Cylinder seamDegrees defaults to 180, measured from +X toward +Z; angularRange is [start degrees, positive sweep <=360] instead of seamDegrees.',
- 'Cylindrical caps default to planar for triangle normals within 1e-6 of frame Y; caps:side uses side mapping. Partial ranges must cover side vertices, allowing 1e-6 radians of Float32 boundary roundoff.',
+ 'Cylindrical caps default to planar for triangle normals within 1e-6 of frame Y; caps:side uses side mapping. Partial ranges must cover side vertices, allowing 1e-6 radians of Float32 boundary roundoff. A side corner on the axis (within 1e-6 of the largest radius: a cone apex, or a revolved profile touching x = 0) takes the mean U of the other corners of its triangle.',
'Nonempty finite positions, valid triangle indices and matching per-vertex attributes; morph targets are unsupported. At most 2000000 source samples/corners and 128 MiB expanded attribute storage.',
],
ownership: 'Owned non-indexed geometry and copied attributes/metadata; input unchanged.',
diff --git a/src/uv-project.ts b/src/uv-project.ts
index 98d12873..c06f751b 100644
--- a/src/uv-project.ts
+++ b/src/uv-project.ts
@@ -21,6 +21,11 @@ export interface ProjectUVOptions {
const MAX_CORNERS = 2_000_000;
const MAX_ATTRIBUTE_BYTES = 128 * 1024 * 1024;
+/**
+ * Cylindrical corners within this fraction of the largest radius lie on the axis (poles).
+ * Relative, so Float32 and frame-rebasing noise on the axis of any size still counts.
+ */
+const POLE_RADIUS_FRACTION = 1e-6;
export function projectUV(
geometry: THREE.BufferGeometry,
@@ -134,9 +139,12 @@ export function projectUV(
const wrap = (angle: number) => ((angle % (2 * Math.PI)) + 2 * Math.PI) % (2 * Math.PI);
// U per unit of angular fraction: 1 when fitted; metric arc length at the largest radius,
// rounded to whole tiles on a full wrap so the seam meets itself.
+ const radius = cylinder
+ ? points.reduce((max, point) => Math.max(max, Math.hypot(point.x, point.z)), 0)
+ : 0;
+ const poleRadius = radius * POLE_RADIUS_FRACTION;
let uScale = 1;
if (metric !== undefined) {
- const radius = points.reduce((max, point) => Math.max(max, Math.hypot(point.x, point.z)), 0);
uScale = fullWrap
? Math.max(1, Math.round((2 * Math.PI * radius) / metric))
: (sweep * radius) / metric;
@@ -154,7 +162,13 @@ export function projectUV(
const cap = cylinder && options.caps !== 'side' && Math.abs(normal.y) >= 1 - 1e-6;
let pairs: [number, number][];
if (cylinder && !cap) {
- const angles = triangle.map((point) => {
+ // A corner on the axis (a pole: a cone apex, or the centre of a revolved profile
+ // that touches x = 0) has no angle of its own. It takes the mean of the other corners'
+ // unwrapped angles below, as sphere and cylinder UV generators treat poles. Corners are
+ // already split per triangle, so each fan triangle gets its own pole U (#178).
+ const poles = triangle.map((point) => Math.hypot(point.x, point.z) <= poleRadius);
+ const angles = triangle.map((point, j) => {
+ if (poles[j]) return 0;
let angle = wrap(Math.atan2(point.z, point.x) - start);
// Float32 positions and frame rebasing introduce angular boundary roundoff.
if (angle > 2 * Math.PI - 1e-6) angle = 0;
@@ -164,12 +178,19 @@ export function projectUV(
);
return (fullWrap ? angle : Math.min(angle, sweep)) / sweep;
});
- if (fullWrap && Math.max(...angles) - Math.min(...angles) > 0.5)
- for (let j = 0; j < 3; j++) if (angles[j]! < 0.5) angles[j]! += 1;
- if (fullWrap && Math.max(...angles) - Math.min(...angles) > 0.5 + 1e-6)
+ const sided = [0, 1, 2].filter((j) => !poles[j]);
+ const span = () =>
+ sided.length
+ ? Math.max(...sided.map((j) => angles[j]!)) - Math.min(...sided.map((j) => angles[j]!))
+ : 0;
+ if (fullWrap && span() > 0.5) for (const j of sided) if (angles[j]! < 0.5) angles[j]! += 1;
+ if (fullWrap && span() > 0.5 + 1e-6)
throw new Error(
'projectUV: side triangle spans more than half a revolution; add profile segments to resolve the mapping.',
);
+ // A triangle lying entirely on the axis is degenerate: every corner takes U 0.
+ const pole = sided.length ? sided.reduce((sum, j) => sum + angles[j]!, 0) / sided.length : 0;
+ for (let j = 0; j < 3; j++) if (poles[j]) angles[j] = pole;
pairs = triangle.map((point, j) => [angles[j]! * uScale, fit(point, 'y')]);
} else if (cap) {
pairs = triangle.map((point) => [fit(point, 'x'), fit(point, 'z')]);