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
Coarse levels keep the vertex colour and mask: each cell's mean of the fine mesh's, updated when a paint stroke ends (0.11.7 regression: paint vanished while the view moved on dense meshes)
islandmedf1bd756c17fc
diff --git a/changes/fix-coarse-lod-colour.md b/changes/fix-coarse-lod-colour.mdnew file mode 100644index 00000000..d6ded773--- /dev/null+++ b/changes/fix-coarse-lod-colour.md@@ -0,0 +1,12 @@+---+whats_new: **Paint stays while the view moves**: on dense meshes, vertex paint, paint layers and the mask no longer+ vanish while you orbit or paint and pop back at rest.+fixed: Dense meshes over the triangle budget lost their vertex colour (paint layers included) and mask while the view+ moved (the coarse level 0.11.7 draws then had none) and showed them again at rest. The coarse level now carries each+ cell's mean colour and mask from the full mesh, updated when a paint stroke ends.+testers:+ - Windows or macOS: open a 10M+ triangle scan, Paint > Fill a colour, then orbit: the colour stays while moving.+ - Paint a stroke, then orbit at once: the stroke's colour shows while moving (it may draw in full for the first frames).+ - Mask part of it (Ctrl-drag), orbit: the masked area stays dark while moving.+---+Face groups and textures already drew in full detail while moving (per-triangle data never takes the coarse path).diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cppindex 80a269f7..6f7082cc 100644--- a/src/app/UiTest.cpp+++ b/src/app/UiTest.cpp@@ -518,6 +518,18 @@ 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("rgb_", 0) == 0 && name.size() > 5 && name[5] == '@') { // rgb_r@X,Y (or _g, _b): that channel 0-255 there+ const int k = name[4] == 'r' ? 0 : name[4] == 'g' ? 1 : name[4] == 'b' ? 2 : -1;+ float fx = 0.0f, fy = 0.0f;+ if (k < 0 || std::sscanf(name.c_str() + 6, "%f,%f", &fx, &fy) != 2) {+ known = false;+ return 0.0;+ }+ if (lastShot_.empty()) return -1.0;+ const int x = std::clamp(int(fx * float(lastShotW_)), 0, lastShotW_ - 1);+ const int y = std::clamp(int(fy * float(lastShotH_)), 0, lastShotH_ - 1);+ return double(lastShot_[size_t(y) * size_t(lastShotW_) * 4 + size_t(x) * 4 + size_t(k)]);+ }if (name.rfind("edges@", 0) == 0) { // edges@X0,Y0,X1,Y1: mean luma step between neighbouring pixels of the last shot therefloat 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) {diff --git a/src/render/Clusters.cpp b/src/render/Clusters.cppindex 13503707..41959460 100644--- a/src/render/Clusters.cpp+++ b/src/render/Clusters.cpp@@ -470,6 +470,7 @@ CoarseLevels buildCoarseLevels(const Mesh& mesh, const UploadOrder& order) {for (uint32_t k = 0; k < cellCount; ++k)if (L.parent[k] != ~0u) L.children[at[L.parent[k]]++] = k;}+ coarseAttributes(L, mesh);// Per cluster, its levels while they pay.std::vector<std::vector<uint32_t>> lists(nc);poolFor(nc, [&](size_t c0, size_t c1) {@@ -541,6 +542,7 @@ std::vector<uint32_t> updateCoarseLevels(CoarseLevels& L, const Mesh& mesh, cons}}, 1024);changed = cells;+ coarseAttributes(L, mesh, cells);for (size_t level = 1; level < L.levels.size() && !cells.empty(); ++level) {std::vector<uint32_t> up;up.reserve(cells.size());@@ -569,6 +571,70 @@ std::vector<uint32_t> updateCoarseLevels(CoarseLevels& L, const Mesh& mesh, consreturn changed;}+bool coarseAttributesMatch(const CoarseLevels& L, const Mesh& mesh) {+ const size_t nv = mesh.vertexCount();+ return (nv > 0 && mesh.colors.size() == nv) == !L.colors.empty() && (nv > 0 && mesh.mask.size() == nv) == !L.mask.empty();+}++void coarseAttributes(CoarseLevels& L, const Mesh& mesh, std::vector<uint32_t> cells) {+ const size_t nv = mesh.vertexCount(), count = L.positions.size();+ if (L.levels.empty() || L.vertexCell.size() != nv) return;+ const bool colors = nv > 0 && mesh.colors.size() == nv, mask = nv > 0 && mesh.mask.size() == nv;+ if (!colors && !mask) {+ std::vector<glm::vec3>().swap(L.colors);+ std::vector<float>().swap(L.mask);+ return;+ }+ const uint32_t base0 = L.levels[0].base, cells0 = uint32_t(L.levels[0].keys.size());+ if (!coarseAttributesMatch(L, mesh)) cells.clear(); // (every cell's)+ if (cells.empty()) {+ cells.resize(cells0);+ for (uint32_t k = 0; k < cells0; ++k) cells[k] = base0 + k;+ }+ if (colors) L.colors.resize(count, glm::vec3(1.0f));+ else std::vector<glm::vec3>().swap(L.colors);+ if (mask) L.mask.resize(count, 0.0f);+ else std::vector<float>().swap(L.mask);+ poolFor(cells.size(), [&](size_t i0, size_t i1) {+ for (size_t i = i0; i < i1; ++i) {+ const uint32_t k = cells[i] - base0;+ glm::vec3 csum(0.0f);+ float msum = 0.0f;+ for (uint32_t m = L.memberFirst[k]; m < L.memberFirst[k + 1]; ++m) {+ if (colors) csum += mesh.colors[L.members[m]];+ if (mask) msum += mesh.mask[L.members[m]];+ }+ const float w = float(std::max(L.memberFirst[k + 1] - L.memberFirst[k], 1u));+ if (colors) L.colors[cells[i]] = csum / w;+ if (mask) L.mask[cells[i]] = msum / w;+ }+ }, 1024);+ for (size_t level = 1; level < L.levels.size() && !cells.empty(); ++level) {+ std::vector<uint32_t> up;+ up.reserve(cells.size());+ for (uint32_t k : cells)+ if (L.parent[k] != ~0u) up.push_back(L.parent[k]);+ std::sort(up.begin(), up.end());+ up.erase(std::unique(up.begin(), up.end()), up.end());+ poolFor(up.size(), [&](size_t i0, size_t i1) {+ for (size_t i = i0; i < i1; ++i) {+ const uint32_t p = up[i];+ glm::vec3 csum(0.0f);+ float msum = 0.0f;+ for (uint32_t j = L.childFirst[p]; j < L.childFirst[p + 1]; ++j) {+ const uint32_t ch = L.children[j];+ if (colors) csum += L.colors[ch] * float(L.weight[ch]);+ if (mask) msum += L.mask[ch] * float(L.weight[ch]);+ }+ const float w = float(std::max(L.weight[p], 1u));+ if (colors) L.colors[p] = csum / w;+ if (mask) L.mask[p] = msum / w;+ }+ }, 1024);+ cells = std::move(up);+ }+}+bool closedOriented(const Mesh& mesh) {const size_t nv = mesh.vertexCount(), nt = mesh.triangleCount();if (nt == 0) return false;diff --git a/src/render/Clusters.h b/src/render/Clusters.hindex 3ecc7ffe..2e6f86d6 100644--- a/src/render/Clusters.h+++ b/src/render/Clusters.h@@ -95,6 +95,10 @@ struct CoarseLevels {float cell = 0.0f; // level 0'sstd::vector<Level> levels;std::vector<glm::vec3> positions, normals; // every level's cell points, one level after the other+ // Per cell the mean of its vertices' colours (linear) and mask, as the full mesh shows them; empty when the mesh+ // has none (colours: vertex paint and the paint layers' composite).+ std::vector<glm::vec3> colors;+ std::vector<float> mask;std::vector<uint32_t> indices; // into positions (the renderer frees them once on the GPU)std::vector<Lod> clusters;// Membership is the build's: an edit moves the cells' points (the means of their vertices where they are now),@@ -112,6 +116,11 @@ CoarseLevels buildCoarseLevels(const Mesh& mesh, const UploadOrder& order);// vertices where they are now, and of their cells up the levels. Returns the cells changed.std::vector<uint32_t> updateCoarseLevels(CoarseLevels& levels, const Mesh& mesh, const UploadOrder& order,const std::vector<uint32_t>& dirty);+// The cells' colours and mask from the mesh's: of the level-0 cells `cells0` (cell ids into positions; empty: all)+// and their cells up the levels. Where the mesh gained or lost colours or a mask since, every cell's.+void coarseAttributes(CoarseLevels& levels, const Mesh& mesh, std::vector<uint32_t> cells0 = {});+// Whether the cells' colours and mask are there exactly where the mesh has them.+bool coarseAttributesMatch(const CoarseLevels& levels, const Mesh& mesh);// Every directed edge a->b has as many b->a: the mesh is the closed, consistently wound boundary of a volume// (it may intersect itself). Wound outwards (a positive signed volume: the sum of ClusterBounds::volume), everydiff --git a/src/render/Renderer.cpp b/src/render/Renderer.cppindex 79880d27..d2e01566 100644--- a/src/render/Renderer.cpp+++ b/src/render/Renderer.cpp@@ -1187,6 +1187,8 @@ void Renderer::freeSlot(MeshSlot& s) {if (s.coarseVao) {gl::DeleteBuffers(1, &s.coarseVertices);gl::DeleteBuffers(1, &s.coarseIndices);+ gl::DeleteBuffers(1, &s.coarseColors);+ gl::DeleteBuffers(1, &s.coarseMask);gl::DeleteVertexArrays(1, &s.coarseVao);}s.micro.reset();@@ -2319,6 +2321,22 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, booif (syncLog) lapMs(lap);if ((proxyUsed_ || (proxyPrebuild_ && s.proxyAllocated)) && !s.outOfMemory && s.drawnTriangles >= kProxyMinTriangles)buildProxies(s, mesh);+ // Colour and mask edits move no point, so they mark the coarse levels here (as moves do in updateBounds): the+ // edited clusters draw in full until the edits stop, then their cells take the new values. Colours or a mask+ // appearing or going: every cluster.+ if (s.coarse && !full) {+ const bool attributes = (nv > 0 && mesh.colors.size() == nv) || (nv > 0 && mesh.mask.size() == nv) ||+ !s.coarse->colors.empty() || !s.coarse->mask.empty();+ if (!coarseAttributesMatch(*s.coarse, mesh)) {+ if (s.coarsePending.size() < s.coarseDirty.size()) // (once: then the next quiet sync updates them)+ for (uint32_t c = 0; c < s.coarseDirty.size(); ++c) markCoarseDirty(s, c);+ } else if (attributes && ranged) {+ for (const auto& [b, e] : dirtyRuns_) {+ if (b >= e) continue;+ for (uint32_t c = s.order.clusterOfSlot(b), c1 = s.order.clusterOfSlot(e - 1); c <= c1; ++c) markCoarseDirty(s, c);+ }+ }+ }if (coarseLod_ && !s.coarseTried && !s.outOfMemory && s.drawnTriangles > (lodBudget_ ? lodBudget_ : triangleBudget(RenderSettings())))buildCoarse(s, mesh);if (s.coarse && !s.coarsePending.empty() && !s.coarseEdited && !stroking_) updateCoarse(s, mesh); // (the stroke is over)@@ -3264,12 +3282,15 @@ void Renderer::buildCoarse(MeshSlot& s, const Mesh& mesh) {gl::GenVertexArrays(1, &s.coarseVao);gl::GenBuffers(1, &s.coarseVertices);gl::GenBuffers(1, &s.coarseIndices);+ gl::GenBuffers(1, &s.coarseColors);+ gl::GenBuffers(1, &s.coarseMask);}s.coarse = std::move(levels);s.coarseDirty.assign(s.coarse->clusters.size(), 0);s.coarsePending.clear();gl::BindVertexArray(s.coarseVao);uploadCoarsePoints(s);+ uploadCoarseAttributes(s);gl::EnableVertexAttribArray(0);gl::VertexAttribIPointer(0, 2, GL_UNSIGNED_INT, sizeof(PackedVertex), nullptr);gl::EnableVertexAttribArray(1);@@ -3302,6 +3323,34 @@ void Renderer::uploadCoarsePoints(MeshSlot& s) {gl::BufferData(GL_ARRAY_BUFFER, GLsizeiptr(packed.size() * sizeof(PackedVertex)), packed.data(), GL_STATIC_DRAW);}+// The cells' colours and mask as the full mesh's go up (whole: a few MB), or the arrays off where it has none.+void Renderer::uploadCoarseAttributes(MeshSlot& s) {+ const CoarseLevels& L = *s.coarse;+ if (!L.colors.empty()) {+ std::vector<uint32_t> packed(L.colors.size());+ poolFor(packed.size(), [&](size_t i0, size_t i1) {+ for (size_t i = i0; i < i1; ++i) packed[i] = packColor(L.colors[i]);+ });+ gl::BindBuffer(GL_ARRAY_BUFFER, s.coarseColors);+ gl::BufferData(GL_ARRAY_BUFFER, GLsizeiptr(packed.size() * sizeof(uint32_t)), packed.data(), GL_STATIC_DRAW);+ gl::VertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, 4, nullptr);+ gl::EnableVertexAttribArray(2);+ } else {+ gl::DisableVertexAttribArray(2);+ }+ if (!L.mask.empty()) {+ std::vector<uint8_t> packed(L.mask.size());+ for (size_t i = 0; i < packed.size(); ++i) packed[i] = packMask(L.mask[i]);+ gl::BindBuffer(GL_ARRAY_BUFFER, s.coarseMask);+ gl::BufferData(GL_ARRAY_BUFFER, GLsizeiptr(packed.size()), packed.data(), GL_STATIC_DRAW);+ gl::VertexAttribPointer(3, 1, GL_UNSIGNED_BYTE, GL_TRUE, 1, nullptr);+ gl::EnableVertexAttribArray(3);+ } else {+ gl::DisableVertexAttribArray(3);+ }+ gl::BindBuffer(GL_ARRAY_BUFFER, s.coarseVertices); // (the points' attributes are set up against it)+}+void Renderer::noteGpuFrame(double gpuMs, uint64_t drawnTriangles) {if (!(gpuMs > 0.2) || drawnTriangles < 500000) return; // (small frames are mostly fixed costs)const double v = gpuMs / double(drawnTriangles);@@ -3329,7 +3378,7 @@ void Renderer::updateCoarse(MeshSlot& s, const Mesh& mesh) {const auto start = std::chrono::steady_clock::now();const std::vector<uint32_t> changed = updateCoarseLevels(*s.coarse, mesh, s.order, s.coarsePending);gl::BindVertexArray(s.coarseVao);- if (!changed.empty()) uploadCoarsePoints(s);+ if (!changed.empty()) uploadCoarsePoints(s), uploadCoarseAttributes(s);gl::BindVertexArray(0);gl::BindBuffer(GL_ARRAY_BUFFER, 0);for (uint32_t c : s.coarsePending) s.coarseDirty[c] = 0;@@ -3547,8 +3596,8 @@ void Renderer::drawSlot(const MeshSlot& s, bool active, bool flat, const RenderS}if (coarse && !coarseClusters_.empty()) { // the spike's coarse levels: one multi-draw, 32-bit indicesgl::BindVertexArray(s.coarseVao);- gl::VertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);- gl::VertexAttrib1f(3, 0.0f);+ if (s.coarse->colors.empty()) gl::VertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);+ if (s.coarse->mask.empty()) gl::VertexAttrib1f(3, 0.0f);gl::Uniform1i(p->twoSided, 0);drawCounts_.clear();drawOffsets_.clear();diff --git a/src/render/Renderer.h b/src/render/Renderer.hindex 69e709a7..6667440c 100644--- a/src/render/Renderer.h+++ b/src/render/Renderer.h@@ -374,7 +374,7 @@ private:bool proxyAllocated = false;// Coarse levels (buildCoarseLevels at the first sync over the budget, again after a new layout). Clusters an// edit moved draw in full (coarseDirty) until the first sync without new edits updates their levels.- GLuint coarseVao = 0, coarseVertices = 0, coarseIndices = 0;+ GLuint coarseVao = 0, coarseVertices = 0, coarseIndices = 0, coarseColors = 0, coarseMask = 0; // (colours RGBA8, mask R8)std::unique_ptr<CoarseLevels> coarse;std::vector<uint8_t> coarseDirty;std::vector<uint32_t> coarsePending; // the dirty clusters, to update@@ -441,6 +441,7 @@ private:size_t lodBudget_ = 0; // the last still draw's triangle budget: sync builds the coarse levels of meshes over itvoid buildCoarse(MeshSlot& s, const Mesh& mesh);void uploadCoarsePoints(MeshSlot& s);+ void uploadCoarseAttributes(MeshSlot& s); // the cells' colours and mask (with the coarse VAO bound)size_t triangleBudget(const RenderSettings& settings) const;// The ghost (setGhost), drawn on the slot ghostId_.GhostStroke ghost_;diff --git a/tests/test_render_clusters.cpp b/tests/test_render_clusters.cppindex b7b16e60..47412f52 100644--- a/tests/test_render_clusters.cpp+++ b/tests/test_render_clusters.cpp@@ -464,3 +464,52 @@ TEST(coarse_levels_follow_edits) {updateCoarseLevels(L, m, order, dirty);CHECK(L.positions == after);}++TEST(coarse_levels_carry_colour_and_mask) {+ // The cells carry their vertices' mean colour and mask (a coarse frame shows the paint as full detail does):+ // built with the colours, gained after a Fill, updated after a stroke over some clusters, dropped with them.+ Mesh m = detailedSphere(100000);+ UploadOrder order;+ order.build(m);+ const size_t nv = m.vertexCount();+ auto colourMeans = [&](const CoarseLevels& L) {+ if (L.colors.size() != L.positions.size() || L.mask.size() != L.positions.size()) return false;+ std::vector<glm::dvec3> sum(L.positions.size(), glm::dvec3(0.0));+ std::vector<double> msum(L.positions.size(), 0.0);+ std::vector<uint32_t> n(L.positions.size(), 0);+ for (size_t v = 0; v < nv; ++v)+ for (uint32_t k = L.vertexCell[v]; k != ~0u; k = L.parent[k]) sum[k] += glm::dvec3(m.colors[v]), msum[k] += m.mask[v], ++n[k];+ float worst = 0.0f;+ for (size_t k = 0; k < sum.size(); ++k)+ if (n[k])+ worst = std::max({worst, glm::distance(L.colors[k], glm::vec3(sum[k] / double(n[k]))),+ std::abs(L.mask[k] - float(msum[k] / double(n[k])))});+ return worst < 1e-4f;+ };+ CoarseLevels L = buildCoarseLevels(m, order);+ CHECK(L.colors.empty() && L.mask.empty() && coarseAttributesMatch(L, m));+ // A Fill (colours and a mask appear): no longer matching; any update recomputes every cell's.+ m.colors.assign(nv, glm::vec3(0.1f, 0.6f, 0.2f));+ m.mask.assign(nv, 0.0f);+ for (size_t v = 0; v < nv; ++v) m.mask[v] = m.positions[v].y > 0.0f ? 1.0f : 0.0f;+ CHECK(!coarseAttributesMatch(L, m));+ updateCoarseLevels(L, m, order, {0u});+ CHECK(coarseAttributesMatch(L, m));+ CHECK(colourMeans(L));+ // A red stroke over a few clusters: their cells (and up) follow.+ std::vector<uint32_t> dirty;+ for (uint32_t c = 0; c < order.clusters().size(); c += 7) {+ dirty.push_back(c);+ const auto& cl = order.clusters()[c];+ for (uint32_t l = 0; l < cl.vertexCount; ++l) m.colors[order.fromGpu()[cl.firstVertex + l]] = glm::vec3(0.9f, 0.05f, 0.05f);+ }+ updateCoarseLevels(L, m, order, dirty);+ CHECK(colourMeans(L));+ // Built with them, the same.+ CHECK(colourMeans(buildCoarseLevels(m, order)));+ // Cleared: gone.+ m.colors.clear();+ m.mask.clear();+ updateCoarseLevels(L, m, order, {0u});+ CHECK(L.colors.empty() && L.mask.empty());+}diff --git a/tests/ui/coarse_lod_colour.sfui b/tests/ui/coarse_lod_colour.sfuinew file mode 100644index 00000000..669e14ba--- /dev/null+++ b/tests/ui/coarse_lod_colour.sfui@@ -0,0 +1,74 @@+# Coarse levels keep the paint (0.11.7 regression, found on a 12M scan): an over-budget mesh drawn at its coarse+# level while the view moves shows the same vertex colour and mask as full detail, the cells' means of the fine+# mesh's. A paint stroke draws the clusters it touched in full while it runs; their cells take the new colour after.+# (Face groups and textures draw in full: per-triangle data never takes the coarse path.)+run ui_look look=classic layout=classic+# (window fractions: 0.41,0.51 is the view centre, 0.38 above the stroke; small budgets, as in coarse_lod; a still+# frame never fits, so every frame here is the coarse one)+env SF_TRIANGLE_BUDGET 40000+env SF_STILL_FRAME_MS 0.001+run new_sphere subdivisions=6+camera 0 0+wait 0.6+expect_value coarse_clusters > 0+shot plain++# Fill green: the coarse frame is green (it was the plain material: the coarse draw had no colours).+run set_color color=20c040+run paint_fill color=20c040+wait 0.6+expect_value colored == 1+expect_value coarse_pending == 0+expect_value coarse_clusters > 0+shot filled-coarse+remember [email protected],0.51 ref1+expect_value [email protected],0.51 > $ref1+40+remember [email protected],0.51 ref2+expect_value [email protected],0.51 > $ref2+40+# moving: orbit, still coarse, still green+camera 35 20+wait 0.6+expect_value coarse_clusters > 0+shot filled-orbit+remember [email protected],0.51 ref3+expect_value [email protected],0.51 > $ref3+40++# A red paint stroke across the middle: in full while it runs, then its cells are red in the coarse frame.+camera 0 0+wait 0.6+remember coarse_clusters before+run set_color color=e02020+key p+stroke 0.3 0.5 0.7 0.5 0.6 hold+wait 0.3+expect_value coarse_pending > 0+release+wait 0.6+expect_value coarse_pending == 0+expect_value coarse_clusters == $before+shot painted-coarse+remember [email protected],0.51 ref4+expect_value [email protected],0.51 > $ref4+40+# the green away from the stroke stays+remember [email protected],0.38 ref5+expect_value [email protected],0.38 > $ref5+40++# Mask: the masked area darkens in the coarse frame as in full detail.+remember [email protected],0.38 unmasked+run mask_rect x0=-1 y0=-1 x1=1 y1=1+wait 0.6+expect_value coarse_pending == 0+expect_value coarse_clusters > 0+shot masked-coarse+expect_value [email protected],0.38 < $unmasked-15++# At rest in full detail: the same colours (the reference).+run mask_clear+env SF_STILL_FRAME_MS 100000+wait 1.2+expect_value coarse_clusters == 0+shot full-detail+remember [email protected],0.51 ref6+expect_value [email protected],0.51 > $ref6+40+remember [email protected],0.38 ref7+expect_value [email protected],0.38 > $ref7+40