Skip to content

3D engine that AI agents drive

Name the bad field in remesh, unwrapUv and generateGeometryLods (#2441)

Nguyen Xuan An24677e0a4c4a
diff --git a/changes/2441.md b/changes/2441.md
new file mode 100644
index 00000000..9e8f9dee
--- /dev/null
+++ b/changes/2441.md
@@ -0,0 +1 @@
+- The mesh library functions `remeshMesh`, `unwrapUv` and `generateGeometryLods` refuse a bad `resolution`, `voxelSize`, `maxDistortion` or `smoothAngle` with an error that names that field, instead of blaming another field, failing late or ignoring it (#2441, #2442, #2443)
diff --git a/docs/mesh.md b/docs/mesh.md
index f62f49a8..1e9c0fe0 100644
--- a/docs/mesh.md
+++ b/docs/mesh.md
@@ -489,6 +489,8 @@ the input's units) and `relativeError` (over the bounds diagonal); the asset for
Skinned and morphed parts are refused. `surfaceDeviation(a, b)` gives the vertex Hausdorff distance both ways. Hand the levels to a
part as `lods` and `CrucibleRenderer` picks one per instance by its size on screen: see `docs/culling-lod.md`.
+Called from code, `remeshMesh` needs `voxelSize` or `resolution` as a finite number above 0, `unwrapUv` takes `maxDistortion` from 0 to 100 and `generateGeometryLods` takes `smoothAngle` from 0 to 180 degrees; each error names the field.
+
Why our own collapse and not meshoptimizer 1.3.0 (npm, MIT, 2026-09-25): on the same sphere, torus and open bowl at
ratios 0.5, 0.2 and 0.05 our Hausdorff error was 0.25 to 0.85 of its error (0.0088 against 0.0153 at 0.5 and 0.0647 against 0.0773
at 0.05 on the sphere, 0.0403 against 0.1603 at 0.05 on the open bowl with its borders locked), and the volume stayed closer
diff --git a/src/mesh-lod.js b/src/mesh-lod.js
index 3e7ad3b1..c6b768ee 100644
--- a/src/mesh-lod.js
+++ b/src/mesh-lod.js
@@ -4,6 +4,7 @@ import { meshBounds } from './mesh-data.js';
import { meshFromGeometry } from './mesh-convert.js';
import { extractRender } from './mesh-render.js';
import { simplifyGeometry } from './geometry-simplify.js';
+import { requireFiniteInRange } from './modeling-limits.js';
// Levels of detail for runtime selection (issue 37). Level 0 is the input; each next level keeps the given ratio of
// the input's triangles. Every level reports the farthest its vertices lie from the input surface, in the input's
@@ -95,6 +96,7 @@ function generateAttributeLods(geometry, { ratios }) {
* level carries a geometry made by extractRender, with edges sharper than `smoothAngle` degrees kept hard.
*/
export function generateGeometryLods(geometry, { ratios = DEFAULT_LOD_RATIOS, smoothAngle = 30 } = {}) {
+ requireFiniteInRange('generateGeometryLods', 'smoothAngle', smoothAngle, 0, 180);
if (hasPerVertexLook(geometry)) return generateAttributeLods(geometry, { ratios });
const mesh = meshFromGeometry(geometry, { quads: false, tags: 'none' });
return generateMeshLods(mesh, { ratios }).map(({ mesh: levelMesh, ...rest }) => ({
diff --git a/src/mesh-remesh.js b/src/mesh-remesh.js
index eb3b4694..a8268373 100644
--- a/src/mesh-remesh.js
+++ b/src/mesh-remesh.js
@@ -2,6 +2,7 @@ import { createMeshData, meshBounds } from './mesh-data.js';
import { lintMesh } from './mesh-lint.js';
import { edgeCornerTable } from './mesh-topology.js';
import { createSurfaceDistance, triangulatedSurface } from './mesh-surface-distance.js';
+import { requireFiniteInRange } from './modeling-limits.js';
import { placeCellVertices, quadsAroundEdges, splitSaddleVertices } from './surface-nets.js';
// Voxel remesh: sample the signed distance to the surface on a grid, then place one vertex in each cell the surface
@@ -49,8 +50,10 @@ export function remeshGridSize(mesh, { voxelSize, resolution }) {
const extent = bounds.max.map((value, axis) => value - bounds.min[axis]);
const longest = Math.max(...extent);
if ((voxelSize === undefined) === (resolution === undefined)) throw new RangeError('remesh takes either voxelSize or resolution.');
+ if (voxelSize !== undefined) requireFiniteInRange('remesh', 'voxelSize', voxelSize, 1e-12, 1e12);
+ if (resolution !== undefined) requireFiniteInRange('remesh', 'resolution', resolution, 1e-12, 1e12);
+ if (!(longest > 0)) throw new RangeError('remesh needs a mesh with volume.');
const edge = voxelSize ?? longest / resolution;
- if (!(edge > 0) || !(longest > 0)) throw new RangeError('remesh needs a positive voxel size and a mesh with volume.');
const points = extent.map(value => Math.ceil(value / edge) + 1 + PADDING_VOXELS * 2);
const cells = points.reduce((product, count) => product * (count - 1), 1);
if (longest / edge > MAX_REMESH_RESOLUTION || cells > MAX_REMESH_CELLS) {
diff --git a/src/mesh-uv.js b/src/mesh-uv.js
index af4871bc..c33a436a 100644
--- a/src/mesh-uv.js
+++ b/src/mesh-uv.js
@@ -623,6 +623,7 @@ function splitIntoAxisCharts(mesh, islands, seam, table, faceNormal) {
*/
export function unwrapUv(mesh, { seams = { angle: 60 }, margin = 0.01, maxDistortion = 0.25 } = {}) {
requireUvMargin('unwrapUv', margin);
+ requireFiniteInRange('unwrapUv', 'maxDistortion', maxDistortion, 0, 100);
const table = edgeCornerTable(mesh);
const { nextCorner } = cornerCycles(mesh);
const facesOfCorners = cornerFaces(mesh);
diff --git a/tests/mesh-lod-uv-remesh-guards.test.js b/tests/mesh-lod-uv-remesh-guards.test.js
new file mode 100644
index 00000000..077dd5f9
--- /dev/null
+++ b/tests/mesh-lod-uv-remesh-guards.test.js
@@ -0,0 +1,42 @@
+import './support/load-agent-scene-modules.mjs';
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { box, sphere } from '../src/geometry.js';
+import { meshFromGeometry, remeshMesh, unwrapUv, generateGeometryLods } from '../src/mesh.js';
+
+const cube = () => meshFromGeometry(box(0, 1));
+
+test('remesh names the resolution field when the resolution is not a positive number (#2441)', () => {
+ for (const resolution of [0, -4, NaN, Infinity, '8']) {
+ assert.throws(() => remeshMesh(cube(), { resolution }), /remesh: resolution was /, `resolution ${resolution}`);
+ }
+});
+
+test('remesh names the voxelSize field when the voxel size is not a positive number (#2441)', () => {
+ for (const voxelSize of [NaN, Infinity, -1, '0.1']) {
+ assert.throws(() => remeshMesh(cube(), { voxelSize }), /remesh: voxelSize was /, `voxelSize ${voxelSize}`);
+ }
+});
+
+test('remesh still reports a numeric voxel size for valid options (#2441)', () => {
+ const { voxelSize } = remeshMesh(cube(), { voxelSize: 0.1 });
+ assert.equal(voxelSize, 0.1);
+});
+
+test('unwrapUv refuses a maxDistortion that is not a number from 0 to 100 (#2442)', () => {
+ for (const maxDistortion of [NaN, -1, Infinity, 101, 'x']) {
+ assert.throws(() => unwrapUv(cube(), { maxDistortion }), /unwrapUv: maxDistortion was .*from 0 to 100/, `maxDistortion ${maxDistortion}`);
+ }
+ assert.equal(unwrapUv(cube(), { maxDistortion: 0 }).islands, 6);
+});
+
+test('generateGeometryLods checks smoothAngle in degrees before it decimates, for plain and attribute geometry (#2443)', () => {
+ const textured = sphere(16, 8);
+ assert.ok(textured.uvs, 'the sphere carries uvs, so it takes the attribute path');
+ for (const geometry of [box(0.08, 2), textured]) {
+ for (const smoothAngle of [NaN, -10, 181, 'x', Infinity]) {
+ assert.throws(() => generateGeometryLods(geometry, { smoothAngle }), /generateGeometryLods: smoothAngle was .*from 0 to 180/, `smoothAngle ${smoothAngle}`);
+ }
+ }
+ assert.equal(generateGeometryLods(box(0.08, 2), { ratios: [0.5], smoothAngle: 180 }).length, 2);
+});