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
Tool > Bake sub-palette, background bake with progress, normal map preview in the viewport, UI test
islandmed4fd820778adc
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 5b5d95b2..9ef3d352 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -164,6 +164,7 @@ if(SCULPTFORGE_BUILD_APP)src/app/PanelPolypaint.cppsrc/app/PanelPaintLayers.cppsrc/app/PanelTexture.cpp+ src/app/PanelBake.cppsrc/app/PanelGoz.cppsrc/app/PanelProject.cppsrc/app/PanelSubTools.cppdiff --git a/src/app/App.cpp b/src/app/App.cppindex 598b35e1..7d44bbbd 100644--- a/src/app/App.cpp+++ b/src/app/App.cpp@@ -340,6 +340,7 @@ int App::runUiTest(int w, int h, int fps) {pollUnreal();pollGenerator();pollAutoUv();+ pollBake();updateCameraAspect();Demo::beginFrame();@@ -492,6 +493,7 @@ int App::runRecording() {pollBrushRequest();pollUnreal();pollAutoUv();+ pollBake();updateCameraAspect();Demo::beginFrame();@@ -556,6 +558,7 @@ int App::run() {pollUnreal();pollGoz();pollAutoUv();+ pollBake();int fbW = 0, fbH = 0, winW = 0, winH = 0;SDL_GetWindowSizeInPixels(window_, &fbW, &fbH);@@ -1064,6 +1067,11 @@ void App::processPendingFiles() {files.swap(pendingFiles_);}for (const auto& f : files) {+ if (f.action == FileAction::SaveObj || f.action == FileAction::ExportGlb || f.action == FileAction::ExportLods ||+ f.action == FileAction::ExportUsd || f.action == FileAction::ExportUnreal) {+ const auto folder = std::filesystem::path(std::u8string(f.path.begin(), f.path.end())).parent_path().u8string();+ lastExportFolder_.assign(folder.begin(), folder.end()); // baked maps go beside exports (PanelBake.cpp)+ }switch (f.action) {case FileAction::OpenObj: openPath(f.path); break;case FileAction::OpenImage: openPath(f.path); break;@@ -1116,7 +1124,8 @@ void App::syncScene() {for (int i = 0; i < doc_.subtoolCount(); ++i) {SubTool& s = doc_.subtools[size_t(i)];const bool active = i == doc_.activeSubtool;- scene.push_back({s.id, active ? &shown : &s.mesh, active, s.visible});+ Mesh* mesh = active ? &shown : &s.mesh;+ scene.push_back({s.id, mesh, active, s.visible, bakeNormalMap(s.id, *mesh)});}renderer_.syncScene(scene);renderer_.syncSplats(doc_.splats, doc_.splatsVersion);diff --git a/src/app/App.h b/src/app/App.hindex 0dfc4869..eec9a40d 100644--- a/src/app/App.h+++ b/src/app/App.h@@ -403,6 +403,20 @@ private:std::unique_ptr<AutoUvJob> autoUv_;int textureSizeChoice_ = 1; // 1K, 2K, 4K, 8Kbool showUvView_ = false; // Texture Map > Show UVs+ // Bake sub-palette (PanelBake.cpp): normal / displacement / AO / cavity / colour maps from a high-poly onto+ // the active SubTool's UVs (core/Bake.h), on a worker thread, and the baked normal map shown on the model.+ void drawBakeContents();+ void registerBakeUiActions();+ void startBake();+ void pollBake();+ // The normal map to preview on SubTool `id` (null when none, it is turned off, or the mesh changed).+ const NormalMapView* bakeNormalMap(uint32_t id, const Mesh& mesh);+ bool bakeBusy() const;+ // UI test values: bake_running, baked_maps (maps in the last bake), bake_size, bake_files, normal_map_preview+ // (1 when the active SubTool shows a baked normal map); NaN for other names.+ double bakeTestValue(const std::string& name);+ std::shared_ptr<struct BakeUiState> bake_;+ std::string lastExportFolder_; // where the last export went: the default folder for baked maps// Cached UV wireframe of the active SubTool for the UV view: segments in 0..1 UV space.std::vector<glm::vec2> uvLines_;uint64_t uvLinesKey_ = 0;diff --git a/src/app/Config.cpp b/src/app/Config.cppindex 9af3f796..7f1c6edb 100644--- a/src/app/Config.cpp+++ b/src/app/Config.cpp@@ -307,6 +307,7 @@ void App::initActions() {// the same id and own P (Paint), the symmetry toggles and Tab.registerShellActions();registerTextureUiActions(); // Tool > Texture Map, the Vertex / Texture paint target (PanelTexture.cpp)+ registerBakeUiActions(); // Tool > Bake (PanelBake.cpp)// Every command, so the command palette finds anything scriptable.actions_.registerCommands(diff --git a/src/app/Palettes.cpp b/src/app/Palettes.cppindex 563a96c7..95815869 100644--- a/src/app/Palettes.cpp+++ b/src/app/Palettes.cpp@@ -357,6 +357,11 @@ void App::drawPalette_Tool() {drawTextureMapContents();Demo::popScope();}+ if (subpalette("tool.bake", "Bake")) {+ Demo::pushScope("Bake");+ drawBakeContents();+ Demo::popScope();+ }if (subpalette("tool.geometry", "Geometry")) {actionRow(*this, {"geometry.subdivide"});const float half = (ImGui::GetContentRegionAvail().x - ImGui::GetStyle().ItemSpacing.x) * 0.5f;diff --git a/src/app/PanelBake.cpp b/src/app/PanelBake.cppnew file mode 100644index 00000000..1f8ed1d7--- /dev/null+++ b/src/app/PanelBake.cpp@@ -0,0 +1,491 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// Bake sub-palette (Tool palette): bakes maps from a high-poly source (one SubTool, or every visible one but+// the active) onto the active SubTool's UVs (core/Bake.h): tangent-space normal, object-space normal,+// displacement, AO, cavity and colour, at 1K-8K with ray distances, anti-aliasing and padding. The bake runs+// on a worker thread on copies of the meshes, with progress and Cancel, and writes PNGs (and a float EXR for+// displacement) into the output folder. The tangent-space normal map then shows on the low-poly in the viewport+// (the Renderer's normal map shader), so people can check it next to the high-poly. A bake run from the+// console or a script (the bake command) shows its normal map too.+#include "app/App.h"+#include "app/Demo.h"+#include "app/Ui.h"++#include "core/Bake.h"++#include <IconsLucide.h>++#include <algorithm>+#include <cctype>+#include <cmath>+#include <cstring>+#include <filesystem>+#include <limits>+#include <thread>++namespace sf {++namespace {++constexpr const char* kPath = "Tool > Bake";+constexpr int kSizes[] = {1024, 2048, 4096, 8192};+constexpr const char* kSizeLabels[] = {"1K", "2K", "4K", "8K"};++std::string utf8(const std::filesystem::path& p) {+ const std::u8string s = p.u8string();+ return std::string(s.begin(), s.end());+}++std::string safeName(std::string name) {+ for (char& c : name)+ if (std::strchr("/\\:*?\"<>|", c)) c = '_';+ return name.empty() ? std::string("bake") : name;+}++uint64_t uvHash(const Mesh& m) {+ uint64_t h = 1469598103934665603ull ^ m.uvs.size();+ const auto* b = reinterpret_cast<const uint8_t*>(m.uvs.data());+ const size_t n = m.uvs.size() * sizeof(glm::vec2);+ for (size_t i = 0; i < n; ++i) h = (h ^ b[i]) * 1099511628211ull;+ return h;+}++std::string fileUrl(const std::string& path) {+ std::string p = path;+ for (char& c : p)+ if (c == '\\') c = '/';+ std::string url = p.rfind('/', 0) == 0 ? "file://" : "file:///";+ const char* hex = "0123456789ABCDEF";+ for (unsigned char c : p) {+ if (std::isalnum(c) || std::strchr("/:-_.~", c)) url += char(c);+ else url += {'%', hex[c >> 4], hex[c & 15]};+ }+ return url;+}++// An integer slider drawn like ui::SliderInside (label inside, thin accent grab).+bool sliderInt(const char* id, int* v, int lo, int hi, const char* format) {+ float f = float(*v);+ if (!ui::SliderInside(id, &f, float(lo), float(hi), format)) return false;+ *v = std::clamp(int(std::lround(f)), lo, hi);+ return true;+}++} // namespace++struct BakeUiState {+ BakeSettings settings;+ int sizeChoice = 1; // 2K+ uint32_t source = 0; // SubTool id to bake from; 0 = every visible SubTool but the active one+ char folder[1024] = ""; // empty = beside the last export / the project / <library>/Bakes+ bool exr = true;+ bool preview = true; // show the baked normal map on the low-poly+ float frontPct = 2.0f, rearPct = 2.0f, aoDistancePct = 5.0f;++ struct Job {+ std::future<bool> done;+ std::shared_ptr<std::atomic<float>> progress;+ std::shared_ptr<std::atomic<bool>> cancel;+ std::shared_ptr<BakeResult> result;+ std::shared_ptr<std::string> error;+ std::shared_ptr<std::vector<std::string>> files;+ uint32_t subtool = 0;+ size_t triangles = 0;+ std::string folder;+ };+ std::unique_ptr<Job> job;++ // The last bake, shown on its SubTool while the mesh keeps its triangles and UVs.+ std::shared_ptr<const BakeResult> result;+ uint32_t subtool = 0;+ size_t triangles = 0;+ uint64_t uvKey = 0;+ const void* uvData = nullptr; // the UV array last checked against uvKey+ NormalMapView view;+ uint64_t serial = 0;+ std::vector<std::string> files;+ std::string writtenFolder; // where the last bake wrote its maps++ void show(uint32_t id, const Mesh& mesh, std::shared_ptr<const BakeResult> r, std::vector<std::string> written,+ std::string where) {+ result = std::move(r);+ subtool = id;+ triangles = mesh.triangleCount();+ uvKey = uvHash(mesh);+ uvData = mesh.uvs.data();+ files = std::move(written);+ writtenFolder = std::move(where);+ if (result && settings.autoDistance && result->diagonal > 0.0f) { // show what Auto measured+ frontPct = 100.0f * result->front / result->diagonal;+ rearPct = 100.0f * result->rear / result->diagonal;+ }+ view = NormalMapView();+ if (result && result->has(BakeMap::Normal)) {+ view.serial = ++serial;+ view.width = result->width;+ view.height = result->height;+ view.rgb = &result->normal;+ view.tangents = &result->tangents;+ view.flipGreen = result->flipGreen;+ }+ }+};++// File dialog answers arrive on another thread.+namespace {+std::mutex g_folderMutex;+std::string g_pickedFolder;+void SDLCALL onBakeFolder(void*, const char* const* files, int) {+ if (!files || !files[0]) return;+ std::lock_guard lock(g_folderMutex);+ g_pickedFolder = files[0];+}+} // namespace++const NormalMapView* App::bakeNormalMap(uint32_t id, const Mesh& mesh) {+ if (!bake_ || !bake_->preview || !bake_->result || bake_->subtool != id || !bake_->view.serial) return nullptr;+ BakeUiState& b = *bake_;+ if (mesh.triangleCount() != b.triangles || mesh.uvs.size() != b.result->tangents.size()) return nullptr;+ if (mesh.uvs.data() != b.uvData) { // the UV array moved (undo, a SubTool switch): same UVs?+ if (uvHash(mesh) != b.uvKey) return nullptr;+ b.uvData = mesh.uvs.data();+ }+ return &b.view;+}++bool App::bakeBusy() const { return bake_ && bake_->job; }++double App::bakeTestValue(const std::string& name) {+ const BakeUiState* b = bake_.get();+ if (name == "bake_running") return b && b->job ? 1.0 : 0.0;+ if (name == "baked_maps") {+ int n = 0;+ for (int i = 0; b && b->result && i < kBakeMapCount; ++i) n += b->result->has(BakeMap(i));+ return n;+ }+ if (name == "bake_size") return b && b->result ? double(b->result->width) : 0.0;+ if (name == "bake_files") return b ? double(b->files.size()) : 0.0;+ if (name == "normal_map_preview") return bakeNormalMap(doc_.activeSubtoolInfo().id, displayedMesh()) ? 1.0 : 0.0;+ return std::numeric_limits<double>::quiet_NaN();+}++void App::registerBakeUiActions() {+ if (!bake_) bake_ = std::make_shared<BakeUiState>();+ auto add = [this](const char* id, const char* label, std::function<void()> run, std::function<bool()> checked,+ std::function<bool()> enabled, const char* description, const char* command = "",+ const char* icon = nullptr) {+ Action a;+ a.id = id;+ a.label = label;+ a.icon = icon;+ a.palette = kPath;+ a.run = std::move(run);+ a.checked = std::move(checked);+ a.enabled = std::move(enabled);+ a.description = description;+ a.command = command;+ actions_.add(std::move(a));+ };+ add("bake.run", "Bake", [this] { startBake(); }, nullptr,+ [this] { return !bake_->job && doc_.mesh.hasUvs() && doc_.subtoolCount() > 1; },+ "Bake the chosen maps from the source SubTool(s) onto this SubTool's UVs, in the background. Maps are saved as "+ "PNGs (displacement also as a 32-bit EXR) in the output folder, and the normal map shows on the model.",+ "bake");+ add("bake.cancel", "Cancel Bake", [this] {+ if (bake_->job) bake_->job->cancel->store(true);+ },+ nullptr, [this] { return bake_->job != nullptr; }, "Stop the bake that is running. Nothing is written.");+ add("bake.preview", "Preview Normal Map", [this] { bake_->preview = !bake_->preview; },+ [this] { return bake_->preview; }, [this] { return bake_->result && bake_->result->has(BakeMap::Normal); },+ "Show the baked tangent-space normal map on the low-poly in the viewport (decoded with the same MikkTSpace "+ "tangents a game engine uses), to check it against the high-poly. Turn it off to see the bare low-poly.");+ add("bake.open_folder", "Open Folder", [this] {+ const std::string& dir = bake_->writtenFolder;+ if (!dir.empty() && !SDL_OpenURL(fileUrl(dir).c_str())) status_ = "Couldn't open " + dir + ": " + SDL_GetError();+ },+ nullptr, [this] { return !bake_->writtenFolder.empty(); }, "Open the folder the last bake wrote its maps to.");+ add("bake.choose_folder", "Output Folder...",+ [this] { SDL_ShowOpenFolderDialog(onBakeFolder, nullptr, window_, nullptr, false); }, nullptr, nullptr,+ "Choose the folder baked maps are written to. Empty: beside your last export, else the project's folder, "+ "else Bakes in your library folder.",+ "", ICON_LC_FOLDER_OPEN);+ // A bake run as a command (console, scripts, UI tests) shows its normal map too.+ setBakeListener([this](const Document& doc, std::shared_ptr<const BakeResult> r, const std::vector<std::string>& files) {+ const std::string folder = files.empty() ? std::string() : utf8(std::filesystem::path(std::u8string(files[0].begin(), files[0].end())).parent_path());+ bake_->show(doc.activeSubtoolInfo().id, doc.mesh, std::move(r), files, folder);+ });+}++void App::startBake() {+ BakeUiState& b = *bake_;+ if (b.job) return;+ if (!doc_.mesh.hasUvs()) {+ status_ = "Bake needs UVs on this SubTool (the low-poly): Tool > Texture Map > Auto UV";+ return;+ }+ // Source SubTools: the chosen one, or every visible SubTool but this one.+ std::string source = "all";+ for (int i = 0; i < doc_.subtoolCount(); ++i)+ if (b.source && doc_.subtools[size_t(i)].id == b.source) source = doc_.subtools[size_t(i)].name;+ std::vector<int> idx;+ std::string err;+ if (!bakeSourceSubtools(doc_, source, idx, err)) {+ status_ = err;+ return;+ }+ if (doc_.sculptor.strokeActive()) endStroke();+ BakeSettings st = b.settings;+ st.size = kSizes[std::clamp(b.sizeChoice, 0, 3)];+ st.front = b.frontPct / 100.0f;+ st.rear = b.rearPct / 100.0f;+ st.aoDistance = b.aoDistancePct / 100.0f;+ if (!st.maps) {+ status_ = "Tick at least one map to bake";+ return;+ }+ // Where the maps go.+ std::string folder = b.folder;+ if (folder.empty()) folder = lastExportFolder_;+ if (folder.empty() && !doc_.path.empty()) folder = defaultBakeFolder(doc_);+ if (folder.empty() && !library_.user.empty()) folder = library_.user + "/Bakes";+ if (folder.empty()) folder = utf8(std::filesystem::temp_directory_path() / "SculptForge Bakes");++ auto job = std::make_unique<BakeUiState::Job>();+ job->progress = std::make_shared<std::atomic<float>>(0.0f);+ job->cancel = std::make_shared<std::atomic<bool>>(false);+ job->result = std::make_shared<BakeResult>();+ job->error = std::make_shared<std::string>();+ job->files = std::make_shared<std::vector<std::string>>();+ job->subtool = doc_.activeSubtoolInfo().id;+ job->triangles = doc_.mesh.triangleCount();+ job->folder = folder;+ // The worker bakes copies, so sculpting can go on meanwhile.+ auto low = std::make_shared<Mesh>(doc_.mesh);+ auto sources = std::make_shared<std::vector<Mesh>>();+ for (int i : idx) sources->push_back(doc_.subtoolMesh(i));+ std::promise<bool> promise;+ job->done = promise.get_future();+ std::thread([promise = std::move(promise), low, sources, st, folder, name = safeName(doc_.activeSubtoolInfo().name),+ exr = b.exr, progress = job->progress, cancel = job->cancel, result = job->result, error = job->error,+ files = job->files]() mutable {+ std::vector<const Mesh*> src;+ for (const Mesh& m : *sources) src.push_back(&m);+ bool ok = bakeMaps(*low, src, st, *result, *error, [&](float f) {+ progress->store(f);+ return !cancel->load();+ });+ if (ok) ok = saveBakeMaps(*result, folder, name, exr, *files, *error);+ promise.set_value(ok);+ }).detach();+ b.job = std::move(job);+ status_ = "Baking " + std::to_string(st.size) + " maps from " + std::to_string(idx.size()) + " SubTool" ++ (idx.size() == 1 ? "" : "s") + "...";+}++void App::pollBake() {+ if (!bake_) return;+ BakeUiState& b = *bake_;+ {+ std::lock_guard lock(g_folderMutex);+ if (!g_pickedFolder.empty()) {+ std::snprintf(b.folder, sizeof b.folder, "%s", g_pickedFolder.c_str());+ g_pickedFolder.clear();+ }+ }+ if (!b.job) return;+ if (b.job->done.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {+ status_ = "Baking: " + std::to_string(int(b.job->progress->load() * 100.0f)) + "%";+ return;+ }+ const std::unique_ptr<BakeUiState::Job> job = std::move(b.job);+ if (!job->done.get()) {+ status_ = *job->error == "Cancelled" ? std::string("Bake cancelled") : "Bake failed: " + *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)+ b.show(job->subtool, doc_.subtoolMesh(index), job->result, *job->files, job->folder);+ else+ b.files = *job->files, b.writtenFolder = job->folder;+ status_ = bakeSummary(*job->result) + ". Saved to " + job->folder;+}++void App::drawBakeContents() {+ BakeUiState& b = *bake_;+ const Mesh& m = doc_.mesh;+ const ImGuiStyle& style = ImGui::GetStyle();+ const float half = (ImGui::GetContentRegionAvail().x - style.ItemSpacing.x) * 0.5f;++ // What the maps go onto.+ if (!m.hasUvs()) {+ ImGui::TextDisabled("No UVs: Tool > Texture Map > Auto UV");+ } else {+ ImGui::TextDisabled("Onto %s (%zu tris, UVs)", doc_.activeSubtoolInfo().name.c_str(), m.triangleCount());+ }+ ui::HelpText("Low-poly", nullptr,+ "Maps are baked onto the active SubTool's UVs: make the low-poly active (it needs UVs from import or "+ "from Auto UV in Tool > Texture Map), and keep the high-poly SubTool(s) visible.",+ kPath);++ // Source.+ std::string current = "All visible SubTools";+ bool found = false;+ for (int i = 0; i < doc_.subtoolCount(); ++i)+ if (b.source && doc_.subtools[size_t(i)].id == b.source && i != doc_.activeSubtool) {+ current = doc_.subtools[size_t(i)].name;+ found = true;+ }+ if (!found) b.source = 0;+ ImGui::AlignTextToFramePadding();+ ImGui::TextUnformatted("Source");+ ImGui::SameLine();+ ImGui::SetNextItemWidth(-1);+ if (ImGui::BeginCombo("##bakesource", current.c_str())) {+ if (ImGui::Selectable("All visible SubTools", b.source == 0)) b.source = 0;+ for (int i = 0; i < doc_.subtoolCount(); ++i) {+ if (i == doc_.activeSubtool) continue;+ const SubTool& st = doc_.subtools[size_t(i)];+ ImGui::PushID(int(st.id));+ if (ImGui::Selectable(st.name.c_str(), b.source == st.id)) b.source = st.id;+ ImGui::PopID();+ }+ ImGui::EndCombo();+ }+ ui::HelpText("Source", nullptr,+ "The high-poly to bake from: one SubTool, or every visible SubTool except the active one (the low-poly). "+ "Hidden faces of the source are ignored.",+ kPath);++ // Maps, two columns.+ static const char* kMapHelp[kBakeMapCount] = {+ "Tangent-space normal map (MikkTSpace tangents, as Blender, Unreal, Unity and Godot compute them). Shown on "+ "the model after baking.",+ "Object-space normal map: the high-poly's normals in model space (no tangents).",+ "Distance from the low-poly to the high-poly along its normal: a 16-bit PNG with mid-grey = 0 (the scale is "+ "reported when the bake finishes) and a 32-bit EXR in world units.",+ "Ambient occlusion: how much of the sky each point of the high-poly sees. Darker in creases and folds.",+ "Cavity: the high-poly's fine surface curvature. Mid-grey on smooth surfaces, dark in cracks, light on ridges.",+ "Colour transfer: the source's polypaint, or its painted texture.",+ };+ for (int i = 0; i < kBakeMapCount; ++i) {+ if (i % 2) ImGui::SameLine(half + style.ItemSpacing.x + style.WindowPadding.x);+ bool on = (b.settings.maps & (1u << i)) != 0;+ if (ImGui::Checkbox(bakeMapLabel(BakeMap(i)), &on)) b.settings.maps = on ? b.settings.maps | (1u << i) : b.settings.maps & ~(1u << i);+ ui::HelpText(bakeMapLabel(BakeMap(i)), nullptr, kMapHelp[i], kPath);+ }++ // Size and anti-aliasing.+ ImGui::SetNextItemWidth(half);+ ImGui::Combo("##bakesize", &b.sizeChoice, kSizeLabels, 4);+ ui::HelpText("Map size", nullptr,+ "Width and height of the maps: 1K, 2K (the default), 4K or 8K. Bake time and memory grow with the area "+ "(an 8K bake holds about 200 MB per map while it runs).",+ kPath);+ ImGui::SameLine();+ ImGui::SetNextItemWidth(-1);+ sliderInt("##bakeaa", &b.settings.samples, 1, 4, "AA %.0fx");+ ui::HelpText("Anti-aliasing", nullptr,+ "Supersampling: n x n rays per texel, averaged. 1 is fastest; 2 (the default) gives clean edges; 4 costs "+ "16 rays per texel.",+ kPath);++ // Ray distances.+ ImGui::Checkbox("Auto distance", &b.settings.autoDistance);+ ui::HelpText("Auto distance", nullptr,+ "Measure how far the high-poly lies in front of and behind the low-poly before baking, and cast rays just "+ "that far. Turn it off to set Front and Rear yourself (e.g. when rays hit a nearby part).",+ kPath);+ ImGui::BeginDisabled(b.settings.autoDistance);+ ImGui::SetNextItemWidth(half);+ ui::SliderInside("##bakefront", &b.frontPct, 0.1f, 20.0f, "Front %.1f%%", ImGuiSliderFlags_Logarithmic);+ ui::HelpText("Max Frontal Distance", nullptr,+ "How far in front of the low-poly rays start, as a percentage of the model's size (its bounding-box "+ "diagonal). Raise it when details that stick out come out flat or missing.",+ kPath);+ ImGui::SameLine();+ ImGui::SetNextItemWidth(-1);+ ui::SliderInside("##bakerear", &b.rearPct, 0.1f, 20.0f, "Rear %.1f%%", ImGuiSliderFlags_Logarithmic);+ ui::HelpText("Max Rear Distance", nullptr,+ "How far behind the low-poly rays look, as a percentage of the model's size. Raise it when carved detail "+ "is missing; lower it when rays reach the far side of thin parts.",+ kPath);+ ImGui::EndDisabled();++ ImGui::SetNextItemWidth(half);+ sliderInt("##bakepad", &b.settings.padding, 0, 32, "Padding %.0f px");+ ui::HelpText("Padding", nullptr,+ "Texels filled around each UV island with the island's edge colours, so seams don't show once the maps "+ "are mipmapped in an engine. 8 is plenty for 2K; use more for 4K and 8K.",+ kPath);+ ImGui::SameLine();+ const char* greens[] = {"OpenGL (Y+)", "DirectX (Y-)"};+ int green = b.settings.flipGreen ? 1 : 0;+ ImGui::SetNextItemWidth(-1);+ if (ImGui::Combo("##bakegreen", &green, greens, 2)) b.settings.flipGreen = green == 1;+ ui::HelpText("Normal map convention", nullptr,+ "OpenGL (green up) suits Blender, Unity, Godot and Substance. DirectX (green down) is Unreal's native "+ "convention; Send to Unreal flips an OpenGL map on import, so either works there.",+ kPath);++ if (b.settings.maps & bakeBit(BakeMap::AmbientOcclusion)) {+ ImGui::SetNextItemWidth(half);+ sliderInt("##bakeaorays", &b.settings.aoRays, 8, 256, "AO rays %.0f");+ ui::HelpText("AO rays", nullptr, "Rays per texel for ambient occlusion: more is smoother and slower.", kPath);+ ImGui::SameLine();+ ImGui::SetNextItemWidth(-1);+ ui::SliderInside("##bakeaodist", &b.aoDistancePct, 0.5f, 50.0f, "AO reach %.1f%%", ImGuiSliderFlags_Logarithmic);+ ui::HelpText("AO reach", nullptr,+ "How far away geometry still occludes, as a percentage of the model's size. Short: only creases "+ "darken. Long: whole cavities and the insides of shapes darken too.",+ kPath);+ }++ // Output.+ ImGui::SetNextItemWidth(-ImGui::GetFrameHeight() - style.ItemSpacing.x);+ ImGui::InputTextWithHint("##bakefolder", lastExportFolder_.empty() ? "Output folder (default: project / library)"+ : "Output folder (default: last export's)",+ b.folder, sizeof b.folder);+ ui::HelpText("Output folder", nullptr,+ "Where the maps are written, as <SubTool>_normal.png, _ao.png, _displacement.png (+ .exr) and so on. "+ "Empty: beside your last export, else the project's folder, else Bakes in your library folder.",+ kPath);+ ImGui::SameLine();+ ui::ActionButton(*this, "bake.choose_folder", ImVec2(ImGui::GetFrameHeight(), 0), ui::Look::Icon);+ ImGui::Checkbox("EXR displacement", &b.exr);+ ui::HelpText("EXR displacement", nullptr,+ "Also write the displacement as a 32-bit float OpenEXR in world units (for renderers that take real "+ "distances). The 16-bit PNG is always written.",+ kPath);++ // Bake / progress.+ if (b.job) {+ ImGui::ProgressBar(b.job->progress->load(), ImVec2(half, 0), "Baking...");+ ui::HelpText("Baking", nullptr, "Baking on every core in the background; you can keep working.", kPath);+ ImGui::SameLine();+ ui::ActionButton(*this, "bake.cancel", ImVec2(-1, 0));+ } else {+ ui::ActionButton(*this, "bake.run", ImVec2(-1, 0));+ }++ // The last bake.+ if (b.result) {+ const BakeResult& r = *b.result;+ ImGui::TextDisabled("%dx%d in %.1f s from %zu tris", r.width, r.height, r.seconds, r.sourceTriangles);+ if (r.has(BakeMap::Displacement))+ ImGui::TextDisabled("Displacement: mid-grey = 0, white = +%.4g", double(r.displacementScale));+ if (r.missRatio() >= 0.0005f) {+ ImGui::PushStyleColor(ImGuiCol_Text, ImGui::ColorConvertU32ToFloat4(ui::color::danger));+ ImGui::TextWrapped("%.1f%% of texels found no surface: raise Front / Rear", double(100.0f * r.missRatio()));+ ImGui::PopStyleColor();+ }+ ImGui::Checkbox("Preview Normal Map", &b.preview);+ ui::HelpAction(*this, "bake.preview");+ ImGui::SameLine();+ ui::ActionButton(*this, "bake.open_folder", ImVec2(-1, 0));+ }+}++} // namespace sfdiff --git a/src/app/UiTest.cpp b/src/app/UiTest.cppindex 1d16a240..27ac7436 100644--- a/src/app/UiTest.cpp+++ b/src/app/UiTest.cpp@@ -56,6 +56,8 @@// texture_painted (texels of the texture composite that aren't white),// texture_mean_r / texture_mean_g / texture_mean_b (mean composite, sRGB 0-255),// auto_uv_running+// bake_running, baked_maps (maps in the last bake), bake_size, bake_files (files it wrote),+// normal_map_preview (1 when the active SubTool shows its baked normal map)// (mesh values are the active SubTool's)// snapshot remember the active mesh's vertices (for the moved* values)// remember VALUE NAME keep VALUE's current value as $NAME@@ -223,6 +225,7 @@ double Demo::query(const std::string& name, bool& known) const {if (name == "texture_layers") return hasTexture(m) ? double(m.texture->layers.size()) : 0.0;if (name == "active_texture_layer") return hasTexture(m) && !m.texture->layers.empty() ? double(m.texture->activeLayer) : -1.0;if (name == "auto_uv_running") return app_.autoUv_ ? 1.0 : 0.0;+ if (const double v = app_.bakeTestValue(name); !std::isnan(v)) return v;if (name == "texture_painted" || name.rfind("texture_mean_", 0) == 0) {if (!hasTexture(m)) return 0.0;const std::vector<uint8_t>& px = m.texture->pixels;@@ -685,7 +688,8 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& esteps_.push_back([this, line, timeout](float t) {const 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_.thumbs_.busy() && !app_.libraryScanner_.busy() && !app_.autoUv_ &&+ !app_.bakeBusy();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/Bake.cpp b/src/core/Bake.cppindex 47eaaf62..cb3265ab 100644--- a/src/core/Bake.cpp+++ b/src/core/Bake.cpp@@ -1106,13 +1106,13 @@ void setBakeListener(BakeListener listener) {g_listener = std::move(listener);}-void notifyBakeListener(const Document& doc, std::shared_ptr<const BakeResult> result) {+void notifyBakeListener(const Document& doc, std::shared_ptr<const BakeResult> result, const std::vector<std::string>& files) {BakeListener l;{std::lock_guard lock(g_listenerMutex);l = g_listener;}- if (l) l(doc, std::move(result));+ if (l) l(doc, std::move(result), files);}// ---------------------------------------------------------------------------@@ -1197,7 +1197,7 @@ void registerBakeCommands(CommandRegistry& reg) {const std::string folder = argString(a, "out", defaultBakeFolder(doc));std::vector<std::string> written;if (!saveBakeMaps(*result, folder, name, argBool(a, "exr", true), written, err)) return {false, err};- notifyBakeListener(doc, result);+ notifyBakeListener(doc, result, written);std::string files;for (const std::string& w : written) files += "\n " + w;return {true, bakeSummary(*result) + "\nWrote:" + files};diff --git a/src/core/Bake.h b/src/core/Bake.hindex 5fc0f2fa..1bb60ece 100644--- a/src/core/Bake.h+++ b/src/core/Bake.h@@ -103,10 +103,11 @@ bool bakeSourceSubtools(const Document& doc, const std::string& source, std::vec// Where maps go when no folder is given: the project's folder, else the current folder.std::string defaultBakeFolder(const Document& doc);-// Called after the bake command succeeds (the app shows the normal map on the model).-using BakeListener = std::function<void(const Document&, std::shared_ptr<const BakeResult>)>;+// Called after the bake command succeeds, with the files it wrote (the app shows the normal map on the model).+using BakeListener =+ std::function<void(const Document&, std::shared_ptr<const BakeResult>, const std::vector<std::string>& files)>;void setBakeListener(BakeListener listener);-void notifyBakeListener(const Document& doc, std::shared_ptr<const BakeResult> result);+void notifyBakeListener(const Document& doc, std::shared_ptr<const BakeResult> result, const std::vector<std::string>& files);// bakevoid registerBakeCommands(CommandRegistry& registry);diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cppindex 4223ee11..c4a0b58d 100644--- a/src/render/Renderer.cpp+++ b/src/render/Renderer.cpp@@ -77,7 +77,48 @@ void main() {})";-// Compiled twice: as is, and with TEXTURED defined (after kMeshGS, multiplying the painted texture in).+// Baked normal map preview (core/Bake.h): kMeshGS plus each corner's MikkTSpace tangent (xyz, bitangent sign+// in w), fetched like the UVs and turned into view space.+const char* kMeshNormalGS = R"(#version 330 core+layout(triangles) in;+layout(triangle_strip, max_vertices = 3) out;+in vec3 vNormal[];+in vec3 vPos[];+in float vCurvature[];+in vec3 vColor[];+in float vMask[];+uniform samplerBuffer uUvs; // RG32F, 3 corners per drawn triangle+uniform samplerBuffer uTangents; // RGBA32F, 3 corners per drawn triangle+uniform mat4 uView;+out FragData {+ vec3 vNormal;+ vec3 vPos;+ float vCurvature;+ vec3 vColor;+ float vMask;+ vec2 vUv;+ vec4 vTangent;+} o;+void main() {+ for (int i = 0; i < 3; ++i) {+ o.vNormal = vNormal[i];+ o.vPos = vPos[i];+ o.vCurvature = vCurvature[i];+ o.vColor = vColor[i];+ o.vMask = vMask[i];+ o.vUv = texelFetch(uUvs, gl_PrimitiveIDIn * 3 + i).xy;+ vec4 t = texelFetch(uTangents, gl_PrimitiveIDIn * 3 + i);+ o.vTangent = vec4(mat3(uView) * t.xyz, t.w);+ gl_PrimitiveID = gl_PrimitiveIDIn;+ gl_Position = gl_in[i].gl_Position;+ EmitVertex();+ }+ EndPrimitive();+}+)";++// Compiled three times: as is, with TEXTURED defined (after kMeshGS, multiplying the painted texture in), and+// with NORMALMAP too (after kMeshNormalGS: a baked normal map bends the shading normal). (after kMeshGS, multiplying the painted texture in).const char* kMeshFS = R"(#ifdef TEXTUREDin FragData {@@ -87,8 +128,16 @@ in FragData {vec3 vColor;float vMask;vec2 vUv;+#ifdef NORMALMAP+ vec4 vTangent;+#endif};uniform sampler2D uPaintTex; // sRGB texture: samples come back linear+#ifdef NORMALMAP+uniform sampler2D uNormalTex; // baked tangent-space normal map (linear RGB)+uniform int uHasPaint; // 1: a painted texture is bound to uPaintTex too+uniform int uNormalFlipY; // 1: DirectX convention (green down)+#endif#elsein vec3 vNormal;in vec3 vPos;@@ -128,10 +177,21 @@ void main() {if (uGroups == 1) albedo = tint = groupColor(texelFetch(uGroupIds, gl_PrimitiveID).r);else if (uGroups == 2) albedo = tint = groupColor(0u);else if (uPaint == 1) albedo = tint = vColor;-#ifdef TEXTURED+#if defined(NORMALMAP)+ if (uGroups == 0 && uHasPaint == 1) albedo = tint = (uPaint == 1 ? vColor : vec3(1.0)) * texture(uPaintTex, vUv).rgb;+#elif defined(TEXTURED)if (uGroups == 0) albedo = tint = (uPaint == 1 ? vColor : vec3(1.0)) * texture(uPaintTex, vUv).rgb;#endifvec3 n = normalize(vNormal);+#ifdef NORMALMAP+ // MikkTSpace's pixel shader: bitangent rebuilt per pixel, n = normalize(tn.x T + tn.y B + tn.z N).+ vec3 tn = texture(uNormalTex, vUv).xyz * 2.0 - 1.0;+ if (uNormalFlipY == 1) tn.y = -tn.y;+ vec3 T = vTangent.xyz - n * dot(n, vTangent.xyz);+ T = dot(T, T) > 1e-12 ? normalize(T) : vec3(1.0, 0.0, 0.0);+ vec3 B = (vTangent.w < 0.0 ? -1.0 : 1.0) * cross(n, T);+ n = normalize(tn.x * T + tn.y * B + tn.z * n);+#endifif (!gl_FrontFacing) n = -n;vec3 v = normalize(-vPos);if (uMatcap == 1) {@@ -252,6 +312,9 @@ void main() {)";constexpr GLenum kTexture2 = 0x84C2; // GL_TEXTURE2 (matcap)+constexpr GLenum kTexture5 = 0x84C5; // kTexture5 (baked normal map)+constexpr GLenum kTexture6 = 0x84C6; // kTexture6 (its tangents)+constexpr GLenum kRgb = 0x1907, kRgb8 = 0x8051; // GL_RGB, GL_RGB8GLuint compile(GLenum type, const char* src, std::string& error, const char* prefix = nullptr) {GLuint s = gl::CreateShader(type);@@ -326,6 +389,8 @@ bool Renderer::init(std::string& error) {if (!meshProgram_) return false;meshTexProgram_ = link(kMeshVS, kMeshFS, error, "#version 330 core\n#define TEXTURED\n", kMeshGS);if (!meshTexProgram_) return false;+ meshNormalProgram_ = link(kMeshVS, kMeshFS, error, "#version 330 core\n#define TEXTURED\n#define NORMALMAP\n", kMeshNormalGS);+ if (!meshNormalProgram_) return false;wireProgram_ = link(kMeshVS, kWireFS, error);if (!wireProgram_) return false;bgProgram_ = link(kBgVS, kBgFS, error);@@ -417,6 +482,9 @@ void Renderer::freeSlot(MeshSlot& s) {gl::DeleteBuffers(1, &s.uvData);gl::DeleteTextures(1, &s.uvTexture);if (s.paintTexture) gl::DeleteTextures(1, &s.paintTexture);+ if (s.normalTexture) gl::DeleteTextures(1, &s.normalTexture);+ if (s.tangentData) gl::DeleteBuffers(1, &s.tangentData);+ if (s.tangentTexture) gl::DeleteTextures(1, &s.tangentTexture);gl::DeleteVertexArrays(1, &s.vao);s = MeshSlot();}@@ -430,6 +498,7 @@ void Renderer::shutdown() {gl::DeleteVertexArrays(1, &emptyVao_);gl::DeleteProgram(meshProgram_);gl::DeleteProgram(meshTexProgram_);+ gl::DeleteProgram(meshNormalProgram_);gl::DeleteProgram(wireProgram_);gl::DeleteProgram(bgProgram_);gl::DeleteProgram(splatProgram_);@@ -544,7 +613,7 @@ void Renderer::updateCurvature(MeshSlot& s, const Mesh& mesh, uint32_t begin, ui// triangle in draw order (looked up by gl_PrimitiveID). Meshes with quads// (Mesh::quadPartners) also get wireframe edges without the quad diagonals,// stored as GL_LINES indices after the triangles.-void Renderer::uploadFaces(MeshSlot& s, const Mesh& mesh) {+void Renderer::uploadFaces(MeshSlot& s, const Mesh& mesh, bool wantUvs) {const size_t nt = mesh.triangleCount();const bool hidden = mesh.hidden.size() == nt && nt > 0;const bool groups = mesh.polygroups.size() == nt && nt > 0;@@ -599,8 +668,10 @@ void Renderer::uploadFaces(MeshSlot& s, const Mesh& mesh) {gl::BindTexture(GL_TEXTURE_BUFFER, 0);gl::BindBuffer(GL_TEXTURE_BUFFER, 0);}- // Texture painting: the UVs of every drawn triangle's corners, looked up by gl_PrimitiveID.- const bool textured = hasTexture(mesh) && drawn * 3 <= size_t(std::max(maxTexels, 0));+ // Texture painting (and a baked normal map preview): the UVs of every drawn triangle's corners, looked up+ // by gl_PrimitiveID.+ const bool textured = wantUvs && mesh.hasUvs() && drawn * 3 <= size_t(std::max(maxTexels, 0));+ s.tangentsStale = true;if (textured) {visibleUvs_.clear();if (hidden) {@@ -642,13 +713,17 @@ void Renderer::syncScene(const std::vector<SceneMesh>& scene) {drawList_.clear();for (const SceneMesh& m : scene) {if (!m.visible || !m.mesh) continue; // hidden: keeps its dirty state until shown- sync(slot(m.id), *m.mesh);+ sync(slot(m.id), *m.mesh, m.normalMap);drawList_.push_back({m.id, m.active});}}-void Renderer::sync(MeshSlot& s, Mesh& mesh) {+void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap) {const size_t nv = mesh.vertexCount();+ if (normalMap && (!normalMap->rgb || !normalMap->tangents || normalMap->tangents->size() != mesh.uvs.size() ||+ !mesh.hasUvs() || normalMap->rgb->size() != size_t(normalMap->width) * size_t(normalMap->height) * 3))+ normalMap = nullptr; // made for another mesh+ const bool wantUvs = hasTexture(mesh) || normalMap;const bool full = s.fresh || mesh.topologyDirty || nv != s.vertexCapacity;const bool ranged = !full && mesh.dirtyBegin < mesh.dirtyEnd && mesh.dirtyEnd <= nv;const GLintptr off3 = ranged ? GLintptr(mesh.dirtyBegin * sizeof(glm::vec3)) : 0;@@ -663,7 +738,7 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh) {gl::BufferData(GL_ARRAY_BUFFER, GLsizeiptr(nv * sizeof(glm::vec3)), mesh.normals.data(), GL_DYNAMIC_DRAW);gl::BindBuffer(GL_ARRAY_BUFFER, s.curvBuffer);gl::BufferData(GL_ARRAY_BUFFER, GLsizeiptr(nv * sizeof(float)), s.curvature.data(), GL_DYNAMIC_DRAW);- uploadFaces(s, mesh);+ uploadFaces(s, mesh, wantUvs);s.vertexCapacity = nv;} else if (ranged) {gl::BindBuffer(GL_ARRAY_BUFFER, s.posBuffer);@@ -674,8 +749,9 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh) {gl::BindBuffer(GL_ARRAY_BUFFER, s.curvBuffer);gl::BufferSubData(GL_ARRAY_BUFFER, off1, size1, s.curvature.data() + mesh.dirtyBegin);}- if (!full && (mesh.facesDirty || hasTexture(mesh) != s.hasUvs)) uploadFaces(s, mesh);+ if (!full && (mesh.facesDirty || wantUvs != s.hasUvs)) uploadFaces(s, mesh, wantUvs);syncTexture(s, mesh);+ syncNormalMap(s, mesh, normalMap);// Optional per-vertex attributes: a full upload when they appear (or the// mesh changed), the dirty range otherwise, and a disabled array when absent.@@ -746,6 +822,53 @@ void Renderer::syncTexture(MeshSlot& s, Mesh& mesh) {tex->clearDirty();}+// The baked normal map preview: the map uploads when its serial changes, the tangents (in draw order, without+// hidden triangles, like the UVs) whenever the faces were re-uploaded too.+void Renderer::syncNormalMap(MeshSlot& s, const Mesh& mesh, const NormalMapView* nm) {+ if (!nm || !s.hasUvs) {+ if (s.normalTexture) gl::DeleteTextures(1, &s.normalTexture);+ s.normalTexture = 0;+ s.normalSerial = 0;+ s.hasTangents = false;+ return;+ }+ if (!s.normalTexture || s.normalSerial != nm->serial) {+ if (!s.normalTexture) gl::GenTextures(1, &s.normalTexture);+ gl::BindTexture(GL_TEXTURE_2D, s.normalTexture);+ gl::PixelStorei(GL_UNPACK_ALIGNMENT, 1);+ gl::TexImage2D(GL_TEXTURE_2D, 0, GLint(kRgb8), nm->width, nm->height, 0, kRgb, GL_UNSIGNED_BYTE, nm->rgb->data());+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GLint(GL_LINEAR_MIPMAP_LINEAR));+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GLint(GL_LINEAR));+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GLint(GL_CLAMP_TO_EDGE));+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GLint(GL_CLAMP_TO_EDGE));+ gl::GenerateMipmap(GL_TEXTURE_2D);+ gl::BindTexture(GL_TEXTURE_2D, 0);+ s.normalSerial = nm->serial;+ s.tangentsStale = true;+ }+ s.normalFlipGreen = nm->flipGreen;+ if (!s.tangentsStale && s.hasTangents) return;+ if (!s.tangentData) gl::GenBuffers(1, &s.tangentData);+ if (!s.tangentTexture) gl::GenTextures(1, &s.tangentTexture);+ const std::vector<glm::vec4>& all = *nm->tangents;+ std::vector<glm::vec4> visible;+ const bool hidden = mesh.hidden.size() == mesh.triangleCount() && mesh.triangleCount() > 0;+ if (hidden) {+ visible.reserve(all.size());+ for (size_t t = 0; t < mesh.triangleCount(); ++t)+ if (!mesh.hidden[t]) visible.insert(visible.end(), {all[t * 3], all[t * 3 + 1], all[t * 3 + 2]});+ }+ const std::vector<glm::vec4>& tans = hidden ? visible : all;+ gl::BindBuffer(GL_TEXTURE_BUFFER, s.tangentData);+ gl::BufferData(GL_TEXTURE_BUFFER, GLsizeiptr(tans.size() * sizeof(glm::vec4)), tans.data(), GL_STATIC_DRAW);+ gl::BindTexture(GL_TEXTURE_BUFFER, s.tangentTexture);+ gl::TexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, s.tangentData);+ gl::BindTexture(GL_TEXTURE_BUFFER, 0);+ gl::BindBuffer(GL_TEXTURE_BUFFER, 0);+ s.hasTangents = true;+ s.tangentsStale = false;+}+GLuint Renderer::paintTexture(uint32_t id) const {for (const auto& s : slots_)if (s.id == id) return s.paintTexture;@@ -898,7 +1021,7 @@ void Renderer::draw(const Camera& camera, int targetWidth, int targetHeight, conconst GLuint matcap = matcapTexture(settings.matcap);const MatcapCavity cavity = matcapCavity(settings.matcap);- for (GLuint prog : {meshTexProgram_, meshProgram_}) { // the plain program stays bound+ for (GLuint prog : {meshNormalProgram_, meshTexProgram_, meshProgram_}) { // the plain program stays boundsetMatrices(prog);gl::Uniform3fv(gl::GetUniformLocation(prog, "uBaseColor"), 1, glm::value_ptr(settings.baseColor));gl::Uniform1i(gl::GetUniformLocation(prog, "uGroupIds"), 1);@@ -907,6 +1030,8 @@ void Renderer::draw(const Camera& camera, int targetWidth, int targetHeight, congl::Uniform2f(gl::GetUniformLocation(prog, "uMatcapCavity"), cavity.crease, cavity.ridge);gl::Uniform1i(gl::GetUniformLocation(prog, "uUvs"), 3);gl::Uniform1i(gl::GetUniformLocation(prog, "uPaintTex"), 4);+ gl::Uniform1i(gl::GetUniformLocation(prog, "uNormalTex"), 5);+ gl::Uniform1i(gl::GetUniformLocation(prog, "uTangents"), 6);}gl::ActiveTexture(kTexture2);gl::BindTexture(GL_TEXTURE_2D, matcap);@@ -916,6 +1041,10 @@ void Renderer::draw(const Camera& camera, int targetWidth, int targetHeight, confor (const DrawItem& item : drawList_)if (const MeshSlot* s = findSlot(item.id); s && s->indexCount > 0) drawSlot(*s, item.active, settings);gl::Disable(GL_POLYGON_OFFSET_FILL);+ gl::ActiveTexture(kTexture5);+ gl::BindTexture(GL_TEXTURE_2D, 0);+ gl::ActiveTexture(kTexture6);+ gl::BindTexture(GL_TEXTURE_BUFFER, 0);gl::ActiveTexture(GL_TEXTURE3);gl::BindTexture(GL_TEXTURE_BUFFER, 0);gl::ActiveTexture(GL_TEXTURE4);@@ -956,14 +1085,25 @@ void Renderer::draw(const Camera& camera, int targetWidth, int targetHeight, convoid Renderer::drawSlot(const MeshSlot& s, bool active, const RenderSettings& settings) {// A painted texture shows through the textured program (polypaint still multiplies it).const bool textured = s.hasUvs && s.paintTexture && settings.showPolypaint && settings.showTexture;- const GLuint prog = textured ? meshTexProgram_ : meshProgram_;+ // A baked normal map preview bends the shading normal (core/Bake.h).+ const bool normalMapped = s.hasUvs && s.normalTexture && s.hasTangents;+ const GLuint prog = normalMapped ? meshNormalProgram_ : textured ? meshTexProgram_ : meshProgram_;gl::UseProgram(prog);- if (textured) {+ if (textured || normalMapped) {gl::ActiveTexture(GL_TEXTURE3);gl::BindTexture(GL_TEXTURE_BUFFER, s.uvTexture);gl::ActiveTexture(GL_TEXTURE4);- gl::BindTexture(GL_TEXTURE_2D, s.paintTexture);+ gl::BindTexture(GL_TEXTURE_2D, textured ? s.paintTexture : 0);+ gl::ActiveTexture(GL_TEXTURE0);+ }+ if (normalMapped) {+ gl::ActiveTexture(kTexture5);+ gl::BindTexture(GL_TEXTURE_2D, s.normalTexture);+ gl::ActiveTexture(kTexture6);+ gl::BindTexture(GL_TEXTURE_BUFFER, s.tangentTexture);gl::ActiveTexture(GL_TEXTURE0);+ gl::Uniform1i(gl::GetUniformLocation(prog, "uHasPaint"), textured ? 1 : 0);+ gl::Uniform1i(gl::GetUniformLocation(prog, "uNormalFlipY"), s.normalFlipGreen ? 1 : 0);}gl::Uniform1i(gl::GetUniformLocation(prog, "uPaint"), settings.showPolypaint && s.hasColors ? 1 : 0);gl::Uniform1i(gl::GetUniformLocation(prog, "uShowMask"), active && settings.showMask && s.hasMask ? 1 : 0);diff --git a/src/render/Renderer.h b/src/render/Renderer.hindex d7fd7080..48c08d2c 100644--- a/src/render/Renderer.h+++ b/src/render/Renderer.h@@ -27,12 +27,23 @@ struct RenderSettings {bool dimInactive = true; // SubTools other than the active one render slightly darker (ZBrush style)};+// A baked tangent-space normal map to preview on a mesh (core/Bake.h): RGB8 rows bottom-up, with the+// MikkTSpace tangent + sign of every triangle corner it was baked against. `serial` changes with the data.+struct NormalMapView {+ uint64_t serial = 0;+ int width = 0, height = 0;+ const std::vector<uint8_t>* rgb = nullptr;+ const std::vector<glm::vec4>* tangents = nullptr;+ bool flipGreen = false; // DirectX convention (green down)+};+// One mesh of the scene for Renderer::syncScene: a SubTool, keyed by its stable id.struct SceneMesh {uint32_t id = 0;Mesh* mesh = nullptr;bool active = false; // gets mask darkening, polygroup colours and wireframe; never dimmedbool visible = true; // hidden meshes keep their GPU slot but aren't drawn (or uploaded)+ const NormalMapView* normalMap = nullptr; // shade with this normal map (needs the mesh's UVs)};// Part of the target the 3D view is drawn into (framebuffer pixels, origin bottom-left); the rest is@@ -87,7 +98,7 @@ public:private:void drawSplats(const glm::mat4& view, const glm::mat4& proj, int fbWidth, int fbHeight, float opacity);- GLuint meshProgram_ = 0, meshTexProgram_ = 0, wireProgram_ = 0, bgProgram_ = 0, splatProgram_ = 0;+ GLuint meshProgram_ = 0, meshTexProgram_ = 0, meshNormalProgram_ = 0, wireProgram_ = 0, bgProgram_ = 0, splatProgram_ = 0;// Splats: per-splat data in a texture buffer, drawn as instanced quads in back-to-front order.GLuint splatVao_ = 0, splatData_ = 0, splatTexture_ = 0, splatOrder_ = 0;GLsizei splatCount_ = 0;@@ -110,6 +121,10 @@ private:GLuint paintTexture = 0; // the painted texture (sRGB, mipmapped)uint64_t textureSerial = 0, textureRevision = 0;int textureW = 0, textureH = 0;+ // Baked normal map preview (NormalMapView): the map, and the tangents per drawn corner (texture buffer).+ GLuint normalTexture = 0, tangentData = 0, tangentTexture = 0;+ uint64_t normalSerial = 0;+ bool hasTangents = false, tangentsStale = true, normalFlipGreen = false;bool hasColors = false, hasMask = false, hasGroups = false, hasUvs = false;bool fresh = true; // nothing uploaded yetstd::vector<float> curvature;@@ -119,9 +134,10 @@ private:};MeshSlot& slot(uint32_t id); // created on first usestatic void freeSlot(MeshSlot& s);- void sync(MeshSlot& s, Mesh& mesh);+ void sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap = nullptr);+ void syncNormalMap(MeshSlot& s, const Mesh& mesh, const NormalMapView* normalMap);static void updateCurvature(MeshSlot& s, const Mesh& mesh, uint32_t begin, uint32_t end);- void uploadFaces(MeshSlot& s, const Mesh& mesh);+ void uploadFaces(MeshSlot& s, const Mesh& mesh, bool wantUvs);void syncTexture(MeshSlot& s, Mesh& mesh);void drawSlot(const MeshSlot& s, bool active, const RenderSettings& settings);std::vector<MeshSlot> slots_;diff --git a/tests/test_bake.cpp b/tests/test_bake.cppindex d06e2998..cd3560ad 100644--- a/tests/test_bake.cpp+++ b/tests/test_bake.cpp@@ -371,13 +371,18 @@ TEST(bake_command) {const std::string dir = tempPath("sf_bake_cmd");std::filesystem::remove_all(dir);std::shared_ptr<const BakeResult> seen;- setBakeListener([&](const Document&, std::shared_ptr<const BakeResult> r) { seen = r; });+ size_t seenFiles = 0;+ setBakeListener([&](const Document&, std::shared_ptr<const BakeResult> r, const std::vector<std::string>& files) {+ seen = r;+ seenFiles = files.size();+ });CommandResult res = reg.execute(doc, "bake", {{"source", std::string("all")}, {"maps", std::string("normal,ao,displacement")},{"size", 256.0}, {"out", dir}, {"ao_rays", 8.0}});std::printf(" %s\n", res.message.c_str());CHECK(res.ok);CHECK(res.message.find("mid-grey = 0") != std::string::npos);CHECK(seen && seen->width == 256 && seen->has(BakeMap::AmbientOcclusion));+ CHECK(seenFiles == 4);for (const char* f : {"Low_normal.png", "Low_ao.png", "Low_displacement.png", "Low_displacement.exr"})CHECK(std::filesystem::exists(std::filesystem::path(dir) / f));res = reg.execute(doc, "bake", {{"source", std::string("High")}, {"size", std::string("1k")}, {"out", dir}, {"name", std::string("x")},diff --git a/tests/ui/bake.sfui b/tests/ui/bake.sfuinew file mode 100644index 00000000..57ba6e70--- /dev/null+++ b/tests/ui/bake.sfui@@ -0,0 +1,100 @@+# Map baking (Tool > Bake): the detail of a 330k-triangle sculpt baked onto a 2,000-triangle low-poly with UVs.+# Two identical sculpted spheres side by side: the right one is the bake source, its decimated copy (with Auto UV)+# the low-poly. After baking, the source is hidden so the shots show the high-poly (left) next to the low-poly+# wearing its baked normal map (right).+run new_sphere subdivisions=5+run subtool_rename to=High+run subtool_append type=sphere size=1 name=Source+run subdivide levels=2+run subtool_select name=High+run subdivide levels=2+# The same strokes on both spheres (the source sits 2.2 units to the right); strokes go to the active SubTool.+run stroke brush=Crease points="-0.6,0.35,1 0,0.55,1 0.6,0.35,1" radius=0.07 strength=1+run stroke brush=Inflate points="-0.35,-0.3,1" radius=0.3 strength=1+run stroke brush=Clay points="0.25,-0.5,1 0.55,-0.05,1" radius=0.1 strength=1+run stroke brush=Crease invert=true points="-0.75,-0.1,1 -0.45,-0.65,1" radius=0.05 strength=1+run stroke brush=Standard points="-0.2,0.05,1 0.2,0.1,1" radius=0.06 strength=1 invert=true+run subtool_select name=Source+run stroke brush=Crease points="1.6,0.35,1 2.2,0.55,1 2.8,0.35,1" radius=0.07 strength=1+run stroke brush=Inflate points="1.85,-0.3,1" radius=0.3 strength=1+run stroke brush=Clay points="2.45,-0.5,1 2.75,-0.05,1" radius=0.1 strength=1+run stroke brush=Crease invert=true points="1.45,-0.1,1 1.75,-0.65,1" radius=0.05 strength=1+run stroke brush=Standard points="2.0,0.05,1 2.4,0.1,1" radius=0.06 strength=1 invert=true+# The low-poly: a decimated copy of the source with automatic UVs.+run subtool_duplicate+run subtool_rename to=Low+run decimate triangles=2000+run uv_auto resolution=2048+expect_value uvs == 1+expect_value triangles <= 2000+expect_value subtools == 3+key f++# The Bake sub-palette, with the source picked and three maps ticked.+click "Bake##tool.bake"+expect "Bake/Bake"+expect_text "Onto Low"+click "Bake/##bakesource"+click "Source"+expect_text "Source"+click "Bake/Displacement"+click "Bake/Ambient Occlusion"+shot bake-palette++# Baking runs in the background with a progress bar, then the normal map shows on the low-poly.+click "Bake/Bake"+expect_value bake_running == 1+wait_idle 300+expect_value bake_running == 0+expect_status "Baked normal, displacement, ao at 2048x2048"+expect_status "mid-grey = 0"+expect_value baked_maps == 3+expect_value bake_size == 2048+expect_value bake_files == 4+expect_value normal_map_preview == 1+expect_text "Displacement: mid-grey = 0"+expect_no_text "found no surface"++# Hide the source: the high-poly (left) next to the baked low-poly (right).+run subtool_visibility name=Source visible=false+expect_value visible_subtools == 2+expect_value normal_map_preview == 1+shot high-and-baked-low+# From an angle: the silhouette stays low-poly, the shading carries the sculpted detail.+orbit -110 50+shot baked-low-angle+orbit 110 -50+# The bare low-poly without its normal map, for comparison.+click "Bake/Preview Normal Map"+expect_value normal_map_preview == 0+shot low-without-map+click "Bake/Preview Normal Map"+expect_value normal_map_preview == 1++# Changing the low-poly's UVs retires the preview (the map no longer fits); undo brings it back.+run uv_auto resolution=1024+expect_value normal_map_preview == 0+key ctrl+z+expect_value normal_map_preview == 1++# The bake command (console / scripts) bakes with the same engine and shows its map too.+run bake source=Source maps=normal,cavity size=1024 out=ui-shots/bake/command-maps+expect_status "Baked normal, cavity at 1024x1024"+expect_value baked_maps == 2+expect_value normal_map_preview == 1++# Ctrl-hover help on the new controls, and the commands in the F3 palette.+hold ctrl+hover "Bake/Bake"+expect_help "UVs"+hover "Bake/##bakefront"+expect_help "in front of the low-poly"+hover "Bake/##bakepad"+expect_help "mipmapped"+hover "Bake/Ambient Occlusion"+expect_help "creases"+hold none+key f3+type "##Command palette" "preview normal"+expect_text "Tool > Bake"+key escape