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.

Material channels in the UI: Texture Map toggles, solo view, Export Maps; brush material; layer channel chips; C picks every channel

islandmeddf2aa7a6eee2
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e5c26a8e..74eccf98 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -241,6 +241,7 @@ if(SCULPTFORGE_BUILD_APP)
src/app/PanelPolypaint.cpp
src/app/PanelPaintLayers.cpp
src/app/PanelTexture.cpp
+ src/app/PanelPbr.cpp
src/app/PanelGeometry.cpp
src/app/Gizmo.cpp
src/app/PanelBake.cpp
diff --git a/src/app/App.cpp b/src/app/App.cpp
index 81e0d5cc..05addaa8 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -1591,6 +1591,8 @@ void App::syncScene() {
SubTool& s = doc_.subtools[size_t(i)];
const bool active = i == doc_.activeSubtool;
Mesh* mesh = active ? &shown : &s.mesh;
+ // Solo channel view (channel_view): the texture's shown texels follow before the renderer uploads them.
+ if (mesh->texture) syncChannelView(*mesh->texture, doc_.channelView);
const bool flat = s.shading == Shading::Flat || (s.shading == Shading::Default && renderSettings_.flatShading);
scene.push_back({s.id, mesh, active, s.visible, bakeNormalMap(s.id, *mesh), renderSettings_.wireframe, flat});
}
diff --git a/src/app/App.h b/src/app/App.h
index fffafe32..bb5569cf 100644
--- a/src/app/App.h
+++ b/src/app/App.h
@@ -499,6 +499,16 @@ private:
bool textureAfterAutoUv_ = false; // the running Auto UV came from New Texture: make the texture after it
int textureSizeChoice_ = 1; // 1K, 2K, 4K, 8K
bool showUvView_ = false; // Texture Map > Show UVs
+ // Material channels (PanelPbr.cpp): channel toggles, the solo channel view and map export under Texture Map,
+ // the brush material (per-channel values and swatches) in the Color palette, and per-channel chips on the
+ // Paint Layers rows (enable, blend, opacity).
+ void drawTextureChannelsContents();
+ void drawBrushMaterialContents();
+ // The channel chips of texture layer `layer` (a row under its name).
+ Args drawLayerChannelChips(int layer); // the texture_layer_channel arguments to run (empty: nothing clicked)
+ void registerPbrUiActions();
+ double pbrTestValue(const std::string& name) const; // UI test values (UiTest.cpp), NaN when not one of ours
+ int exportPresetChoice_ = 0;
// 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();
diff --git a/src/app/Config.cpp b/src/app/Config.cpp
index ac6e7afe..78c8ed41 100644
--- a/src/app/Config.cpp
+++ b/src/app/Config.cpp
@@ -469,6 +469,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)
+ registerPbrUiActions(); // material channels, solo view, map export (PanelPbr.cpp)
registerGizmoActions(); // Q W E R: Draw, and the Gizmo 3D's Move, Scale, Rotate (Gizmo.cpp)
registerBakeUiActions(); // Tool > Bake (PanelBake.cpp)
registerRetopoUiActions(); // Geometry > QRemesh, SubTool > ProjectAll (PanelRetopo.cpp)
diff --git a/src/app/PanelPaintLayers.cpp b/src/app/PanelPaintLayers.cpp
index 64e00b15..6bbae751 100644
--- a/src/app/PanelPaintLayers.cpp
+++ b/src/app/PanelPaintLayers.cpp
@@ -113,7 +113,10 @@ void App::drawPaintLayersContents() {
const float scale = uiScale_;
const ImVec2 cellPad(6.0f * scale, 4.0f * scale);
const float rowH = ImGui::GetTextLineHeight() + 2.0f * cellPad.y;
- const float listH = rowH * float(std::min(count + 1, kPaintListRows)) + 2.0f;
+ // A texture with material channels shows a row of channel chips under each layer (PanelPbr.cpp).
+ const bool chips = texture && hasMaterialChannels(*mesh.texture);
+ Args chipArgs;
+ const float listH = rowH * float(std::min(count * (chips ? 2 : 1) + 1, kPaintListRows + (chips ? 3 : 0))) + 2.0f;
ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, cellPad);
if (ImGui::BeginTable("##paintlayers", 4, ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter,
ImVec2(-1, listH))) {
@@ -206,10 +209,18 @@ void App::drawPaintLayersContents() {
listedPaintLayer_ = i;
}
ImGui::PopID();
+ if (chips && l) {
+ ImGui::TableNextRow(ImGuiTableRowFlags_None, rowH);
+ ImGui::TableSetColumnIndex(0);
+ ImGui::PushID(1000 + i);
+ if (Args a = drawLayerChannelChips(i); !a.empty()) chipArgs = std::move(a);
+ ImGui::PopID();
+ }
}
ImGui::EndTable();
}
ImGui::PopStyleVar();
+ if (!chipArgs.empty()) runCommand("texture_layer_channel", chipArgs);
if (selectIndex >= -1) runCommand("paint_layer_select", indexArg(selectIndex));
if (toggleVisible >= 0)
diff --git a/src/app/PanelPbr.cpp b/src/app/PanelPbr.cpp
new file mode 100644
index 00000000..0a52728f
--- /dev/null
+++ b/src/app/PanelPbr.cpp
@@ -0,0 +1,371 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Material channels in the interface (core/TextureChannels.h, core/MapExport.h):
+// - Tool > Texture Map > Material Channels: which channels the texture has, the solo channel view and Export
+// Maps with a preset;
+// - Color > Material: what the Paint brush lays on each channel (a checkbox, value swatches and a slider per
+// channel) and material swatches that set every channel at once; C over the model picks every channel;
+// - Tool > Paint Layers: a row of channel chips under each texture layer (click: the layer affects that channel
+// or not; right-click: its blend mode and opacity).
+// Every change goes through a command (texture_channels, channel_view, texture_layer_channel, export_maps), so it
+// is undoable where it edits the texture and scriptable; the brush material is a setting (paint_material).
+#include "app/App.h"
+#include "app/Ui.h"
+
+#include "core/MapExport.h"
+#include "core/Polypaint.h"
+#include "core/Texture.h"
+#include "core/TextureChannels.h"
+
+#include <IconsLucide.h>
+#include <SDL3/SDL_dialog.h>
+
+#include <algorithm>
+#include <cmath>
+#include <cstdio>
+#include <filesystem>
+#include <limits>
+#include <mutex>
+#include <string>
+#include <vector>
+
+namespace sf {
+
+namespace {
+
+constexpr const char* kMapPath = "Tool > Texture Map";
+constexpr const char* kColorPath = "Color > Material";
+constexpr const char* kLayersPath = "Tool > Paint Layers";
+
+// Export Maps answers on another thread: the folder waits here for the next frame.
+std::mutex g_exportMutex;
+std::string g_exportFolder;
+void SDLCALL onExportFolder(void*, const char* const* files, int) {
+ wakeMainLoop();
+ if (!files || !files[0]) return;
+ std::lock_guard lock(g_exportMutex);
+ g_exportFolder = files[0];
+}
+
+// Material swatches: every channel at once (our own starting points, not measured materials).
+struct MaterialSwatch {
+ const char* name;
+ glm::vec3 color; // display (sRGB) colour
+ float roughness, metallic;
+};
+const MaterialSwatch kMaterialSwatches[] = {
+ {"Bare metal", {0.78f, 0.78f, 0.80f}, 0.30f, 1.0f}, {"Gold", {1.0f, 0.86f, 0.57f}, 0.25f, 1.0f},
+ {"Copper", {0.98f, 0.73f, 0.62f}, 0.35f, 1.0f}, {"Rust", {0.52f, 0.25f, 0.12f}, 0.85f, 0.15f},
+ {"Gloss paint", {0.75f, 0.10f, 0.08f}, 0.15f, 0.0f}, {"Rubber", {0.08f, 0.08f, 0.08f}, 0.90f, 0.0f},
+ {"Stone", {0.55f, 0.53f, 0.50f}, 0.80f, 0.0f}, {"Clay", {0.72f, 0.55f, 0.45f}, 0.65f, 0.0f},
+};
+
+// The channels a texture can turn on (all but base colour).
+constexpr Channel kMaterialChannels[] = {Channel::Roughness, Channel::Metallic, Channel::Normal, Channel::Height,
+ Channel::AO, Channel::Emissive, Channel::Opacity};
+
+std::string channelHelp(Channel c) {
+ switch (c) {
+ case Channel::Roughness: return "How rough the surface is: 0 is mirror-like, 1 fully matte.";
+ case Channel::Metallic: return "0 for non-metals (paint, stone, skin), 1 for bare metal.";
+ case Channel::Normal: return "Fine surface direction (a tangent-space normal map).";
+ case Channel::Height: return "Painted relief, 0.5 = none. It also adds to the normal (height to normal).";
+ case Channel::AO: return "Ambient occlusion: how much light reaches crevices (1 = open).";
+ case Channel::Emissive: return "Light the surface gives off (black = none).";
+ case Channel::Opacity: return "How opaque the surface is (exported in the base colour's alpha).";
+ default: return "The surface colour (always there).";
+ }
+}
+
+float* materialValue(BrushMaterial& m, Channel c) {
+ switch (c) {
+ case Channel::Roughness: return &m.roughness;
+ case Channel::Metallic: return &m.metallic;
+ case Channel::Height: return &m.height;
+ case Channel::AO: return &m.ao;
+ case Channel::Opacity: return &m.opacity;
+ default: return nullptr;
+ }
+}
+
+} // namespace
+
+void App::registerPbrUiActions() {
+ // (The material channel controls are drawn directly in their palettes; Export Maps is an action so it can sit
+ // on a shelf and in the command palette.)
+ Action a;
+ a.id = "texture.export_maps";
+ a.label = "Export Maps...";
+ a.palette = kMapPath;
+ a.run = [this] { SDL_ShowOpenFolderDialog(onExportFolder, nullptr, window_, nullptr, false); };
+ a.enabled = [this] { return hasTexture(doc_.mesh); };
+ a.description = "Write the texture's channels as image files for an engine or DCC, with the export preset chosen "
+ "beside the button (Unreal, Unity URP / HDRP, glTF, Godot, Blender, USD).";
+ a.command = "export_maps";
+ actions_.add(std::move(a));
+}
+
+void App::drawTextureChannelsContents() {
+ {
+ std::string folder;
+ {
+ std::lock_guard lock(g_exportMutex);
+ folder.swap(g_exportFolder);
+ }
+ const auto& presets = builtinExportPresets();
+ if (!folder.empty() && !presets.empty())
+ runCommand("export_maps", {{"preset", presets[size_t(std::clamp(exportPresetChoice_, 0, int(presets.size()) - 1))].id},
+ {"folder", folder}});
+ }
+ const Mesh& m = doc_.mesh;
+ if (!hasTexture(m)) return;
+ const PaintTexture& t = *m.texture;
+ const ImGuiStyle& style = ImGui::GetStyle();
+ ImGui::SeparatorText("Material Channels");
+ // Channel toggles, two per row.
+ const float half = (ImGui::GetContentRegionAvail().x - style.ItemSpacing.x) * 0.5f;
+ int column = 0;
+ for (Channel c : kMaterialChannels) {
+ bool on = channelEnabled(t, c);
+ if (column) ImGui::SameLine(half + style.ItemSpacing.x + ImGui::GetCursorStartPos().x);
+ const std::string label = std::string(channelFormat(c).label) + "##texchan";
+ if (ImGui::Checkbox(label.c_str(), &on))
+ runCommand("texture_channels", {{on ? "enable" : "disable", std::string(channelFormat(c).id)}});
+ ui::HelpText(channelFormat(c).label, nullptr,
+ (channelHelp(c) + " Turning a channel on costs no memory until it is painted; turning it off "
+ "discards its paint (undoable).")
+ .c_str(),
+ kMapPath);
+ column = 1 - column;
+ }
+ char mem[64];
+ std::snprintf(mem, sizeof mem, "Channel memory %.1f MB", double(channelBytes(t)) / (1024.0 * 1024.0));
+ ImGui::TextDisabled("%s", mem);
+
+ // Solo channel view.
+ const char* viewLabel = doc_.channelView == Channel::BaseColor ? "Normal view" : channelFormat(doc_.channelView).label;
+ ImGui::SetNextItemWidth(-1);
+ if (ImGui::BeginCombo("##channelview", viewLabel)) {
+ if (ImGui::Selectable("Normal view", doc_.channelView == Channel::BaseColor))
+ runCommand("channel_view", {{"channel", std::string("off")}});
+ for (Channel c : kMaterialChannels) {
+ if (!channelEnabled(t, c)) continue;
+ const std::string label = std::string("Solo ") + channelFormat(c).label;
+ if (ImGui::Selectable(label.c_str(), doc_.channelView == c))
+ runCommand("channel_view", {{"channel", std::string(channelFormat(c).id)}});
+ }
+ ImGui::EndCombo();
+ }
+ ui::HelpText("Channel view", nullptr,
+ "Show one channel on its own on the model: roughness, metallic, height, AO and opacity in grey, the "
+ "normal as its colours (with the height's normal in it), emission in colour. Painting updates it live.",
+ kMapPath);
+
+ // Export Maps with a preset.
+ const auto& presets = builtinExportPresets();
+ if (!presets.empty()) {
+ exportPresetChoice_ = std::clamp(exportPresetChoice_, 0, int(presets.size()) - 1);
+ ImGui::SetNextItemWidth(half);
+ if (ImGui::BeginCombo("##exportpreset", presets[size_t(exportPresetChoice_)].label.c_str())) {
+ for (int i = 0; i < int(presets.size()); ++i) {
+ if (ImGui::Selectable(presets[size_t(i)].label.c_str(), i == exportPresetChoice_)) exportPresetChoice_ = i;
+ if (ImGui::IsItemHovered() && !presets[size_t(i)].description.empty())
+ ImGui::SetTooltip("%s", presets[size_t(i)].description.c_str());
+ }
+ ImGui::EndCombo();
+ }
+ ui::HelpText("Export preset", nullptr,
+ "Which files Export Maps writes and how channels are packed. Your own presets are JSON files "
+ "(export_maps preset=path/to/preset.json; see the guide's Export presets page).",
+ kMapPath);
+ ImGui::SameLine();
+ ui::ActionButton(*this, "texture.export_maps", ImVec2(-1, 0));
+ }
+}
+
+void App::drawBrushMaterialContents() {
+ const Mesh& m = doc_.mesh;
+ if (!paintsTexture(m) || !hasMaterialChannels(*m.texture)) return;
+ const PaintTexture& t = *m.texture;
+ const ImGuiStyle& style = ImGui::GetStyle();
+ ImGui::SeparatorText("Material");
+ BrushMaterial& mat = doc_.paintMaterial;
+ // Material swatches: every enabled channel at once.
+ const float sw = (ImGui::GetContentRegionAvail().x - 3 * style.ItemSpacing.x) / 4.0f;
+ for (int i = 0; i < int(std::size(kMaterialSwatches)); ++i) {
+ const MaterialSwatch& s = kMaterialSwatches[i];
+ ImGui::PushID(i);
+ // The swatch shows colour; a metal's darker rim hints at its metalness, a rough one is flatter.
+ if (ImGui::ColorButton("##matswatch", ImVec4(s.color.x, s.color.y, s.color.z, 1.0f), ImGuiColorEditFlags_NoTooltip,
+ ImVec2(sw, sw * 0.45f))) {
+ doc_.paintColor = srgbToLinear(s.color);
+ mat.roughness = s.roughness;
+ mat.metallic = s.metallic;
+ mat.channels |= channelBit(Channel::BaseColor) | channelBit(Channel::Roughness) | channelBit(Channel::Metallic);
+ status_ = std::string("Brush material: ") + s.name;
+ }
+ if (ImGui::IsItemHovered())
+ ImGui::SetTooltip("%s: roughness %.2f, metallic %.0f", s.name, double(s.roughness), double(s.metallic));
+ ImGui::PopID();
+ if (i % 4 != 3) ImGui::SameLine();
+ }
+ ui::HelpText("Material swatches", nullptr,
+ "Set the brush's colour, roughness and metallic at once, and make it paint those channels.", kColorPath);
+
+ // One row per channel the texture has: whether the brush paints it, quick values, and the value.
+ for (int ci = 0; ci < kChannelCount; ++ci) {
+ const Channel c = Channel(ci);
+ if (!channelEnabled(t, c)) continue;
+ ImGui::PushID(ci);
+ bool paints = (mat.channels & channelBit(c)) != 0;
+ if (ImGui::Checkbox(channelFormat(c).label, &paints)) mat.channels ^= channelBit(c);
+ ui::HelpText(channelFormat(c).label, nullptr,
+ (channelHelp(c) + " Ticked: the Paint brush paints this channel. C over the model picks every "
+ "channel at once.")
+ .c_str(),
+ kColorPath);
+ if (float* v = materialValue(mat, c)) {
+ // Five grey swatches (0, 25, 50, 75, 100%) right-aligned on the checkbox's line, then the slider.
+ const float box = ImGui::GetFrameHeight() * 0.8f;
+ ImGui::SameLine(ImGui::GetContentRegionMax().x - 5 * box - 4 * style.ItemInnerSpacing.x);
+ for (int k = 0; k < 5; ++k) {
+ const float g = float(k) / 4.0f;
+ ImGui::PushID(k);
+ if (ImGui::ColorButton("##val", ImVec4(g, g, g, 1.0f), ImGuiColorEditFlags_NoTooltip, ImVec2(box, box))) {
+ *v = g;
+ mat.channels |= channelBit(c);
+ }
+ ImGui::PopID();
+ if (k < 4) ImGui::SameLine(0, style.ItemInnerSpacing.x);
+ }
+ ImGui::SetNextItemWidth(-1);
+ const std::string fmt = std::string(channelFormat(c).label) + " %.2f";
+ if (ui::SliderInside("##value", v, 0.0f, 1.0f, fmt.c_str())) mat.channels |= channelBit(c);
+ } else if (c == Channel::BaseColor) {
+ ImGui::SameLine();
+ const glm::vec3 s = linearToSrgb(doc_.paintColor);
+ ImGui::ColorButton("##basecolor", ImVec4(s.x, s.y, s.z, 1.0f), ImGuiColorEditFlags_NoTooltip,
+ ImVec2(ImGui::GetContentRegionAvail().x, ImGui::GetFrameHeight()));
+ } else if (c == Channel::Emissive) {
+ ImGui::SameLine();
+ glm::vec3 s = linearToSrgb(mat.emissive);
+ if (ImGui::ColorEdit3("##emissive", &s.x, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel)) {
+ mat.emissive = srgbToLinear(s);
+ mat.channels |= channelBit(c);
+ }
+ } else if (c == Channel::Normal) {
+ // The direction as two tilts (x, y); 0, 0 = flat.
+ float tilt[2] = {mat.normal.x, mat.normal.y};
+ ImGui::SetNextItemWidth(-1);
+ if (ImGui::SliderFloat2("##tilt", tilt, -0.9f, 0.9f, "%.2f")) {
+ const float z = std::sqrt(std::max(0.05f, 1.0f - tilt[0] * tilt[0] - tilt[1] * tilt[1]));
+ mat.normal = glm::normalize(glm::vec3(tilt[0], tilt[1], z));
+ mat.channels |= channelBit(c);
+ }
+ ui::HelpText("Normal tilt", nullptr, "Which way painted normals lean (x, y); 0, 0 paints a flat normal.", kColorPath);
+ }
+ ImGui::PopID();
+ }
+ mat.color = doc_.paintColor;
+}
+
+Args App::drawLayerChannelChips(int layer) {
+ Args run;
+ const Mesh& m = doc_.mesh;
+ if (!hasTexture(m) || layer < 0 || layer >= int(m.texture->layers.size())) return run;
+ const PaintTexture& t = *m.texture;
+ const TextureLayer& l = t.layers[size_t(layer)];
+ const ImGuiStyle& style = ImGui::GetStyle();
+ ImGui::Indent(ImGui::GetTextLineHeight());
+ bool first = true;
+ for (int ci = 0; ci < kChannelCount; ++ci) {
+ const Channel c = Channel(ci);
+ if (!channelEnabled(t, c)) continue;
+ const LayerChannel& lc = l.channels[size_t(ci)];
+ if (!first) ImGui::SameLine(0, style.ItemInnerSpacing.x);
+ first = false;
+ ImGui::PushID(ci);
+ const bool on = lc.enabled;
+ const uint8_t blend = layerChannelBlend(t, layer, c);
+ const float opacity = layerChannelOpacity(t, layer, c);
+ ImGui::PushStyleColor(ImGuiCol_Button, on ? style.Colors[ImGuiCol_ButtonActive] : style.Colors[ImGuiCol_FrameBg]);
+ ImGui::PushStyleColor(ImGuiCol_Text, on ? style.Colors[ImGuiCol_Text] : style.Colors[ImGuiCol_TextDisabled]);
+ ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(style.FramePadding.x * 0.6f, 0.0f));
+ // A dot after the letters marks a channel that doesn't use Normal blending at full opacity.
+ const std::string chip = std::string(channelFormat(c).shortLabel) +
+ ((blend != 0 || opacity < 1.0f) && ci > 0 ? "*" : "") + "##chip";
+ if (ImGui::SmallButton(chip.c_str()))
+ run = {{"index", double(layer)}, {"channel", std::string(channelFormat(c).id)}, {"enabled", !on}};
+ ImGui::PopStyleVar();
+ ImGui::PopStyleColor(2);
+ if (ImGui::IsItemHovered() && !ui::CtrlHeld())
+ ImGui::SetTooltip("%s: %s, %s, %d%%\nClick: on / off. Right-click: blend and opacity.", channelFormat(c).label,
+ on ? "on" : "off", channelBlendLabel(blend), int(std::lround(opacity * 100.0f)));
+ ui::HelpText("Layer channel", nullptr,
+ "Whether this layer affects the channel. Right-click for the channel's blend mode (Normal "
+ "Combine lays normal detail onto the normals below) and opacity.",
+ kLayersPath);
+ if (ImGui::BeginPopupContextItem("##chipmenu")) {
+ ImGui::TextDisabled("%s on '%s'", channelFormat(c).label, l.name.c_str());
+ const int modes = c == Channel::Normal ? kChannelBlendCount : kBlendModeCount;
+ for (int b = 0; b < modes; ++b)
+ if (ImGui::MenuItem(channelBlendLabel(uint8_t(b)), nullptr, b == blend) && b != blend)
+ run = {{"index", double(layer)}, {"channel", std::string(channelFormat(c).id)},
+ {"blend", std::string(channelBlendId(uint8_t(b)))}};
+ ImGui::Separator();
+ float pct = opacity * 100.0f;
+ ImGui::SetNextItemWidth(ImGui::GetFontSize() * 10.0f);
+ ImGui::SliderFloat("##chipopacity", &pct, 0.0f, 100.0f, "Opacity %.0f%%");
+ if (ImGui::IsItemDeactivatedAfterEdit())
+ run = {{"index", double(layer)}, {"channel", std::string(channelFormat(c).id)}, {"opacity", double(pct / 100.0f)}};
+ ImGui::EndPopup();
+ }
+ ImGui::PopID();
+ }
+ ImGui::Unindent(ImGui::GetTextLineHeight());
+ return run;
+}
+
+double App::pbrTestValue(const std::string& name) const {
+ const Mesh& m = doc_.mesh;
+ const bool tex = hasTexture(m);
+ if (name == "material_channels") {
+ if (!tex) return 0.0;
+ double n = 0;
+ for (Channel c : kMaterialChannels) n += channelEnabled(*m.texture, c);
+ return n;
+ }
+ if (name == "channel_view") return double(int(doc_.channelView));
+ if (name == "brush_channels") return double(doc_.paintMaterial.channels);
+ if (name == "roughness_painted") {
+ if (!tex || !channelEnabled(*m.texture, Channel::Roughness)) return 0.0;
+ const TileImage& img = channelImage(*m.texture, Channel::Roughness);
+ const float def = channelFormat(Channel::Roughness).defaultValue[0];
+ double n = 0;
+ for (size_t ti = 0; ti < img.tileCount(); ++ti) {
+ if (!img.tile(ti)) continue;
+ const int x0 = int(ti % size_t(img.tilesX())) * kTileImageTile, y0 = int(ti / size_t(img.tilesX())) * kTileImageTile;
+ for (int y = y0; y < std::min(y0 + kTileImageTile, img.height()); ++y)
+ for (int x = x0; x < std::min(x0 + kTileImageTile, img.width()); ++x) n += std::abs(img.value(x, y) - def) > 0.02f;
+ }
+ return n;
+ }
+ if (name == "shown_mean_r") {
+ if (!tex) return 0.0;
+ const std::vector<uint8_t>& px = m.texture->shown();
+ double sum = 0;
+ for (size_t i = 0; i < px.size(); i += 4) sum += px[i];
+ return px.empty() ? 0.0 : sum / double(px.size() / 4);
+ }
+ if (name.rfind("layer_channel_", 0) == 0) {
+ Channel c;
+ if (!tex || !parseChannel(name.substr(14), c)) return 0.0;
+ const PaintTexture& t = *m.texture;
+ if (t.activeLayer < 0 || t.activeLayer >= int(t.layers.size())) return 0.0;
+ return t.layers[size_t(t.activeLayer)].channels[size_t(c)].enabled ? 1.0 : 0.0;
+ }
+ return std::numeric_limits<double>::quiet_NaN();
+}
+
+} // namespace sf
diff --git a/src/app/PanelPolypaint.cpp b/src/app/PanelPolypaint.cpp
index ca1b12f0..4a79844e 100644
--- a/src/app/PanelPolypaint.cpp
+++ b/src/app/PanelPolypaint.cpp
@@ -232,7 +232,11 @@ const BrushRecipe* App::strokeRecipe() const {
void App::paintAt(const DabParams& dab) {
DabParams p = dab;
p.strength = rgbIntensity_ * dabStrengthScale_; // paint has its own strength (ZBrush RGB Intensity)
- if (texturePainter_.active()) texturePainter_.dab(doc_.mesh, doc_.bvh, p, doc_.paintColor, symmetry_);
+ if (texturePainter_.active()) { // the brush material: colour plus the material channels it paints
+ BrushMaterial m = doc_.paintMaterial;
+ m.color = doc_.paintColor;
+ texturePainter_.dab(doc_.mesh, doc_.bvh, p, m, symmetry_);
+ }
else if (!paintsTexture(doc_.mesh)) paintDab(doc_.sculptor, doc_.mesh, doc_.bvh, p, doc_.paintColor, symmetry_);
}
@@ -240,9 +244,11 @@ void App::beginPaintStroke() {
paintSnapshot_.reset();
const Mesh& m = doc_.mesh;
if (strokePaint_ && paintsTexture(m)) { // texture painting: tiles go into the stroke's undo step
- if (!texturePainter_.begin(doc_.mesh)) {
+ if (!texturePainter_.begin(doc_.mesh, doc_.paintMaterial.channels)) {
const PaintTexture& t = *m.texture;
- if (t.activeLayer >= 0 && t.activeLayer < int(t.layers.size()))
+ if (!(doc_.paintMaterial.channels & enabledChannelMask(t)))
+ status_ = "The brush paints no channel this texture has (Color > Material)";
+ else if (t.activeLayer >= 0 && t.activeLayer < int(t.layers.size()))
status_ = "Texture layer '" + t.layers[size_t(t.activeLayer)].name + "' is " +
(t.layers[size_t(t.activeLayer)].locked ? "locked" : "hidden");
}
@@ -272,6 +278,19 @@ void App::pickColorUnderCursor() {
if (!pick(mouseX_, mouseY_, hit)) return;
if (paintsTexture(doc_.mesh)) {
doc_.paintColor = sampleTextureAt(doc_.mesh, hit.triangle, hit.position);
+ if (hasMaterialChannels(*doc_.mesh.texture)) {
+ // C picks every channel: the brush material becomes the texture's material under the cursor, and the
+ // brush then paints all of the texture's channels.
+ BrushMaterial picked = doc_.paintMaterial;
+ sampleMaterialAt(doc_.mesh, hit.triangle, hit.position, picked);
+ doc_.paintMaterial = picked;
+ doc_.paintMaterial.color = doc_.paintColor;
+ char buf[160];
+ std::snprintf(buf, sizeof buf, "Picked the material: %s, roughness %.2f, metallic %.2f (every channel)",
+ colorToHex(doc_.paintColor).c_str(), double(picked.roughness), double(picked.metallic));
+ status_ = buf;
+ return;
+ }
status_ = "Paint colour " + colorToHex(doc_.paintColor) + " (from the texture)";
return;
}
@@ -315,7 +334,8 @@ void App::drawPalette_Color() {
ImGui::PopID();
if (i % 5 != 4) ImGui::SameLine();
}
- ImGui::TextDisabled("C over the model picks its colour");
+ ImGui::TextDisabled(paintsTexture(doc_.mesh) && hasMaterialChannels(*doc_.mesh.texture) ? "C over the model picks its material" : "C over the model picks its colour");
+ drawBrushMaterialContents(); // per-channel brush values when the texture has material channels (PanelPbr.cpp)
{
const Mesh& m = doc_.mesh;
const bool layered = !m.paintLayers.empty() && m.activePaintLayer >= 0 && m.activePaintLayer < int(m.paintLayers.size());
diff --git a/src/app/PanelTexture.cpp b/src/app/PanelTexture.cpp
index a16115cf..d94dcabc 100644
--- a/src/app/PanelTexture.cpp
+++ b/src/app/PanelTexture.cpp
@@ -331,6 +331,7 @@ void App::drawTextureMapContents() {
"the texture fills in here.",
kPath);
}
+ drawTextureChannelsContents(); // Material Channels: toggles, solo view, Export Maps (PanelPbr.cpp)
}
} // namespace sf
diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cpp
index 41adc74d..a4003f06 100644
--- a/src/app/UiTest.cpp
+++ b/src/app/UiTest.cpp
@@ -80,6 +80,11 @@
// painting goes into the texture), texture_layers, active_texture_layer,
// 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),
+// material_channels (material channels on the texture, base colour not counted),
+// channel_view (0 = colour, else the solo channel: 1 roughness ... 7 opacity),
+// brush_channels (bit mask of what the brush paints), roughness_painted (texels whose
+// roughness differs from the default), shown_mean_r (mean red the viewport is sent),
+// layer_channel_<id> (1 when the active texture layer has channel <id> on, e.g. roughness)
// auto_uv_running, qremesh_running (QRemesh working in the background),
// qremesh_guides (guide curves on the active SubTool), qremesh_guide_loops (closed ones),
// guides_mode (1 while the QRemesh Guides brush is picked),
@@ -351,6 +356,7 @@ double Demo::query(const std::string& name, bool& known) const {
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 (const double v = app_.pbrTestValue(name); !std::isnan(v)) return v; // material_channels, channel_view, ...
if (const double v = app_.splatTrainTestValue(name); !std::isnan(v)) return v; // splat_training, ...
if (const double v = app_.sculptableTestValue(name); !std::isnan(v)) return v;
if (name == "qremesh_running") return app_.qremesh_ ? 1.0 : 0.0;
diff --git a/src/core/EnvironmentMath.h b/src/core/EnvironmentMath.h
new file mode 100644
index 00000000..305c1fcd
--- /dev/null
+++ b/src/core/EnvironmentMath.h
@@ -0,0 +1,15 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Environment maths for the PBR viewport (PBR texturing phase 1B; scaffolding only). Will hold the CPU side:
+// SH9 irradiance, a split-sum BRDF lookup table, procedural environments and the SCULPTFORGE_HDRI_PATH library.
+// The GPU prefilter (render/Environment) and the renderer's PBR shader bit wait for the renderer's upload
+// rewrite; until then the viewport shows material channels one at a time (channel_view).
+#pragma once
+
+namespace sf {
+
+constexpr int kBrdfLutSize = 64;
+constexpr const char* kHdriPathVariable = "SCULPTFORGE_HDRI_PATH";
+
+} // namespace sf
diff --git a/src/core/Generators.h b/src/core/Generators.h
new file mode 100644
index 00000000..f2168591
--- /dev/null
+++ b/src/core/Generators.h
@@ -0,0 +1,14 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Mask generators (PBR texturing phase 2C; scaffolding only). Will compute masks for Fill layers from mesh maps:
+// Edge Wear, Dirt, Gradient, Thickness and Light Direction.
+#pragma once
+
+#include <cstdint>
+
+namespace sf {
+
+enum class GeneratorKind : uint8_t { EdgeWear, Dirt, Gradient, Thickness, LightDirection };
+
+} // namespace sf
diff --git a/src/core/LayerStack.h b/src/core/LayerStack.h
new file mode 100644
index 00000000..e0761f5e
--- /dev/null
+++ b/src/core/LayerStack.h
@@ -0,0 +1,18 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Layer stack (PBR texturing phase 2B; scaffolding only). Will add layer kinds beyond paint layers (Fill layer,
+// Folder, Filter) and mask effects (paint, fill, generator, filter, geometry, levels, invert) over the material
+// channels of core/TextureChannels.h, evaluated tile by tile with a cache, previewing at 1/4 resolution during
+// slider drags. Paint layers with per-channel blend and opacity exist today (TextureLayer::channels); project IO
+// for the stack goes through core/LayerStackIO.h.
+#pragma once
+
+#include <cstdint>
+
+namespace sf {
+
+enum class LayerKind : uint8_t { Paint, Fill, Folder, Filter };
+enum class MaskEffectKind : uint8_t { Paint, Fill, Generator, Filter, Geometry, Levels, Invert };
+
+} // namespace sf
diff --git a/src/core/MaterialRecipe.h b/src/core/MaterialRecipe.h
new file mode 100644
index 00000000..303073de
--- /dev/null
+++ b/src/core/MaterialRecipe.h
@@ -0,0 +1,13 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Material Recipes and Mask Recipes (PBR texturing phase 3; scaffolding only). Our own .sfmaterial files (ZIP +
+// JSON) describing a layer stack, with original procedural recipes generated in code for a LightBox "Surfaces"
+// tab. Today a brush material (core/TextureChannels.h BrushMaterial, paint_material) is the closest thing.
+#pragma once
+
+namespace sf {
+
+constexpr const char* kMaterialRecipeExtension = ".sfmaterial";
+
+} // namespace sf
diff --git a/src/core/MeshMaps.h b/src/core/MeshMaps.h
new file mode 100644
index 00000000..82b8ad51
--- /dev/null
+++ b/src/core/MeshMaps.h
@@ -0,0 +1,16 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Mesh Maps (PBR texturing phase 2A; scaffolding only). Will bake the texture-space maps the layer stack reads:
+// curvature, position, thickness, ID, world normal and bent normal, from the mesh itself, from the SubTool's own
+// top subdivision level, from a name-matched high-poly SubTool (_high / _low) or through a cage. Maps will be
+// fingerprinted (mesh, UVs, settings) so stale ones are detected after sculpting. Today's bakes: core/Bake.h.
+#pragma once
+
+#include <cstdint>
+
+namespace sf {
+
+enum class MeshMapKind : uint8_t { Curvature, Position, Thickness, Id, WorldNormal, BentNormal };
+
+} // namespace sf
diff --git a/src/core/Procedural.h b/src/core/Procedural.h
new file mode 100644
index 00000000..a4d899ef
--- /dev/null
+++ b/src/core/Procedural.h
@@ -0,0 +1,14 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Procedural textures (PBR texturing phase 2C; scaffolding only). Noise will be evaluated in object space
+// through the position mesh map (core/MeshMaps.h), so it is seamless across UV seams.
+#pragma once
+
+#include <cstdint>
+
+namespace sf {
+
+enum class NoiseKind : uint8_t { Value, Gradient, Cellular, Fractal };
+
+} // namespace sf
diff --git a/src/core/TextureFilters.h b/src/core/TextureFilters.h
new file mode 100644
index 00000000..ab820088
--- /dev/null
+++ b/src/core/TextureFilters.h
@@ -0,0 +1,14 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Texture filters (PBR texturing phase 2D; scaffolding only). Will run island-aware blur, sharpen, HSL, levels
+// and warp over a TileImage (core/TileImage.h) without bleeding across UV seams.
+#pragma once
+
+#include <cstdint>
+
+namespace sf {
+
+enum class TextureFilterKind : uint8_t { Blur, Sharpen, Hsl, Levels, Warp };
+
+} // namespace sf
diff --git a/src/core/TextureProjection.h b/src/core/TextureProjection.h
new file mode 100644
index 00000000..2fc8ae04
--- /dev/null
+++ b/src/core/TextureProjection.h
@@ -0,0 +1,14 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Texture projection (PBR texturing phase 3; scaffolding only). Will fill material channels by triplanar,
+// planar, spherical or cylindrical projection, and paint through screen stencils and clone sources.
+#pragma once
+
+#include <cstdint>
+
+namespace sf {
+
+enum class ProjectionKind : uint8_t { Triplanar, Planar, Spherical, Cylindrical };
+
+} // namespace sf
diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp
index a19b422e..c4a30ead 100644
--- a/src/render/Renderer.cpp
+++ b/src/render/Renderer.cpp
@@ -1787,7 +1787,7 @@ void Renderer::syncTexture(MeshSlot& s, Mesh& mesh) {
if (!s.paintTexture) gl::GenTextures(1, &s.paintTexture);
gl::BindTexture(GL_TEXTURE_2D, s.paintTexture);
gl::TexImage2D(GL_TEXTURE_2D, 0, GLint(GL_SRGB8_ALPHA8), tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
- tex->pixels.data());
+ tex->shown().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));
@@ -1802,7 +1802,7 @@ void Renderer::syncTexture(MeshSlot& s, Mesh& mesh) {
auto upload = [&](int x0, int y0, int x1, int y1) {
x1 = std::min(x1, tex->width), y1 = std::min(y1, tex->height);
gl::TexSubImage2D(GL_TEXTURE_2D, 0, x0, y0, x1 - x0, y1 - y0, GL_RGBA, GL_UNSIGNED_BYTE,
- tex->pixels.data() + (size_t(y0) * size_t(tex->width) + size_t(x0)) * 4);
+ tex->shown().data() + (size_t(y0) * size_t(tex->width) + size_t(x0)) * 4);
};
if (!rect) {
upload(0, 0, tex->width, tex->height);
diff --git a/tests/ui/pbr_channels.sfui b/tests/ui/pbr_channels.sfui
new file mode 100644
index 00000000..da938920
--- /dev/null
+++ b/tests/ui/pbr_channels.sfui
@@ -0,0 +1,79 @@
+# Material channels (PBR texture painting): Texture Map turns channels on, the Color palette's Material section
+# sets what the brush paints per channel, Paint Layers rows get channel chips, C picks every channel, and the
+# view can show one channel on its own.
+run new_sphere subdivisions=4
+run uv_auto resolution=1024
+run texture_create size=1024
+expect_value texture_size == 1024
+expect_value material_channels == 0
+
+# Tool > Texture Map > Material Channels: two clicks turn on roughness and metallic.
+click "Texture Map##tool.texture"
+click "Texture Map/Roughness##texchan"
+click "Texture Map/Metallic##texchan"
+expect_value material_channels == 2
+run texture_channels enable=normal,height
+expect_value material_channels == 4
+expect_text "Channel memory"
+
+# The brush material: glossy metal on Base.
+run paint_material color=c8c8d0 roughness=0.1 metallic=1
+expect_value brush_channels == 7
+key p
+stroke 0.15 0.5 0.85 0.5
+expect_value roughness_painted > 3000
+remember roughness_painted base_stroke
+
+# Color > Material shows a row per channel with swatches.
+click "Color##palette.color"
+expect "Polypaint & Materials/Roughness"
+wait 0.3
+shot brush-material
+
+# A layer: chips under its row; a rough stroke on roughness only.
+key escape
+click "Paint Layers##tool.paint_layers"
+click "Paint Layers/New"
+expect_value texture_layers == 1
+expect "Paint Layers/R##chip"
+run paint_material channels=roughness roughness=0.95
+stroke 0.5 0.15 0.5 0.85
+expect_value roughness_painted > $base_stroke
+remember roughness_painted with_layer
+shot layer-rows
+
+# Clicking the R chip turns the layer's roughness off (undoable); the composite goes back.
+click "Paint Layers/R##chip"
+expect_value layer_channel_roughness == 0
+expect_value roughness_painted == $base_stroke
+key ctrl+z
+expect_value layer_channel_roughness == 1
+expect_value roughness_painted == $with_layer
+
+# Solo roughness view: the viewport is sent the roughness as grey (the default 0.65 is ~166).
+run channel_view channel=roughness
+expect_value channel_view == 1
+wait 0.2
+expect_value shown_mean_r > 120
+expect_value shown_mean_r < 200
+shot solo-roughness
+run channel_view channel=off
+expect_value channel_view == 0
+
+# Save and reopen: format 7 brings the channels and the layer chips back.
+run save_project path=sf_ui_pbr_channels.sfproj
+run new_sphere subdivisions=2
+expect_value material_channels == 0
+run open path=sf_ui_pbr_channels.sfproj
+expect_value material_channels == 4
+expect_value texture_layers == 1
+expect_value roughness_painted == $with_layer
+
+# Ctrl-hover help on the new controls.
+click "Texture Map##tool.texture"
+hold ctrl
+hover "Texture Map/Roughness##texchan"
+expect_help "rough"
+hover "Texture Map/##channelview"
+expect_help "own"
+hold none