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.

Dynamic topology: split on the curve the normals describe (no facets showing through), keep scratch across strokes, faster renumbering; guide image and shots

islandmed1918f2ade7a3
diff --git a/docs/guide/images/dynamic-topology.jpg b/docs/guide/images/dynamic-topology.jpg
new file mode 100644
index 00000000..4e8873bc
Binary files /dev/null and b/docs/guide/images/dynamic-topology.jpg differ
diff --git a/src/core/Benchmark.cpp b/src/core/Benchmark.cpp
index 0edc2e27..393722e5 100644
--- a/src/core/Benchmark.cpp
+++ b/src/core/Benchmark.cpp
@@ -336,18 +336,8 @@ std::string runDyntopoBenchmark(const BenchOptions& o, const BenchGpu* gpu) {
log(" " + row.brush.name);
doc.sculptor.dyntopo.enabled = true;
doc.sculptor.dyntopo.detail = row.detail;
- for (double& x : doc.sculptor.dynamicTopology().phaseMs) x = 0;
const StrokeStats s = runStroke(doc, row.brush, o.dabsPerStroke, gpu, order);
doc.sculptor.dyntopo.enabled = false;
- {
- std::string ph = " phases:";
- for (double x : doc.sculptor.dynamicTopology().phaseMs) ph += " " + fmt("%.1f", x);
- ph += " gather:";
- for (double& x : doc.sculptor.dynamicTopology().gatherMs) { ph += " " + fmt("%.1f", x); x = 0; }
- ph += " ops " + std::to_string(doc.sculptor.dynamicTopology().stats.splits) + "/" + std::to_string(doc.sculptor.dynamicTopology().stats.collapses) + "/" + std::to_string(doc.sculptor.dynamicTopology().stats.flips);
- log(ph);
- doc.sculptor.dynamicTopology().stats = {};
- }
const double d = std::max(s.dabs, 1);
md << "| " << row.brush.name << " | " << fmt("%.2f", s.remesh / d) << " | " << fmt("%.2f", s.kernel / d) << " | "
<< fmt("%.2f", s.normals / d) << " | " << fmt("%.2f", s.bvh / d) << " | ";
diff --git a/src/core/Dyntopo.cpp b/src/core/Dyntopo.cpp
index eb9166f4..6f49a8b9 100644
--- a/src/core/Dyntopo.cpp
+++ b/src/core/Dyntopo.cpp
@@ -656,9 +656,22 @@ uint32_t DynamicTopology::split(uint32_t a, uint32_t b) {
uint32_t tris[4];
const int n = edgeTriangles(a, b, tris);
if (n < 1 || n > 2) return kNone;
- const glm::vec3 mid = (m.positions[a] + m.positions[b]) * 0.5f;
+ // On the curve the end normals describe (a PN-triangle edge's midpoint), not the chord: splitting a
+ // coarse curved surface then keeps it round instead of showing its old flat facets. Kept within a quarter
+ // of the edge, for creases.
+ const glm::vec3 pa = m.positions[a], pb = m.positions[b];
+ glm::vec3 bend(0.0f);
+ if (m.normals.size() == m.vertexCount()) {
+ const glm::vec3 na = m.normals[a], nb = m.normals[b], e = pb - pa;
+ bend = (na * glm::dot(e, na) - nb * glm::dot(e, nb)) * -0.125f;
+ const float len = glm::length(bend), limit = 0.25f * glm::length(e);
+ if (!std::isfinite(len)) bend = glm::vec3(0.0f);
+ else if (len > limit) bend *= limit / len;
+ }
+ const glm::vec3 mid = (pa + pb) * 0.5f + bend;
const uint32_t v = newVertex();
interpolateVertex(v, a, b, 0.5f, mid);
+ if (!m.base.empty()) m.base[v] += bend; // (sculpt layers: positions stay base + offsets)
for (int i = 0; i < n; ++i) {
const uint32_t t = tris[i];
glm::uvec3 tri = m.triangles[t];
@@ -1092,14 +1105,7 @@ void DynamicTopology::relaxPass() {
}
void DynamicTopology::gatherRegion() {
- auto t0 = std::chrono::steady_clock::now();
- auto tick = [&](int i) {
- const auto now = std::chrono::steady_clock::now();
- gatherMs[i] += std::chrono::duration<double, std::milli>(now - t0).count();
- t0 = now;
- };
bvh_->queryTriangles(*mesh_, center_, radius_, regionTris_);
- tick(0);
if (mirrorSigns_.empty()) return;
// Every vertex of the triangles under each mirror image of the brush, to pair up.
std::vector<uint32_t>& verts = regionVerts_;
@@ -1123,8 +1129,6 @@ void DynamicTopology::gatherRegion() {
bvh_->queryTriangles(*mesh_, center_ * s, reach, tris);
addCorners(tris);
}
- tick(1);
- tick(2);
// Matched within a third of the edge length (the target, or the mesh's own where it is finer, so the
// search grid's cells hold a vertex or two however fine the mesh is).
std::vector<float> lengths;
@@ -1139,9 +1143,7 @@ void DynamicTopology::gatherRegion() {
typical = std::min(typical, lengths[lengths.size() / 2]);
}
eps_ = std::max(typical * 0.3f, 1e-7f);
- tick(3);
matchMirrors(verts);
- tick(4);
}
bool DynamicTopology::remesh(Mesh& mesh, BVH& bvh, const glm::vec3& center, float radius, float edgeLength,
@@ -1185,33 +1187,23 @@ bool DynamicTopology::remesh(Mesh& mesh, BVH& bvh, const glm::vec3& center, floa
ops_ = 0;
maxOps_ = 6000; // (bounds a dab's time: a big brush over fine triangles coarsens them over several dabs)
- auto lap = std::chrono::steady_clock::now();
- auto phase = [&](int i) {
- const auto now = std::chrono::steady_clock::now();
- phaseMs[i] += std::chrono::duration<double, std::milli>(now - lap).count();
- lap = now;
- };
gatherRegion();
- phase(0);
if (regionTris_.empty()) return false;
splitPass();
bvh.refitChanged(mesh);
bvh.queryTriangles(mesh, center_, radius_, regionTris_);
- phase(1);
collapsePass();
- phase(2);
flipPass();
- phase(3);
relaxPass();
- phase(4);
const bool changed = !changedV_.empty() || !changedT_.empty();
compact();
- phase(5);
// Rings and normals of everything that changed, the BVH's boxes, and what the renderer must patch.
const uint32_t nv = uint32_t(mesh.vertexCount());
for (uint32_t v : changedV_)
if (v < changedVFlag_.size()) changedVFlag_[v] = 0;
+ std::vector<std::pair<uint32_t, uint32_t>> moves(moved_.begin(), moved_.end());
+ std::sort(moves.begin(), moves.end());
std::vector<uint32_t> verts;
verts.reserve(changedV_.size());
for (uint32_t v : changedV_) {
@@ -1219,8 +1211,9 @@ bool DynamicTopology::remesh(Mesh& mesh, BVH& bvh, const glm::vec3& center, floa
verts.push_back(v);
continue;
}
- for (const auto& [from, to] : moved_)
- if (from == v) verts.push_back(to);
+ // (Past the end now: moved down, or removed.)
+ const auto it = std::lower_bound(moves.begin(), moves.end(), std::make_pair(v, 0u));
+ if (it != moves.end() && it->first == v) verts.push_back(it->second);
}
std::sort(verts.begin(), verts.end());
verts.erase(std::unique(verts.begin(), verts.end()), verts.end());
@@ -1236,7 +1229,6 @@ bool DynamicTopology::remesh(Mesh& mesh, BVH& bvh, const glm::vec3& center, floa
bvh.refitChanged(mesh);
mesh.changedTriangles.insert(mesh.changedTriangles.end(), changedT_.begin(), changedT_.end());
changedT_.clear();
- phase(6);
lastMs = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - clock).count();
return changed;
}
@@ -1255,11 +1247,11 @@ std::unique_ptr<TopologyUndo> DynamicTopology::finish(Mesh& mesh, BVH& bvh) {
std::vector<uint8_t>().swap(deadV_);
std::vector<uint8_t>().swap(deadT_);
std::vector<uint8_t>().swap(changedVFlag_);
- std::vector<MirrorMap>().swap(mirrorMaps_);
+ // (The mirror maps and the vertex stamps stay for the next stroke: their stamps make them empty, and
+ // sized for the whole mesh they would cost a pass over it to make again.)
std::vector<uint64_t>().swap(gridKeys_);
std::vector<uint32_t>().swap(gridHead_);
std::vector<uint32_t>().swap(gridNext_);
- std::vector<uint32_t>().swap(seen_);
std::vector<uint32_t>().swap(regionVerts_);
if (undo->vertexSlots.empty() && undo->triangleSlots.empty() && undo->uvs.empty()) return nullptr;
return undo;
diff --git a/src/core/Dyntopo.h b/src/core/Dyntopo.h
index 8576c63b..f2a40075 100644
--- a/src/core/Dyntopo.h
+++ b/src/core/Dyntopo.h
@@ -113,8 +113,6 @@ public:
DyntopoStats stats; // since the last reset
double lastMs = 0.0; // the last remesh's time
- double phaseMs[8] = {}; // TEMP
- double gatherMs[8] = {}; // TEMP
private:
struct Edge {
diff --git a/src/core/Mesh.cpp b/src/core/Mesh.cpp
index cc15ac8b..0a3e3331 100644
--- a/src/core/Mesh.cpp
+++ b/src/core/Mesh.cpp
@@ -257,7 +257,7 @@ void Mesh::endDynamicAdjacency() {
std::vector<uint32_t> packed;
size_t total = 0;
for (size_t v = 0; v < nv; ++v) total += ends[v] - offsets[v];
- packed.reserve(total);
+ packed.reserve(total + total / 16); // (room for the next stroke's first edits without copying it all)
for (size_t v = 0; v < nv; ++v) {
const uint32_t b = offsets[v], e = ends[v];
offsets[v] = uint32_t(packed.size());
diff --git a/tools/guide/make_images.py b/tools/guide/make_images.py
index ac65a87e..b86b5698 100644
--- a/tools/guide/make_images.py
+++ b/tools/guide/make_images.py
@@ -140,13 +140,26 @@ def subdivision(shots: pathlib.Path):
return save(img, "subdivision-levels", 1200)
+def dyntopo(shots: pathlib.Path):
+ box = (300, 125, 1020, 845)
+ names = [("dyntopo-wire", "Dynamic on: new triangles where the brush went"), ("dyntopo-shaded", "Snake Hook horns")]
+ panels = [Image.open(shots / f"{n}.png").convert("RGB").crop(box) for n, _ in names]
+ w, h = panels[0].size
+ img = Image.new("RGB", (w * 2 + 12, h), (20, 20, 22))
+ d = ImageDraw.Draw(img)
+ for i, (p, (_, text)) in enumerate(zip(panels, names)):
+ img.paste(p, (i * (w + 12), 0))
+ label(d, (i * (w + 12) + w // 2, 24), text, 22, "mt")
+ return save(img, "dynamic-topology", 1000)
+
+
def main() -> int:
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("shots", type=pathlib.Path, help="folder with the screenshots (--ui-out of shots.sfui)")
ap.add_argument("--preview", type=pathlib.Path, help="also write a contact sheet of every image here")
args = ap.parse_args()
OUT.mkdir(parents=True, exist_ok=True)
- made = [overview(args.shots), remesh(args.shots), subdivision(args.shots)]
+ made = [overview(args.shots), remesh(args.shots), subdivision(args.shots), dyntopo(args.shots)]
for name, (shot, box, width) in CROPS.items():
src = args.shots / f"{shot}.png"
if not src.exists():
diff --git a/tools/guide/shots.sfui b/tools/guide/shots.sfui
index 2e05cbe5..8bdc700e 100644
--- a/tools/guide/shots.sfui
+++ b/tools/guide/shots.sfui
@@ -246,3 +246,23 @@ run subdiv_level level=6
click "Tool/Geometry"
wait 0.2
shot sdiv-6
+
+# Dynamic topology: Snake Hook pulls horns out of a sphere (mirrored), with new triangles all along them.
+run new_sphere subdivisions=3
+click "Dynamic##draw.dynamic"
+run dyntopo detail=60
+click "SnakeHook##brush.snakehook"
+drag_path 0.76 0.32 0.85 0.25 0.95 0.17 1.05 0.08 1.12 0.0 S=1.5
+drag_path 0.5 0.2 0.5 0.05 0.5 -0.1 S=1.0
+click "Standard##brush.standard"
+stroke 0.3 0.65 0.7 0.65 1.0
+key f
+orbit 60 -25
+wait 0.2
+click "PolyF##view.polyf"
+wait 0.2
+shot dyntopo-wire
+click "PolyF##view.polyf"
+wait 0.2
+shot dyntopo-shaded
+click "Dynamic##draw.dynamic"