/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */ "use strict"; // dep: geo.base // dep: ext.manifold gapp.register("geo.csg", [], (root, exports) => { const { base } = root; const debug = true; const CSG = { // accepts 2 or more arguments with threejs vertex position arrays union() { return CSG.moduleOp('manifold.union', 'union', ...arguments); }, // accepts 2 or more arguments with threejs vertex position arrays // the fist argument is the target mesh. the rest are negatives subtract() { return CSG.moduleOp('manifold.subtract', 'difference', ...arguments); }, // accepts 2 or more arguments with threejs vertex position arrays intersect() { return CSG.moduleOp('manifold.intersect', 'intersection', ...arguments); }, moduleOp(name, op) { mesh.log(`${name} ${arguments.length} meshes`); const args = [...arguments].slice(2).map(a => new Module.Manifold(a)); if (args.length < 2) { throw "missing one or more meshes"; } const result = Module[op](...args); const output = result.getMesh({ normal: () => undefined }); const errors = [ ...args, result ].map(m => m.status().value); for (let m of [ ...args, result ]) { m.delete(); } if (errors.reduce((a,b) => a+b)) { throw `${errors}`; } return output; }, toPositionArray(mesh) { const { vertPos, triVerts, vertex, index } = mesh; if (vertex && index) { const vertices = new Float32Array(index.length * 3); for (let p=0, i=0, l=index.length; i= 0) { index.push(ip); } else { index.push(ipos); cache[key] = (ipos++); vertices.push(temp.x, temp.y, temp.z); } } return { index, vertices }; } function pos2mesh(pos) { } Module.onRuntimeInitialized = () => { Module.setup(); const tests = false; const mesh_perf = false; const ball_torture = false; if (tests) { console.log('running Manifold tests'); let c = Module.cube([1,1,1], true); console.log({ c, cm: c.getMesh(), cmd: c.getMesh({ normal: () => undefined }), cmdn: c.getMesh({}) }); if (mesh_perf) { console.log('running Manifold getMesh() benchmark'); let l = 1000; let s = Module.sphere(10, 100); let sm = s.getMesh(); let smd = s.getMesh({}); console.log({ s, sm, smd, }); console.time('getMesh'); for (let i=0; i undefined }); console.timeEnd(`getMeshDirect`); console.time(`getMeshDirect`); for (let i=0; i maxBatch) { batch = 0; let nv = box.numVert(); let now = Date.now(); console.log( (i / (iter * 360)).toFixed(2), // % complete nv, // num vertices in target block now - last, // elapsed time for maxBatch boolean ops ((now - last) / maxBatch).toFixed(4) // time per boolean op ); last = now; } rad -= 0.04; z -= 0.08; } console.log(box.getMesh()); ball.delete(); box.delete(); console.timeEnd('ball test'); } } }; gapp.overlay(base, { CSG }); });