Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(modeling): a decimate can start from the form before its detail, and a mirrored displace takes its spot with it

yhh2e5328973fab
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0aa72e02d..da72a3d57 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,7 +24,7 @@ published separately; it ships inside the editor.
- **An AI building a model can carve fine detail into it.** A `displace` step pushes bumps, wrinkles, cracks, scales, pores, ribs, weave or scratches out of a surface along its normal. A pattern needs no UV and has no seams, can be limited to some faces or to around a point, stacks with others, and splits the surface as far as it needs. Followed by `decimate` and `normalBake`, half a million triangles of detail draw on 1,500.
-- **An AI building a model can make it light and keep its detail.** A `decimate` step thins a model to a share or a number of triangles, keeping paint and tags on the side they were on, and rigs and shapes are found on what it leaves. `normalBake` writes the surface as it stood before that as a normal map on the thinned one, rigged or not: a 77,000-triangle rock at 800 triangles lights close to the original, and the map bakes in about a second.
+- **An AI building a model can make it light and keep its detail.** A `decimate` step thins a model to a share or a number of triangles, keeping paint and tags on the side they were on, and rigs and shapes are found on what it leaves. With `from` it thins the form as it stood before the fine detail, so edges and features keep their triangles. `normalBake` writes the surface as it stood before that as a normal map on the thinned one, rigged or not: a 77,000-triangle rock at 800 triangles lights close to the original, and the map bakes in about a second.
- **A mesh can have 256 joints, up from 64.** A rig with fingers, a face and hair no longer imports as a statue. A draw sends only the joints it has, so a small rig costs what it did.
diff --git a/desktop b/desktop
index 21fa931c6..fb93b94e1 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit 21fa931c61681fb5e7594de72942697b3ddde968
+Subproject commit fb93b94e1310fd28e9e7a7fdc4e91c1e4c5ee50d
diff --git a/pipeline/src/modeling/displace.ts b/pipeline/src/modeling/displace.ts
index d48e5682b..a12c56b70 100644
--- a/pipeline/src/modeling/displace.ts
+++ b/pipeline/src/modeling/displace.ts
@@ -9,7 +9,7 @@
* pattern's finest feature has triangles to land on. That count is what a
* `decimate` and a `normalBake` afterwards exist to pay back.
*/
-import { boundsOf, compact, dot, faceArea, faceNormal, length, normalize, selectFaces, sub, type Face, type FaceSelector, type PolyMesh, type Vec3 } from './polyMesh';
+import { boundsOf, compact, dot, faceArea, faceNormal, normalize, selectFaces, type Face, type FaceSelector, type PolyMesh, type Vec3 } from './polyMesh';
import { hash3 } from './organic';
import { triangulateFace } from './triangulate';
@@ -318,10 +318,14 @@ export function displace(mesh: PolyMesh, p: DisplaceParams, budget = MAX_DISPLAC
if (p.near) {
const { at, radius } = p.near;
// By the box round a face: one large face can cross the ball with every corner far outside it.
+ const across = p.mirror ? { x: 0, y: 1, z: 2 }[p.mirror] : -1;
+ const twin: Vec3 = across === 0 ? [-at[0], at[1], at[2]] : across === 1 ? [at[0], -at[1], at[2]] : across === 2 ? [at[0], at[1], -at[2]] : at;
hit = hit.filter((f) => {
const { min, max } = boundsOf(mesh, mesh.faces[f]!.v);
- const gap = [0, 1, 2].map((k) => Math.max(min[k]! - at[k]!, 0, at[k]! - max[k]!));
- return Math.hypot(gap[0]!, gap[1]!, gap[2]!) < radius;
+ return [at, twin].some((c) => {
+ const gap = [0, 1, 2].map((k) => Math.max(min[k]! - c[k]!, 0, c[k]! - max[k]!));
+ return Math.hypot(gap[0]!, gap[1]!, gap[2]!) < radius;
+ });
});
if (hit.length === 0) throw new Error(`no surface of \`faces\` within ${radius} of ${JSON.stringify(at)}`);
}
@@ -358,17 +362,20 @@ export function displace(mesh: PolyMesh, p: DisplaceParams, budget = MAX_DISPLAC
const warp = p.warp ?? 0;
const positions: Vec3[] = fine.positions.map((at, v) => {
if (!inside[v]) return at;
+ // Mirrored first, so `near` is mirrored with the pattern: one spot asked for on one side is that spot on both.
+ let [x, y, z] = [at[0], at[1], at[2]];
+ if (p.mirror === 'x') x = Math.abs(x);
+ if (p.mirror === 'y') y = Math.abs(y);
+ if (p.mirror === 'z') z = Math.abs(z);
let weight = 1;
if (p.near) {
- const t = 1 - length(sub(at, p.near.at)) / p.near.radius;
+ const c = p.near.at;
+ const toward = (axis: 'x' | 'y' | 'z', k: 0 | 1 | 2, v: number): number => (p.mirror === axis ? Math.abs(c[k]) : v);
+ const t = 1 - Math.hypot(x - toward('x', 0, c[0]), y - toward('y', 1, c[1]), z - toward('z', 2, c[2])) / p.near.radius;
if (t <= 0) return at;
weight = t * t * (3 - 2 * t);
}
const n = normalize([normals[v * 3]!, normals[v * 3 + 1]!, normals[v * 3 + 2]!]);
- let [x, y, z] = [at[0], at[1], at[2]];
- if (p.mirror === 'x') x = Math.abs(x);
- if (p.mirror === 'y') y = Math.abs(y);
- if (p.mirror === 'z') z = Math.abs(z);
x /= sx; y /= sy; z /= sz;
if (warp > 0) {
const [wx, wy, wz] = [
diff --git a/pipeline/src/modeling/modelSource.ts b/pipeline/src/modeling/modelSource.ts
index 040423efd..7d0508d89 100644
--- a/pipeline/src/modeling/modelSource.ts
+++ b/pipeline/src/modeling/modelSource.ts
@@ -97,7 +97,7 @@ export const MODEL_OPS: Record<string, OpSpec> = {
boolean: { params: { operation: 'operation', with: 'faces', from: 'faces', keep: 'boolean', tag: 'string', color: 'color' }, required: ['operation', 'with'], run: booleanOp as unknown as OpSpec['run'], says: 'cuts or joins closed solids: subtract `with` from `from` makes a real hole' },
subdivide: { params: { faces: 'faces', levels: 'integer', crease: 'number' }, required: [], run: subdivide as unknown as OpSpec['run'], says: 'rounds whole shells into smooth surfaces, keeping edges sharper than `crease` degrees' },
displace: { params: { faces: 'faces', near: 'near', pattern: 'pattern', amplitude: 'number', scale: 'number', octaves: 'integer', seed: 'integer', stretch: 'vec3', warp: 'number', width: 'number', axis: 'axis', mirror: 'axis', detail: 'number' }, required: ['pattern', 'amplitude', 'scale'], run: ((mesh: PolyMesh, params: never) => displace(mesh, params)) as OpSpec['run'], says: 'pushes fine detail out of the surface along its normal, splitting faces as far as the pattern needs: bumps, wrinkles, cracks, scales, pores, ribs, weave or scratches' },
- decimate: { params: { faces: 'faces', ratio: 'number', triangles: 'integer', error: 'number' }, required: [], run: decimate as unknown as OpSpec['run'], says: 'leaves fewer triangles over the same surface: `ratio` to keep or `triangles` to end with, stopping sooner if the surface would move more than `error` (default 0.02) of its size' },
+ decimate: { params: { from: 'string', faces: 'faces', ratio: 'number', triangles: 'integer', error: 'number' }, required: [], run: decimate as unknown as OpSpec['run'], says: 'leaves fewer triangles over the same surface: `ratio` to keep or `triangles` to end with, stopping sooner if the surface would move more than `error` (default 0.02) of its size; `from` thins the surface as it stood after that step, before its fine detail' },
uv: { params: { faces: 'faces', mode: 'uvmode', axis: 'string', at: 'vec3', scale: 'number|vec2only', repeat: 'number|vec2only', offset: 'vec2', rotate: 'number' }, required: ['mode'], run: uvStep as unknown as OpSpec['run'], says: 'sets how faces take a texture: box, planar, cylinder or sphere' },
delete: { params: { faces: 'faces' }, required: ['faces'], run: ops.deleteFaces as OpSpec['run'], says: 'removes faces, leaving a hole' },
paint: { params: { faces: 'faces', color: 'color' }, required: ['faces', 'color'], run: ops.paint as OpSpec['run'], says: 'sets vertex colour: linear [r, g, b] in 0..1, multiplied into the material' },
@@ -354,6 +354,7 @@ export function validateModelSource(raw: unknown): ModelSource {
if (bad) refuse(`${key} ${bad}`);
}
for (const need of spec.required) if (step[need] === undefined) refuse(`${need} is required`);
+ if (step.op === 'decimate' && step.from !== undefined && !(source.steps as Partial<ModelStep>[]).slice(0, index).some((s) => s?.id === step.from)) refuse(`from "${String(step.from)}" is no earlier step's id`);
return undefined;
});
return { version: MODEL_SOURCE_VERSION, ...(source as unknown as Omit<ModelSource, 'version'>) };
@@ -388,11 +389,16 @@ export function buildPolyMesh(source: ModelSource): ModelBuild {
const highAfter = !bake ? -2 : bake.from !== undefined ? source.steps.findIndex((s) => s.id === bake.from) : source.steps.map((s) => s.op).lastIndexOf('decimate') - 1;
let high: PolyMesh | undefined;
const notes: string[] = [];
+ // The states a later decimate starts from instead of the one it finds, and only those: a dense model is a lot to keep.
+ const wanted = new Set(source.steps.flatMap((s) => (s.op === 'decimate' && typeof s.from === 'string' ? [s.from] : [])));
+ const kept = new Map<string, PolyMesh>();
source.steps.forEach((step, index) => {
const { id, op, ...params } = step;
- const before = op === 'decimate' ? triangleCount(mesh) : 0;
+ const { from, ...rest } = params as { from?: string };
+ const input = op === 'decimate' && from !== undefined ? kept.get(from)! : mesh;
+ const before = op === 'decimate' ? triangleCount(input) : 0;
try {
- mesh = MODEL_OPS[op]!.run(mesh, params as never);
+ mesh = MODEL_OPS[op]!.run(input, (op === 'decimate' ? rest : params) as never);
} catch (e) {
throw new ModelBuildError(`${stepLabel(step, index)}: ${e instanceof Error ? e.message : String(e)}`, index, id ?? null, op);
}
@@ -402,6 +408,7 @@ export function buildPolyMesh(source: ModelSource): ModelBuild {
}
steps.push({ index, ...(id ? { id } : {}), op, faces: mesh.faces.length, ...(op === 'decimate' ? { trianglesBefore: before, trianglesAfter: triangleCount(mesh) } : {}) });
if (index === highAfter) high = compact(mesh);
+ if (id && wanted.has(id)) kept.set(id, mesh);
});
return { mesh: compact(mesh), steps, notes, ...(high ? { high } : {}) };
}
diff --git a/pipeline/tests/modeling-decimate-bake.test.ts b/pipeline/tests/modeling-decimate-bake.test.ts
index f96cf6618..016b86707 100644
--- a/pipeline/tests/modeling-decimate-bake.test.ts
+++ b/pipeline/tests/modeling-decimate-bake.test.ts
@@ -128,6 +128,31 @@ describe('decimate', () => {
expect(m.data.channels.some((c) => c.semantic === MeshChannel.Joints)).toBe(true);
});
+ it('`from` thins the surface before its fine detail, so the triangles go to the form and not to the noise', () => {
+ const box: ModelStep = { id: 'form', op: 'organic', resolution: 64, blend: 0.02, parts: [{ shape: 'box', size: [2, 2, 2], rounding: 0.05 }] };
+ const noise: ModelStep = { op: 'displace', pattern: 'bumps', amplitude: 0.04, scale: 0.1, octaves: 2 };
+ const after = model(box, noise, { op: 'decimate', triangles: 300, error: 0.5 });
+ const before = model(box, noise, { op: 'decimate', from: 'form', triangles: 300, error: 0.5 });
+ // Its corners are where the form put them: the noise cannot have moved or spent them.
+ const reach = (m: BakedModel): number => Math.max(...m.poly.positions.map((p) => Math.min(Math.abs(p[0]), Math.abs(p[1]), Math.abs(p[2]))));
+ expect(reach(before)).toBeGreaterThan(0.9);
+ expect(reach(before)).toBeLessThanOrEqual(1.001);
+ expect(before.report.steps.at(-1)!.trianglesBefore).toBeLessThan(after.report.steps.at(-1)!.trianglesBefore!);
+ }, 60000);
+
+ it('`from` with a bake carries what the step before it made onto what it left', () => {
+ const m = bakeModel({ version: 1, normalBake: { size: 128 }, steps: [{ ...ball, id: 'form' }, { op: 'displace', pattern: 'bumps', amplitude: 0.04, scale: 0.2, octaves: 2 }, { op: 'decimate', from: 'form', triangles: 300, error: 0.5 }] });
+ const map = bakeNormalMap(m);
+ expect(map.report.from).toBeGreaterThan(map.report.to * 10);
+ expect(map.report.meanBend).toBeGreaterThan(3);
+ expect(map.report.missed).toBeLessThan(0.05);
+ }, 60000);
+
+ it('`from` must name an earlier step', () => {
+ expect(() => validateModelSource({ steps: [ball, { op: 'decimate', from: 'nope', ratio: 0.5 }] })).toThrow(/no earlier step's id/);
+ expect(() => validateModelSource({ steps: [{ op: 'decimate', from: 'later', ratio: 0.5 }, { ...ball, id: 'later' }] })).toThrow(/no earlier step's id/);
+ });
+
it('refuses what cannot be meant', () => {
expect(() => model(ball, { op: 'decimate' })).toThrow(/one of `ratio` and `triangles`/);
expect(() => model(ball, { op: 'decimate', ratio: 0.5, triangles: 10 })).toThrow(/one of `ratio` and `triangles`/);
diff --git a/pipeline/tests/modeling-displace.test.ts b/pipeline/tests/modeling-displace.test.ts
index 2622f337b..48d471b23 100644
--- a/pipeline/tests/modeling-displace.test.ts
+++ b/pipeline/tests/modeling-displace.test.ts
@@ -129,6 +129,14 @@ describe('displace', () => {
expect(pairs).toBeGreaterThan(50);
});
+ it('`mirror` takes `near` with it: a spot named on one side is on both', () => {
+ const m = model(sheet, { op: 'displace', pattern: 'bumps', amplitude: 0.1, scale: 0.15, near: { at: [0.5, 0, 0], radius: 0.3 }, mirror: 'x' });
+ const movedAt = (side: number): number => m.poly.positions.filter((p) => Math.sign(p[0]) === side && Math.abs(p[1]) > 1e-4).length;
+ expect(movedAt(1)).toBeGreaterThan(20);
+ expect(movedAt(-1)).toBe(movedAt(1));
+ for (const p of m.poly.positions) if (Math.abs(p[1]) > 1e-4) expect(Math.hypot(Math.abs(p[0]) - 0.5, p[2])).toBeLessThan(0.31);
+ });
+
it('`detail` sets how far it splits', () => {
const coarse = model(sheet, { op: 'displace', pattern: 'bumps', amplitude: 0.05, scale: 0.5, detail: 0.5 });
const fine = model(sheet, { op: 'displace', pattern: 'bumps', amplitude: 0.05, scale: 0.5, detail: 0.1 });
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index f3bfc15ad..1bba714ef 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -3874,6 +3874,10 @@ export const NOTED = {
{"internal":"a check, for a fix the engine commit carries"},
"perf(lightmap): charts are laid out by their outlines at the largest size that fits, about half of an atlas where it was a third":
{"note":"A lightmap or a baked normal map gets nearly twice as many texels on its model."},
+ "feat(modeling): a decimate can start from the form before its detail, and a mirrored displace takes its spot with it":
+ {"note":"An AI building a model can make it light and keep its detail."},
+ "feat(mcp): model_write teaches thinning the form rather than its detail":
+ {"note":"An AI building a model can make it light and keep its detail."},
"feat(modeling): a displace step carves wrinkles, cracks, scales and weave into a surface":
{"note":"An AI building a model can carve fine detail into it."},
"feat(mcp): model_write carves fine detail, and a check takes every pattern through the bake":