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.

Stencil: built-in patterns made at 1024 px when they become a Stencil (the alpha's 128 px stair-stepped every groove)

islandmedfc9d9b9de978
diff --git a/changes/fix-stencil-edges.md b/changes/fix-stencil-edges.md
new file mode 100644
index 00000000..b8a8da43
--- /dev/null
+++ b/changes/fix-stencil-edges.md
@@ -0,0 +1,9 @@
+---
+fixed: **Stencil** edges sculpt as clean grooves instead of a beaded zipper: a built-in pattern made into a Stencil
+ (**Use alpha**) is generated again at 1024 px instead of the alpha's 128, whose pixels showed as stair-steps
+ along every edge it sculpted.
+---
+Found reviewing the What's New shots of 0.11.11's Stencil. tests/test_stencil.cpp stencil_groove_walls_are_smooth_along_the_groove:
+roughness along a 30-degree Cobblestone-profile groove (per-bin line fit, p99 / depth) < 1% at 1024 px; the 128 px
+source measures more than twice that. (Tried and dropped: reading box-filtered mip levels by the vertices' spacing made
+it worse, 8.4% vs 0.2%: the coarse levels' square pixels stair-step diagonal edges again.)
diff --git a/src/core/BrushAlpha.cpp b/src/core/BrushAlpha.cpp
index 39765bcb..b011db7f 100644
--- a/src/core/BrushAlpha.cpp
+++ b/src/core/BrushAlpha.cpp
@@ -15,6 +15,8 @@ namespace sf {
namespace {
constexpr int kBuiltinSize = 128;
+// The size generate() fills: kBuiltinSize, or larger while BrushAlpha::builtinAt makes one (a Stencil spans the view).
+thread_local int gGenerateSize = kBuiltinSize;
float smooth01(float t) {
t = std::clamp(t, 0.0f, 1.0f);
@@ -44,13 +46,14 @@ template <class F>
BrushAlpha generate(std::string name, F&& f) {
BrushAlpha a;
a.name = std::move(name);
- a.width = a.height = kBuiltinSize;
- a.values.resize(size_t(kBuiltinSize) * kBuiltinSize);
- for (int y = 0; y < kBuiltinSize; ++y)
- for (int x = 0; x < kBuiltinSize; ++x) {
- const float u = (float(x) + 0.5f) / kBuiltinSize * 2.0f - 1.0f;
- const float v = 1.0f - (float(y) + 0.5f) / kBuiltinSize * 2.0f;
- a.values[size_t(y) * kBuiltinSize + size_t(x)] = std::clamp(f(u, v), 0.0f, 1.0f);
+ const int n = gGenerateSize;
+ a.width = a.height = n;
+ a.values.resize(size_t(n) * size_t(n));
+ for (int y = 0; y < n; ++y)
+ for (int x = 0; x < n; ++x) {
+ const float u = (float(x) + 0.5f) / float(n) * 2.0f - 1.0f;
+ const float v = 1.0f - (float(y) + 0.5f) / float(n) * 2.0f;
+ a.values[size_t(y) * size_t(n) + size_t(x)] = std::clamp(f(u, v), 0.0f, 1.0f);
}
return a;
}
@@ -450,6 +453,18 @@ bool BrushAlpha::builtin(const std::string& name, BrushAlpha& out) {
return false;
}
+bool BrushAlpha::builtinAt(const std::string& name, int size, BrushAlpha& out) {
+ const std::string key = lookupKey(name);
+ for (const Generated& g : kGenerated)
+ if (lookupKey(g.name) == key) {
+ gGenerateSize = std::clamp(size, 8, 4096);
+ out = g.make();
+ gGenerateSize = kBuiltinSize;
+ return true;
+ }
+ return builtin(name, out); // (Round, Square, Noise: as made)
+}
+
BrushAlpha BrushAlpha::fromImage(const Image& image, std::string name, bool invert) {
BrushAlpha a;
a.name = std::move(name);
diff --git a/src/core/BrushAlpha.h b/src/core/BrushAlpha.h
index 0392dc46..cf2c5279 100644
--- a/src/core/BrushAlpha.h
+++ b/src/core/BrushAlpha.h
@@ -44,6 +44,8 @@ struct BrushAlpha {
static const std::vector<BrushAlpha>& builtins();
// A built-in by name, ignoring case, spaces, '_' and '-' ("none" = Round); false for unknown names.
static bool builtin(const std::string& name, BrushAlpha& out);
+ // A built-in pattern made at `size` x `size` (the Stencil's, which spans the view: 128 shows its pixels there).
+ static bool builtinAt(const std::string& name, int size, BrushAlpha& out);
// One line on what a built-in is for (empty for unknown names), and all their names for messages.
static std::string builtinHelp(const std::string& name);
static std::string builtinNames();
diff --git a/src/core/Stencil.cpp b/src/core/Stencil.cpp
index 03bbf0da..1c5da817 100644
--- a/src/core/Stencil.cpp
+++ b/src/core/Stencil.cpp
@@ -17,6 +17,7 @@ namespace sf {
namespace {
constexpr int kMaxSide = 2048; // a stencil spans the view: more detail than an alpha's 512, at most 16 MB
+constexpr int kBuiltinStencilSize = 1024; // a built-in pattern made into a Stencil (BrushAlpha::builtinAt)
// sRGB byte -> linear, once.
const std::array<float, 256>& linearTable() {
@@ -177,10 +178,18 @@ void registerStencilCommands(CommandRegistry& registry) {
if (al.empty()) return {false, "The brush has no alpha (Round): pick one, or load an image"};
Image img;
std::string err;
- // An image alpha at its own resolution (the alpha keeps 512 px); a built-in as made.
- if (al.path.empty() || !Image::load(al.path, img, err)) img = alphaImage(al);
- else if (al.inverted) for (size_t i = 0; i < img.rgba.size(); i += 4)
- for (int k = 0; k < 3; ++k) img.rgba[i + size_t(k)] = uint8_t(255 - img.rgba[i + size_t(k)]);
+ // An image alpha at its own resolution (the alpha keeps 512 px); a built-in pattern made
+ // again at 1024 px (at 128 its pixels showed as stair-steps along every edge it sculpted).
+ BrushAlpha big;
+ if (!al.path.empty() && Image::load(al.path, img, err)) {
+ if (al.inverted)
+ for (size_t i = 0; i < img.rgba.size(); i += 4)
+ for (int k = 0; k < 3; ++k) img.rgba[i + size_t(k)] = uint8_t(255 - img.rgba[i + size_t(k)]);
+ } else if (al.path.empty() && BrushAlpha::builtinAt(al.name, kBuiltinStencilSize, big) && !big.empty()) {
+ img = alphaImage(big);
+ } else {
+ img = alphaImage(al);
+ }
st.set(std::move(img), al.name, al.path);
} else {
return {false, "stencil_load needs path=<image> or alpha=true"};
diff --git a/tests/test_stencil.cpp b/tests/test_stencil.cpp
index 438ebd8a..4754700b 100644
--- a/tests/test_stencil.cpp
+++ b/tests/test_stencil.cpp
@@ -14,6 +14,7 @@
#include <glm/gtc/matrix_transform.hpp>
#include <cmath>
+#include <tuple>
using namespace sf;
@@ -188,6 +189,8 @@ TEST(stencil_commands) {
CHECK(reg.execute(doc, "brush_set_alpha", {{"name", "cracks"}}).ok);
CHECK(reg.execute(doc, "stencil_load", {{"alpha", true}, {"reset", true}}).ok);
CHECK(doc.stencil.active() && doc.stencil.name == "Cracks");
+ CHECK(doc.stencil.image->width == 1024); // a built-in pattern made again at the Stencil's size (the alpha's is 128)
+ CHECK(doc.brushAlpha.width == 128);
CHECK(reg.execute(doc, "stencil_set", {{"x", 0.25}, {"angle", 450.0}, {"size", 100.0}, {"opacity", 2.0}, {"tile", true}}).ok);
CHECK(near(doc.stencil.center.x, 0.25f) && near(glm::degrees(doc.stencil.angle), 90.0f, 1e-3f));
CHECK(doc.stencil.size == 20.0f && doc.stencil.opacity == 1.0f && doc.stencil.tile);
@@ -195,3 +198,87 @@ TEST(stencil_commands) {
CHECK(reg.execute(doc, "stencil_clear", {}).ok && !doc.stencil.image);
CHECK(!reg.execute(doc, "stencil_load", {{"path", "/nonexistent/stencil.png"}}).ok);
}
+
+namespace {
+// A straight groove across the view at `angleDeg`, its profile as the Cobblestone pattern's (sqrt of a smoothstep:
+// steep at the floor), `halfWidth` canvas heights; w x w pixels.
+Image grooveImage(int w, float angleDeg, float halfWidth) {
+ Image img = blackImage(w, w);
+ const glm::vec2 dir(std::cos(glm::radians(angleDeg)), std::sin(glm::radians(angleDeg)));
+ for (int y = 0; y < w; ++y)
+ for (int x = 0; x < w; ++x) {
+ const glm::vec2 p((x + 0.5f) / w - 0.5f, (y + 0.5f) / w - 0.5f);
+ const float d = std::abs(p.x * dir.y - p.y * dir.x); // distance from the line through the centre
+ float t = std::clamp(d / halfWidth, 0.0f, 1.0f);
+ t = t * t * (3.0f - 2.0f * t);
+ const uint8_t g = uint8_t(std::lround(std::sqrt(t) * 255.0f));
+ for (int k = 0; k < 3; ++k) img.rgba[(size_t(y) * size_t(w) + size_t(x)) * 4 + size_t(k)] = g;
+ }
+ return img;
+}
+// Sculpts Standard dabs along the groove of `st` (laid over topView, the plane 2 x 2 world = the canvas), and returns
+// the roughness along it: vertices binned by their distance from the groove line, the p99 of how far each is from its
+// bin's mean height, over the groove's depth. A clean groove's height depends on the distance alone (0); beads along
+// it don't.
+float grooveRoughness(const Stencil& st, float angleDeg, int resolution) {
+ Document doc = makeDoc(makePlane(resolution));
+ Mesh& m = doc.mesh;
+ // The groove on the plane: canvas y runs along +z (topView), so the image's (x, y) is the world's (x, z).
+ const glm::vec3 along(std::cos(glm::radians(angleDeg)), 0.0f, std::sin(glm::radians(angleDeg)));
+ CHECK(doc.sculptor.beginStroke(m, "Standard"));
+ for (float s = -0.6f; s <= 0.6f; s += 0.01f) {
+ DabParams p = topDab(along * s, 0.25f);
+ p.strength = 0.3f;
+ p.stencil = {&st, topView(), 1.0f};
+ doc.sculptor.dab(m, doc.bvh, BrushType::Standard, p, Symmetry{false, false, false});
+ }
+ doc.sculptor.endStroke();
+ const glm::vec3 across = glm::cross(along, glm::vec3(0, 1, 0));
+ // Each bin's heights against a straight line through them (height by distance), so a steep wall inside one bin
+ // isn't counted as roughness; what is left is how the height wanders along the groove.
+ constexpr int kBins = 60;
+ constexpr float kReach = 0.15f; // world units each side of the groove line
+ struct Fit { double n = 0, x = 0, y = 0, xx = 0, xy = 0; };
+ std::vector<Fit> fit(kBins);
+ std::vector<std::tuple<int, float, float>> samples; // bin, distance, height
+ float lo = 1e9f, hi = -1e9f;
+ for (const glm::vec3& v : m.positions) {
+ if (std::abs(glm::dot(v, along)) > 0.3f) continue; // the stroke's even middle
+ const float d = std::abs(glm::dot(v, across));
+ if (d >= kReach) continue;
+ const int b = std::min(int(d / kReach * kBins), kBins - 1);
+ Fit& f = fit[size_t(b)];
+ f.n += 1; f.x += d; f.y += v.y; f.xx += double(d) * d; f.xy += double(d) * v.y;
+ samples.emplace_back(b, d, v.y);
+ lo = std::min(lo, v.y);
+ hi = std::max(hi, v.y);
+ }
+ std::vector<float> dev;
+ for (auto [b, d, y] : samples) {
+ const Fit& f = fit[size_t(b)];
+ const double den = f.n * f.xx - f.x * f.x;
+ const double k = std::abs(den) > 1e-18 ? (f.n * f.xy - f.x * f.y) / den : 0.0;
+ const double c = (f.y - k * f.x) / f.n;
+ dev.push_back(float(std::abs(y - (c + k * d))));
+ }
+ std::sort(dev.begin(), dev.end());
+ const float depth = std::max(hi - lo, 1e-9f);
+ return dev.empty() ? 1.0f : dev[size_t(double(dev.size() - 1) * 0.99)] / depth;
+}
+} // namespace
+
+TEST(stencil_groove_walls_are_smooth_along_the_groove) {
+ // A Cobblestone-like groove, 30 degrees across the plane's grid, about three vertices wide each side: its walls
+ // and floor must follow the groove, not bead along the triangles (#155, the 0.11.11 What's New shots).
+ const float angle = 30.0f;
+ Stencil st;
+ st.set(grooveImage(1024, angle, 0.02f), "groove", "");
+ st.size = 1.0f;
+ const float r = grooveRoughness(st, angle, 160);
+ CHECK(r < 0.01f);
+ // The cause in 0.11.11: the pattern at the alpha's 128 px, its pixels stair-stepping along the groove.
+ Stencil coarse;
+ coarse.set(grooveImage(128, angle, 0.02f), "groove", "");
+ coarse.size = 1.0f;
+ CHECK(grooveRoughness(coarse, angle, 160) > 2.0f * r);
+}