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, big brushes on the base: through the plain mesh's own dab path (its fused batches included) instead of a kernel of their own; a 240 px stroke's queued dabs at pen-up each cost a full base pass (3060 Lucy 112M release 1.4 -> 3.1-3.5 s with more dabs placed). No base path with dynamic topology on. Test: the base moves exactly as the plain brush moves the same mesh
islandmed9485c1bb5700
diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cppindex 87f3b155..147da391 100644--- a/src/core/Sculpt.cpp+++ b/src/core/Sculpt.cpp@@ -424,7 +424,7 @@ bool Sculptor::microBaseDab(const Mesh& mesh, BrushType type, const DabParams& pconst 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 &&+ mesh.layers.empty() && !dyntopo.enabled && mm.baseEdge > 0.0f &&p.radius >= kMicroBaseSpan * mm.baseEdge? 1: 0;@@ -432,91 +432,6 @@ bool Sculptor::microBaseDab(const Mesh& mesh, BrushType type, const DabParams& preturn 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);- }- }- // 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;- baseMoves_[i] = type == BrushType::Inflate ? baseNormal_[i] * (sign * s * r * 0.05f * f) : plane.offsetWith(baseCorner_[i], f);- }- }, 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) {@@ -884,12 +799,9 @@ void Sculptor::dab(Mesh& mesh, BVH& bvh, BrushType type, const DabParams& p, SymdabKelvinlet(mesh, bvh, type, p, sym);return;}- // 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)) {+ // A micro level: the grid takes the dab (core/MicroSculpt.cpp), or a big one moves the base below it through the+ // plain path below (fused batches and all), the detail riding along (microBaseDab).+ if (microReady(mesh) && !microBaseDab(mesh, type, p)) {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 1ad18098..02f31743 100644--- a/src/core/Sculpt.h+++ b/src/core/Sculpt.h@@ -295,8 +295,9 @@ public: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.+ // has tiny base triangles (Lucy: 28M), where the per-patch work of the grid cost seconds a dab. They take the plain+ // mesh's path (its fused batches included). Not with a mask or hidden faces (the grid masks per point and keeps+ // hidden patches whole), an alpha, layers or dynamic topology.// SF_MICRO_BASE=0: always the grid (A/B).static constexpr float kMicroBaseSpan = 16.0f;bool microBaseDabs = microBaseDefault();@@ -368,14 +369,6 @@ 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 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_;- 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/src/core/SculptBatch.cpp b/src/core/SculptBatch.cppindex be7ca5d9..e539fce4 100644--- a/src/core/SculptBatch.cpp+++ b/src/core/SculptBatch.cpp@@ -30,7 +30,7 @@namespace sf {bool Sculptor::batchable(const Mesh& mesh, BrushType type, const DabParams& p, const Symmetry& sym) const {- if (!adaptiveDabs || !isPlaneKernelBrush(type) || type == BrushType::Trim || dyntopo.enabled || draft_ || mesh.micro)+ if (!adaptiveDabs || !isPlaneKernelBrush(type) || type == BrushType::Trim || dyntopo.enabled || draft_ || (mesh.micro && microBase_ != 1))return false;if ((p.alpha && !p.alpha->empty()) || p.frontFacesOnly || p.connectedOnly || p.viewFalloff) return false;if (mesh.normals.size() != mesh.vertexCount()) return false;diff --git a/tests/test_micro.cpp b/tests/test_micro.cppindex b12dea61..501514e5 100644--- a/tests/test_micro.cpp+++ b/tests/test_micro.cpp@@ -719,13 +719,11 @@ std::vector<glm::vec3> microSurface(const Mesh& m) {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+ // (Sculptor::microBaseDab): through the plain path, exactly as the plain brush moves the same mesh without the level, 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 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;@@ -741,9 +739,7 @@ TEST(micro_base_stroke_is_the_plain_brush) {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);- // (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));+ CHECK(worst <= height * 1e-6); // (the plain path itself, on the same base)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) == surface);