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: no spiky shards where Clay strokes end on rough, folded surface
islandmedf084f9fd0218
diff --git a/changes/fix-micro-stroke-spikes.md b/changes/fix-micro-stroke-spikes.mdnew file mode 100644index 00000000..39e579fc--- /dev/null+++ b/changes/fix-micro-stroke-spikes.md@@ -0,0 +1,9 @@+---+fixed: Micro levels: Clay (and the other plane brushes: Clay Block, Flatten, Trim) no longer leave spiky shards where a+ stroke's dabs pile up over rough, folded surface such as strands meeting skin, also on the symmetric side. A big+ brush's dab that would fold the base mesh over now sculpts the detail instead, and the detail's own plane brushes no+ longer push points they can't bring to the plane.+testers:+ - A dense scan or sculpt with a micro level (2+), symmetry X on: Clay strokes across the brow where hair or mane meets it, ending on the mane; orbit: no spikes or shards at the stroke ends, on either side.+---+Seen on a 3M-base lion with a 48M micro level (hero footage). Test: micro_stroke_end_no_spikes_on_a_rough_surface.diff --git a/src/core/MicroSculpt.cpp b/src/core/MicroSculpt.cppindex 954dc424..ec32bb6e 100644--- a/src/core/MicroSculpt.cpp+++ b/src/core/MicroSculpt.cpp@@ -526,7 +526,15 @@ void MicroStroke::dab(Mesh& m, const BVH& bvh, BrushType type, const glm::vec3&if (texture) f *= texture(p);if (mk && stride == 1) f *= 1.0f - float(mk[idx]) / 255.0f; // (coarse: the fine points' mask, below)if (f <= 0.0f) continue;- const float facing = std::max(glm::dot(nn, areaNormal), 0.3f); // moving along nn reaches a plane this much slower+ const float facingRaw = glm::dot(nn, areaNormal);+ const float facing = std::max(facingRaw, 0.3f); // moving along nn reaches a plane this much slower+ // The plane brushes (Flatten, Clay, Clay Block, Trim) move a point along its normal towards their+ // plane. A point turned edge-on or away from it (a fold's far wall, a scan's noisy normal) can't+ // get there that way: moving it anyway only takes it further, dab after dab, into a spike where+ // the dabs pile up (a stroke's end). Those stay put, and no point moves more than a quarter of+ // the brush a dab.+ const bool reachesPlane = facingRaw > 0.1f;+ const float planeCap = r * 0.25f * f;float delta = 0.0f;auto neighbourAverage = [&](float& lo, float& hi) {float sum = 0.0f;@@ -562,26 +570,30 @@ void MicroStroke::dab(Mesh& m, const BVH& bvh, BrushType type, const glm::vec3&break;}case BrushType::Flatten: {+ if (!reachesPlane) break;const float h = glm::dot(p - areaCenter, areaNormal);- delta = -h * s * f * (invert ? -0.5f : 1.0f) / facing;+ delta = std::clamp(-h * s * f * (invert ? -0.5f : 1.0f) / facing, -planeCap, planeCap);break;}case BrushType::Clay: {+ if (!reachesPlane) break;const glm::vec3 pp = areaCenter + areaNormal * (sign * r * 0.05f * s);const float h = glm::dot(p - pp, areaNormal);- if (sign * h < 0.0f) delta = -h * f * clayPull(s) / facing;+ if (sign * h < 0.0f) delta = std::clamp(-h * f * clayPull(s) / facing, -planeCap, planeCap);break;}case BrushType::ClayBlock: {+ if (!reachesPlane) break;const glm::vec3 pp = areaCenter + areaNormal * (sign * r * 0.08f * s);const float h = glm::dot(p - pp, areaNormal);- if (sign * h < 0.0f) delta = -h * f * clayPull(s) / facing;+ if (sign * h < 0.0f) delta = std::clamp(-h * f * clayPull(s) / facing, -planeCap, planeCap);break;}case BrushType::Trim: {+ if (!reachesPlane) break;const glm::vec3 pp = areaCenter - areaNormal * (sign * r * 0.02f * s);const float h = glm::dot(p - pp, areaNormal);- if (sign * h > 0.0f) delta = -h * std::min(1.0f, std::sqrt(f) * (0.5f + s)) / facing;+ if (sign * h > 0.0f) delta = std::clamp(-h * std::min(1.0f, std::sqrt(f) * (0.5f + s)) / facing, -planeCap, planeCap);break;}case BrushType::Layer: {diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cppindex 8f45894a..d7bf271c 100644--- a/src/core/Sculpt.cpp+++ b/src/core/Sculpt.cpp@@ -509,6 +509,14 @@ void Sculptor::microBaseKernel(Mesh& mesh, BVH& bvh, BrushType type, const DabPaif (baseMoves_[i] != glm::vec3(0.0f)) baseVerts_[k] = baseVerts_[i], baseMoves_[k++] = baseMoves_[i];baseVerts_.resize(k);baseMoves_.resize(k);+ // A base rough at its own edges' scale (mane strands meeting the brow): the plane pulls its grooves up past+ // the ridges beside them and folds base triangles over; the detail riding on them swings out into shards+ // (the lion, 11-06). There the dab goes to the grid instead (its points move along their own normals, the+ // base stays).+ if (baseWouldFold(mesh, baseVerts_, baseMoves_)) {+ micro_.dab(mesh, bvh, type, c, view, r, s, p.invert, p.focal, m.dir(p.alphaUp), false, 0.0f, p.alpha, {}, m.dir(p.strokeDelta));+ continue;+ }timings.kernelMs += lapMs(clock);timings.vertices += k;moveVertices(mesh, bvh, baseVerts_, baseMoves_); // (the undo record, normals, BVH)@@ -517,6 +525,34 @@ void Sculptor::microBaseKernel(Mesh& mesh, BVH& bvh, BrushType type, const DabPa++timings.dabs;}+// Whether moving `verts` by `moves` would turn a base triangle around them by more than 60 degrees (or flip it).+bool Sculptor::baseWouldFold(const Mesh& mesh, const std::vector<uint32_t>& verts, const std::vector<glm::vec3>& moves) {+ if (verts.empty()) return false;+ const size_t nv = mesh.vertexCount();+ if (baseSlot_.size() != nv) baseSlot_.assign(nv, ~0u);+ for (size_t i = 0; i < verts.size(); ++i) baseSlot_[verts[i]] = uint32_t(i);+ auto moved = [&](uint32_t v) {+ const uint32_t i = baseSlot_[v];+ return i != ~0u ? mesh.positions[v] + moves[i] : mesh.positions[v];+ };+ bool fold = false;+ for (size_t i = 0; i < verts.size() && !fold; ++i)+ for (uint32_t t : mesh.vertexTriangles(verts[i])) {+ const glm::uvec3 tr = mesh.triangles[t];+ const glm::vec3 a = mesh.positions[tr.x], b = mesh.positions[tr.y], c = mesh.positions[tr.z];+ const glm::vec3 before = glm::cross(b - a, c - a);+ const glm::vec3 a2 = moved(tr.x), b2 = moved(tr.y), c2 = moved(tr.z);+ const glm::vec3 after = glm::cross(b2 - a2, c2 - a2);+ const float l = glm::length(before) * glm::length(after);+ if (l > 0.0f && glm::dot(before, after) < 0.5f * l) {+ fold = true;+ break;+ }+ }+ for (uint32_t v : verts) baseSlot_[v] = ~0u;+ return fold;+}+bool Sculptor::microReady(Mesh& mesh) {if (!mesh.micro || !microMatches(mesh)) return false;if (!micro_.active() || microMesh_ != &mesh) {diff --git a/src/core/Sculpt.h b/src/core/Sculpt.hindex f853bfd3..fe054dde 100644--- a/src/core/Sculpt.h+++ b/src/core/Sculpt.h@@ -395,6 +395,8 @@ private:std::vector<glm::vec3> baseMoves_;std::vector<float> baseFall_;std::vector<glm::vec3> baseCorner_, baseNormal_;+ std::vector<uint32_t> baseSlot_; // baseWouldFold's: per vertex, its index in the dab's list+ bool baseWouldFold(const Mesh& mesh, const std::vector<uint32_t>& verts, const std::vector<glm::vec3>& moves);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 d1d5c027..a61a72db 100644--- a/tests/test_micro.cpp+++ b/tests/test_micro.cpp@@ -898,3 +898,55 @@ TEST(micro_crease_strokes_stay_smooth) {CHECK(watertight(doc.mesh));}}++TEST(micro_stroke_end_no_spikes_on_a_rough_surface) {+ // Dabs piled on one spot (a stroke's end) over a rough surface (strands leaning over the skin beside them, the+ // lion's mane meeting its brow, 11-06) used to grow spikes: on the grid, Clay moved points that can't reach its+ // plane along their own normals further away every dab; on the base (a big brush), the plane folded base+ // triangles over and their detail swung out (102 here). Neither path may turn a base triangle over, nor move any+ // point of the surface more than twice the brush's radius in these 15 full-strength dabs (Clay's own build-up+ // reaches 1.7; the grid's runaway reached 2.8).+ for (bool base : {false, true}) {+ Mesh rough = makeBenchSphere(200000);+ rough.recomputeAllNormals();+ for (size_t v = 0; v < rough.vertexCount(); ++v) { // narrow ridges leaning over the surface beside them+ const glm::vec3 p = rough.positions[v], nn = rough.normals[v];+ const float ridge = std::pow(std::max(0.0f, std::cos(p.x * 60.0f)), 8.0f);+ const glm::vec3 lean = glm::normalize(glm::cross(nn, glm::vec3(0, 1, 0)) + glm::vec3(1e-6f));+ rough.positions[v] += nn * (0.05f * ridge) + lean * (0.08f * ridge);+ }+ rough.recomputeAllNormals();+ Document doc = makeDoc(std::move(rough));+ std::string err;+ CHECK(microDivide(doc.mesh, 2, err));+ microNoise(doc.mesh, 0.002f, 60.0f, 7);+ doc.sculptor.microBaseDabs = base;+ const std::vector<glm::vec3> positions = doc.mesh.positions;+ const std::vector<glm::vec3> surface = microSurface(doc.mesh);+ const float r = base ? 0.6f : 0.3f; // (the base path takes Sculptor::kMicroBaseSpan base edges and more)+ Symmetry sym;+ doc.sculptor.beginStroke(doc.mesh, "Clay");+ for (int i = 0; i < 15; ++i)+ doc.sculptor.dab(doc.mesh, doc.bvh, BrushType::Clay, dabAt(glm::normalize(glm::vec3(0.002f * float(i % 3), 0.05f, 1.0f)), r, 1.0f), sym);+ doc.undo.push(doc.sculptor.endStroke());+ size_t turned = 0;+ for (const glm::uvec3& t : doc.mesh.triangles) {+ const glm::vec3 a = glm::cross(positions[t.y] - positions[t.x], positions[t.z] - positions[t.x]);+ const glm::vec3 b = glm::cross(doc.mesh.positions[t.y] - doc.mesh.positions[t.x], doc.mesh.positions[t.z] - doc.mesh.positions[t.x]);+ turned += glm::dot(a, b) < 0.0f;+ }+ const std::vector<glm::vec3> after = microSurface(doc.mesh);+ float worst = 0.0f, moved = 0.0f;+ for (size_t k = 0; k < after.size(); ++k) {+ const float d = glm::distance(after[k], surface[k]);+ worst = std::max(worst, d);+ moved += d;+ }+ std::printf(" %s, 15 Clay dabs (r %.2f): %zu base triangles turned over, largest move %.4f\n", base ? "base" : "grid", r, turned,+ worst);+ CHECK(moved > 0.0f);+ CHECK(turned == 0);+ CHECK(worst <= 2.0f * r);+ CHECK(watertight(doc.mesh));+ }+}