Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/core/mesh-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import { MeshoptSimplifier } from '../vendor/meshoptimizer/simplifier';
import { deserializeGeometry } from './geometry';
import type { BinaryGeometry } from './types';
import { hasClosedEdges } from './mesh-topology';
import { RemeshLimitError, sparseRemesh } from './sparse-remesh';
import {
DENSE_REMESH_RESOLUTION,
isRemeshResolution,
MAX_REMESH_RESOLUTION,
MIN_REMESH_RESOLUTION,
SPARSE_REMESH_MAX_TRIANGLES,
SPARSE_REMESH_MAX_VOXELS,
} from './remesh-settings';

export type MeshProcessOptions =
Expand Down Expand Up @@ -78,6 +82,29 @@ function compact(data: BinaryGeometry, indices: Uint32Array): BinaryGeometry {
};
}

/** Above the dense grid, remesh sparsely with quadric vertex placement so fine detail
* stays within a small fraction of a voxel. The result is indexed. */
function sparse(data: BinaryGeometry, resolution: number): BinaryGeometry {
try {
const mesh = sparseRemesh(data.indices as Uint32Array, data.positions, resolution, {
solve: true,
maxVoxels: SPARSE_REMESH_MAX_VOXELS,
maxTriangles: SPARSE_REMESH_MAX_TRIANGLES,
});
if (!mesh.indices.length)
throw new Error('No surface survived at this resolution. Increase voxel resolution.');
return { positions: mesh.positions, indices: mesh.indices };
} catch (error) {
if (error instanceof RemeshLimitError)
throw new Error(
error.limit === 'voxels'
? 'The remesh exceeded the voxel memory limit. Lower voxel resolution.'
: 'The remesh exceeded ten million triangles. Lower voxel resolution.',
);
throw error;
}
}

/** Owns one immutable source snapshot and reuses its welded topology across attempts. */
export class MeshProcessor {
private prepared?: BinaryGeometry;
Expand Down Expand Up @@ -137,6 +164,8 @@ export class MeshProcessor {
'Voxel remeshing requires a closed, manifold mesh. Repair open edges first, or use Decimate.',
);
onProgress?.('Rebuilding surface on voxel grid');
if (options.resolution > DENSE_REMESH_RESOLUTION)
return { mesh: sparse(data, options.resolution), error: null };
const positions = await this.kernels.remesh(data, options.resolution);
if (!positions.length)
throw new Error('No surface survived at this resolution. Increase voxel resolution.');
Expand Down
10 changes: 9 additions & 1 deletion src/core/remesh-settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export const MIN_REMESH_RESOLUTION = 24;
export const DEFAULT_REMESH_RESOLUTION = 128;
export const MAX_REMESH_RESOLUTION = 256;
export const MAX_REMESH_RESOLUTION = 1024;
/** meshoptimizer's dense grid handles resolutions up to this; higher ones use the sparse remesher. */
export const DENSE_REMESH_RESOLUTION = 256;

export function isRemeshResolution(resolution: number) {
return (
Expand All @@ -9,3 +11,9 @@ export function isRemeshResolution(resolution: number) {
resolution <= MAX_REMESH_RESOLUTION
);
}

/** Sparse remeshing keeps about 70 bytes of working data per occupied voxel plus 12 per
* triangle. These caps bound a remesh to under 1 GB and keep its result within the app's
* 10 million triangle tools. A 32 mm sphere fits at the maximum resolution; a cube does not. */
export const SPARSE_REMESH_MAX_VOXELS = 6_000_000;
export const SPARSE_REMESH_MAX_TRIANGLES = 10_000_000;
Loading
Loading