Add clay pieces, masking, and topology editing to sculpt

Add pastel presets, piece selection and colors, mask brushes, subdivision and conservative decimation with bounded worker operations and mixed undo history.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0b4b4-202c-73f1-984d-9a55c88f9c83
Co-authored-by: Heaust Azure <heaust.azure@gmail.com>
This commit is contained in:
Amp
2026-10-01 07:55:48 +00:00
co-authored by heaust
parent d930d8172f
commit d279ec4123
21 changed files with 2564 additions and 131 deletions
+47 -22
View File
@@ -1,5 +1,51 @@
import { unit } from "./math.js";
export function buildMesh(positions, indices, level = 5, masks) {
positions =
positions instanceof Float32Array
? positions
: new Float32Array(positions.flat ? positions.flat(Infinity) : positions);
indices =
indices instanceof Uint32Array
? indices
: new Uint32Array(indices.flat ? indices.flat(Infinity) : indices);
if (positions.length % 3 || indices.length % 3)
throw new Error(
"Mesh positions and indices must contain complete triples.",
);
const vertexCount = positions.length / 3;
const incident = Array.from({ length: vertexCount }, () => []);
const neighbors = Array.from({ length: vertexCount }, () => new Set());
for (let f = 0; f < indices.length / 3; f++) {
const face = indices.subarray(f * 3, f * 3 + 3);
for (const v of face) {
if (v >= vertexCount) throw new Error("Mesh index is out of range.");
incident[v].push(f);
for (const other of face) if (other !== v) neighbors[v].add(other);
}
}
if (masks && masks.length !== vertexCount)
throw new Error("Mask count must match vertex count.");
const mesh = {
level,
positions,
indices,
masks:
masks instanceof Float32Array
? masks
: new Float32Array(masks || vertexCount),
normals: new Float32Array(positions.length),
faceNormals: new Float32Array(indices.length),
incident: incident.map((a) => Uint32Array.from(a)),
neighbors: neighbors.map((a) => Uint32Array.from(a)),
faceMarks: new Uint32Array(indices.length / 3),
vertexMarks: new Uint32Array(vertexCount),
generation: 0,
};
updateNormals(mesh);
return mesh;
}
export function sphere(level = 5) {
if (!Number.isInteger(level) || level < 4 || level > 6)
throw new Error("Choose a supported resolution.");
@@ -64,28 +110,7 @@ export function sphere(level = 5) {
}
const positions = new Float32Array(vertices.flat());
const indices = new Uint32Array(faces.flat());
const incident = Array.from({ length: vertices.length }, () => []);
const neighbors = Array.from({ length: vertices.length }, () => new Set());
faces.forEach((face, f) =>
face.forEach((v) => {
incident[v].push(f);
for (const other of face) if (other !== v) neighbors[v].add(other);
}),
);
const mesh = {
level,
positions,
indices,
normals: new Float32Array(positions.length),
faceNormals: new Float32Array(indices.length),
incident: incident.map((a) => Uint32Array.from(a)),
neighbors: neighbors.map((a) => Uint32Array.from(a)),
};
mesh.faceMarks = new Uint32Array(faces.length);
mesh.vertexMarks = new Uint32Array(vertices.length);
mesh.generation = 0;
updateNormals(mesh);
return mesh;
return buildMesh(positions, indices, level);
}
// Include the unchanged incident faces when recomputing a boundary vertex normal.