2022-02-17 15:04:36 -05:00
|
|
|
/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */
|
|
|
|
|
|
|
|
|
|
"use strict";
|
2022-01-29 20:37:45 -05:00
|
|
|
|
|
|
|
|
// dep: geo.base
|
2022-10-24 11:43:43 -04:00
|
|
|
// dep: add.manifold
|
2022-02-20 14:31:27 -05:00
|
|
|
gapp.register("geo.csg", [], (root, exports) => {
|
|
|
|
|
|
|
|
|
|
const { base } = root;
|
2022-10-24 11:43:43 -04:00
|
|
|
const debug = true;
|
2022-01-29 20:37:45 -05:00
|
|
|
|
2022-02-20 14:31:27 -05:00
|
|
|
const CSG = {
|
2022-01-29 20:37:45 -05:00
|
|
|
|
2022-10-24 11:43:43 -04:00
|
|
|
// accepts 2 or more arguments with threejs vertex position arrays
|
2022-01-30 20:24:02 -05:00
|
|
|
union() {
|
2022-10-24 11:43:43 -04:00
|
|
|
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();
|
2022-02-26 19:11:59 -05:00
|
|
|
}
|
2022-10-24 11:43:43 -04:00
|
|
|
return output;
|
2022-01-30 20:24:02 -05:00
|
|
|
},
|
2022-01-29 20:37:45 -05:00
|
|
|
|
2022-10-24 11:43:43 -04:00
|
|
|
// accepts 2 or more arguments with threejs vertex position arrays
|
|
|
|
|
// the fist argument is the target mesh. the rest are negatives
|
2022-01-30 20:24:02 -05:00
|
|
|
subtract() {
|
2022-10-24 11:43:43 -04:00
|
|
|
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;
|
2022-01-30 20:24:02 -05:00
|
|
|
},
|
2022-01-29 20:37:45 -05:00
|
|
|
|
2022-10-24 11:43:43 -04:00
|
|
|
// accepts 2 or more arguments with threejs vertex position arrays
|
2022-01-30 20:24:02 -05:00
|
|
|
intersect() {
|
2022-10-24 11:43:43 -04:00
|
|
|
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;
|
2022-01-30 20:24:02 -05:00
|
|
|
},
|
2022-01-29 20:37:45 -05:00
|
|
|
|
2022-01-30 20:24:02 -05:00
|
|
|
toPositionArray(geom) {
|
2022-10-24 11:43:43 -04:00
|
|
|
return add.manifold.mesh2pos(geom);
|
2022-01-30 20:24:02 -05:00
|
|
|
},
|
2022-01-29 20:37:45 -05:00
|
|
|
|
2022-01-30 20:24:02 -05:00
|
|
|
fromPositionArray(array) {
|
2022-10-24 11:43:43 -04:00
|
|
|
return add.manifold.pos2mesh(array);
|
2022-01-29 20:37:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-20 14:31:27 -05:00
|
|
|
gapp.overlay(base, {
|
|
|
|
|
CSG
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|