add precision/factor for CSG ops to eliminate sharps. add difference op to CSG

This commit is contained in:
Stewart Allen 2024-07-20 09:59:22 -04:00
commit 41479e200b
5 changed files with 66 additions and 18 deletions

View file

@ -8,6 +8,8 @@ gapp.register("geo.csg", [], (root, exports) => {
const { base, ext } = root;
const debug = true;
const precision = 0.001;
const factor = 1/precision;
function log() {
if (root?.mesh?.log) {
@ -34,6 +36,11 @@ const CSG = {
return CSG.moduleOp('manifold.intersect', 'intersection', ...arguments);
},
// accepts 2 or more arguments with threejs vertex position arrays
difference() {
return CSG.moduleOp('manifold.difference', 'difference', ...arguments);
},
moduleOp(name, op) {
log(`${name} ${arguments.length} meshes`);
const args = [...arguments].slice(2).map(a => new Instance.Manifold(a));
@ -63,7 +70,7 @@ const CSG = {
vertices[p++] = vertPos[vi++];
vertices[p++] = vertPos[vi++];
}
return vertices;
return vertices.map(v => v/factor);
}
throw "mesh missing required fields";
},
@ -71,7 +78,7 @@ const CSG = {
fromPositionArray(pos) {
const { index, vertices } = indexVertices(pos);
let mesh = new Instance.Mesh({
vertProperties: vertices.toFloat32(),
vertProperties: vertices.map(v => (v*factor)|0).toFloat32(),
triVerts: index.toUint32()
});
return mesh;