diff --git a/src/load/svg.js b/src/load/svg.js index b2952cf3..917366ce 100644 --- a/src/load/svg.js +++ b/src/load/svg.js @@ -22,11 +22,11 @@ function parse(text, opt = { }) { const rez = (opt.resolution || 1); const dpi = (opt.dpi || 0); const segmin = Math.max(1, opt.segmin || 10); - const objs = []; const data = new THREE.SVGLoader().parse(text); const paths = data.paths; const xmlat = data.xml.attributes; - const polys = fromSoup ? [] : undefined; + const objs = []; + const polys = []; const isinch = xmlat.width?.value.endsWith('in'); const scale = isinch ? 25.4 : (dpi ? 1 / (dpi / 25.4) : 1); const depth = parseFloat(opt.depth || xmlat['data-km-extrude']?.value @@ -39,76 +39,50 @@ function parse(text, opt = { }) { let type = path.userData?.node?.nodeName; let width = path.userData?.style?.strokeWidth; let miter = path.userData?.style?.strokeMiterLimit; - if (fromSoup) { - for (let sub of path.subPaths) { - let points = sub.curves.map(curve => { - let length = curve.getLength(); - let segs = curve.type === 'LineCurve' ? - 1 : Math.max(Math.ceil(length * rez), segmin); - return curve.getPoints(segs); - }).flat(); - if (points.length < 3) { - // console.log({ sub, length, points }); - continue; - } - let poly = base.newPolygon().addPoints(points.map(p => base.newPoint(p.x, -p.y, 0))); - if (poly.appearsClosed()) poly.points.pop(); - if (type === 'polyline') poly.setOpen(true); - poly._svg = { width, miter }; - polys.push(poly); - if (scale !== 1) { - poly.scale({ x: scale, y: scale, z: 1 }); - } + for (let sub of path.subPaths) { + let points = sub.curves.map(curve => { + let length = curve.getLength(); + let segs = curve.type === 'LineCurve' ? + 1 : Math.max(Math.ceil(length * rez), segmin); + return curve.getPoints(segs); + }).flat(); + if (points.length < 3) { + // console.log({ sub, length, points }); + continue; } - continue; - } - if (justPoly) { - continue; - } - let geom = new THREE.ExtrudeGeometry(shapes, { - depth, - steps: 1, - bevelEnabled: false - }); - let array = geom.attributes.position.array; - // invert y - for (let i=1; i base.newPoint(p.x, -p.y, 0))); + if (poly.appearsClosed()) poly.points.pop(); + if (type === 'polyline') poly.setOpen(true); + poly._svg = { width, miter }; + polys.push(poly); + if (scale !== 1) { + poly.scale({ x: scale, y: scale, z: 1 }); } } - objs.push([ ...array ]); - } - - if (fromSoup) { - const nest = base.polygons.nest(polys.filter(p => { - // filter duplicates - for (let pc of polys) { - if (pc === p) { - return true; - } else { - return !pc.isEquivalent(p); - } - } - })); - - if (justPoly) { - return nest; - } - - for (let poly of nest) { - let obj = poly.extrude(depth); - objs.push(obj); - } } - return justPoly ? polys : objs; + const sub = fromSoup ? base.polygons.nest(polys) : polys; + const nest = sub.filter(p => { + // filter duplicates + for (let pc of polys) { + if (pc === p) { + return true; + } else { + return !pc.isEquivalent(p); + } + } + }); + + if (justPoly) { + return nest; + } + + for (let poly of nest) { + let obj = poly.extrude(depth); + objs.push(obj); + } + + return objs; } });