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
Voxel Remesh keeps a texture in the background with Keep Texture; Auto UV no longer folds or crashes on dense meshes and caps chart size
islandmed9dfd036ea38b
diff --git a/src/app/App.h b/src/app/App.hindex 44299527..a22da0c1 100644--- a/src/app/App.h+++ b/src/app/App.h@@ -430,7 +430,22 @@ private:void drawQRemeshContents();void drawProjectContents();void startQRemesh();- void drawKeepTextureCheckbox(const char* path); // Keep Texture under Remesh and QRemesh (PanelTexture.cpp)+ void drawKeepTextureCheckbox(const char* id, const char* path); // Keep Texture under Remesh and QRemesh (PanelTexture.cpp)+ // Voxel Remesh of a painted SubTool with Keep Texture: the remesh, Auto UV and texture transfer run on a+ // worker thread (PanelRetopo.cpp); otherwise Voxel Remesh is the plain remesh command.+ void startVoxelRemesh();+ void pollVoxelRemesh();+ struct VoxelRemeshJob {+ std::future<bool> done;+ std::shared_ptr<std::atomic<float>> progress;+ std::shared_ptr<std::atomic<bool>> cancel;+ std::shared_ptr<Mesh> result;+ std::shared_ptr<std::string> error, note;+ uint32_t subtool = 0;+ size_t triangles = 0;+ int resolution = 200;+ };+ std::unique_ptr<VoxelRemeshJob> voxelRemesh_;void pollQRemesh();struct QRemeshJob {std::future<bool> done;diff --git a/src/app/Palettes.cpp b/src/app/Palettes.cppindex 60376254..a29c5341 100644--- a/src/app/Palettes.cpp+++ b/src/app/Palettes.cpp@@ -383,14 +383,27 @@ void App::drawPalette_Tool() {}if (subpalette("tool.remesh", "Remesh")) {const float half = (ImGui::GetContentRegionAvail().x - ImGui::GetStyle().ItemSpacing.x) * 0.5f;- ui::ActionButton(*this, "geometry.remesh", ImVec2(half, 0));- ui::HelpAction(*this, "geometry.remesh");+ if (voxelRemesh_) { // remeshing a painted SubTool in the background (PanelRetopo.cpp)+ ImGui::ProgressBar(voxelRemesh_->progress->load(), ImVec2(half, 0), "Remeshing...");+ ui::HelpText("Voxel Remesh", nullptr,+ "Remeshing and keeping the texture in the background (Auto UV of the new surface, then the "+ "texture projected onto it). The result replaces the SubTool when it's done.",+ "Tool > Remesh");+ } else {+ ui::ActionButton(*this, "geometry.remesh", ImVec2(half, 0));+ ui::HelpAction(*this, "geometry.remesh");+ }ImGui::SameLine();ImGui::SetNextItemWidth(-1);ImGui::SliderInt("##voxres", &voxelResolution_, 64, 400, "Resolution %d");ui::HelpText("Resolution", nullptr, "Voxels across the model: higher keeps more detail and makes more triangles.","Tool > Remesh");- if (hasTexture(doc_.mesh)) drawKeepTextureCheckbox("Tool > Remesh"); // only for a painted SubTool+ if (voxelRemesh_) {+ if (ImGui::Button("Cancel##voxelremesh", ImVec2(half, 0))) voxelRemesh_->cancel->store(true);+ ui::HelpText("Cancel", nullptr, "Stop the remesh; the SubTool stays as it is.", "Tool > Remesh");+ } else if (hasTexture(doc_.mesh)) {+ drawKeepTextureCheckbox("Keep Texture##remesh", "Tool > Remesh"); // only for a painted SubTool+ }}if (subpalette("tool.masking", "Masking")) drawMaskingSection();if (subpalette("tool.polygroups", "Polygroups")) drawPolygroupsSection();diff --git a/src/app/PanelRetopo.cpp b/src/app/PanelRetopo.cppindex 7459f98b..64c3c3dc 100644--- a/src/app/PanelRetopo.cpp+++ b/src/app/PanelRetopo.cpp@@ -10,6 +10,7 @@#include "app/Ui.h"#include "core/TextureTransfer.h"+#include "core/Voxel.h"#include <algorithm>#include <thread>@@ -119,7 +120,81 @@ void App::startQRemesh() {" quads...";}+void App::startVoxelRemesh() {+ if (voxelRemesh_) return;+ Args args{{"resolution", double(voxelResolution_)}};+ if (!hasTexture(doc_.mesh) || !keepTexture_) {+ if (hasTexture(doc_.mesh)) args["keep_texture"] = false; // Keep Texture off+ runCommand("remesh", args);+ return;+ }+ // Keeping a texture adds Auto UV of the result, which takes seconds to minutes: run it all in the background.+ if (doc_.sculptor.strokeActive()) endStroke();+ auto job = std::make_unique<VoxelRemeshJob>();+ job->progress = std::make_shared<std::atomic<float>>(0.0f);+ job->cancel = std::make_shared<std::atomic<bool>>(false);+ job->result = std::make_shared<Mesh>();+ job->error = std::make_shared<std::string>();+ job->note = std::make_shared<std::string>();+ job->subtool = doc_.activeSubtoolInfo().id;+ job->triangles = doc_.mesh.triangleCount();+ job->resolution = std::clamp(voxelResolution_, 32, 512);+ std::promise<bool> promise;+ job->done = promise.get_future();+ std::thread([promise = std::move(promise), source = std::make_shared<Mesh>(doc_.mesh), res = job->resolution,+ progress = job->progress, cancel = job->cancel, result = job->result, error = job->error,+ note = job->note]() mutable {+ progress->store(0.02f);+ *result = voxelRemesh(*source, res, error.get());+ bool ok = result->triangleCount() > 0;+ progress->store(0.15f);+ if (ok) *note = carryTextureNote(*source, *result, true, [&](float f) {+ progress->store(0.15f + 0.85f * f);+ return !cancel->load();+ });+ if (ok && cancel->load()) {+ ok = false;+ *error = "Voxel Remesh cancelled";+ }+ promise.set_value(ok);+ }).detach();+ voxelRemesh_ = std::move(job);+ status_ = "Voxel Remesh: remeshing and keeping the texture...";+}++void App::pollVoxelRemesh() {+ if (!voxelRemesh_) return;+ if (voxelRemesh_->done.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {+ status_ = "Voxel Remesh (keeping the texture): " + std::to_string(int(voxelRemesh_->progress->load() * 100.0f)) + "%";+ return;+ }+ const std::unique_ptr<VoxelRemeshJob> job = std::move(voxelRemesh_);+ if (!job->done.get()) {+ status_ = *job->error;+ return;+ }+ int index = -1;+ for (int i = 0; i < doc_.subtoolCount(); ++i)+ if (doc_.subtools[size_t(i)].id == job->subtool) index = i;+ if (index < 0 || doc_.subtoolMesh(index).triangleCount() != job->triangles) {+ status_ = "Voxel Remesh skipped: the SubTool changed while it was being remeshed";+ return;+ }+ if (index != doc_.activeSubtool) doc_.selectSubtool(index);+ if (doc_.sculptor.strokeActive()) endStroke();+ doc_.replaceMeshUndoable(std::move(*job->result), "Remesh"); // as the remesh command+ const CommandResult r{true, "Remeshed to " + std::to_string(doc_.mesh.triangleCount()) + " triangles" + *job->note};+ status_ = r.message;+ lastCommandOk_ = true;+ clearLods();+ playingLayer_ = -1;+ uvLinesKey_ = 0;+ updateTitle();+ consoleEcho("remesh", {{"resolution", double(job->resolution)}}, r);+}+void App::pollQRemesh() {+ pollVoxelRemesh(); // polled with QRemesh, its sibling rebuild jobif (!qremesh_) return;if (qremesh_->done.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {status_ = "QRemesh: " + std::to_string(int(qremesh_->progress->load() * 100.0f)) + "%";@@ -222,7 +297,7 @@ void App::drawQRemeshContents() {kQRemeshPath);if (hasTexture(doc_.mesh)) { // only matters for a painted SubToolImGui::SameLine(half + style.ItemSpacing.x);- drawKeepTextureCheckbox(kQRemeshPath);+ drawKeepTextureCheckbox("Keep Texture##qremesh", kQRemeshPath);}}diff --git a/src/app/PanelTexture.cpp b/src/app/PanelTexture.cppindex ed3d6a19..fef3e539 100644--- a/src/app/PanelTexture.cpp+++ b/src/app/PanelTexture.cpp@@ -156,8 +156,8 @@ void App::registerTextureUiActions() {for (int i = 0; i < 2; ++i) g_dialogs[i] = {this, TextureFile(i)};}-void App::drawKeepTextureCheckbox(const char* path) {- ImGui::Checkbox("Keep Texture", &keepTexture_);+void App::drawKeepTextureCheckbox(const char* id, const char* path) {+ ImGui::Checkbox(id, &keepTexture_);ui::HelpText("Keep Texture", nullptr,"Keep the painted texture: the new mesh gets Auto UVs and the texture (every layer) is projected onto ""it from the old surface, in the same undo step (it adds the Auto UV time). Off: the texture is "diff --git a/src/app/Shell.cpp b/src/app/Shell.cppindex ba749dc7..eccad188 100644--- a/src/app/Shell.cpp+++ b/src/app/Shell.cpp@@ -396,12 +396,7 @@ void App::registerShellActions() {add({"geometry.remesh", "Voxel Remesh", nullptr,"Rebuild as an even, watertight surface (like DynaMesh): merges parts, fixes scans and generated models.","Tool > Remesh", "", "remesh",- [this] {- Args a{{"resolution", double(voxelResolution_)}};- if (!keepTexture_ && hasTexture(doc_.mesh)) a["keep_texture"] = false; // Keep Texture off- runCommand("remesh", a);- },- nullptr, nullptr});+ [this] { startVoxelRemesh(); }, nullptr, [this] { return !voxelRemesh_; }});add({"geometry.to_splats", "Sculpt to Splats", nullptr, "Turn the sculpt into Gaussian splats.", "Splats", "","mesh_to_splats", [this] { runCommand("mesh_to_splats"); }, nullptr, nullptr});add({"color.fill", "Fill", ICON_LC_PAINT_BUCKET, "Fill the unmasked area with the colour (FillObject).", "Color", "",diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cppindex 068fed08..f2270ece 100644--- a/src/app/UiTest.cpp+++ b/src/app/UiTest.cpp@@ -766,7 +766,7 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& econst bool idle = !busy(app_.pendingBrush_) && !busy(app_.pendingGenerator_) && !busy(app_.pendingModels_) &&!busy(app_.pendingUnreal_) && !busy(app_.pendingComfyCheck_) && !app_.galleryBusy() &&!app_.thumbs_.busy() && !app_.libraryScanner_.busy() && !app_.autoUv_ &&- !app_.bakeBusy() && !app_.qremesh_;+ !app_.bakeBusy() && !app_.qremesh_ && !app_.voxelRemesh_;if (idle) return true;SDL_Delay(10); // real work runs in real timeif (t > timeout) fail(line, "still busy after " + number(timeout) + " s");diff --git a/src/core/AutoUv.cpp b/src/core/AutoUv.cppindex c2dcf83e..04374f39 100644--- a/src/core/AutoUv.cpp+++ b/src/core/AutoUv.cpp@@ -66,6 +66,16 @@ bool computeAutoUv(const std::vector<glm::vec3>& positions, const std::vector<glreturn false;}xatlas::ChartOptions charts;+ // Charts of at most about kChartFaces triangles (by area): flattening a chart costs more than linearly in its+ // size, so a dense, smooth sculpt cut into a few huge charts took minutes (a 1.3M-triangle sphere: about+ // 8 minutes; 16 s with the cap). Detailed sculpts get smaller charts than that anyway.+ constexpr double kChartFaces = 4000.0;+ if (double(triangles.size()) > kChartFaces) {+ double area = 0.0;+ for (const glm::uvec3& t : triangles)+ area += 0.5 * double(glm::length(glm::cross(positions[t.y] - positions[t.x], positions[t.z] - positions[t.x])));+ charts.maxChartArea = float(area * kChartFaces / double(triangles.size()));+ }xatlas::PackOptions pack;pack.resolution = uint32_t(std::clamp(resolution, 256, 8192));pack.padding = uint32_t(kTextureGutter); // the painter pads islands by this muchdiff --git a/src/core/TextureTransfer.cpp b/src/core/TextureTransfer.cppindex 49d58f32..8c5e2815 100644--- a/src/core/TextureTransfer.cpp+++ b/src/core/TextureTransfer.cpp@@ -233,12 +233,12 @@ std::string setUvsKeepingTexture(Mesh& mesh, std::vector<glm::vec2> uvs, bool keconst size_t layers = moved->layers.size();mesh.texture = std::move(moved);char buf[160];- std::snprintf(buf, sizeof buf, " (the texture%s moved onto the new UVs in %.1f s)",- layers ? (" and its " + std::to_string(layers) + " layer" + (layers > 1 ? "s" : "")).c_str() : "",+ std::snprintf(buf, sizeof buf, "; paint moved to the new UVs (%s%.1f s)",+ layers ? (std::to_string(layers) + " layer" + (layers > 1 ? "s, " : ", ")).c_str() : "",st.transferSeconds);note = buf;} else {- note = " (the texture was painted on the old UVs, so it no longer lines up; undo to get them back)";+ note = "; the texture was left on the old layout, so it no longer lines up (undo to get the old UVs back)";}}mesh.uvs = std::move(uvs);diff --git a/src/core/TextureTransfer.h b/src/core/TextureTransfer.hindex 4ba667d0..1fbee199 100644--- a/src/core/TextureTransfer.h+++ b/src/core/TextureTransfer.h@@ -41,7 +41,7 @@ std::shared_ptr<PaintTexture> retargetTexture(const Mesh& mesh, const std::vectoTextureTransferStats* stats = nullptr);// Gives `mesh` new UVs (Auto UV), moving its texture and layers onto them (retargetTexture) unless `keep` is-// false. Returns the status text to add (" (texture moved onto the new UVs)"), empty without a texture.+// false. Returns the status text to add (" (texture moved to the new UVs)"), empty without a texture.std::string setUvsKeepingTexture(Mesh& mesh, std::vector<glm::vec2> uvs, bool keep = true);// Gives `target` (which must have UVs) a texture of `source`'s size whose texels take the colour of the closestdiff --git a/tests/test_texture_transfer.cpp b/tests/test_texture_transfer.cppindex df6133d2..26b86912 100644--- a/tests/test_texture_transfer.cpp+++ b/tests/test_texture_transfer.cpp@@ -124,7 +124,7 @@ TEST(texture_transfer_auto_uv_keeps_the_paint) {CommandRegistry reg;const CommandResult r = reg.execute(doc, "uv_auto", {{"resolution", 2048.0}}); // packed differentlyCHECK(r.ok);- CHECK(r.message.find("moved onto the new UVs") != std::string::npos);+ CHECK(r.message.find("moved to the new UVs") != std::string::npos);CHECK(doc.mesh.uvs != oldUvs);CHECK(hasTexture(doc.mesh));CHECK(doc.mesh.texture != oldTexture);@@ -296,6 +296,28 @@ TEST(texture_transfer_pads_the_new_islands) {CHECK(white * 100 < border); // the pattern never reaches pure white}+TEST(texture_transfer_auto_uv_of_a_dense_sphere_does_not_fold) {+ // xatlas chained the near-coplanar faces of a dense smooth mesh into one "planar" chart, folded in two (every+ // texel covered twice), or crashed on it. Now: separate islands, no texel covered twice.+ Mesh m = makeIcosphere(6); // 82k triangles+ AutoUvResult r;+ std::string err;+ CHECK(computeAutoUv(m, r, err));+ CHECK(r.charts > 4);+ std::vector<uint8_t> hits(1024 * 1024, 0);+ size_t twice = 0, covered = 0;+ for (size_t t = 0; t < m.triangleCount(); ++t)+ rasterizeUvTriangle(r.uvs[t * 3], r.uvs[t * 3 + 1], r.uvs[t * 3 + 2], 1024, 1024, [&](int x, int y, float, float, float) {+ uint8_t& h = hits[size_t(y) * 1024 + size_t(x)];+ covered += h == 0;+ twice += h == 1;+ h = uint8_t(std::min(h + 1, 2));+ });+ std::printf(" dense sphere Auto UV: %d islands, %zu texels covered, %zu twice\n", r.charts, covered, twice);+ CHECK(covered > 1024 * 1024 / 3);+ CHECK(twice < covered / 1000);+}+TEST(texture_transfer_speed_at_4k_on_a_million_triangles) {using Clock = std::chrono::steady_clock;auto ms = [](Clock::time_point a, Clock::time_point b) { return std::chrono::duration<double, std::milli>(b - a).count(); };diff --git a/tests/ui/texture_transfer.sfui b/tests/ui/texture_transfer.sfuinew file mode 100644index 00000000..c6b081d0--- /dev/null+++ b/tests/ui/texture_transfer.sfui@@ -0,0 +1,77 @@+# Keeping a painted texture when the UVs or the surface are rebuilt (core/TextureTransfer.h): Auto UV moves the+# paint and its layers onto the new UVs, and Voxel Remesh / QRemesh with Keep Texture give the new surface+# Auto UVs and project the texture onto it, each as one undo step.+run new_sphere subdivisions=4+run uv_auto resolution=256+run texture_create size=1k+run set_color color=c81e1e+key p+stroke 0.15 0.45 0.85 0.45+run set_color color=1f4fd0+stroke 0.5 0.1 0.5 0.9+click "Paint Layers##tool.paint_layers"+click "Paint Layers/New"+run set_color color=f0d020+stroke 0.2 0.75 0.8 0.75+expect_value texture_layers == 1+remember texture_painted painted+shot painted++# Auto UV again: the islands are packed for the 1K texture now (a new layout); the paint stays put.+click "Texture Map##tool.texture"+click "Texture Map/Auto UV"+wait_idle 120+expect_status "moved to the new UVs"+expect_status "1 layer"+expect_value texture_layers == 1+expect_value texture_size == 1024+shot after-auto-uv+click "Texture Map/Show UVs"+shot after-auto-uv-layout+click "Texture Map/Show UVs"++# One undo step brings back the old UVs and the old texture exactly.+key ctrl+z+expect_value texture_painted == $painted+key ctrl+shift+z+expect_value texture_layers == 1++# Voxel Remesh with Keep Texture (shown because the SubTool has a texture), in the background.+click "Remesh##tool.remesh"+expect "Keep Texture##remesh"+hold ctrl+hover "Keep Texture##remesh"+expect_help "projected"+hold none+remember triangles before_remesh+click "Voxel Remesh##geometry.remesh"+wait_idle 300+expect_status "texture kept"+expect_value triangles != $before_remesh+expect_value texture_size == 1024+expect_value texture_layers == 1+expect_value paint_texture == 1+shot remeshed+key ctrl+z+expect_value triangles == $before_remesh++# QRemesh keeps it too; with Keep Texture off the texture is dropped and the status says so.+click "Geometry##tool.geometry"+expect "QRemesh/Keep Texture##qremesh"+click "QRemesh/QRemesh"+wait_idle 300+expect_status "texture kept"+expect_value quad_percent == 100+expect_value texture_size == 1024+click "PolyF##view.polyf"+shot qremeshed+click "PolyF##view.polyf"+key ctrl+z+expect_value triangles == $before_remesh+click "QRemesh/Keep Texture##qremesh"+click "QRemesh/QRemesh"+wait_idle 300+expect_status "texture was dropped"+expect_value texture_size == 0+key ctrl+z+expect_value texture_size == 1024diff --git a/third_party/xatlas/SOURCE.txt b/third_party/xatlas/SOURCE.txtindex eb84c384..35117e02 100644--- a/third_party/xatlas/SOURCE.txt+++ b/third_party/xatlas/SOURCE.txt@@ -1 +1,4 @@xatlas.h, xatlas.cpp, LICENSE from https://github.com/jpcy/xatlas commit f700c7790aaa030e794b52ba7791a05c085faf0c (source/xatlas; MIT)+Local changes (marked "SculptForge:" in xatlas.cpp): planar regions must also be coplanar with their first face+(neighbour-only tests chained whole dense, smooth meshes into one folded "planar" chart), and a chart without+boundary vertices no longer crashes BoundingBox2D::compute.diff --git a/third_party/xatlas/xatlas.cpp b/third_party/xatlas/xatlas.cppindex 5c5c57ec..416a47f1 100644--- a/third_party/xatlas/xatlas.cpp+++ b/third_party/xatlas/xatlas.cpp@@ -2241,7 +2241,18 @@ public:// If vertices are empty, the boundary vertices are used.void compute(ConstArrayView<Vector2> vertices = ConstArrayView<Vector2>()){- XA_DEBUG_ASSERT(!m_boundaryVertices.isEmpty());+ // SculptForge: a chart without boundary vertices (it happens on dense spheres) used to crash in+ // convexHull; take the hull of all of its vertices instead.+ if (m_boundaryVertices.isEmpty()) {+ for (uint32_t v = 0; v < vertices.length; v++)+ m_boundaryVertices.push_back(vertices[v]);+ if (m_boundaryVertices.isEmpty()) {+ majorAxis = Vector2(1.0f, 0.0f);+ minorAxis = Vector2(0.0f, 1.0f);+ minCorner = maxCorner = Vector2(0.0f);+ return;+ }+ }if (vertices.length == 0)vertices = m_boundaryVertices;convexHull(m_boundaryVertices, m_hull, 0.00001f);@@ -5205,6 +5216,11 @@ struct PlanarChartscontinue; // Already in a chart.if (!equal(dot(m_data.faceNormals[face], m_data.faceNormals[oface]), 1.0f, kEpsilon))continue; // Not coplanar.+ // SculptForge: also coplanar with the region's first face. Comparing neighbours only chains+ // faces across any dense, smoothly curved surface (a subdivided sphere, a sculpt), which+ // then became one "planar" chart projected flat onto itself (folded, overlapping UVs).+ if (!equal(dot(m_data.faceNormals[f], m_data.faceNormals[oface]), 1.0f, kEpsilon))+ continue;const uint32_t next = m_nextRegionFace[face];m_nextRegionFace[face] = oface;m_nextRegionFace[oface] = next;