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(import): far LOD levels may collapse across borders and seams and shed specks, so they reach their budget

yhhfe068ce34adf
diff --git a/pipeline/src/assets/meshSimplify.ts b/pipeline/src/assets/meshSimplify.ts
index 112ecdc2f..e6eb1b588 100644
--- a/pipeline/src/assets/meshSimplify.ts
+++ b/pipeline/src/assets/meshSimplify.ts
@@ -24,8 +24,13 @@ export interface SimplifyOptions {
ratio: number;
/** The most the surface may move, relative to the mesh's extent. */
maxError?: number;
+ /** How freely the collapse may treat the surface. Far levels are looked at small, so they can give up a mesh's open borders, collapse across a seam
+ * and drop specks that are no longer a shape (`Permissive`, `Prune`); a near level keeps its borders (`LockBorder`), where a gap would show. */
+ flags?: SimplifierFlag[];
}
+export type SimplifierFlag = 'LockBorder' | 'Sparse' | 'ErrorAbsolute' | 'Prune' | 'Regularize' | 'Permissive' | 'RegularizeLight';
+
export interface Simplified {
data: MeshData;
triangles: number;
@@ -80,10 +85,11 @@ export async function simplifyMesh(data: MeshData, options: SimplifyOptions): Pr
const triangles = data.indices.length / 3;
const targetTriangles = Math.max(1, Math.floor(triangles * options.ratio));
if (targetTriangles >= triangles) return null;
+ const flags = options.flags ?? ['LockBorder'];
const [kept, error] = attributeStride > 0
? MeshoptSimplifier.simplifyWithAttributes(data.indices, positions, 3, attributes, attributeStride, weights,
- null, targetTriangles * 3, options.maxError ?? 0.05, ['LockBorder'])
- : MeshoptSimplifier.simplify(data.indices, positions, 3, targetTriangles * 3, options.maxError ?? 0.05, ['LockBorder']);
+ null, targetTriangles * 3, options.maxError ?? 0.05, flags)
+ : MeshoptSimplifier.simplify(data.indices, positions, 3, targetTriangles * 3, options.maxError ?? 0.05, flags);
if (kept.length === 0 || kept.length >= data.indices.length) return null;
// Only the vertices the survivors name are carried: a collapse leaves the rest behind.
diff --git a/pipeline/src/assets/modelImport.ts b/pipeline/src/assets/modelImport.ts
index 84511761e..0608e3957 100644
--- a/pipeline/src/assets/modelImport.ts
+++ b/pipeline/src/assets/modelImport.ts
@@ -16,7 +16,7 @@ import {
type MeshData, type MaterialAssetData, type PrefabData, type PrefabEntityData,
type PrefabComponentData as ComponentData,
} from 'esengine';
-import { canSimplify, simplifyMesh } from './meshSimplify';
+import { canSimplify, simplifyMesh, type SimplifierFlag } from './meshSimplify';
import { SPARSE_LIGHTMAP_UV, OVERLAPPING_LIGHTMAP_UV } from '../../../sdk/src/lightmap/atlas';
/** One primitive's worth of geometry, named for the file it will be written to. */
@@ -491,6 +491,8 @@ const LOD_SIZES = [0.4, 0.18, 0.07];
const LOD_MIN_SAVING = 0.15;
/** The collapse may move the surface this far, relative to the mesh's extent. */
const LOD_ERROR = [0.01, 0.03, 0.08];
+/** What each level may do to the surface: the near one keeps its borders; the far ones, seen small, may collapse across them and shed specks. */
+const LOD_FLAGS: SimplifierFlag[][] = [['LockBorder'], ['Prune', 'Permissive'], ['Prune', 'Permissive']];
export interface LodOptions {
/** How many levels to make below the original, 1–3. */
@@ -525,7 +527,7 @@ export async function generateLods(meshes: ImportedMesh[], options: LodOptions):
let above = mesh.triangleCount;
for (let level = 0; level < levels; level++) {
// Collapse from the ORIGINAL each time: stacking collapses compounds their error.
- const made = await simplifyMesh(mesh.data, { ratio: LOD_RATIOS[level]!, maxError: LOD_ERROR[level]! });
+ const made = await simplifyMesh(mesh.data, { ratio: LOD_RATIOS[level]!, maxError: LOD_ERROR[level]!, flags: LOD_FLAGS[level]! });
if (!made || made.triangles > above * (1 - LOD_MIN_SAVING)) break;
out.push({ data: made.data, triangleCount: made.triangles, size: LOD_SIZES[level]! });
above = made.triangles;
diff --git a/pipeline/tests/mesh-simplify.test.ts b/pipeline/tests/mesh-simplify.test.ts
index c2b1fccd8..5d228e985 100644
--- a/pipeline/tests/mesh-simplify.test.ts
+++ b/pipeline/tests/mesh-simplify.test.ts
@@ -46,6 +46,14 @@ function sphere(rings: number, segments: number): MeshData {
}
describe('simplifyMesh', () => {
+ it('collapses further across borders and seams when the level is only seen small', async () => {
+ const src = sphere(32, 48);
+ const near = (await simplifyMesh(src, { ratio: 0.05, maxError: 0.5, flags: ['LockBorder'] }))!;
+ const far = (await simplifyMesh(src, { ratio: 0.05, maxError: 0.5, flags: ['Prune', 'Permissive'] }))!;
+ // Border and seam vertices that a near level holds still are free to go: the target is reached, not stopped short of.
+ expect(far.triangles).toBeLessThan(near.triangles * 0.85);
+ });
+
it('keeps about the share of triangles it was asked for, and fewer vertices', async () => {
const src = sphere(32, 48);
const out = (await simplifyMesh(src, { ratio: 0.25, maxError: 0.5 }))!;