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: clean Sharp Crease / Crease / Pinch (#125), base wires (#128), micro count in the top bar
islandmed76933ee6d7cc
diff --git a/changes/fix-rod-micro.md b/changes/fix-rod-micro.mdnew file mode 100644index 00000000..c18c26d5--- /dev/null+++ b/changes/fix-rod-micro.md@@ -0,0 +1,20 @@+---+whats_new: **Clean creases on micro levels**: **Sharp Crease**, **Crease** and **Pinch** at full strength no longer+ leave random noise on a micro level. The title bar counts the micro triangles beside the triangles, and **Wire**+ shows the level below the micro level on the micro surface.+fixed: **Micro levels: Sharp Crease, Crease and Pinch** at high intensity left prominent random noise. They+ now pull the height profile together the way the brushes pull a mesh, resampling the heights from where the pull+ brings the surface, so nothing grows from dab to dab; big brushes stay clean too (#125, reported by RodTDai).+fixed: **Wire** on a micro level showed nothing; it now draws the edges of the level below the micro level (quad+ diagonals left out, as on a mesh) on the micro surface (#128, reported by RodTDai).+added: The title bar's triangle count adds the micro triangles of the visible Parts while there are any+ ("20.5k + 5.24M micro"), and so does the classic look's ActiveTris readout (#125).+testers:+ - Any OS: Tool > Geometry > Divide to Micro on a sphere, Sharp Crease at Intensity 100 with a pen pressed hard:+ a clean deep groove, no speckled noise around it. Same with Crease and Pinch, small and large sizes.+ - Turn Wire on with the micro level: the base edges show on the sculpted surface and follow it; off again: gone.+ - The title bar reads "<triangles> + <count> micro"; Delete Micro: the micro part goes.+---+Reviewer note: the old micro sharpen was an anti-diffusion (each point pushed away from its neighbours' average),+which amplifies grid-scale detail every dab. tests/test_micro.cpp micro_crease_strokes_stay_smooth measures it+(roughness 0.276 -> 0.014 for Sharp Crease).diff --git a/docs/guide/micro-levels.md b/docs/guide/micro-levels.mdindex d12f9e27..91a68889 100644--- a/docs/guide/micro-levels.md+++ b/docs/guide/micro-levels.md@@ -36,8 +36,8 @@ These brushes work on micro levels: **Standard**, **Clay**, **Clay Block**, **Smstroke is one undo step.- A micro level stores one height per point, so it moves the surface in and out, never sideways. **Pinch**,- **Crease** and **Sharp Crease** sharpen the height profile instead of pulling points together (the result- looks the same for creases and folds).+ **Crease** and **Sharp Crease** pull the height profile together instead of the points (the result looks the+ same for creases and folds), and stay clean at full strength.- **Move** reshapes the level below the micro level (the base) and the micro detail rides along on it, in its ownframe: drag an ear back without smearing the pores. To move the micro surface itself along its normals, tick**Move along normals**.diff --git a/src/app/Shell.cpp b/src/app/Shell.cppindex 7c0079c0..381efc6d 100644--- a/src/app/Shell.cpp+++ b/src/app/Shell.cpp@@ -15,6 +15,7 @@#include "app/Ui.h"#include "app/I18n.h"#include "core/LitSphere.h"+#include "core/Micro.h"#include "core/VertexPaint.h"#include "core/Texture.h"@@ -236,6 +237,16 @@ std::string elide(ImFont* font, const std::string& text, float width) {}constexpr float kPointsReadoutW = 190.0f; // the ActiveTris / TotalTris readout at the right of the top shelf, unscaled+constexpr float kMicroReadoutW = 100.0f; // ... and its "Micro 300.00M" in front while a visible Part has a micro level (#125)++// The micro triangles of the visible Parts (core/Micro.h).+size_t visibleMicroTriangles(const Document& doc) {+ size_t micro = 0;+ for (int i = 0; i < doc.partCount(); ++i)+ if (doc.parts[size_t(i)].visible) micro += microTriangleCount(doc.partMesh(i));+ return micro;+}+float pointsReadoutW(const Document& doc) { return kPointsReadoutW + (visibleMicroTriangles(doc) ? kMicroReadoutW : 0.0f); }bool segmentPair(const std::string& a, const std::string& b) {auto group = [](const std::string& id) {@@ -1474,7 +1485,9 @@ void App::drawStatusBar(const ShellGeometry& g) {mods.push_back({ICON_LC_FILE, "file", name});size_t tris = 0;for (int i = 0; i < doc_.partCount(); ++i) tris += doc_.partMesh(i).triangleCount();- mods.push_back({ICON_LC_TRIANGLE, "tris", shortCount(tris)});+ // ... and the visible Parts' micro triangles beside them while there are any (#125): "20.5k + 5.24M micro".+ const size_t micro = visibleMicroTriangles(doc_);+ mods.push_back({ICON_LC_TRIANGLE, "tris", shortCount(tris) + (micro ? " + " + shortCount(micro) + " micro" : std::string())});if (ImGui::GetTime() - fpsShownAt_ > 0.5 || fpsShown_ < 0.0f) {fpsShown_ = ImGui::GetIO().Framerate;fpsShownAt_ = ImGui::GetTime();@@ -1989,7 +2002,7 @@ void App::drawShelf(int shelf, const CanvasRect& r) {ImGui::SameLine(0.0f, prev == "sep" || item == "sep" ? 0.0f : segmentPair(prev, item) ? 1.0f * s : 4.0f * s);}if (!vertical && item == "widget.points") { // right-aligned readout- const float w = kPointsReadoutW * s;+ const float w = pointsReadoutW(doc_) * s;ImGui::SameLine(std::max(ImGui::GetCursorPosX(), ImGui::GetWindowContentRegionMax().x - w));}lineStart = false;@@ -2147,23 +2160,31 @@ void App::drawShelfItem(int shelf, int index, const std::string& item, bool vertactiveTris_ += m.triangleCount() - std::min(hidden, m.triangleCount());}}- const size_t active = activeTris_;- // One line on the row's centre line, right-aligned to the shelf edge: "ActiveTris 20.5k TotalTris 20.5k".+ const size_t active = activeTris_, micro = visibleMicroTriangles(doc_);+ // One line on the row's centre line, right-aligned to the shelf edge: "ActiveTris 20.5k TotalTris 20.5k",+ // after "Micro 300.00M" while a visible Part has a micro level.ImGui::BeginGroup();if (fontSmall_) ImGui::PushFont(fontSmall_);const ImVec2 p = ImGui::GetCursorScreenPos();- ImGui::Dummy(ImVec2(kPointsReadoutW * s, rowH));+ const float readoutW = pointsReadoutW(doc_) * s;+ ImGui::Dummy(ImVec2(readoutW, rowH));ImDrawList* dl = ImGui::GetWindowDrawList();- const std::string a = shortCount(active), t = shortCount(total);+ const std::string a = shortCount(active), t = shortCount(total), mc = micro ? shortCount(micro) : std::string();const float gap = 5.0f * s, groupGap = 14.0f * s;- const float w = ImGui::CalcTextSize("ActiveTris").x + ImGui::CalcTextSize(a.c_str()).x + ImGui::CalcTextSize("TotalTris").x +- ImGui::CalcTextSize(t.c_str()).x + 2.0f * gap + groupGap;- float x = std::floor(p.x + kPointsReadoutW * s - w);+ float w = ImGui::CalcTextSize("ActiveTris").x + ImGui::CalcTextSize(a.c_str()).x + ImGui::CalcTextSize("TotalTris").x ++ ImGui::CalcTextSize(t.c_str()).x + 2.0f * gap + groupGap;+ if (micro) w += ImGui::CalcTextSize("Micro").x + ImGui::CalcTextSize(mc.c_str()).x + gap + groupGap;+ float x = std::floor(p.x + readoutW - w);const float y = std::floor(p.y + (rowH - ImGui::GetTextLineHeight()) * 0.5f);auto put = [&](const char* text, const ImVec4& col, float after) {dl->AddText(ImVec2(x, y), theme::u32(col), text);x += ImGui::CalcTextSize(text).x + after;};+ if (micro) {+ put("Micro", theme::textDim, gap);+ put(mc.c_str(), theme::text, groupGap);+ ImGui::LogText("Micro %s ", mc.c_str());+ }put("ActiveTris", theme::textDim, gap);put(a.c_str(), theme::text, groupGap);put("TotalTris", theme::textDim, gap);@@ -2172,7 +2193,8 @@ void App::drawShelfItem(int shelf, int index, const std::string& item, bool vertif (fontSmall_) ImGui::PopFont();ImGui::EndGroup();ui::HelpText("ActiveTris / TotalTris", nullptr,- "Triangles you can see and sculpt (visible Parts, hidden parts left out) and in the whole scene.",+ "Triangles you can see and sculpt (visible Parts, hidden parts left out) and in the whole scene. "+ "Micro: the micro triangles of the visible Parts' micro levels (Geometry > Micro).","Draw");} else if (item.rfind("widget.tile.", 0) == 0) {const float w = (shell_.narrow ? 60.0f : 76.0f) * s;diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cppindex ddd19380..a47116d7 100644--- a/src/app/UiTest.cpp+++ b/src/app/UiTest.cpp@@ -436,6 +436,24 @@ double Demo::query(const std::string& name, bool& known) const {// (rows as saved: top first)return 0.2126 * lastShot_[i] + 0.7152 * lastShot_[i + 1] + 0.0722 * lastShot_[i + 2];}+ if (name.rfind("edges@", 0) == 0) { // edges@X0,Y0,X1,Y1: mean luma step between neighbouring pixels of the last shot there+ float x0 = 0.0f, y0 = 0.0f, x1 = 0.0f, y1 = 0.0f;+ if (std::sscanf(name.c_str() + 6, "%f,%f,%f,%f", &x0, &y0, &x1, &y1) != 4) {+ known = false;+ return 0.0;+ }+ if (lastShot_.empty()) return -1.0;+ auto luma = [&](int x, int y) {+ const size_t i = size_t(y) * size_t(lastShotW_) * 4 + size_t(x) * 4;+ return 0.2126 * lastShot_[i] + 0.7152 * lastShot_[i + 1] + 0.0722 * lastShot_[i + 2];+ };+ const int ax = std::clamp(int(x0 * float(lastShotW_)), 0, lastShotW_ - 2), bx = std::clamp(int(x1 * float(lastShotW_)), ax + 1, lastShotW_ - 1);+ const int ay = std::clamp(int(y0 * float(lastShotH_)), 0, lastShotH_ - 2), by = std::clamp(int(y1 * float(lastShotH_)), ay + 1, lastShotH_ - 1);+ double sum = 0.0;+ for (int y = ay; y < by; ++y)+ for (int x = ax; x < bx; ++x) sum += std::abs(luma(x + 1, y) - luma(x, y)) + std::abs(luma(x, y + 1) - luma(x, y));+ return sum / double((bx - ax) * (by - ay));+ }if (name == "triangles") return double(m.triangleCount());if (name == "vertices") return double(m.vertexCount());if (name == "layers") return double(m.layers.size());diff --git a/src/core/Micro.h b/src/core/Micro.hindex 7be15ab3..53787930 100644--- a/src/core/Micro.h+++ b/src/core/Micro.h@@ -32,7 +32,7 @@//// What a scalar displacement can't do: move points sideways. Brushes that slide the surface (Move's sideways// part, Snake Hook, Nudge, VDM stamps with overhangs) work on real subdivision levels; on a micro level Pinch-// sharpens the height profile instead (see microBrushSupport).+// resamples the heights from where its pull would bring the surface (see microBrushSupport).#pragma once#include "core/Mesh.h"@@ -271,7 +271,7 @@ private:// --- Brushes (driven by Sculptor, which calls these when the mesh has a micro level) ---// Whether a brush works on micro levels: 2 = as on a mesh, 1 = approximated (Pinch, Crease and Sharp Crease-// sharpen instead of pulling sideways; Move moves along the normal), 0 = not at all.+// resample the heights from where their pull would bring the surface; Move moves along the normal), 0 = not at all.int microBrushSupport(BrushType type);struct BrushAlpha;@@ -284,8 +284,10 @@ public:// toward `alphaUp` (#92).void dab(Mesh& m, const BVH& bvh, BrushType type, const glm::vec3& center, const glm::vec3& viewDir, float radius,float strength, bool invert, float focal, const glm::vec3& alphaUp, bool maskBrush, float maskStrength,- const BrushAlpha* alpha = nullptr, const std::function<float(const glm::vec3&)>& texture = {});- // (texture: a brush recipe's surface pattern, 0..1 at a surface point, times the tip; #120)+ const BrushAlpha* alpha = nullptr, const std::function<float(const glm::vec3&)>& texture = {},+ const glm::vec3& strokeDir = glm::vec3(0.0f));+ // (texture: a brush recipe's surface pattern, 0..1 at a surface point, times the tip; #120. strokeDir: where the+ // stroke is heading, for Sharp Crease's pull across it; 0 = none)// Move: captures the points within the dab (with each mirror's signs), then moves them along their normals.void beginGrab(Mesh& m, const BVH& bvh, const glm::vec3& center, float radius, float focal, const glm::mat3& mirror);void grab(Mesh& m, const glm::vec3& delta);@@ -306,6 +308,8 @@ public:private:std::vector<uint8_t> changed_; // per candidate of the last dab: the kernel changed its grid+ std::vector<int16_t> snap_; // Pinch, Crease, Sharp Crease: the grids of this dab's triangles as it found them+ std::vector<uint32_t> snapOf_; // per base triangle: its grid in snap_ + 1 (0 = none)std::vector<uint32_t> stitchStamp_; // per base triangle: dabEpoch_ when the stitch changed a point of itvoid touch(MicroMesh& mm, std::span<const uint32_t> tris);void widen(MicroMesh& mm, float need); // MicroMesh::widen, keeping this stroke's saved tiles in stepdiff --git a/src/core/MicroSculpt.cpp b/src/core/MicroSculpt.cppindex ffe999ca..954dc424 100644--- a/src/core/MicroSculpt.cpp+++ b/src/core/MicroSculpt.cpp@@ -60,11 +60,10 @@ bool triangleNear(const Mesh& m, const MicroMesh& mm, uint32_t t, const glm::vecreturn glm::dot(q - c, q - c) <= r * r;}-// Writes the change at every grid point of an n-grid from `coarse` changes on the points `stride` apart (a grid of-// n / stride): each point gets the coarse micro triangle's values interpolated (exactly the coarse value on coarse-// points). apply(index, change) is called for the points whose change isn't 0.-template <class F>-void interpolateCoarse(const float* coarse, uint32_t n, uint32_t stride, F&& apply) {+// Every grid point of an n-grid with the values on the points `stride` apart (a grid of n / stride) interpolated+// over the coarse micro triangle holding it (exactly the coarse value on coarse points): each(index, i, j, value).+template <class T, class F>+void forInterpolated(const T* coarse, uint32_t n, uint32_t stride, F&& each) {const uint32_t cm = n / stride;const float inv = 1.0f / float(stride);uint32_t idx = 0;@@ -74,7 +73,7 @@ void interpolateCoarse(const float* coarse, uint32_t n, uint32_t stride, F&& appfor (uint32_t i = 0; i + j <= n; ++i, ++idx) {const uint32_t ci = i / stride, ri = i % stride;const float fi = float(ri) * inv;- float d;+ T d;if (ri == 0 && rj == 0) d = coarse[MicroMesh::index(ci, cj, cm)];else if (fi + fj <= 1.0f)d = coarse[MicroMesh::index(ci, cj, cm)] * (1.0f - fi - fj) + coarse[MicroMesh::index(ci + 1, cj, cm)] * fi +@@ -82,11 +81,20 @@ void interpolateCoarse(const float* coarse, uint32_t n, uint32_t stride, F&& appelsed = coarse[MicroMesh::index(ci + 1, cj, cm)] * (1.0f - fj) + coarse[MicroMesh::index(ci + 1, cj + 1, cm)] * (fi + fj - 1.0f) +coarse[MicroMesh::index(ci, cj + 1, cm)] * (1.0f - fi);- if (d != 0.0f) apply(idx, d);+ each(idx, i, j, d);}}}+// Writes the change at every grid point from `coarse` changes on the points `stride` apart (forInterpolated):+// apply(index, change) for the points whose change isn't 0.+template <class F>+void interpolateCoarse(const float* coarse, uint32_t n, uint32_t stride, F&& apply) {+ forInterpolated(coarse, n, stride, [&](uint32_t idx, uint32_t, uint32_t, float d) {+ if (d != 0.0f) apply(idx, d);+ });+}+// Rounds `units` (a displacement over its step) to the nearest storable value (no libm call: brushes write// millions of these).inline int16_t quantizeUnits(float units) {@@ -105,6 +113,77 @@ void forNeighbors(uint32_t i, uint32_t j, uint32_t n, F&& f) {if (i > 0) f(i - 1, j + 1);}+// Pinch on a height field (Pinch, Crease, Sharp Crease; #125). Pulling the surface sideways towards the brush+// centre is, for a scalar displacement, resampling it from farther out: each point takes the value found where the+// pull brings the surface from (semi-Lagrangian). The values come from the grids as the dab found them (`snap` for+// the triangles it writes), interpolated on the micro triangles, walking across base triangles as far as the pull+// reaches. A scale-adaptive dab (stride > 1) works out the pull on its coarse points and resamples every grid point+// with it interpolated: resampling only the coarse points and interpolating their change handed each coarse point's+// detail to the points between, dab after dab (a hatch along big strokes). An interpolated value never leaves the range of the values around it, so+// unlike sharpening against the neighbours' average (which made every dab grow grid-scale noise) nothing amplifies,+// however many dabs land on a point.+struct PinchSampler {+ const Mesh& m;+ const MicroMesh& mm;+ const int16_t* snap = nullptr;+ const uint32_t* snapOf = nullptr; // per base triangle: its tile in `snap` + 1 (0: not written this dab, read mm.disp)++ const int16_t* grid(uint32_t t) const {+ return snapOf[t] ? snap + size_t(snapOf[t] - 1) * mm.samples : mm.disp.data() + size_t(t) * mm.samples;+ }+ // The flat base point of grid point (i, j) of triangle t (corners in triangle order).+ glm::vec3 flat(uint32_t t, float i, float j) const {+ const glm::uvec3 tr = m.triangles[t];+ const glm::vec3 a = m.positions[tr.x];+ const float inv = 1.0f / float(mm.n);+ return a + (m.positions[tr.y] - a) * (i * inv) + (m.positions[tr.z] - a) * (j * inv);+ }+ // The displacement (in units of the step) at `q` near the flat base, starting from triangle t.+ float sample(uint32_t t, const glm::vec3& q) const {+ float w1 = 0.0f, w2 = 0.0f;+ for (int steps = 0;; ++steps) {+ const glm::uvec3 tr = m.triangles[t];+ const glm::vec3 a = m.positions[tr.x], e1 = m.positions[tr.y] - a, e2 = m.positions[tr.z] - a, d = q - a;+ const float g11 = glm::dot(e1, e1), g12 = glm::dot(e1, e2), g22 = glm::dot(e2, e2);+ const float det = g11 * g22 - g12 * g12;+ if (!(det > 0.0f)) break; // (degenerate: its first corner)+ const float d1 = glm::dot(d, e1), d2 = glm::dot(d, e2);+ w1 = (g22 * d1 - g12 * d2) / det;+ w2 = (g11 * d2 - g12 * d1) / det;+ const float w0 = 1.0f - w1 - w2;+ // Outside: on to the neighbour across the edge facing the most negative weight (edge e runs from corner+ // e to e + 1, so w2 < 0 is past edge 0, w0 past edge 1, w1 past edge 2).+ const float lowest = std::min({w0, w1, w2});+ if (lowest >= -1e-5f || steps == 32) break;+ const int e = lowest == w2 ? 0 : lowest == w0 ? 1 : 2;+ const uint32_t nb = mm.edgeNeighbor[size_t(t) * 3 + size_t(e)];+ if (nb == MicroMesh::kNone) break; // (a boundary: the nearest point inside)+ t = nb >> 2;+ }+ // Clamped into the triangle.+ w1 = std::max(w1, 0.0f), w2 = std::max(w2, 0.0f);+ if (w1 + w2 > 1.0f) {+ const float k = 1.0f / (w1 + w2);+ w1 *= k, w2 *= k;+ }+ const uint32_t cm = mm.n, n = mm.n;+ const int16_t* g = grid(t);+ const float fi = w1 * float(cm), fj = w2 * float(cm);+ uint32_t i0 = std::min(uint32_t(fi), cm - 1), j0 = std::min(uint32_t(fj), cm - 1);+ if (i0 + j0 > cm - 1) {+ if (fi >= fj) i0 = cm - 1 - j0;+ else j0 = cm - 1 - i0;+ }+ const float a = std::clamp(fi - float(i0), 0.0f, 1.0f), b = std::clamp(fj - float(j0), 0.0f, 1.0f);+ auto at = [&](uint32_t i, uint32_t j) { return float(g[MicroMesh::index(i, j, n)]); };+ if (a + b <= 1.0f || i0 + j0 + 2 > cm) {+ const float c = std::max(1.0f - a - b, 0.0f);+ return (at(i0, j0) * c + at(i0 + 1, j0) * a + at(i0, j0 + 1) * b) / (c + a + b);+ }+ return at(i0 + 1, j0) * (1.0f - b) + at(i0 + 1, j0 + 1) * (a + b - 1.0f) + at(i0, j0 + 1) * (1.0f - a);+ }+};+} // namespacevoid MicroStroke::begin(Mesh& m) {@@ -278,7 +357,8 @@ void MicroStroke::stitch(const Mesh& m, MicroMesh& mm, std::span<const uint32_t>void MicroStroke::dab(Mesh& m, const BVH& bvh, BrushType type, const glm::vec3& center, const glm::vec3& viewDir, float radius,float strength, bool invert, float focal, const glm::vec3& alphaUp, bool maskBrush, float maskStrength,- const BrushAlpha* alpha, const std::function<float(const glm::vec3&)>& texture) {+ const BrushAlpha* alpha, const std::function<float(const glm::vec3&)>& texture,+ const glm::vec3& strokeDir) {if (!active_ || !m.micro || radius <= 0.0f) return;if (!maskBrush && microBrushSupport(type) == 0) return;auto clock = std::chrono::steady_clock::now();@@ -366,8 +446,22 @@ void MicroStroke::dab(Mesh& m, const BVH& bvh, BrushType type, const glm::vec3&up = glm::normalize(up);const glm::vec3 right = glm::cross(up, areaNormal);const float layerHeight = r * 0.08f * s;- const bool readsNeighbours = type == BrushType::Smooth || type == BrushType::Pinch || type == BrushType::Crease ||- type == BrushType::SharpCrease;+ const bool readsNeighbours = type == BrushType::Smooth;+ // Pinch, Crease and Sharp Crease pull the surface towards the centre (PinchSampler), reading the grids as the+ // dab found them: those it writes copied first.+ const bool pinches = !maskBrush && (type == BrushType::Pinch || type == BrushType::Crease || type == BrushType::SharpCrease);+ if (pinches) {+ if (snapOf_.size() != mm.baseTriangles) snapOf_.assign(mm.baseTriangles, 0);+ snap_.resize(cand_.size() * S);+ poolFor(cand_.size(), [&](size_t b, size_t e) {+ for (size_t ci = b; ci < e; ++ci) {+ std::copy_n(mm.disp.data() + size_t(cand_[ci]) * S, S, snap_.data() + ci * S);+ snapOf_[cand_[ci]] = uint32_t(ci + 1);+ }+ }, 64);+ }+ // Sharp Crease pulls across the stroke only (as on a mesh), so the groove narrows without bunching up along it.+ const glm::vec3 alongStroke = glm::length(strokeDir) > 1e-12f ? glm::normalize(strokeDir) : glm::vec3(0.0f);// 3. The kernel, in parallel over the triangles (each writes only its own grid). Scale-adaptive: the brush is// evaluated on the grid points `stride` apart, about kBrushSamples across its radius, and the points in between@@ -383,9 +477,12 @@ void MicroStroke::dab(Mesh& m, const BVH& bvh, BrushType type, const glm::vec3&std::vector<uint64_t> visited((cand_.size() + 7) / 8, 0);std::vector<float> clipped((cand_.size() + 7) / 8, 0.0f); // per block: the largest |d| that didn't fitconst float invStep = 1.0f / mm.step;+ const PinchSampler pinchSampler{m, mm, snap_.data(), snapOf_.data()};poolFor(cand_.size(), [&](size_t b, size_t e) {thread_local std::vector<int16_t> before;thread_local std::vector<float> coarse;+ thread_local std::vector<glm::vec3> coarsePull; // pinches at stride > 1: the pull on the coarse points+ thread_local std::vector<glm::vec4> coarsePinch; // ... with the depth changeMicroTriangle T;uint64_t count = 0;float over = 0.0f;@@ -397,6 +494,8 @@ void MicroStroke::dab(Mesh& m, const BVH& bvh, BrushType type, const glm::vec3&if (readsNeighbours && !maskBrush) before.assign(g, g + S); // Jacobi: neighbours as they wereconst int16_t* old = !maskBrush && type == BrushType::Layer ? oldDisp_.data() + size_t(tileOf_[t] - 1) * S : nullptr;coarse.assign(size_t(cm + 1) * (cm + 2) / 2, 0.0f);+ const bool pullCoarse = pinches && stride > 1;+ if (pullCoarse) coarsePull.assign(coarse.size(), glm::vec3(0.0f));bool any = false;uint32_t cidx = 0;for (uint32_t cj = 0; cj <= cm; ++cj)@@ -440,12 +539,18 @@ void MicroStroke::dab(Mesh& m, const BVH& bvh, BrushType type, const glm::vec3&});return c ? sum / float(c) : float(before[idx]);};- auto sharpen = [&](float k) { // pinch, on a height field: away from the neighbour average, kept within them- float lo, hi;- const float avg = neighbourAverage(lo, hi);- const float cur = float(before[idx]);- const float target = std::clamp(cur + (cur - avg) * k, lo, hi);- return (target - cur) * mm.step;+ // The surface pulled by k times its offset from the centre (along the base): the change, in world+ // units (at stride > 1 the pull is kept for the write below, which resamples every grid point).+ auto pinch = [&](float k, bool across) {+ glm::vec3 pull = projectOnPlane(center - p, nn);+ if (across) pull -= alongStroke * glm::dot(pull, alongStroke);+ if (pullCoarse) {+ coarsePull[cidx] = pull * k;+ any = any || k != 0.0f;+ return 0.0f;+ }+ const glm::vec3 from = pinchSampler.flat(t, float(i), float(j)) - pull * k;+ return (pinchSampler.sample(t, from) - float(pinchSampler.grid(t)[idx])) * mm.step;};switch (type) {case BrushType::Standard: delta = sign * s * r * 0.06f * f * glm::dot(areaNormal, nn); break;@@ -486,12 +591,11 @@ void MicroStroke::dab(Mesh& m, const BVH& bvh, BrushType type, const glm::vec3&delta = sign > 0.0f ? std::max(0.0f, target - d) : std::min(0.0f, target - d);break;}- case BrushType::Pinch: delta = sign * sharpen(0.6f * s * f); break;- case BrushType::Crease:- delta = -sign * s * r * 0.05f * f * f + sharpen(0.5f * s * f);- break;+ // (the pulls of the mesh brushes, core/SculptKernel.h)+ case BrushType::Pinch: delta = pinch(sign * 0.2f * s * f, false); break;+ case BrushType::Crease: delta = -sign * s * r * 0.05f * f * f + pinch(0.25f * s * f, false); break;case BrushType::SharpCrease:- delta = -sign * s * r * 0.08f * f * f * f * f + sharpen(0.7f * s * f);+ delta = -sign * s * r * 0.08f * f * f * f * f + pinch(0.3f * s * f, true);break;default: break;}@@ -519,6 +623,20 @@ void MicroStroke::dab(Mesh& m, const BVH& bvh, BrushType type, const glm::vec3&if (stride == 1) {for (uint32_t k = 0; k < S; ++k)if (coarse[k] != 0.0f) apply(k, coarse[k]);+ } else if (pullCoarse) {+ // The depth change and the pull interpolated together; each grid point resampled with its pull.+ coarsePinch.resize(coarse.size());+ for (size_t k = 0; k < coarse.size(); ++k) coarsePinch[k] = glm::vec4(coarsePull[k], coarse[k]);+ const int16_t* own = pinchSampler.grid(t);+ forInterpolated(coarsePinch.data(), n, stride, [&](uint32_t idx, uint32_t i, uint32_t j, const glm::vec4& v) {+ float delta = v.w;+ if (v.x != 0.0f || v.y != 0.0f || v.z != 0.0f) {+ const glm::vec3 from = pinchSampler.flat(t, float(i), float(j)) - glm::vec3(v);+ delta += (pinchSampler.sample(t, from) - float(own[idx])) * mm.step;+ }+ if (delta != 0.0f) apply(idx, delta);+ });+ count += S;} else {interpolateCoarse(coarse.data(), n, stride, apply);count += S;@@ -533,6 +651,9 @@ void MicroStroke::dab(Mesh& m, const BVH& bvh, BrushType type, const glm::vec3&float over = 0.0f;for (float c : clipped) over = std::max(over, c);+ if (pinches)+ for (uint32_t t : cand_) snapOf_[t] = 0;+// 4. Stitch the shared points.stitch(m, mm, cand_, maskBrush ? nullptr : changed_.data());if (over > 0.0f) widen(mm, over * 1.5f);diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cppindex 87f3b155..f4f0ad80 100644--- a/src/core/Sculpt.cpp+++ b/src/core/Sculpt.cpp@@ -894,7 +894,7 @@ void Sculptor::dab(Mesh& mesh, BVH& bvh, BrushType type, const DabParams& p, Symconst uint64_t pts = micro_.points;for (const SymCopy& m : copies(sym))micro_.dab(mesh, bvh, type, m.point(p.center), m.dir(p.viewDir), p.radius, p.strength * std::clamp(p.pressure, 0.0f, 1.0f),- p.invert, p.focal, m.dir(p.alphaUp), false, 0.0f, p.alpha);+ p.invert, p.focal, m.dir(p.alphaUp), false, 0.0f, p.alpha, {}, m.dir(p.strokeDelta));timings.kernelMs += micro_.findMs + micro_.kernelMs - k0;timings.normalsMs += micro_.stitchMs - s0;timings.vertices += micro_.points - pts;diff --git a/src/render/MicroRenderer.cpp b/src/render/MicroRenderer.cppindex 38d0ff2a..4c3af026 100644--- a/src/render/MicroRenderer.cpp+++ b/src/render/MicroRenderer.cpp@@ -28,7 +28,7 @@ const char* kMicroVS = R"(layout(location = 0) in uvec2 aIJ; // the patch's grid point at uLeveluniform mat4 uView;uniform mat4 uProj;-uniform usamplerBuffer uTris; // per slot: (corner vertices, -), (cluster across each edge, -)+uniform usamplerBuffer uTris; // per slot: (corner vertices, wireframe edges), (cluster across each edge, -)uniform samplerBuffer uBase; // per base vertex: position, normaluniform isamplerBuffer uDisp; // this chunk's displacement gridsuniform usamplerBuffer uLevels; // per cluster: the level it draws@@ -39,6 +39,7 @@ uniform int uUnrolled;uniform int uCorners[48];uniform float uStep;flat out int vSlot;+flat out int vEdges; // the base triangle's edges the wireframe draws (bit e: edge e; quad diagonals left out)out vec2 vIJ;#ifdef FLATout vec3 vViewPos;@@ -87,6 +88,7 @@ void main() {// The base triangle alone (a cluster far away, a scan's millions of tiny patches): each vertex is one// corner, where the weights below are (1, 0, 0) and the bulge 0, so the same point from one base vertex.uvec4 t0 = texelFetch(uTris, slot * 2);+ vEdges = int(t0.w);int corner = int(ij.x) + 2 * int(ij.y);int vi = int(corner == 0 ? t0.x : corner == 1 ? t0.y : t0.z);vec3 bp = texelFetch(uBase, vi * 2).xyz, bn = texelFetch(uBase, vi * 2 + 1).xyz;@@ -104,6 +106,7 @@ void main() {}uvec4 tv = texelFetch(uTris, slot * 2);uvec4 nb = texelFetch(uTris, slot * 2 + 1);+ vEdges = int(tv.w);uint v[3] = uint[3](tv.x, tv.y, tv.z);ord[0] = 0; ord[1] = 1; ord[2] = 2;if (v[ord[0]] > v[ord[1]]) { int t = ord[0]; ord[0] = ord[1]; ord[1] = t; }@@ -245,14 +248,17 @@ void MicroRenderer::buildLayout(const Mesh& mesh) {clusterShift_ = uint32_t(std::countr_zero(clusterSize));chunkSlots_ = std::max(clusterSize, (kChunkTexels / mm.samples) & ~(clusterSize - 1));const uint32_t clusters = (nt + clusterSize - 1) >> clusterShift_;- // Corners and the clusters across the edges, per slot.+ // Corners, the edges the wireframe draws (#128: the base's, without quad diagonals, as Renderer's own+ // wireframe) and the clusters across the edges, per slot.std::vector<uint32_t> tri(size_t(nt) * 8);poolFor(nt, [&](size_t b, size_t e) {for (size_t s = b; s < e; ++s) {const uint32_t t = order_[s];const glm::uvec3 tr = mesh.triangles[t];uint32_t* o = tri.data() + s * 8;- o[0] = tr.x, o[1] = tr.y, o[2] = tr.z, o[3] = 0;+ const uint32_t partner = mesh.quadPartnerAt(t);+ const int diagonal = partner != Mesh::kNoQuad && !mesh.triangleHidden(partner) ? mesh.quadDiagonal(t, partner) : -1;+ o[0] = tr.x, o[1] = tr.y, o[2] = tr.z, o[3] = diagonal < 0 ? 7u : 7u & ~(1u << diagonal);for (int k = 0; k < 3; ++k) {const uint32_t nb = mm.edgeNeighbor[size_t(t) * 3 + size_t(k)];o[4 + k] = nb == MicroMesh::kNone ? kNoCluster : slotOf_[nb >> 2] >> clusterShift_;diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cppindex f1a7a7d2..97d0b045 100644--- a/src/render/Renderer.cpp+++ b/src/render/Renderer.cpp@@ -319,13 +319,16 @@ flat in int vPrimBase;// Micro-displacement levels (render/MicroRenderer.cpp): the full-resolution shading grid of the base triangle,// interpolated on the micro triangle under the pixel (normal, curvature, mask), whatever level the geometry drew.flat in int vSlot;+flat in int vEdges;in vec2 vIJ;uniform samplerBuffer uShade;uniform int uN;uniform int uSamples;uniform mat4 uView;+uniform int uMicroWire; // 1: the wireframe of the base (the level below the micro level) on the micro surfacevec4 gNC;float gMask;+float gWire = 0.0;vec4 shadeAt(int i, int j) { return texelFetch(uShade, vSlot * uSamples + j * (uN + 1) - (j * (j - 1)) / 2 + i); }vec3 octNormal(vec2 v) {vec2 e = v * 2.0 - 1.0;@@ -358,6 +361,16 @@ void microFetch() {float c = dot(c3, w);gNC = vec4(mat3(uView) * normalize(n), c * abs(c));gMask = dot(vec3(s0.w, s1.w, s2.w), w);+ if (uMicroWire == 1) {+ // The base triangle's barycentric weights, in pixels from each edge: about a pixel wide line on its edges.+ vec3 b = vec3(float(uN) - vIJ.x - vIJ.y, vIJ.x, vIJ.y) / float(uN);+ vec3 px = b / max(fwidth(b), vec3(1e-6));+ float d = 1e9;+ if ((vEdges & 1) != 0) d = min(d, px.z); // edge 0: corners 0 -> 1+ if ((vEdges & 2) != 0) d = min(d, px.x); // edge 1: 1 -> 2+ if ((vEdges & 4) != 0) d = min(d, px.y); // edge 2: 2 -> 0+ gWire = 1.0 - smoothstep(0.0, 1.0, d);+ }}#define vNormalCurv gNC#undef V_MASK@@ -386,7 +399,11 @@ uniform int uTwoSided; // 1: back faces shade with the normal turned towaout vec4 fragColor;vec4 finish(vec3 c) {if (uDim == 1) c = mix(c, vec3(dot(c, vec3(0.2126, 0.7152, 0.0722))), 0.25) * 0.72;- return vec4(pow(c, vec3(1.0 / 2.2)), uAlpha);+ vec3 g = pow(c, vec3(1.0 / 2.2));+#ifdef MICRO+ g = mix(g, vec3(0.05, 0.05, 0.08), 0.5775 * gWire); // (the wireframe program's colour and opacity)+#endif+ return vec4(g, uAlpha);}// Keep in sync with sf::face groupColor.vec3 groupColor(uint id) {@@ -947,6 +964,7 @@ const Renderer::MeshProgram* Renderer::meshProgram(int features) {p.lightRot = at("uLightRot");p.lightLevels = at("uLightLevels");p.alpha = at("uAlpha");+ p.microWire = at("uMicroWire");p.primBase = at("uPrimBase");p.hasPaint = at("uHasPaint");p.normalFlipY = at("uNormalFlipY");@@ -2976,7 +2994,7 @@ void Renderer::draw(const Camera& camera, int targetWidth, int targetHeight, congl::DepthMask(GL_FALSE);for (const DrawItem& item : drawList_) {const MeshSlot* s = item.active ? findSlot(item.id) : nullptr;- if (!s || s->drawnTriangles == 0 || s->micro) continue; // (micro levels: no wireframe)+ if (!s || s->drawnTriangles == 0 || s->micro) continue; // (micro levels: drawMicro draws theirs)gl::Uniform3fv(gl::GetUniformLocation(wireProgram_, "uGridOrigin"), 1, glm::value_ptr(s->grid.origin));gl::Uniform1f(gl::GetUniformLocation(wireProgram_, "uGridStep"), s->grid.step);cull(*s, frameCull_);@@ -3139,6 +3157,7 @@ void Renderer::drawMicro(const MeshSlot& s, bool active, bool flat, const Rendergl::Uniform1i(p->showMask, active && settings.showMask ? 1 : 0);gl::Uniform1i(p->groups, active && settings.showFaceGroups ? 2 : 0);gl::Uniform1i(p->dim, !active && settings.dimInactive ? 1 : 0);+ gl::Uniform1i(p->microWire, active && settings.wireframe ? 1 : 0); // (#128: the wireframe pass skips micro levels)setLightUniforms(*p, settings);gl::Uniform1i(p->twoSided, 1);// Moving (Fast Navigation): a coarser target and half the budget, so orbiting stays smooth.diff --git a/src/render/Renderer.h b/src/render/Renderer.hindex 94460b27..aadc6ff6 100644--- a/src/render/Renderer.h+++ b/src/render/Renderer.h@@ -479,7 +479,7 @@ private:GLint view = -1, proj = -1, gridOrigin = -1, gridStep = -1, viewport = -1, projScale = -1, baseColor = -1, paint = -1,showMask = -1, groups = -1, litsphere = -1, litsphereCavity = -1, dim = -1, primBase = -1, hasPaint = -1,normalFlipY = -1, twoSided = -1, checker = -1, checkerScale = -1, tintShift = -1, lightRot = -1,- lightLevels = -1, alpha = -1;+ lightLevels = -1, alpha = -1, microWire = -1;GLint hasOrm = -1, hasEmissive = -1, hasMatNormal = -1, envLevels = -1, exposure = -1, toneMap = -1, material = -1,pbrCavity = -1, viewToEnv = -1, sh = -1;GLint ghostGroups = -1, ghostRange = -1, ghostEdge = -1, ghostA = -1, ghostB = -1, ghostC = -1, ghostBound = -1, ghostKind = -1, ghostFocal = -1;diff --git a/tests/test_micro.cpp b/tests/test_micro.cppindex b12dea61..0c814fe6 100644--- a/tests/test_micro.cpp+++ b/tests/test_micro.cpp@@ -802,3 +802,48 @@ TEST(micro_base_stroke_symmetry_hidden_mask) {}}+TEST(micro_crease_strokes_stay_smooth) {+ // #125: a deep Sharp Crease stroke at full strength left grid-scale noise on the micro level (the old sharpen+ // pushed every point away from its neighbours' average, which grows any noise dab after dab).+ for (float radius : {0.08f, 0.3f}) // (0.3: scale-adaptive, stride 2)+ for (BrushType type : {BrushType::Standard, BrushType::SharpCrease, BrushType::Crease, BrushType::Pinch}) {+ Document doc = microDoc(4);+ microNoise(doc.mesh, 0.0004f, 40.0f); // a little fine detail, as a sculpt has+ Symmetry sym;+ doc.sculptor.beginStroke(doc.mesh, brushName(type));+ for (int k = 0; k <= 60; ++k) {+ const float x = -0.15f + 0.3f * float(k) / 60.0f;+ DabParams p = dabAt(glm::normalize(glm::vec3(x, 0.0f, 1.0f)), radius, 1.0f);+ p.strokeDelta = glm::vec3(0.005f, 0.0f, 0.0f);+ doc.sculptor.dab(doc.mesh, doc.bvh, type, p, sym);+ }+ doc.undo.push(doc.sculptor.endStroke());+ // Grid-scale roughness: each interior point's distance from its six neighbours' average, against the+ // stroke's depth.+ const MicroMesh& mm = *doc.mesh.micro;+ const uint32_t n = mm.n;+ double lap = 0.0, deepest = 0.0;+ size_t count = 0;+ for (uint32_t t = 0; t < mm.baseTriangles; ++t) {+ const glm::uvec3 tr = doc.mesh.triangles[t];+ const glm::vec3 c = (doc.mesh.positions[tr.x] + doc.mesh.positions[tr.y] + doc.mesh.positions[tr.z]) / 3.0f;+ if (c.z < 0.9f || std::abs(c.y) > 0.05f || std::abs(c.x) > 0.12f) continue;+ const int16_t* g = mm.disp.data() + size_t(t) * mm.samples;+ for (uint32_t j = 1; j < n; ++j)+ for (uint32_t i = 1; i + j < n; ++i) {+ const double v = g[MicroMesh::index(i, j, n)];+ const double avg = (g[MicroMesh::index(i - 1, j, n)] + g[MicroMesh::index(i + 1, j, n)] ++ g[MicroMesh::index(i, j - 1, n)] + g[MicroMesh::index(i, j + 1, n)] ++ g[MicroMesh::index(i + 1, j - 1, n)] + g[MicroMesh::index(i - 1, j + 1, n)]) / 6.0;+ lap += std::abs(v - avg), ++count;+ deepest = std::max(deepest, std::abs(v));+ }+ }+ const double rough = lap / double(count) / deepest;+ // (11-05, radius 0.08: Sharp Crease 0.276 and Crease 0.144 before, 0.014 and 0.012 after, Standard 0.0016;+ // radius 0.3 brushes at stride 2: 0.011 and 0.008, and Pinch 0.021 with the pull interpolated, 0.081 without)+ CHECK(rough < 0.04);+ if (type == BrushType::Crease || type == BrushType::SharpCrease) CHECK(deepest * mm.step > 0.01); // (a groove)+ CHECK(watertight(doc.mesh));+ }+}diff --git a/tests/ui/micro_levels.sfui b/tests/ui/micro_levels.sfuiindex 29d88c0d..854e9e49 100644--- a/tests/ui/micro_levels.sfui+++ b/tests/ui/micro_levels.sfui@@ -8,6 +8,19 @@ click "Geometry/Divide to Micro"expect_status "Divided to micro"expect "Geometry/Bake Micro"expect_text "Micro 4"+# The title bar counts the micro triangles beside the triangles (#125).+expect_text "20.5k + 5.24M micro"++# Wire on a micro level shows the base's wireframe on the micro surface (#128).+shot micro-plain+remember [email protected],0.35,0.6,0.65 plain+click "Wire##view.wire"+expect_value wireframe == 1+shot micro-wire+expect_value [email protected],0.35,0.6,0.65 > $plain*1.5+click "Wire##view.wire"+expect_value wireframe == 0+run micro_noise amplitude=0.003 scale=40shot micro-noise