Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
Level topology: exact capacities; quad pairing and UV corners derived instead of stored (stack 38 -> 29.5 B/triangle)
islandmedeeab5efefad3
diff --git a/src/core/Multires.cpp b/src/core/Multires.cppindex 0ed36fe3..daab0be5 100644--- a/src/core/Multires.cpp+++ b/src/core/Multires.cpp@@ -177,7 +177,7 @@ std::shared_ptr<SubdivTopology> buildCoarse(const std::vector<glm::uvec3>& triantp->polyOffsets.reserve(nt + 1);tp->polyOffsets.push_back(0);tp->polyVerts.reserve(nt * 3);- tp->polyCorner.reserve(nt * 3);+ tp->polyDiag.reserve(nt);tp->polyTris.reserve(nt);for (uint32_t t = 0; t < nt; ++t) {const uint32_t u = pairing.empty() ? kNone : pairing[t];@@ -185,9 +185,9 @@ std::shared_ptr<SubdivTopology> buildCoarse(const std::vector<glm::uvec3>& trianif (u == kNone) {for (uint32_t c = 0; c < 3; ++c) {tp->polyVerts.push_back(a[c]);- tp->polyCorner.push_back(t * 3 + c);}tp->polyTris.push_back({t, kNone});+ tp->polyDiag.push_back(0);} else {if (u < t) continue;const int e = quadDiagonalOf(triangles, t, u);@@ -197,15 +197,19 @@ std::shared_ptr<SubdivTopology> buildCoarse(const std::vector<glm::uvec3>& trianif (b[k] != a[0] && b[k] != a[1] && b[k] != a[2]) far = k;for (int c : {(e + 1) % 3, (e + 2) % 3, e}) {tp->polyVerts.push_back(a[c]);- tp->polyCorner.push_back(t * 3 + uint32_t(c));}tp->polyVerts.push_back(b[far]);- tp->polyCorner.push_back(u * 3 + far);tp->polyTris.push_back({t, u});+ tp->polyDiag.push_back(uint8_t(e | far << 2));}tp->polyOffsets.push_back(uint32_t(tp->polyVerts.size()));}- tp->pairing = std::move(pairing);+ tp->quads = anyPair;+ // Exact sizes: the polygon lists were reserved for all triangles.+ tp->polyOffsets.shrink_to_fit();+ tp->polyVerts.shrink_to_fit();+ tp->polyTris.shrink_to_fit();+ tp->polyDiag.shrink_to_fit();// Edges: half-edges bucketed by their lower vertex, then matched within each (small) bucket.const size_t np = tp->polygonCount();@@ -819,7 +823,7 @@ void prolongUvs(const SubdivTopology& tp, const std::vector<glm::vec2>& coarse,forEachFineFace(tp, [&](size_t f, uint32_t p, uint32_t, const uint8_t* code) {if (flags && !(*flags)[tp.polyTris[p].x] && (tp.polyTris[p].y == kNone || !(*flags)[tp.polyTris[p].y])) return;const uint32_t o = tp.polyOffsets[p], n = polySize(tp, p);- auto cu = [&](uint32_t i) { return coarse[tp.polyCorner[o + i % n]]; };+ auto cu = [&](uint32_t i) { return coarse[tp.polyCorner(p, i % n)]; };for (int c = 0; c < 3; ++c) {glm::vec2 uv;if (code[c] < 4) uv = cu(code[c]);@@ -1320,9 +1324,20 @@ std::shared_ptr<Mesh> divideData(const SubdivTopology& tp, const Mesh& coarse) {} // namespace+std::vector<uint32_t> SubdivTopology::pairing() const {+ if (!quads) return {};+ std::vector<uint32_t> out(polygonCount() + std::count_if(polyTris.begin(), polyTris.end(), [](const glm::uvec2& t) { return t.y != kNone; }), kNone);+ for (const glm::uvec2& t : polyTris)+ if (t.y != kNone) {+ out[t.x] = t.y;+ out[t.y] = t.x;+ }+ return out;+}+size_t SubdivTopology::bytes() const {auto b = [](const auto& v) { return v.capacity() * sizeof(v[0]); };- return b(pairing) + b(polyOffsets) + b(polyVerts) + b(polyEdges) + b(polyCorner) + b(polyTris) + b(edges) ++ return b(polyOffsets) + b(polyVerts) + b(polyEdges) + b(polyDiag) + b(polyTris) + b(edges) +b(edgeFaces) + b(vertEdgeOffsets) + b(vertEdges) + b(vertexChild) + b(edgeChild) + b(faceChild) +b(fineParentPoly) + b(fineParentTri) + b(fineCode);}diff --git a/src/core/Multires.h b/src/core/Multires.hindex abe75d27..905b3483 100644--- a/src/core/Multires.h+++ b/src/core/Multires.h@@ -64,13 +64,14 @@ struct SubdivTopology {bool generated = false;uint32_t coarseVertices = 0, fineVertices = 0;size_t fineTriangles = 0;- // The quad pairing of the coarse level's triangles this level was built with (Mesh::kNoQuad = triangle).- std::vector<uint32_t> pairing;+ bool quads = false; // some coarse triangles were paired into quads// Coarse polygons (triangles and quads), corners in winding order (CSR).std::vector<uint32_t> polyOffsets, polyVerts;std::vector<uint32_t> polyEdges; // per polygon side i (corner i -> i + 1): its edge- std::vector<uint32_t> polyCorner; // per polygon corner: the coarse triangle corner (tri * 3 + slot) for UVsstd::vector<glm::uvec2> polyTris; // per polygon: its coarse triangles (y = kNone for a triangle)+ // Per quad: which edge e of its first triangle t is the diagonal, and the slot of the second triangle's far+ // corner (e | far << 2); the quad's corners are t[e+1], t[e+2], t[e], far.+ std::vector<uint8_t> polyDiag;// Coarse edges (a < b) and the polygons on each side; y = kNone on boundaries and non-manifold edges.std::vector<glm::uvec2> edges, edgeFaces;std::vector<uint32_t> vertEdgeOffsets, vertEdges; // coarse vertex -> incident edges (CSR)@@ -85,6 +86,16 @@ struct SubdivTopology {static constexpr uint32_t kNone = ~0u;size_t polygonCount() const { return polyOffsets.empty() ? 0 : polyOffsets.size() - 1; }+ // The coarse triangle corner (tri * 3 + slot) of polygon p's corner i, for UVs.+ uint32_t polyCorner(size_t p, uint32_t i) const {+ const glm::uvec2 t = polyTris[p];+ if (t.y == kNone) return t.x * 3 + i;+ const uint32_t e = polyDiag[p] & 3u;+ return i == 3 ? t.y * 3 + (polyDiag[p] >> 2) : t.x * 3 + (e + 1 + i) % 3;+ }+ // The quad pairing of the coarse level's triangles this level was built with (Mesh::kNoQuad = triangle;+ // empty without quads).+ std::vector<uint32_t> pairing() const;uint32_t vertexChildOf(uint32_t v) const { return generated ? v : vertexChild[v]; }uint32_t edgeChildOf(uint32_t e) const { return generated ? coarseVertices + e : edgeChild[e]; }uint32_t faceChildOf(uint32_t p) const { return generated ? coarseVertices + uint32_t(edges.size()) + p : faceChild[p]; }diff --git a/src/core/Project.cpp b/src/core/Project.cppindex 63e8c12b..59da6379 100644--- a/src/core/Project.cpp+++ b/src/core/Project.cpp@@ -447,7 +447,8 @@ Json writeSubdiv(Writer& w, const std::string& prefix, const Mesh& m) {Json t = Json::object();t.set("catmull_clark", tp.catmullClark);t.set("smooth", tp.smooth);- if (!tp.pairing.empty()) t.set("pairing", w.uints(p + "pairing.bin", tp.pairing.data(), tp.pairing.size(), 1));+ if (const std::vector<uint32_t> pairing = tp.pairing(); !pairing.empty())+ t.set("pairing", w.uints(p + "pairing.bin", pairing.data(), pairing.size(), 1));if (generated) {t.set("generated", true);} else {