swap in manifold for jscad/modeling

This commit is contained in:
Stewart Allen 2022-10-24 11:43:43 -04:00
commit 95edfc1ea9
8 changed files with 171 additions and 56 deletions

View file

@ -3,76 +3,57 @@
"use strict";
// dep: geo.base
// jscad dep injected in app so it comes before everything else
// dep: add.manifold
gapp.register("geo.csg", [], (root, exports) => {
const { base } = root;
const debug = false;
const debug = true;
const CSG = {
// accepts 2 or more arguments with threejs vertex position arrays
union() {
if (debug) {
console.log(JSON.stringify([ ...arguments ]));
mesh.log(`manifold.union ${arguments.length} meshes`);
const args = [...arguments].map(a => new Module.Manifold(a));
const result = Module.union(...args);
const output = result.getMesh();
for (let m of [ ...args, result ]) {
m.delete();
}
let union = jscadModeling.booleans.union(...arguments);
if (debug) {
console.log(JSON.stringify(union));
}
return union;
return output;
},
// accepts 2 or more arguments with threejs vertex position arrays
// the fist argument is the target mesh. the rest are negatives
subtract() {
return jscadModeling.booleans.subtract(...arguments);
mesh.log(`manifold.subtract ${arguments.length} meshes`);
const args = [...arguments].map(a => new Module.Manifold(a));
const result = Module.difference(...args);
const output = result.getMesh();
for (let m of [ ...args, result ]) {
m.delete();
}
return output;
},
// accepts 2 or more arguments with threejs vertex position arrays
intersect() {
return jscadModeling.booleans.intersect(...arguments);
mesh.log(`manifold.intersect ${arguments.length} meshes`);
const args = [...arguments].map(a => new Module.Manifold(a));
const result = Module.intersection(...args);
const output = result.getMesh();
for (let m of [ ...args, result ]) {
m.delete();
}
return output;
},
toPositionArray(geom) {
if (!geom || geom.length === 0) {
return [].toFloat32();
}
const out = [];
const poly = jscadModeling.geometries.geom3
.toPolygons(geom)
.map(p => p.vertices);
for (let p of poly) {
let p0 = p[0];
for (let i = 0; i<p.length - 2; i++) {
out.push(p0, p[i+1], p[i+2]);
}
}
return out.flat().toFloat32();
// return geom.polygons
// .map(p => p.vertices.flat())
// .map(a => base.util.triangulate(a, undefined, 3))
// .flat().toFloat32();
return add.manifold.mesh2pos(geom);
},
// Construct a CSG from a THREE Geometry BufferAttribute array (or similar)
fromPositionArray(array) {
const polys = [];
if (debug) {
array = [...array].map(v => v.round(3));
}
for (let i=0, l=array.length; i<l; ) {
polys.push([ [
array[i++],
array[i++],
array[i++],
],[
array[i++],
array[i++],
array[i++],
],[
array[i++],
array[i++],
array[i++],
] ]);
}
return jscadModeling.geometries.geom3.fromPoints(polys);
return add.manifold.pos2mesh(array);
}
};