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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,10 @@ to change geometry explicitly:
- **Decimate** reduces triangles toward a percentage target within an approximate error limit
expressed as a percentage of mesh size. Open borders are preserved by default. Vertex colors
are retained, but this is not a guarantee that a precision mating surface remains unchanged.
- **Voxel remesh** rebuilds a closed, manifold mesh at a resolution of 24–128. It can remove thin
details and change mating surfaces. Vertex colors are removed; material tint remains.
- **Voxel remesh** rebuilds a closed, manifold mesh at a resolution of 24–256 (default 128).
Higher values use smaller voxels and can preserve finer features. Lower the resolution if the
job reaches the voxel memory or one-million-triangle limit. Remeshing can remove thin details
and change mating surfaces. It removes vertex colors and retains material tint.

**Prepare mesh** processes in a worker and reports the actual triangle count before applying.
Cancel stops the worker. By default, applying creates an independent part in the same position
Expand Down
2 changes: 1 addition & 1 deletion docs/acceleration-opportunities.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ Run `npm test`, `npm run build`, `npm run test:e2e:acceleration`, and the mesh-p

## Further work

No SIMD speedup is claimed. The vendored simplifier's embedded WASM is unchanged; only its remesh binding is patched. A SIMD build needs separate profiling and equivalence checks. Native adjacency kernels, GPU solid operations, inspection surface-band batching and a GPU MCP snapshot renderer remain separate projects. The previous Manifold experiment changed triangulation and did not consistently beat the existing engine, so the Boolean engine is retained.
The vendored simplifier's WASM includes the same 128 MiB occupied-voxel limit as the native remesher. A SIMD build needs separate profiling and equivalence checks. Native adjacency kernels, GPU solid operations, inspection surface-band batching and a GPU MCP snapshot renderer remain separate projects. The previous Manifold experiment changed triangulation and did not consistently beat the existing engine, so the Boolean engine is retained.
13 changes: 7 additions & 6 deletions native/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Optional native mesh kernels

`meshoptimizer/` contains unmodified source files from `zeux/meshoptimizer` tag
`v1.3`, commit `9e1f07b159d3cb777f1c67ed31fc11fd117986f4`, under its included MIT
license. Keep these aligned with `src/vendor/meshoptimizer/`.
`meshoptimizer/` contains source files from `zeux/meshoptimizer` tag `v1.3`,
commit `9e1f07b159d3cb777f1c67ed31fc11fd117986f4`, under its included MIT license.
The remesher has a Kitbasher patch that caps occupied-voxel storage at 128 MiB
before allocation. Keep this patch aligned with `src/vendor/meshoptimizer/`.

`mesh-kernel.cpp` is Kitbasher's bounded, one-job binary stdin/stdout adapter.
It reduces indexed meshes, including colors and locked borders, or remeshes into
an arena of at most one million triangles. It validates dimensions, finite data
and index bounds before entering the kernel. It reads no scene files and opens
no network connections.
an arena of at most one million triangles at resolutions 24 through 256. It
validates dimensions, finite data and index bounds before entering the kernel.
It reads no scene files and opens no network connections.

