Vertices and loose edges
32-bit in partThe point positions of a model definition and its loose edges.
Each coordinate is assigned from the model value into a Float32Array, which rounds it to the nearest 32-bit number; loose edges are rounded the same way and written as point3f[]. Counts and indexes stay integers.
const writeGeometry = (p, def) => {
if (def.mesh && typeof def.mesh.toArrays === 'function') return writeMesh(p, def);
if (!def.verts || !def.verts.size) return;
const vids = [...def.verts.keys()];
const ix = new Map(vids.map((v, i) => [v, i]));
const pts = new Float32Array(vids.length * 3);
vids.forEach((v, i) => { const q = def.verts.get(v); pts[i * 3] = q[0]; pts[i * 3 + 1] = q[1]; pts[i * 3 + 2] = q[2]; });
const counts = [], indices = [], byPaint = new Map();
let nf = 0;
for (const f of def.faces.values()) {
const polys = f.loops.length === 1 ? [f.loops[0]] : faceTris(def, f);
for (const poly of polys) {
counts.push(poly.length);
for (const v of poly) indices.push(ix.get(v));
if (f.color) { let a = byPaint.get(f.color); if (!a) byPaint.set(f.color, (a = [])); a.push(nf); }
nf++;
}
}
if (nf) {
const g = b.mesh(p, 'Geometry', { points: pts, counts, indices }, subsetsOf(byPaint, nf));
onGeometry(g, def.id);
}
if (edgeFaces) {
const ef = edgeFaces(def);
const loose = [...def.edges].filter(([id]) => !(ef.get(id) || []).length);
if (loose.length) {
const c = b.prim(p, 'Edges', 'BasisCurves');
const lp = new Float32Array(loose.length * 6);
loose.forEach(([, e], k) => { const a = def.verts.get(e.a), q = def.verts.get(e.b); lp.set([a[0], a[1], a[2], q[0], q[1], q[2]], k * 6); });
b.attr(c, 'type', 'token', 'linear', { uniform: true });
b.attr(c, 'curveVertexCounts', 'int[]', new Int32Array(loose.length).fill(2));
b.attr(c, 'points', 'point3f[]', lp);
b.attr(c, 'widths', 'float[]', [0.005], { meta: { interpolation: V('token', 'constant') } });
b.attr(c, 'cadcraft:edges', 'string', def.id, { custom: true, uniform: true });
}
}
};