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
Base path: the plain mesh's brush kernels (Standard and Clay along the plane's normal, Inflate along the vertex's) at the micro corners; the grid's Clay along each point's normal ran away on Lucy (bulge bound 507, picks over all 28M patches). Tests against the plain brush (within 3%, 8% with detail), detail bit-identical, undo/redo exact
islandmede6612d002719
diff --git a/src/core/Micro.cpp b/src/core/Micro.cppindex 8b83f39c..50cad1b7 100644--- a/src/core/Micro.cpp+++ b/src/core/Micro.cpp@@ -564,7 +564,13 @@ bool microRaycast(const Mesh& m, const BVH& bvh, const glm::vec3& origin, constRayHit base;if (!bvh.raycast(m, origin, dir, base)) return false;std::vector<uint32_t> cand;+ const auto traceStart = std::chrono::steady_clock::now();microRayTriangles(m, bvh, origin, dir, cand);+ static const bool trace = [] { const char* e = std::getenv("SF_MICRO_TRACE"); return e && e[0] == '1'; }();+ if (trace)+ std::fprintf(stderr, "micropick %zu candidates in %.1f ms (pad root %.4f, bulgeMax %.4f)\n", cand.size(),+ std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - traceStart).count(),+ mm.reach.pad.empty() ? 0.0 : double(mm.reach.pad[0]), double(mm.bulgeMax));// Only the triangles whose own reach (their bulge and displacement range) the ray passes through.const glm::vec3 inv(1.0f / (std::abs(dir.x) > 1e-20f ? dir.x : 1e-20f), 1.0f / (std::abs(dir.y) > 1e-20f ? dir.y : 1e-20f),1.0f / (std::abs(dir.z) > 1e-20f ? dir.z : 1e-20f));diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cppindex 67538da1..df9ee967 100644--- a/src/core/Sculpt.cpp+++ b/src/core/Sculpt.cpp@@ -482,22 +482,26 @@ void Sculptor::microBaseKernel(Mesh& mesh, BVH& bvh, BrushType type, const DabPaif (len > 1e-12) areaNormal = glm::vec3(nsum / len);}}+ // The plain mesh's kernels (SculptKernel.h: Standard and Clay along the plane's normal, Inflate along the+ // vertex's): the grid's own Clay moves each point along its normal by up to 3x the gap to the plane, which on+ // a scan's noisy normals and deep folds overshoots, dab after dab, into spikes hundreds of units long.+ PlaneKernel plane;+ plane.type = type;+ plane.center = c;+ plane.viewDir = view;+ plane.areaNormal = areaNormal;+ plane.areaCenter = areaCenter;+ plane.r = r;+ plane.s = s;+ plane.sign = sign;+ plane.focal = p.focal;+ plane.invert = p.invert;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;+ baseMoves_[i] = type == BrushType::Inflate ? baseNormal_[i] * (sign * s * r * 0.05f * f) : plane.offsetWith(baseCorner_[i], f);}}, 4096);size_t k = 0;diff --git a/src/core/Sculpt.h b/src/core/Sculpt.hindex dd196fd6..721c9e09 100644--- a/src/core/Sculpt.h+++ b/src/core/Sculpt.h@@ -368,9 +368,9 @@ private:// 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).+ // The plain mesh's brush (Standard, Clay, Inflate) at the base vertices, evaluated at the micro surface's corners+ // (base point + normal * corner height): the base moves as a plain mesh would and the patches' points follow+ // (Phong over the corners, as N2's coarse lattice), their heights untouched.void microBaseKernel(Mesh& mesh, BVH& bvh, BrushType type, const DabParams& p, Symmetry sym);std::vector<uint32_t> baseVerts_;std::vector<glm::vec3> baseMoves_;diff --git a/tests/test_micro.cpp b/tests/test_micro.cppindex d168c60e..b12dea61 100644--- a/tests/test_micro.cpp+++ b/tests/test_micro.cpp@@ -717,52 +717,36 @@ std::vector<glm::vec3> microSurface(const Mesh& m) {} // 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.+TEST(micro_base_stroke_is_the_plain_brush) {+ // A big Standard, Inflate or Clay brush on a micro level over a dense base (a scan's) moves the base vertices+ // (Sculptor::microBaseDab): as the plain brush moves the same mesh without the level (no detail: the corners are+ // the vertices), the detail riding along untouched (its heights bit-identical), one undo step+ // bringing every vertex and height back bit-exactly, 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;+ Document plain = makeDoc(makeBenchSphere(200000)), base = microDoc(2, 200000);+ plain.sculptor.adaptiveDabs = false; // (its plane from every vertex, as the base path's)+ if (detail) microNoise(base.mesh, 0.002f, 60.0f, 7);+ const std::vector<glm::vec3> 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;+ const std::vector<glm::vec3> surface = microSurface(base.mesh);+ for (Document* d : {&plain, &base}) baseTestStroke(*d, type, 0.3f);+ CHECK(base.mesh.micro->disp == heights);+ CHECK(base.sculptor.microStroke().points == 0); // (not one grid point visited)+ double height = 0, worst = 0;+ for (size_t v = 0; v < positions.size(); ++v) {+ height = std::max(height, double(glm::distance(plain.mesh.positions[v], positions[v])));+ worst = std::max(worst, double(glm::distance(plain.mesh.positions[v], base.mesh.positions[v])));}- // 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);+ std::printf(" base stroke %d%s: height %.4f, off the plain brush by %.4f%% at most\n", int(type), detail ? " (detail)" : "",+ height, 100.0 * worst / height);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;+ // (within a few percent: the plain brush picks and re-normals its region its own way, dab after dab; the+ // detail's corner heights tilt the brush plane a little more)+ CHECK(worst <= height * (detail ? 0.08 : 0.03));+ const std::vector<glm::vec3> after = microSurface(base.mesh);CHECK(base.undo.undo(base.mesh, base.bvh));- CHECK(base.mesh.positions == positions && base.mesh.micro->disp == heights && microSurface(base.mesh) == before);+ CHECK(base.mesh.positions == positions && base.mesh.micro->disp == heights && microSurface(base.mesh) == surface);CHECK(base.undo.redo(base.mesh, base.bvh));CHECK(microSurface(base.mesh) == after);CHECK(watertight(base.mesh));@@ -770,39 +754,6 @@ TEST(micro_base_stroke_matches_exact) {}}-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.