Build with `npm run build:native`. Use `KITBASHER_NATIVE_MESH=1` when launching the
local host to opt into native reduction; `all` additionally enables remeshing
Expand Down
3 changes: 2 additions & 1 deletion native/mesh-kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main() {
count<3 || count>30000000 || count%3 || colors>1 || lock>1 ||
!std::isfinite(error) || error<0 || error>1 ||
(mode==0 && (target<3 || target>count || target%3)) ||
(mode==1 && (resolution<24 || resolution>128))) return fail("Invalid mesh kernel request.");
(mode==1 && (resolution<24 || resolution>256))) return fail("Invalid mesh kernel request.");
const size_t bytes=size_t(vertices)*12*(colors?2:1)+size_t(count)*4;
if(bytes>512u*1024u*1024u) return fail("Native mesh request exceeds the memory limit.");
std::vector<float> positions(size_t(vertices)*3), attributes(colors?size_t(vertices)*3:0);
Expand All @@ -49,6 +49,7 @@ int main() {
const size_t capacity=1000000;
surface.resize(capacity*9);
result=meshopt_remesh(surface.data(),capacity,indices.data(),count,positions.data(),vertices,12,resolution,0);
if(result==size_t(-1)) return fail("The remesh exceeded the voxel memory limit. Lower voxel resolution.");
if(result>capacity) return fail("The remesh exceeded one million triangles. Lower voxel resolution.");
result*=9;
}
Expand Down
4 changes: 4 additions & 0 deletions native/meshoptimizer/remesher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,10 @@ size_t meshopt_remesh(float* destination, size_t max_triangle_count, const unsig

if (destination)
{
// Kitbasher: bound occupied-voxel scratch before allocating it.
if (voxel_count > (128u * 1024u * 1024u) / sizeof(Voxel))
return size_t(-1);

voxels = allocator.allocate<Voxel>(voxel_count);
memset(voxels, 0, voxel_count * sizeof(Voxel));
}
Expand Down
22 changes: 15 additions & 7 deletions src/components/MeshProcessingDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { useEffect, useRef, useState } from 'react';
import type { BufferGeometry } from 'three';
import { triangleCount } from '../core/geometry';
import { processMeshInWorker } from '../core/mesh-job';
import {
DEFAULT_REMESH_RESOLUTION,
isRemeshResolution,
MAX_REMESH_RESOLUTION,
MIN_REMESH_RESOLUTION,
} from '../core/remesh-settings';
import { useActivity } from '../core/activity';
import { useEditor } from '../core/store';
import { Modal, RadioGroup } from './ui';
Expand All @@ -17,7 +23,7 @@ export function MeshProcessingDialog() {
const [mode, setMode] = useState<'decimate' | 'remesh'>('decimate');
const [percent, setPercent] = useState(25);
const [errorLimit, setErrorLimit] = useState(0.1);
const [resolution, setResolution] = useState(64);
const [resolution, setResolution] = useState(DEFAULT_REMESH_RESOLUTION);
const [lockBorder, setLockBorder] = useState(true);
const [keepOriginal, setKeepOriginal] = useState(true);
const [busy, setBusy] = useState(false);
Expand Down Expand Up @@ -114,7 +120,7 @@ export function MeshProcessingDialog() {
const valid =
mode === 'decimate'
? percent > 0 && percent <= 100 && errorLimit >= 0 && errorLimit <= 100
: Number.isInteger(resolution) && resolution >= 24 && resolution <= 128;
: isRemeshResolution(resolution);
return (
<Modal
title="Decimate or remesh"
Expand Down Expand Up @@ -186,20 +192,22 @@ export function MeshProcessingDialog() {
</>
) : (
<label className="field-label">
<span>Voxel resolution (24–128)</span>
<span>
Voxel resolution ({MIN_REMESH_RESOLUTION}–{MAX_REMESH_RESOLUTION})
</span>
<input
aria-label="Voxel resolution"
type="number"
min="24"
max="128"
step="8"
min={MIN_REMESH_RESOLUTION}
max={MAX_REMESH_RESOLUTION}
step="1"
value={resolution}
onChange={(e) => {
clearResult();
setResolution(Number(e.target.value));
}}
/>
<small>Higher resolution retains smaller features and takes more memory.</small>
<small>Higher resolution can retain smaller features and takes more memory.</small>
</label>
)}
<label className="mesh-check">
Expand Down
6 changes: 4 additions & 2 deletions src/components/WarpGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
type Camera,
} from 'three';
import { isWarpDragging, useEditor, type WarpSession } from '../core/store';
import { shownDetail } from '../core/warp-handles';
import { writtenNodePositions } from '../core/warp-evaluate';
import type { WarpModel } from '../core/warp-model';
import {
Expand All @@ -43,10 +44,11 @@ const blended = new WeakMap<WarpModel, Float64Array>();
export function gridNodePositions(warp: WarpSession) {
const model = warp.model!,
state = warp.state!;
if (!model.blend) return state.positions;
const detail = shownDetail(warp.output);
if (!model.blend && !detail) return state.positions;
let out = blended.get(model);
if (!out) blended.set(model, (out = new Float64Array(state.positions.length)));
return writtenNodePositions(model, state, out);
return writtenNodePositions(model, state, out, detail);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/components/WarpOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
type WarpTransformSelection,
} from '../core/transform-selection';
import type { Part, Vec3 } from '../core/types';
import { handleGuides, reachWeights, writtenCentre } from '../core/warp-handles';
import { handleGuides, reachWeights, shownDetail, writtenCentre } from '../core/warp-handles';
import { CombinedTransform } from './CombinedTransform';
import { ManagedTransformControls } from './ManagedTransformControls';
import { warpRegionColor } from './SurfacePainter';
Expand Down Expand Up @@ -80,7 +80,7 @@ export function WarpOverlay({ onPress, pressed }: { onPress: () => void; pressed
<PointMarker
key={point.id}
id={point.id}
position={writtenCentre(warp.model!, warp.state!, point)}
position={writtenCentre(warp.model!, warp.state!, point, shownDetail(warp.output))}
selected={warp.selectedPoints.includes(point.id)}
auto={point.auto}
pressed={pressed}
Expand Down
15 changes: 9 additions & 6 deletions src/core/mesh-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { MeshoptSimplifier } from '../vendor/meshoptimizer/simplifier';
import { deserializeGeometry } from './geometry';
import type { BinaryGeometry } from './types';
import { hasClosedEdges } from './mesh-topology';
import {
isRemeshResolution,
MAX_REMESH_RESOLUTION,
MIN_REMESH_RESOLUTION,
} from './remesh-settings';

export type MeshProcessOptions =
| { mode: 'decimate'; ratio: number; error: number; lockBorder: boolean }
Expand Down Expand Up @@ -90,12 +95,10 @@ export class MeshProcessor {
throw new Error(
'Choose a triangle percentage above 0 and at most 100, and a valid error limit.',
);
} else if (
!Number.isInteger(options.resolution) ||
options.resolution < 24 ||
options.resolution > 128
) {
throw new Error('Voxel resolution must be between 24 and 128.');
} else if (!isRemeshResolution(options.resolution)) {
throw new Error(
`Voxel resolution must be a whole number from ${MIN_REMESH_RESOLUTION} to ${MAX_REMESH_RESOLUTION}.`,
);
}
if (!this.prepared) {
onProgress?.('Validating mesh');
Expand Down
11 changes: 11 additions & 0 deletions src/core/remesh-settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const MIN_REMESH_RESOLUTION = 24;
export const DEFAULT_REMESH_RESOLUTION = 128;
export const MAX_REMESH_RESOLUTION = 256;

export function isRemeshResolution(resolution: number) {
return (
Number.isInteger(resolution) &&
resolution >= MIN_REMESH_RESOLUTION &&
resolution <= MAX_REMESH_RESOLUTION
);
}
Loading
Loading