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
Micro levels on dense bases: big Standard / Clay / Inflate dabs (radius >= 16 base edges, no micro mask, hidden faces or alpha) move the base vertices with the grid brush's own kernel at the micro corners; the detail rides along untouched, one undo step (SF_MICRO_BASE=0: the grid as before). Tests: one dab within 0.3% mean of the grid brush, a stroke within 3-6% (the base's normals follow it), detail bit-identical, undo/redo exact, symmetry, hidden and mask fall back to the grid
islandmed1e27daed02f7
diff --git a/src/core/Micro.cpp b/src/core/Micro.cppindex ceadf385..8b83f39c 100644--- a/src/core/Micro.cpp+++ b/src/core/Micro.cpp@@ -296,6 +296,24 @@ float triangleBulge(const MicroTriangle& T) {} // namespace+void microRaiseBulge(Mesh& m) {+ if (!m.micro) return;+ MicroMesh& mm = *m.micro;+ const uint32_t nt = mm.baseTriangles, n = mm.n;+ const size_t block = 1 << 14;+ std::vector<float> bulge((nt + block - 1) / block, 0.0f);+ poolFor(nt, [&](size_t b, size_t e) {+ MicroTriangle T;+ float g = 0.0f;+ for (size_t t = b; t < e; ++t) {+ T.set(m, uint32_t(t), n);+ g = std::max(g, triangleBulge(T));+ }+ bulge[b / block] = g;+ }, block);+ for (float g : bulge) mm.bulgeMax = std::max(mm.bulgeMax, g);+}+bool microWantsOutOfCore(size_t points) {if (const char* e = std::getenv("SF_MICRO_OUT_OF_CORE"); e && *e) return std::string(e) == "1";if (const size_t limit = currentTierConfig().outOfCorePoints) return points > limit;diff --git a/src/core/Micro.h b/src/core/Micro.hindex 1c001456..7be15ab3 100644--- a/src/core/Micro.h+++ b/src/core/Micro.h@@ -233,6 +233,9 @@ void microNoise(Mesh& m, float amplitude, float frequency, uint32_t seed = 1);// Ray against the micro surface: the base triangle hit (BVH) and its neighbourhood within the displacement range,// at full micro resolution. hit.triangle is the base triangle; hit.position / normal are on the micro surface.bool microRaycast(const Mesh& m, const BVH& bvh, const glm::vec3& origin, const glm::vec3& dir, RayHit& hit);+// After the base moved under a micro level (brushes on the base: Sculptor::microBaseDab, Move): its bulge bound+// raised to what the base's triangles now need.+void microRaiseBulge(Mesh& m);// The base triangles whose displaced surface may come within `radius` of `center` (each BVH node grown by the// largest displacement and bulge under it, MicroMesh::Reach), for the caller's exact test per triangle.void microQueryTriangles(const Mesh& m, const BVH& bvh, const glm::vec3& center, float radius, std::vector<uint32_t>& out);diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cppindex d9fa4e90..67538da1 100644--- a/src/core/Sculpt.cpp+++ b/src/core/Sculpt.cpp@@ -11,6 +11,7 @@#include <atomic>#include <cctype>#include <cmath>+#include <cstdlib>#include <iterator>#include <limits>#include <unordered_map>@@ -405,10 +406,113 @@ bool Sculptor::beginStroke(const Mesh& mesh, std::string label, int layer) {lastRemesh_ = false;topologyHeld_ = false;microMesh_ = nullptr;+ microBase_ = -1;active_ = true;return true;}+bool Sculptor::microBaseDefault() {+ static const bool on = [] {+ const char* e = std::getenv("SF_MICRO_BASE");+ return !(e && e[0] == '0');+ }();+ return on;+}++bool Sculptor::microBaseDab(const Mesh& mesh, BrushType type, const DabParams& p) {+ if (microBase_ < 0) { // (decided once a stroke: its dabs all go one way)+ const MicroMesh& mm = *mesh.micro;+ microBase_ = microBaseDabs && (type == BrushType::Standard || type == BrushType::Clay || type == BrushType::Inflate) &&+ !mm.hasMask() && mesh.mask.empty() && !mesh.hasHidden() && !(p.alpha && !p.alpha->empty()) &&+ mesh.layers.empty() && mm.baseEdge > 0.0f &&+ p.radius >= kMicroBaseSpan * mm.baseEdge+ ? 1+ : 0;+ }+ return microBase_ == 1;+}++void Sculptor::microBaseKernel(Mesh& mesh, BVH& bvh, BrushType type, const DabParams& p, Symmetry sym) {+ auto clock = std::chrono::steady_clock::now();+ const MicroMesh& mm = *mesh.micro;+ const uint32_t n = mm.n, S = mm.samples;+ const float r = p.radius, r2 = r * r;+ const float s = std::clamp(p.strength * std::clamp(p.pressure, 0.0f, 1.0f), 0.0f, 1.0f);+ const float sign = p.invert ? -1.0f : 1.0f;+ for (const SymCopy& m : copies(sym)) {+ const glm::vec3 c = m.point(p.center), view = m.dir(p.viewDir);+ // The corners within reach: base points whose displaced corner may be in the sphere.+ bvh.queryVertices(mesh, c, r + float(mm.rangeMax) * mm.step, baseVerts_);+ const size_t nv = baseVerts_.size();+ baseFall_.assign(nv, 0.0f);+ baseCorner_.resize(nv);+ baseNormal_.resize(nv);+ poolFor(nv, [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) {+ const uint32_t v = baseVerts_[i];+ const IndexRing ring = mesh.vertexTriangles(v);+ if (ring.empty()) continue;+ const uint32_t t = ring[0];+ const glm::uvec3 tr = mesh.triangles[t];+ const int k = tr.x == v ? 0 : tr.y == v ? 1 : 2;+ const int16_t d = mm.disp[size_t(t) * S + (k == 0 ? 0 : k == 1 ? MicroMesh::index(n, 0, n) : MicroMesh::index(0, n, n))];+ const glm::vec3 nn = mesh.normals[v];+ const float len = glm::length(nn);+ const glm::vec3 un = len > 1e-20f ? nn / len : glm::vec3(0.0f, 0.0f, 1.0f);+ const glm::vec3 q = mesh.positions[v] + un * (float(d) * mm.step);+ const float d2 = glm::dot(q - c, q - c);+ baseCorner_[i] = q, baseNormal_[i] = un;+ if (d2 < r2) baseFall_[i] = brushFalloff(std::sqrt(d2), r, p.focal);+ }+ }, 4096);+ // The brush plane (Standard, Clay): weighted normal and centre of the corners under the brush.+ glm::vec3 areaNormal = -view, areaCenter = c;+ if (type != BrushType::Inflate) {+ glm::dvec3 nsum(0.0), csum(0.0);+ double w = 0.0;+ for (size_t i = 0; i < nv; ++i)+ if (baseFall_[i] > 0.0f) {+ nsum += glm::dvec3(baseNormal_[i] * baseFall_[i]);+ csum += glm::dvec3(baseCorner_[i] * baseFall_[i]);+ w += baseFall_[i];+ }+ if (w > 0.0) {+ areaCenter = glm::vec3(csum / w);+ const double len = glm::length(nsum);+ if (len > 1e-12) areaNormal = glm::vec3(nsum / len);+ }+ }+ baseMoves_.assign(nv, glm::vec3(0.0f));+ poolFor(nv, [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) {+ const float f = baseFall_[i];+ if (f <= 0.0f) continue;+ const glm::vec3 nn = baseNormal_[i], q = baseCorner_[i];+ float delta = 0.0f;+ if (type == BrushType::Standard) delta = sign * s * r * 0.06f * f * glm::dot(areaNormal, nn);+ else if (type == BrushType::Inflate) delta = sign * s * r * 0.05f * f;+ else { // Clay (MicroStroke::dab)+ const float facing = std::max(glm::dot(nn, areaNormal), 0.3f);+ const glm::vec3 pp = areaCenter + areaNormal * (sign * r * 0.05f * s);+ const float h = glm::dot(q - pp, areaNormal);+ if (sign * h < 0.0f) delta = -h * f * clayPull(s) / facing;+ }+ baseMoves_[i] = nn * delta;+ }+ }, 4096);+ size_t k = 0;+ for (size_t i = 0; i < nv; ++i)+ if (baseMoves_[i] != glm::vec3(0.0f)) baseVerts_[k] = baseVerts_[i], baseMoves_[k++] = baseMoves_[i];+ baseVerts_.resize(k);+ baseMoves_.resize(k);+ timings.kernelMs += lapMs(clock);+ timings.vertices += k;+ moveVertices(mesh, bvh, baseVerts_, baseMoves_); // (the undo record, normals, BVH)+ timings.normalsMs += lapMs(clock);+ }+ ++timings.dabs;+}+bool Sculptor::microReady(Mesh& mesh) {if (!mesh.micro || !microMatches(mesh)) return false;if (!micro_.active() || microMesh_ != &mesh) {@@ -730,6 +834,7 @@ UndoRecord Sculptor::endStroke() {record_.topology = std::move(topo);}}+ if (microBase_ == 1 && microMesh_) microRaiseBulge(*microMesh_); // (the base moved under it)if (micro_.active() && microMesh_) record_.micro = micro_.end(*microMesh_);microMesh_ = nullptr;clearRecorded();@@ -775,7 +880,12 @@ void Sculptor::dab(Mesh& mesh, BVH& bvh, BrushType type, const DabParams& p, SymdabKelvinlet(mesh, bvh, type, p, sym);return;}- if (microReady(mesh)) { // a micro level: the grid takes the dab (core/MicroSculpt.cpp)+ // A micro level: a big dab moves the base below it (microBaseKernel), the grid takes the rest (core/MicroSculpt.cpp).+ if (microReady(mesh) && microBaseDab(mesh, type, p)) {+ microBaseKernel(mesh, bvh, type, p, sym);+ return;+ }+ if (microReady(mesh)) {const double k0 = micro_.findMs + micro_.kernelMs, s0 = micro_.stitchMs;const uint64_t pts = micro_.points;for (const SymCopy& m : copies(sym))diff --git a/src/core/Sculpt.h b/src/core/Sculpt.hindex 0d06dc2e..dd196fd6 100644--- a/src/core/Sculpt.h+++ b/src/core/Sculpt.h@@ -293,6 +293,14 @@ public:// Move on a micro level: false (default) deforms the base cage and the detail rides along; true moves the// micro displacement along its normals instead.bool microMoveAlongNormals = false;+ // Standard, Clay and Inflate dabs whose radius spans at least this many base edges move the base vertices of a+ // micro level (the detail rides along, as with Move), not its millions of grid points: a scan's micro level+ // has tiny base triangles (Lucy: 28M), where the per-patch work of the grid cost seconds a dab. Not with a mask+ // or hidden faces (the grid masks per point and keeps hidden patches whole) or an alpha.+ // SF_MICRO_BASE=0: always the grid (A/B).+ static constexpr float kMicroBaseSpan = 16.0f;+ bool microBaseDabs = microBaseDefault();+ static bool microBaseDefault();MicroStroke& microStroke() { return micro_; }// Mirror sign vectors for the enabled symmetry axes (always starts with (1,1,1)); ignores radial and local.static std::vector<glm::vec3> mirrors(Symmetry sym);@@ -357,6 +365,17 @@ private:// Micro-displacement levels (core/Micro.h): strokes on a mesh with one go to its grid instead.MicroStroke micro_;Mesh* microMesh_ = nullptr;+ // This stroke's dabs on a micro level go to the base vertices (-1: not decided yet; microBaseDab).+ int microBase_ = -1;+ bool microBaseDab(const Mesh& mesh, BrushType type, const DabParams& p);+ // The grid brush's own kernel (Standard, Clay, Inflate) at the base vertices, i.e. at the micro surface's corners+ // (base point + normal * corner height): each moves along its normal by the change the grid brush would give+ // its corner point, and the patches' points follow (Phong over the corners, as N2's coarse lattice).+ void microBaseKernel(Mesh& mesh, BVH& bvh, BrushType type, const DabParams& p, Symmetry sym);+ std::vector<uint32_t> baseVerts_;+ std::vector<glm::vec3> baseMoves_;+ std::vector<float> baseFall_;+ std::vector<glm::vec3> baseCorner_, baseNormal_;bool microReady(Mesh& mesh); // the mesh has a micro level: starts the micro stroke on first usefloat strokeDiagonal_ = 0.0f;// The last remesh (repairDab remeshes it again when the brush squashed a triangle).diff --git a/tests/test_micro.cpp b/tests/test_micro.cppindex a47e7f99..d168c60e 100644--- a/tests/test_micro.cpp+++ b/tests/test_micro.cpp@@ -299,6 +299,7 @@ TEST(micro_move_brush_moves_along_normals) {TEST(micro_range_widens_for_big_strokes) {Document doc = microDoc(2);+ doc.sculptor.microBaseDabs = false; // (the grid's range; a brush this big moves the base otherwise)const float step0 = doc.mesh.micro->step;Symmetry sym;doc.sculptor.beginStroke(doc.mesh, "Standard");@@ -668,6 +669,7 @@ TEST(micro_dab_marks_only_what_changed) {// and not the candidates it left alone: a Clay dab's plane misses much of what it covers (on a scan's micro// level, millions of tiny patches).Document doc = microDoc(2, 200000);+ doc.sculptor.microBaseDabs = false; // (the grid's dabs; one this big moves the base otherwise)MicroMesh& mm = *doc.mesh.micro;Symmetry sym;sym.x = false;@@ -690,3 +692,162 @@ TEST(micro_dab_marks_only_what_changed) {CHECK(marked <= changed + changed / 10); // (the ones changed, give or take a stitched edge)CHECK(watertight(doc.mesh));}++namespace {++// A stroke across the sphere's front on `d` (a big brush: Sculptor::kMicroBaseSpan base edges and more).+void baseTestStroke(Document& d, BrushType type, float r, Symmetry sym = {}) {+ d.sculptor.beginStroke(d.mesh, "Stroke");+ for (int i = 0; i < 10; ++i) {+ const float x = -0.2f + 0.04f * float(i);+ d.sculptor.dab(d.mesh, d.bvh, type, dabAt(glm::vec3(x, 0.05f, std::sqrt(1.0f - x * x - 0.0025f)), r, 0.4f), sym);+ }+ d.undo.push(d.sculptor.endStroke());+}++// Every grid point of the micro surface, in order.+std::vector<glm::vec3> microSurface(const Mesh& m) {+ const MicroMesh& mm = *m.micro;+ std::vector<glm::vec3> out;+ for (uint32_t t = 0; t < mm.baseTriangles; ++t)+ for (uint32_t j = 0; j <= mm.n; ++j)+ for (uint32_t i = 0; i + j <= mm.n; ++i) out.push_back(microPoint(m, t, i, j));+ return out;+}++} // namespace++TEST(micro_base_stroke_matches_exact) {+ // A big Standard, Inflate or Clay brush on a micro level over a dense base (a scan's) moves the base vertices,+ // the detail riding along (Sculptor::microBaseDab): the surface lands within a few percent of the stroke's height+ // of the grid brush (every grid point), the detail is untouched (no smearing), one undo step brings back every+ // vertex and height bit-exactly, and redo the stroke.+ for (BrushType type : {BrushType::Standard, BrushType::Inflate, BrushType::Clay}) {+ for (bool detail : {false, true}) {+ Document grid = microDoc(2, 200000), base = microDoc(2, 200000);+ if (detail)+ for (Document* d : {&grid, &base}) microNoise(d->mesh, 0.002f, 60.0f, 7);+ grid.sculptor.microBaseDabs = false;+ base.sculptor.microBaseDabs = true;+ const std::vector<glm::vec3> before = microSurface(base.mesh), positions = base.mesh.positions;+ const std::vector<int16_t> heights = base.mesh.micro->disp;+ for (Document* d : {&grid, &base}) baseTestStroke(*d, type, 0.3f);+ CHECK(base.mesh.micro->disp == heights); // (the detail is untouched: it rides on the base)+ CHECK(base.mesh.positions != positions);+ CHECK(grid.sculptor.microStroke().points > 0);+ const std::vector<glm::vec3> g = microSurface(grid.mesh), b = microSurface(base.mesh);+ double height = 0, worst = 0, sum = 0;+ size_t moved = 0;+ for (size_t k = 0; k < g.size(); ++k) {+ const double h = glm::distance(g[k], before[k]);+ height = std::max(height, h);+ if (h == 0.0 && b[k] == before[k]) continue;+ ++moved;+ const double e = glm::distance(g[k], b[k]);+ worst = std::max(worst, e), sum += e;+ }+ // where the worst ones are: by the grid stroke's height there (the rim of the stroke, or its middle)+ size_t rim = 0, bad = 0;+ for (size_t k = 0; k < g.size(); ++k) {+ const double e = glm::distance(g[k], b[k]);+ if (e < worst * 0.5) continue;+ ++bad;+ rim += glm::distance(g[k], before[k]) < height * 0.25;+ }+ std::printf(" base stroke %d%s: height %.4f, error max %.1f%% mean %.2f%% (%zu points over half the worst, %zu at the rim)\n", int(type),+ detail ? " (detail)" : "", height, 100.0 * worst / height, 100.0 * sum / double(std::max<size_t>(moved, 1)) / height, bad, rim);+ CHECK(height > 0.005);+ // (A stroke drifts from the grid's by a few percent: the base's normals follow its moved vertices, the+ // grid keeps pushing along the base's original ones. One dab, below, lands on it.)+ CHECK(sum / double(std::max<size_t>(moved, 1)) <= height * (type == BrushType::Clay ? 0.08 : 0.04));+ const std::vector<glm::vec3> after = b;+ CHECK(base.undo.undo(base.mesh, base.bvh));+ CHECK(base.mesh.positions == positions && base.mesh.micro->disp == heights && microSurface(base.mesh) == before);+ CHECK(base.undo.redo(base.mesh, base.bvh));+ CHECK(microSurface(base.mesh) == after);+ CHECK(watertight(base.mesh));+ }+ }+}++TEST(micro_base_dab_matches_grid) {+ // One big dab on the base lands on the grid brush's (the same kernel at the corners; the points between follow).+ for (BrushType type : {BrushType::Standard, BrushType::Inflate, BrushType::Clay}) {+ Document grid = microDoc(2, 200000), base = microDoc(2, 200000);+ grid.sculptor.microBaseDabs = false;+ base.sculptor.microBaseDabs = true;+ const std::vector<glm::vec3> before = microSurface(base.mesh);+ Symmetry sym;+ sym.x = false;+ for (Document* d : {&grid, &base}) {+ d->sculptor.beginStroke(d->mesh, "Stroke");+ d->sculptor.dab(d->mesh, d->bvh, type, dabAt(glm::vec3(0, 0.05f, std::sqrt(1.0f - 0.0025f)), 0.3f, 0.4f), sym);+ d->undo.push(d->sculptor.endStroke());+ }+ CHECK(grid.mesh.positions != base.mesh.positions); // (each took its own path)+ const std::vector<glm::vec3> g = microSurface(grid.mesh), b = microSurface(base.mesh);+ double height = 0, worst = 0, sum = 0;+ size_t moved = 0;+ for (size_t k = 0; k < g.size(); ++k) {+ const double h = glm::distance(g[k], before[k]);+ height = std::max(height, h);+ if (h == 0.0 && b[k] == before[k]) continue;+ ++moved;+ const double e = glm::distance(g[k], b[k]);+ worst = std::max(worst, e), sum += e;+ }+ std::printf(" base dab %d: height %.4f, error max %.2f%% mean %.3f%%\n", int(type), height, 100 * worst / height, 100 * sum / double(moved) / height);+ CHECK(height > 0.002);+ CHECK(sum / double(moved) <= height * 0.01);+ CHECK(worst <= height * 0.06);+ }+}++TEST(micro_base_stroke_symmetry_hidden_mask) {+ // Symmetry: both sides move alike. Hidden faces: their vertices stay. A micro mask: the grid brush (it masks+ // per grid point), not the base.+ {+ Document doc = microDoc(2, 200000);+ Symmetry sym; // (x on)+ const std::vector<glm::vec3> before = doc.mesh.positions;+ doc.sculptor.beginStroke(doc.mesh, "Stroke");+ doc.sculptor.dab(doc.mesh, doc.bvh, BrushType::Standard, dabAt(glm::vec3(0.4f, 0.0f, std::sqrt(1.0f - 0.16f)), 0.25f), sym);+ doc.undo.push(doc.sculptor.endStroke());+ double left = 0, right = 0;+ for (size_t v = 0; v < before.size(); ++v) {+ const double d = glm::distance(before[v], doc.mesh.positions[v]);+ (before[v].x > 0 ? right : left) += d;+ }+ CHECK(right > 0.0);+ CHECK(std::abs(left - right) <= right * 0.02);+ }+ {+ // Hidden faces: the grid (it keeps hidden patches whole, and the base path would move their shared corners).+ Document doc = microDoc(2, 200000);+ Mesh& m = doc.mesh;+ m.hidden.assign(m.triangles.size(), 0);+ for (size_t t = 0; t < m.triangles.size(); ++t) {+ const glm::uvec3 tr = m.triangles[t];+ if ((m.positions[tr[0]].x + m.positions[tr[1]].x + m.positions[tr[2]].x) / 3.0f > 0.0f) m.hidden[t] = 1;+ }+ const std::vector<glm::vec3> before = m.positions;+ baseTestStroke(doc, BrushType::Standard, 0.3f);+ CHECK(m.positions == before);+ CHECK(doc.sculptor.microStroke().points > 0);+ CHECK(watertight(m));+ }+ {+ Document doc = microDoc(2, 200000);+ Symmetry sym;+ sym.x = false;+ doc.sculptor.beginStroke(doc.mesh, "Mask");+ doc.sculptor.dabMask(doc.mesh, doc.bvh, dabAt(glm::vec3(0, 0, 1), 0.1f, 1.0f), sym);+ doc.undo.push(doc.sculptor.endStroke());+ const std::vector<glm::vec3> before = doc.mesh.positions;+ const uint64_t p0 = doc.sculptor.microStroke().points;+ baseTestStroke(doc, BrushType::Standard, 0.3f);+ CHECK(doc.mesh.positions == before); // (the grid took it)+ CHECK(doc.sculptor.microStroke().points > p0);+ }+}+