Skip to content

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.

Kelvinlets on dense meshes: one pass per move, radial table, relative cutoff; captures keep curved form

islandmed2261eed25e4c
diff --git a/src/core/Commands.cpp b/src/core/Commands.cpp
index 39a9b59a..f233eb4d 100644
--- a/src/core/Commands.cpp
+++ b/src/core/Commands.cpp
@@ -581,6 +581,7 @@ CommandRegistry::CommandRegistry() {
registerBrushLibraryCommands(*this); // brush_install, brush_code, brush_delete, ... (BrushLibrary.cpp)
registerBrushCommands(*this); // brush_set_alpha (BrushAlpha.cpp)
registerVdmCommands(*this); // vdm_set, vdm_capture, vdm_stamp, ... (Vdm.cpp)
+ registerKelvinletCommands(*this); // kelvinlet_settings (Kelvinlet.cpp)
registerCaptureCommands(*this); // capture_alpha, capture_vdm: alpha / VDM from the viewport (ViewCapture.cpp)
registerRetopoCommands(*this); // qremesh, project_all (RetopoCommands.cpp)
registerQRemeshGuideCommands(*this); // qremesh_guide, ... (QRemeshGuides.cpp)
diff --git a/src/core/Kelvinlet.cpp b/src/core/Kelvinlet.cpp
index 2e4af721..9ffb06d7 100644
--- a/src/core/Kelvinlet.cpp
+++ b/src/core/Kelvinlet.cpp
@@ -4,6 +4,7 @@
// Kelvinlet brushes (see Kelvinlet.h for the math) and the Detail Clone brush (VDM stamps along a stroke).
#include "core/Kelvinlet.h"
+#include "core/Commands.h"
#include "core/Sculpt.h"
#include "core/ThreadPool.h"
#include "core/Vdm.h"
@@ -77,7 +78,7 @@ KelvinletKernel makeKelvinletKernel(KelvinletKind kind, float radius, const Kelv
radius = std::max(radius, 1e-6f);
if (kind == KelvinletKind::Grab && !tight) {
// Tri-scale (1/r^5 far field). The weights cancel the 1/r and 1/r^3 terms of the far field.
- const float e1 = 0.6f * radius;
+ const float e1 = 0.75f * radius;
k.scales = 3;
k.eps[0] = e1;
k.eps[1] = 1.5f * e1;
@@ -208,9 +209,12 @@ void Sculptor::kelvinletSingle(Mesh& mesh, BVH& bvh, const KelvinletKernel& k, c
case KelvinletKind::Scale: amount = std::abs(s.logScale), moveScale = amount * s.radius; break;
}
if (!(amount > 0.0f)) return;
- // Adaptive cutoff: where this step moves the surface less than a small fraction of the brush radius (and of
- // the step: a long jump between frames doesn't drag the whole mesh into the pass; the window hides the rest).
- const float epsilon = 0.006f * s.radius + 0.02f * moveScale;
+ // Adaptive cutoff: where this step's displacement falls below epsilon = 2% of its largest (the window takes the
+ // rest smoothly to 0), so a slow drag of tiny steps still moves the surface and a fast one doesn't drag the whole
+ // mesh into the pass.
+ float peak = 0.0f;
+ for (int i = 0; i <= 32; ++i) peak = std::max(peak, k.envelope(1.5f * s.radius * float(i) / 32.0f));
+ const float epsilon = std::max(0.02f * amount * peak, 1e-7f * s.radius);
const float cut = k.cutoff(amount, epsilon, 3.0f * s.radius);
if (!(cut > 0.0f)) return;
// Grab: the load point travels from `center` to `center + travel` in `substeps` pieces, each a linear Kelvinlet
@@ -435,4 +439,26 @@ void Sculptor::flushDetailClone(Mesh& mesh, BVH& bvh, Symmetry sym) {
clone_.stamped = true;
}
+void registerKelvinletCommands(CommandRegistry& registry) {
+ using T = ParamSpec::Type;
+ registry.add({"kelvinlet_settings",
+ "Set the Kelvinlet brushes' material (Brush > Kelvinlet & Capture): Squash (Poisson's ratio: 0 "
+ "rubbery, 1 incompressible clay that bulges), Sharpness, and Detail Clone's spacing. Unset values stay.",
+ {{"squash", T::Number, "0-1 (default 0.5).", false},
+ {"sharpness", T::Number, "0-1: a tighter, cusped peak (default 0).", false},
+ {"clone_spacing", T::Number, "Detail Clone: stamp spacing in stamp widths, 0.25-3 (default 1).", false},
+ {"clone_follow", T::Bool, "Detail Clone: stamps turn with the stroke (default true).", false}},
+ [](Document& doc, const Args& a) -> CommandResult {
+ KelvinletSettings& k = doc.sculptor.kelvinlet;
+ k.squash = float(std::clamp(argNumber(a, "squash", k.squash), 0.0, 1.0));
+ k.sharpness = float(std::clamp(argNumber(a, "sharpness", k.sharpness), 0.0, 1.0));
+ k.cloneSpacing = float(std::clamp(argNumber(a, "clone_spacing", k.cloneSpacing), 0.25, 3.0));
+ k.cloneFollowStroke = argBool(a, "clone_follow", k.cloneFollowStroke);
+ char buf[160];
+ std::snprintf(buf, sizeof buf, "Kelvinlet: Squash %.0f (Poisson's ratio %.2f), Sharpness %.0f, clone spacing %.2f",
+ k.squash * 100.0f, k.poisson(), k.sharpness * 100.0f, k.cloneSpacing);
+ return {true, buf};
+ }});
+}
+
} // namespace sf
diff --git a/src/core/Kelvinlet.h b/src/core/Kelvinlet.h
index 16dfaf48..52a3bf42 100644
--- a/src/core/Kelvinlet.h
+++ b/src/core/Kelvinlet.h
@@ -33,6 +33,8 @@
namespace sf {
+class CommandRegistry;
+
// Brush > Kelvinlet (saved with the project's brush settings by the app).
struct KelvinletSettings {
float squash = 0.5f; // 0..1: Poisson's ratio nu = 0.49 * squash. 0 = rubbery (no bulge), 1 = incompressible clay
@@ -89,4 +91,7 @@ struct KelvinletStep {
bool frontFacesOnly = false;
};
+// kelvinlet_settings: Squash, Sharpness and the Detail Clone options from a script.
+void registerKelvinletCommands(CommandRegistry& registry);
+
} // namespace sf
diff --git a/src/core/ViewCapture.cpp b/src/core/ViewCapture.cpp
index c9fd3538..5d54b88e 100644
--- a/src/core/ViewCapture.cpp
+++ b/src/core/ViewCapture.cpp
@@ -400,6 +400,64 @@ bool captureVdmFromView(const Mesh& mesh, const BVH& bvh, const CaptureFrame& f,
if (smoothing <= 0.0f) {
for (size_t i = 0; i < n; ++i) base[i] = X[i] - np * glm::dot(X[i] - f.center, np);
} else {
+ // The broad form first: a quadric height field over the plane fitted to the outer band of the square (where
+ // the detail usually isn't). Fairing alone would flatten a curved surface (a harmonic fill can't hold a
+ // sphere's dome), so the dome would come out as "detail"; the quadric's own curvature stays in the base.
+ glm::vec3 qr = f.right - np * glm::dot(f.right, np);
+ qr = glm::length(qr) > 1e-6f ? glm::normalize(qr) : f.right;
+ const glm::vec3 qu = glm::cross(np, qr);
+ auto terms = [](double u, double v, double t[6]) {
+ t[0] = 1.0, t[1] = u, t[2] = v, t[3] = u * u, t[4] = u * v, t[5] = v * v;
+ };
+ double coef[6] = {0, 0, 0, 0, 0, 0};
+ for (int pass = 0; pass < 2; ++pass) {
+ double M[6][7] = {};
+ size_t used = 0;
+ for (size_t i = 0; i < n; ++i) {
+ const glm::vec3 o = X[i] - f.center;
+ const double u = glm::dot(o, qr) * inv, v = glm::dot(o, qu) * inv;
+ if (pass == 0 && std::max(std::abs(u), std::abs(v)) < 0.55 && !rim[i]) continue;
+ double t[6];
+ terms(u, v, t);
+ const double h = glm::dot(o, np) * inv;
+ for (int a = 0; a < 6; ++a) {
+ for (int c = 0; c < 6; ++c) M[a][c] += t[a] * t[c];
+ M[a][6] += t[a] * h;
+ }
+ ++used;
+ }
+ if (pass == 0 && used < 40) continue; // too few in the band: fit everything
+ for (int a = 0; a < 6; ++a) M[a][a] += 1e-9; // (a degenerate band: stay near flat)
+ // Gaussian elimination with partial pivoting.
+ bool ok = true;
+ for (int c = 0; c < 6 && ok; ++c) {
+ int piv = c;
+ for (int r2 = c + 1; r2 < 6; ++r2)
+ if (std::abs(M[r2][c]) > std::abs(M[piv][c])) piv = r2;
+ if (std::abs(M[piv][c]) < 1e-14) {
+ ok = false;
+ break;
+ }
+ for (int k = 0; k < 7; ++k) std::swap(M[c][k], M[piv][k]);
+ for (int r2 = 0; r2 < 6; ++r2) {
+ if (r2 == c) continue;
+ const double fct = M[r2][c] / M[c][c];
+ for (int k = c; k < 7; ++k) M[r2][k] -= fct * M[c][k];
+ }
+ }
+ if (ok)
+ for (int a = 0; a < 6; ++a) coef[a] = M[a][6] / M[a][a];
+ break;
+ }
+ auto quadric = [&](const glm::vec3& p) {
+ const glm::vec3 o = p - f.center;
+ double t[6];
+ terms(glm::dot(o, qr) * inv, glm::dot(o, qu) * inv, t);
+ double h = 0.0;
+ for (int a = 0; a < 6; ++a) h += coef[a] * t[a];
+ return float(h) * half;
+ };
+ for (size_t i = 0; i < n; ++i) X[i] -= np * quadric(X[i]); // flattened: the fairing works on the rest
// Implicit fairing with the rim held: (1 + l deg_i) x_i - l sum_{j inner} x_j = X_i + l sum_{j rim} X_j,
// solved for the inner vertices by Jacobi-preconditioned conjugate gradients (three right-hand sides).
const float sigma = half * (0.2f + 1.6f * smoothing); // the size of form the base keeps
@@ -469,7 +527,11 @@ bool captureVdmFromView(const Mesh& mesh, const BVH& bvh, const CaptureFrame& f,
for (size_t i = lo; i < hi; ++i) p[i] = z[i] + beta * p[i];
});
}
- for (size_t i = 0; i < n; ++i) base[i] = rim[i] ? X[i] : x[i];
+ for (size_t i = 0; i < n; ++i) {
+ base[i] = rim[i] ? X[i] : x[i];
+ base[i] += np * quadric(base[i]); // the broad form back on the base
+ X[i] += np * quadric(X[i]); // and on the surface
+ }
np = areaNormal(base);
}
// 4. The base's normals and the stamp's frame (the screen's right and up laid on the base plane).
@@ -586,7 +648,7 @@ CaptureOptions optionsFromArgs(const Args& a) {
CaptureOptions o;
o.resolution = std::clamp(int(argNumber(a, "resolution", 256)), 32, 1024);
o.smoothing = float(std::clamp(argNumber(a, "smoothing", 0.5), 0.0, 1.0));
- o.edgeFade = float(std::clamp(argNumber(a, "edge_fade", 0.12), 0.0, 0.9));
+ o.edgeFade = float(std::clamp(argNumber(a, "edge_fade", 0.25), 0.0, 0.9));
o.fitPlane = argBool(a, "fit_plane", true);
o.invert = argBool(a, "invert", false);
return o;
@@ -604,7 +666,7 @@ void registerCaptureCommands(CommandRegistry& registry) {
{"circle", T::Bool, "Capture the circle inside the square only.", false},
{"resolution", T::Number, "Pixels a side (32-1024, default 256).", false},
{"smoothing", T::Number, "0-1: how much of the smooth form the base takes away (0 = flat plane; default 0.5).", false},
- {"edge_fade", T::Number, "0-0.9: border fade, as a fraction of the half size (default 0.12).", false},
+ {"edge_fade", T::Number, "0-0.9: border fade, as a fraction of the half size (default 0.25).", false},
};
std::vector<ParamSpec> alphaParams = common;
alphaParams.push_back({"fit_plane", T::Bool, "Height over a plane fitted to the surface (default true) instead of the view plane.", false});
diff --git a/src/core/ViewCapture.h b/src/core/ViewCapture.h
index 999a0b6e..74d82645 100644
--- a/src/core/ViewCapture.h
+++ b/src/core/ViewCapture.h
@@ -40,7 +40,7 @@ struct CaptureFrame {
struct CaptureOptions {
int resolution = 256; // pixels a side (32..1024)
- float edgeFade = 0.12f; // fraction of the half size over which the capture fades to 0 at its border
+ float edgeFade = 0.25f; // fraction of the half size over which the capture fades to 0 at its border
float smoothing = 0.5f; // 0..1: how much smooth form the base takes away (0: plain plane; VDM and alpha)
bool fitPlane = true; // height alpha: relative to a plane fitted to the surface (removes tilt)
bool invert = false; // height alpha: white = deepest
diff --git a/tests/test_kelvinlet.cpp b/tests/test_kelvinlet.cpp
index 955f41dc..09a457df 100644
--- a/tests/test_kelvinlet.cpp
+++ b/tests/test_kelvinlet.cpp
@@ -255,6 +255,17 @@ TEST(kelvinlet_twist_and_snake_strokes) {
CHECK(std::abs(glm::length(b) - glm::length(a)) < 1e-3f); // a rotation in the sheet
CHECK(std::abs(b.y) < 1e-6f);
CHECK(glm::length(b - a) > 0.02f);
+ // Scale: a drag to the left pinches toward the press point, to the right swells.
+ for (float dir : {-1.0f, 1.0f}) {
+ Document sc = makeDoc(makePlane(120, 2.0f));
+ PathStroke ps = st;
+ ps.points = {glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.2f * dir, 1.0f, 0.0f)};
+ CHECK(strokePath(sc, nullptr, BrushType::KelvinScale, ps, err) > 0);
+ const uint32_t q = nearest(makePlane(120, 2.0f), glm::vec3(0.1f, 0.0f, 0.05f));
+ const float r0 = glm::length(start[q]), r1 = glm::length(sc.mesh.positions[q]);
+ std::printf(" scale %+.0f: %.4f -> %.4f\n", dir, r0, r1);
+ CHECK(dir < 0.0f ? r1 < r0 * 0.9f : r1 > r0 * 1.1f);
+ }
// Snake pulls a tapering horn straight out of a sphere.
Document s = makeDoc(makeIcosphere(6, 1.0f));
PathStroke pull;
@@ -408,6 +419,32 @@ TEST(capture_vdm_from_view_round_trips_a_stamp) {
CHECK(a.vdms.size() == before + 1 && a.brushVdm && a.brushVdm->name == "Ear Copy");
}
+TEST(capture_vdm_on_a_curved_surface_keeps_only_the_detail) {
+ // A horn on a sphere: the sphere's dome stays in the base (the quadric), so the map is the horn alone.
+ Document d = makeDoc(makeIcosphere(7, 1.0f));
+ d.brushVdm = builtinVdm("horn");
+ CHECK(run(d, "vdm_stamp", {{"point", std::string("0,0,2")}, {"view", std::string("0,0,-1")}, {"radius", 0.15}}));
+ d.syncBvh();
+ const CaptureFrame f = makeCaptureFrame(glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f), 0.4f);
+ CaptureOptions opt;
+ opt.resolution = 128;
+ VectorDisplacement v;
+ std::string err;
+ CHECK(captureVdmFromView(d.mesh, d.bvh, f, opt, v, err));
+ float centre = 0.0f, band = 0.0f;
+ for (int y = 0; y < v.height; ++y)
+ for (int x = 0; x < v.width; ++x) {
+ const float u = (float(x) + 0.5f) / float(v.width) * 2.0f - 1.0f, w = 1.0f - (float(y) + 0.5f) / float(v.height) * 2.0f;
+ const float m = glm::length(v.vectors[size_t(y) * size_t(v.width) + size_t(x)]);
+ const float d2 = std::max(std::abs(u), std::abs(w));
+ if (d2 < 0.3f) centre = std::max(centre, m);
+ else if (d2 > 0.5f && d2 < 0.8f) band = std::max(band, m);
+ }
+ std::printf(" horn on a sphere: centre %.3f, outer band %.4f (radii)\n", centre, band);
+ CHECK(centre > 0.2f);
+ CHECK(band < 0.15f * centre); // (what is left: the sphere is not quite a quadric over so wide a square)
+}
+
TEST(kelvinlet_is_deterministic_with_threads) {
std::vector<glm::vec3> result[2];
for (int pass = 0; pass < 2; ++pass) {