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.

Merge feat/polypaint: polypaint and MatCap materials

islandmed15b563064694
diff --cc CMakeLists.txt
index 8cf0a580,459115fd..d334da0a
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@@ -42,9 -41,8 +42,11 @@@ add_library(sculptforge_core STATI
src/core/Splats.cpp
src/core/Voxel.cpp
src/core/EnginePresets.cpp
+ src/core/Masking.cpp
+ src/core/BrushAlpha.cpp
+ src/core/Stroke.cpp
+ src/core/Polypaint.cpp
+ src/core/Matcap.cpp
src/io/ObjIO.cpp
src/io/FbxIO.cpp
third_party/ufbx/ufbx.c
@@@ -107,9 -105,7 +109,10 @@@ if(SCULPTFORGE_BUILD_APP
src/app/main.cpp
src/app/App.cpp
src/app/Demo.cpp
+ src/app/PanelMasking.cpp
+ src/app/PanelPolygroups.cpp
+ src/app/PanelBrushes.cpp
+ src/app/PanelPolypaint.cpp
src/render/GL.cpp
src/render/Camera.cpp
src/render/Renderer.cpp
diff --cc src/app/App.cpp
index 33e2da15,8c177128..63029db9
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@@ -531,10 -521,16 +532,20 @@@ void App::onKey(SDL_Keycode key, SDL_Ke
case SDLK_W: renderSettings_.wireframe = !renderSettings_.wireframe; break;
case SDLK_F: camera_.frame(sceneBounds()); break;
case SDLK_F1: showHelp_ = !showHelp_; break;
+ case SDLK_P:
+ paintMode_ = !paintMode_;
+ if (paintMode_) showPolypaintPanel_ = true; // colour picker at hand
+ break;
+ case SDLK_C: pickColorUnderCursor(); break;
default:
- if (key >= SDLK_1 && key < SDLK_1 + int(BrushType::Count)) {
+ // 1-9 pick the first nine brushes, 0 the tenth.
- if (key >= SDLK_1 && key <= SDLK_9) brush_ = BrushType(key - SDLK_1);
- else if (key == SDLK_0) brush_ = BrushType(9);
++ if (key >= SDLK_1 && key <= SDLK_9) {
+ brush_ = BrushType(key - SDLK_1);
+ paintMode_ = false;
++ } else if (key == SDLK_0) {
++ brush_ = BrushType(9);
++ paintMode_ = false;
+ }
break;
}
}
@@@ -573,16 -575,6 +590,16 @@@ void App::beginStroke(float x, float y
grabPlanePoint_ = grabLast_ = hit.position;
return;
}
+ lazyMouse_.reset(glm::vec2(x, y));
- if (strokeBrush_ == BrushType::SnakeHook && !recipe) {
++ if (strokeBrush_ == BrushType::SnakeHook && !recipe && strokeSculpt_) {
+ grabPlanePoint_ = grabLast_ = hit.position; // the pulled tip (see snakeHookTo)
+ return;
+ }
+ if (strokeType_ == StrokeType::DragRect) {
+ dragAnchor_ = glm::vec2(x, y);
+ dragAnchorHit_ = hit.position;
+ return; // the stamp appears once the drag gives it a size
+ }
dabAt(x, y);
}
@@@ -602,19 -594,6 +619,19 @@@ void App::strokeTo(float x, float y)
grabLast_ = p;
return;
}
- if (strokeBrush_ == BrushType::SnakeHook && !recipe) {
++ if (strokeBrush_ == BrushType::SnakeHook && !recipe && strokeSculpt_) {
+ snakeHookTo(x, y);
+ return;
+ }
+ if (strokeType_ == StrokeType::DragRect && !strokeMask_) { // mask strokes paint dots
+ dragRectTo(x, y);
+ return;
+ }
+ if (strokeType_ == StrokeType::LazyMouse) {
+ const glm::vec2 lazy = lazyMouse_.follow(glm::vec2(x, y));
+ x = lazy.x;
+ y = lazy.y;
+ }
// Place dabs at even screen-space spacing along the cursor path.
const float spacing = std::max(1.0f, radiusPx_ * (recipe ? recipe->spacing : 0.15f));
@@@ -669,17 -638,12 +686,19 @@@ void App::dabAt(float x, float y)
p.pressure = pressureStrength_ ? pressure : 1.0f;
p.invert = strokeInvert_;
p.frontFacesOnly = frontFacesOnly_;
+ if (strokeMask_) {
+ p.strength = maskStrength_;
+ doc_.sculptor.dabMask(doc_.mesh, doc_.bvh, p, symmetry_);
+ return;
+ }
+ p.alpha = &doc_.brushAlpha;
+ p.alphaUp = screenUpVector(dabAngle_);
+ p.strokeDelta = haveLastDab_ ? hit.position - lastDabHit_ : glm::vec3(0.0f);
+ lastDabHit_ = hit.position;
+ haveLastDab_ = true;
+ if (strokePaint_) paintAt(p);
+ if (!strokeSculpt_) return;
if (recipe) {
- p.strokeDelta = haveLastDab_ ? hit.position - lastDabHit_ : glm::vec3(0.0f);
- lastDabHit_ = hit.position;
- haveLastDab_ = true;
doc_.sculptor.dabRecipe(doc_.mesh, doc_.bvh, *recipe, p, symmetry_);
return;
}
@@@ -687,8 -651,8 +706,9 @@@
}
void App::endStroke() {
+ strokeMask_ = false;
UndoRecord r = doc_.sculptor.endStroke();
+ finishPaintStroke(r);
if (!r.empty()) {
doc_.undo.push(std::move(r));
doc_.modified = true;
@@@ -921,17 -886,11 +943,19 @@@ void App::drawUi()
"Smooth Hold Shift while sculpting\n"
"Rotate Left drag on background / right drag\n"
"Pan Alt + left drag background / middle drag\n"
- "Zoom Mouse wheel / Ctrl + left drag background\n"
+ "Zoom Mouse wheel / Ctrl + middle drag\n"
+ " (Ctrl + left drag background when nothing is masked)\n"
+ "Paint mask Hold Ctrl while stroking (Ctrl+Alt erases)\n"
+ "Invert mask Ctrl + click background\n"
+ "Clear mask Ctrl + drag background\n"
+ "Isolate polygroup Ctrl+Shift+click (again: invert)\n"
+ "Hide polygroup Ctrl+Shift+Alt+click\n"
+ "Show all Ctrl+Shift+click background\n"
+ "Hide outside rect Ctrl+Shift+drag (Alt: inside)\n"
"Brush size [ and ]\n"
- "Brushes 1-8\n"
+ "Brushes 1-9, 0\n"
+ "Paint brush P (colour only; Alt paints white)\n"
+ "Pick colour C over the model\n"
"Symmetry X X\n"
"Wireframe W\n"
"Frame model F\n"
@@@ -1646,11 -1606,9 +1671,11 @@@ void App::drawToolPanel()
const float bw = (ImGui::GetContentRegionAvail().x - ImGui::GetStyle().ItemSpacing.x) * 0.5f;
for (int i = 0; i < int(BrushType::Count); ++i) {
const auto type = BrushType(i);
- const bool selected = brush_ == type && doc_.activeBrush < 0;
+ const bool selected = brush_ == type && doc_.activeBrush < 0 && !paintMode_;
if (selected) ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive));
- std::string label = std::to_string(i + 1) + " " + brushName(type);
+ // Hotkeys 1-9 and 0 cover the first ten brushes.
+ std::string label = (i < 9 ? std::to_string(i + 1) : i == 9 ? std::string("0") : std::string(" ")) + " " +
+ brushName(type);
if (ImGui::Button(label.c_str(), ImVec2(bw, 0))) {
brush_ = type;
doc_.activeBrush = -1;
@@@ -1773,20 -1733,15 +1804,24 @@@ void App::drawBrushCursor()
const float r = radiusPx_ * (pressureSize_ && lastInputPen_ ? std::max(penPressure_, 0.05f) : 1.0f);
const ImVec2 c(mouseX_, mouseY_);
const bool inverted = invertToggle_ != ((SDL_GetModState() & SDL_KMOD_ALT) != 0);
- ImU32 col = activeBrush() == BrushType::Smooth ? IM_COL32(120, 200, 255, 200)
+ const SDL_Keymod mod = SDL_GetModState();
+ const bool masking = drag_ == Drag::Stroke ? strokeMask_ : ctrlDown(mod) && !(mod & SDL_KMOD_SHIFT);
- const ImU32 col = masking ? IM_COL32(230, 230, 230, 200)
- : activeBrush() == BrushType::Smooth ? IM_COL32(120, 200, 255, 200)
- : inverted ? IM_COL32(255, 110, 110, 200)
- : IM_COL32(255, 190, 120, 200);
++ ImU32 col = masking ? IM_COL32(230, 230, 230, 200)
++ : activeBrush() == BrushType::Smooth ? IM_COL32(120, 200, 255, 200)
+ : inverted ? IM_COL32(255, 110, 110, 200)
+ : IM_COL32(255, 190, 120, 200);
- if (paintMode_ && activeBrush() != BrushType::Smooth) { // Paint brush: ring in the paint colour
++ if (!masking && paintMode_ && activeBrush() != BrushType::Smooth) { // Paint brush: ring in the paint colour
+ const glm::vec3 c = linearToSrgb(inverted ? glm::vec3(1.0f) : doc_.paintColor);
+ col = ImGui::GetColorU32(ImVec4(c.x, c.y, c.z, 0.9f));
+ }
dl->AddCircle(c, r, col, 64, 1.5f);
dl->AddCircle(c, r * (0.25f + 0.75f * (1.0f - strength_)), IM_COL32(255, 255, 255, 60), 48, 1.0f);
+ if (strokeType_ == StrokeType::LazyMouse && drag_ == Drag::Stroke) {
+ // The string from the cursor to the lagging brush point.
+ const ImVec2 lp(lazyMouse_.point.x, lazyMouse_.point.y);
+ dl->AddLine(c, lp, IM_COL32(255, 255, 255, 120), 1.0f);
+ dl->AddCircleFilled(lp, 3.0f, col);
+ }
}
} // namespace sf
diff --cc src/app/App.h
index be19435a,20713545..82a2c08f
--- a/src/app/App.h
+++ b/src/app/App.h
@@@ -231,43 -230,20 +231,57 @@@ private
std::mutex pendingMutex_;
std::vector<PendingFile> pendingFiles_;
+ // Masking (PanelMasking.cpp): Ctrl strokes paint mask, Ctrl+Alt erases;
+ // Ctrl+click on the background inverts it, Ctrl+drag there clears it.
+ void drawMaskingSection();
+ void beginMaskStroke(float x, float y);
+ void maskCanvasMove(float x, float y);
+ void maskCanvasRelease();
+ bool strokeMask_ = false; // the current stroke edits the mask
+ float maskStrength_ = 1.0f;
+ int maskIterations_ = 1;
+ float cavityThreshold_ = 0.02f;
+ int maskGroup_ = 0;
+ float maskCanvasX_ = 0.0f, maskCanvasY_ = 0.0f;
+ bool maskCanvasMoved_ = false;
+ // Polygroups & visibility (PanelPolygroups.cpp): Ctrl+Shift click/drag.
+ bool polygroupMouseDown(float x, float y, SDL_Keymod mod); // true if it took the click
+ void polygroupMouseUp();
+ void drawPolygroupsSection();
+ void drawPolygroupOverlay();
+ bool hideRectActive_ = false;
+ float hideRectX_ = 0.0f, hideRectY_ = 0.0f;
+ float polygroupAngle_ = 30.0f;
+ // Stroke types, alphas and the extra brushes (PanelBrushes.cpp).
+ void drawBrushOptions();
+ void snakeHookTo(float x, float y);
+ void dragRectTo(float x, float y);
+ glm::vec3 screenUpVector(float angle) const; // camera up rotated by `angle` around the view axis
+ static void SDLCALL onAlphaChosen(void* userdata, const char* const* files, int filter);
+ StrokeType strokeType_ = StrokeType::Dots;
+ LazyMouse lazyMouse_;
+ Spray spray_;
+ float sprayScatter_ = 1.0f; // jitter radius as a fraction of brush size
+ glm::vec2 dragAnchor_{0.0f}; // DragRect: where the click landed (window pixels)
+ glm::vec3 dragAnchorHit_{0.0f};
+ float dabRadiusScale_ = 1.0f, dabStrengthScale_ = 1.0f, dabAngle_ = 0.0f; // per-dab overrides
+ bool alphaInvert_ = false;
+ std::mutex alphaMutex_;
+ std::string pendingAlphaPath_; // set by the file dialog thread
+ // Polypaint and MatCap materials (PanelPolypaint.cpp)
+ bool paintMode_ = false; // Paint brush: colour only, no sculpting (ZBrush Rgb on, Zadd off)
+ bool paintRgb_ = false; // also paint the colour while sculpting (ZBrush Rgb + Zadd)
+ bool strokePaint_ = false, strokeSculpt_ = true;
+ bool showPolypaintPanel_ = false; // off by default (keeps the viewport and demo videos clear)
+ void registerPaintUiCommands();
+ void drawPolypaintPanel();
+ void paintAt(const DabParams& p);
+ void pickColorUnderCursor();
+ // First paint stroke on a mesh without polypaint: undo restores this snapshot so the attribute goes away again.
+ std::unique_ptr<Mesh> paintSnapshot_;
+ void beginPaintStroke();
+ void finishPaintStroke(UndoRecord& record);
+ const BrushRecipe* strokeRecipe() const; // recipe the current stroke sculpts with (none for Smooth / paint only)
};
} // namespace sf
diff --cc src/app/PanelBrushes.cpp
index c35c7f41,00000000..2116a7b5
mode 100644,000000..100644
--- a/src/app/PanelBrushes.cpp
+++ b/src/app/PanelBrushes.cpp
@@@ -1,149 -1,0 +1,151 @@@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Brush options panel (stroke type, lazy mouse, spray, alpha) and the
+// stroke handling of the SnakeHook brush and the DragRect stroke.
+#include "app/App.h"
+
+#include <algorithm>
+#include <cmath>
+
+namespace sf {
+
+namespace {
+
+const SDL_DialogFileFilter kAlphaFilters[] = {{"Images", "png;jpg;jpeg;bmp;tga;psd;gif"}, {"All files", "*"}};
+
+} // namespace
+
+void SDLCALL App::onAlphaChosen(void* userdata, const char* const* files, int) {
+ if (!files || !files[0]) return;
+ auto* app = static_cast<App*>(userdata);
+ std::lock_guard lock(app->alphaMutex_);
+ app->pendingAlphaPath_ = files[0];
+}
+
+glm::vec3 App::screenUpVector(float angle) const {
+ const glm::quat q = camera_.orientation();
+ return q * glm::vec3(-std::sin(angle), std::cos(angle), 0.0f);
+}
+
+void App::drawBrushOptions() {
+ std::string alphaPath;
+ {
+ std::lock_guard lock(alphaMutex_);
+ alphaPath.swap(pendingAlphaPath_);
+ }
+ if (!alphaPath.empty()) runCommand("brush_set_alpha", {{"path", alphaPath}, {"invert", alphaInvert_}});
+
+ ImGui::SeparatorText("Stroke");
+ ImGui::SetNextItemWidth(-1);
+ if (ImGui::BeginCombo("##stroke", strokeTypeName(strokeType_))) {
+ for (int i = 0; i < int(StrokeType::Count); ++i)
+ if (ImGui::Selectable(strokeTypeName(StrokeType(i)), strokeType_ == StrokeType(i))) strokeType_ = StrokeType(i);
+ ImGui::EndCombo();
+ }
+ if (ImGui::IsItemHovered())
+ ImGui::SetTooltip("Dots: even dabs along the path\nDragRect: click-drag one stamp (length = size, direction = "
+ "rotation)\nSpray: scattered dabs of random size and strength\nLazyMouse: the brush trails the "
+ "cursor for smooth lines");
+ if (strokeType_ == StrokeType::Spray) {
+ ImGui::SetNextItemWidth(-1);
+ ImGui::SliderFloat("##scatter", &sprayScatter_, 0.0f, 2.0f, "scatter %.2f");
+ } else if (strokeType_ == StrokeType::LazyMouse) {
+ ImGui::SetNextItemWidth(-1);
+ ImGui::SliderFloat("##lazy", &lazyMouse_.radius, 1.0f, 300.0f, "lazy radius %.0f px");
+ }
+
+ ImGui::SeparatorText("Alpha");
+ const BrushAlpha& alpha = doc_.brushAlpha;
+ const float preview = ImGui::GetFrameHeight() * 2.0f;
+ {
+ // Thumbnail of the stamp.
+ const ImVec2 p0 = ImGui::GetCursorScreenPos();
+ ImDrawList* dl = ImGui::GetWindowDrawList();
+ constexpr int kCells = 20;
+ const float cell = preview / kCells;
+ for (int y = 0; y < kCells; ++y)
+ for (int x = 0; x < kCells; ++x) {
+ const float u = (float(x) + 0.5f) / kCells * 2.0f - 1.0f, v = 1.0f - (float(y) + 0.5f) / kCells * 2.0f;
+ const int g = int(std::clamp(alpha.sample(u, v), 0.0f, 1.0f) * 255.0f);
+ dl->AddRectFilled(ImVec2(p0.x + x * cell, p0.y + y * cell), ImVec2(p0.x + (x + 1) * cell, p0.y + (y + 1) * cell),
+ IM_COL32(g, g, g, 255));
+ }
+ ImGui::Dummy(ImVec2(preview, preview));
+ }
+ ImGui::SameLine();
+ ImGui::BeginGroup();
+ ImGui::SetNextItemWidth(-1);
+ if (ImGui::BeginCombo("##alpha", alpha.name.c_str())) {
+ for (const char* name : {"Round", "Square", "Noise"})
+ if (ImGui::Selectable(name, alpha.name == name)) runCommand("brush_set_alpha", {{"name", std::string(name)}});
+ ImGui::EndCombo();
+ }
+ if (ImGui::Button("Load image...", ImVec2(-1, 0)))
+ SDL_ShowOpenFileDialog(onAlphaChosen, this, window_, kAlphaFilters, 2, nullptr, false);
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("Grayscale image: white = full effect, black = none");
+ ImGui::EndGroup();
+ ImGui::Checkbox("Invert loaded image", &alphaInvert_);
+}
+
+// SnakeHook: pull the region under the click along with the cursor, like Move,
+// but re-centred on the pulled tip every step so it can be dragged out into horns.
+void App::snakeHookTo(float x, float y) {
+ int w = 0, h = 0;
+ SDL_GetWindowSize(window_, &w, &h);
+ glm::vec3 origin, dir;
+ camera_.screenRay(x, y, float(w), float(h), origin, dir);
+ const glm::vec3 n = camera_.forward();
+ const float denom = glm::dot(dir, n);
+ if (std::abs(denom) < 1e-6f) return;
+ const glm::vec3 target = origin + dir * (glm::dot(grabLast_ - origin, n) / denom);
+
+ const float pressure = lastInputPen_ ? std::clamp(penPressure_, 0.0f, 1.0f) : 1.0f;
+ DabParams p;
+ p.viewDir = n;
+ p.radius = radiusPx_ * (pressureSize_ ? std::max(pressure, 0.05f) : 1.0f) * camera_.worldPerPixel(grabLast_, float(h));
+ p.strength = strength_;
+ p.pressure = pressureStrength_ ? pressure : 1.0f;
+ p.invert = strokeInvert_;
+ p.alpha = &doc_.brushAlpha;
+ p.alphaUp = screenUpVector(0.0f);
+ // Small steps keep the tip under the cursor and the pulled surface even.
+ const glm::vec3 delta = target - grabLast_;
+ const int steps = std::clamp(int(std::ceil(glm::length(delta) / (p.radius * 0.15f))), 1, 64);
+ p.strokeDelta = delta / float(steps);
+ for (int i = 0; i < steps; ++i) {
+ p.center = grabLast_;
+ doc_.sculptor.dab(doc_.mesh, doc_.bvh, BrushType::SnakeHook, p, symmetry_);
+ grabLast_ += p.strokeDelta;
+ }
+}
+
+// DragRect: undo the previous stamp and press a new one sized and rotated by the drag.
+void App::dragRectTo(float x, float y) {
+ doc_.sculptor.restoreStroke(doc_.mesh, doc_.bvh);
+ const glm::vec2 drag = glm::vec2(x, y) - dragAnchor_;
+ const float len = glm::length(drag);
+ if (len < 2.0f) return;
+ int w = 0, h = 0;
+ SDL_GetWindowSize(window_, &w, &h);
+ const glm::quat q = camera_.orientation();
+ const float pressure = lastInputPen_ ? std::clamp(penPressure_, 0.0f, 1.0f) : 1.0f;
+
+ DabParams p;
+ p.center = dragAnchorHit_;
+ p.viewDir = camera_.forward();
+ p.radius = len * camera_.worldPerPixel(dragAnchorHit_, float(h));
+ p.strength = strength_;
+ p.pressure = pressureStrength_ ? pressure : 1.0f;
+ p.invert = strokeInvert_;
+ p.frontFacesOnly = frontFacesOnly_;
+ p.alpha = &doc_.brushAlpha;
+ p.alphaUp = glm::normalize(q * glm::vec3(drag.x, -drag.y, 0.0f)); // the alpha's top follows the drag
- const BrushRecipe* recipe = strokeBrush_ == BrushType::Smooth ? nullptr : activeRecipe();
++ if (strokePaint_) paintAt(p); // restoreStroke reverted the previous stamp's colours too
++ if (!strokeSculpt_) return;
++ const BrushRecipe* recipe = strokeRecipe();
+ if (recipe) doc_.sculptor.dabRecipe(doc_.mesh, doc_.bvh, *recipe, p, symmetry_);
+ else doc_.sculptor.dab(doc_.mesh, doc_.bvh, strokeBrush_, p, symmetry_);
+}
+
+} // namespace sf
diff --cc src/app/PanelMasking.cpp
index 20794a5c,00000000..01052ce1
mode 100644,000000..100644
--- a/src/app/PanelMasking.cpp
+++ b/src/app/PanelMasking.cpp
@@@ -1,95 -1,0 +1,98 @@@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Masking: Ctrl mask strokes, the Ctrl + background gestures and the
+// "Masking" section of the Tools panel. The operations themselves are the
+// mask_* commands (core/Masking.cpp).
+#include "app/App.h"
+
+#include <algorithm>
+#include <cmath>
+
+namespace sf {
+
+namespace {
+
+constexpr float kMaskDragThresholdPx = 4.0f; // movement that turns a Ctrl+click into a drag
+
+bool anyMasked(const Mesh& mesh) {
+ return std::any_of(mesh.mask.begin(), mesh.mask.end(), [](float m) { return m > 0.0f; });
+}
+
+} // namespace
+
+void App::beginMaskStroke(float x, float y) {
+ // Masks are not layered, so the stroke always targets the base (its weight is never 0).
+ strokeInvert_ = (SDL_GetModState() & SDL_KMOD_ALT) != 0; // Ctrl+Alt erases
+ if (!doc_.sculptor.beginStroke(doc_.mesh, strokeInvert_ ? "Unmask" : "Mask", -1)) return;
+ strokeMask_ = true;
++ strokePaint_ = false; // mask strokes never paint or sculpt
++ strokeSculpt_ = true;
++ paintSnapshot_.reset();
+ strokeBrush_ = BrushType::Standard;
+ haveLastDab_ = false;
+ drag_ = Drag::Stroke;
+ lastX_ = x;
+ lastY_ = y;
+ residual_ = 0.0f;
+ lazyMouse_.reset(glm::vec2(x, y));
+ dabAt(x, y);
+}
+
+// ZBrush convention: Ctrl+drag on the background clears the mask. With
+// nothing masked the drag zooms instead, as it did before masking existed.
+void App::maskCanvasMove(float x, float y) {
+ if (maskCanvasMoved_) return;
+ if (std::hypot(x - maskCanvasX_, y - maskCanvasY_) < kMaskDragThresholdPx) return;
+ maskCanvasMoved_ = true;
+ if (anyMasked(doc_.mesh)) runCommand("mask_clear");
+ else drag_ = Drag::Zoom;
+}
+
+void App::maskCanvasRelease() {
+ if (!maskCanvasMoved_) runCommand("mask_invert"); // Ctrl+click on the background
+}
+
+void App::drawMaskingSection() {
+ ImGui::SeparatorText("Masking");
+ ImGui::Checkbox("Show mask", &renderSettings_.showMask);
+ ImGui::SameLine();
+ ImGui::TextDisabled("(Ctrl paints, Ctrl+Alt erases)");
+ ImGui::SetNextItemWidth(-1);
+ ImGui::SliderFloat("##maskstrength", &maskStrength_, 0.01f, 1.0f, "mask strength %.2f");
+
+ const ImGuiStyle& style = ImGui::GetStyle();
+ const float third = (ImGui::GetContentRegionAvail().x - 2 * style.ItemSpacing.x) / 3.0f;
+ const float half = (ImGui::GetContentRegionAvail().x - style.ItemSpacing.x) * 0.5f;
+ if (ImGui::Button("Clear##mask", ImVec2(third, 0))) runCommand("mask_clear");
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("Unmask everything (Ctrl + drag background)");
+ ImGui::SameLine();
+ if (ImGui::Button("Invert##mask", ImVec2(third, 0))) runCommand("mask_invert");
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("Ctrl + click background");
+ ImGui::SameLine();
+ if (ImGui::Button("All##mask", ImVec2(third, 0))) runCommand("mask_all");
+
+ const Args passes = {{"iterations", double(maskIterations_)}};
+ if (ImGui::Button("Blur##mask", ImVec2(half, 0))) runCommand("mask_blur", passes);
+ ImGui::SameLine();
+ if (ImGui::Button("Sharpen##mask", ImVec2(half, 0))) runCommand("mask_sharpen", passes);
+ if (ImGui::Button("Grow##mask", ImVec2(half, 0))) runCommand("mask_grow", passes);
+ ImGui::SameLine();
+ if (ImGui::Button("Shrink##mask", ImVec2(half, 0))) runCommand("mask_shrink", passes);
+ ImGui::SetNextItemWidth(-1);
+ ImGui::SliderInt("##maskpasses", &maskIterations_, 1, 20, "passes %d");
+
+ if (ImGui::Button("By cavity", ImVec2(half, 0)))
+ runCommand("mask_by_cavity", {{"strength", double(maskStrength_)}, {"threshold", double(cavityThreshold_)}});
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("Mask creases and folds (replaces the mask; uses the mask strength)");
+ ImGui::SameLine();
+ ImGui::SetNextItemWidth(-1);
+ ImGui::SliderFloat("##cavitythreshold", &cavityThreshold_, -0.2f, 0.5f, "threshold %.2f");
+ if (ImGui::Button("By polygroup", ImVec2(half, 0))) runCommand("mask_by_polygroup", {{"index", double(maskGroup_)}});
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("Mask one polygroup (replaces the mask)");
+ ImGui::SameLine();
+ ImGui::SetNextItemWidth(-1);
+ if (ImGui::InputInt("##maskgroup", &maskGroup_)) maskGroup_ = std::max(0, maskGroup_);
+}
+
+} // namespace sf
diff --cc src/app/PanelPolypaint.cpp
index 00000000,5efef8e5..229f013e
mode 000000,100644..100644
--- a/src/app/PanelPolypaint.cpp
+++ b/src/app/PanelPolypaint.cpp
@@@ -1,0 -1,162 +1,162 @@@
+ // SculptForge - open source digital sculpting
+ // SPDX-License-Identifier: GPL-3.0-or-later
+ //
+ // Polypaint & Materials panel: the Paint brush (ZBrush Rgb / Zadd), colour
+ // picker and swatches, fills, and the viewport MatCap material picker.
+ #include "app/App.h"
+
+ #include "core/Matcap.h"
+ #include "core/Polypaint.h"
+
+ #include <glm/gtc/type_ptr.hpp>
+
+ #include <cfloat>
+ #include <iterator>
+
+ namespace sf {
+
+ void App::registerPaintUiCommands() {
+ // A view setting, so it lives with the app (scripts: --script "set_material name=Chrome").
+ std::string names = "studio";
+ for (int i = 0; i < matcapCount(); ++i) names += std::string(", ") + matcapName(i);
+ commands_.add({"set_material",
+ "Viewport material: studio (default clay lighting) or a built-in MatCap. Polypaint multiplies "
+ "the matcap; cavity shading stays on.",
+ {{"name", ParamSpec::Type::String, "One of: " + names + ".", true}},
+ [this, names](Document&, const Args& a) -> CommandResult {
+ const std::string name = argString(a, "name");
+ if (name == "studio" || name == "Studio") {
+ renderSettings_.matcap = -1;
+ return {true, "Material: Studio"};
+ }
+ const int index = findMatcap(name);
+ if (index < 0) return {false, "Unknown material '" + name + "' (" + names + ")"};
+ renderSettings_.matcap = index;
+ return {true, std::string("Material: ") + matcapName(index)};
+ }});
+ }
+
+ const BrushRecipe* App::strokeRecipe() const {
- return strokeBrush_ == BrushType::Smooth || !strokeSculpt_ ? nullptr : activeRecipe();
++ return strokeBrush_ == BrushType::Smooth || !strokeSculpt_ || strokeMask_ ? nullptr : activeRecipe();
+ }
+
+ void App::paintAt(const DabParams& p) {
+ paintDab(doc_.sculptor, doc_.mesh, doc_.bvh, p, doc_.paintColor, symmetry_);
+ }
+
+ void App::beginPaintStroke() {
+ paintSnapshot_.reset();
+ if (strokePaint_ && !doc_.mesh.hasColors()) paintSnapshot_ = std::make_unique<Mesh>(doc_.mesh);
+ }
+
+ void App::finishPaintStroke(UndoRecord& record) {
+ if (!paintSnapshot_) return;
+ UndoRecord snapshot;
+ snapshot.label = std::move(record.label);
+ snapshot.mesh = std::move(paintSnapshot_);
+ record = std::move(snapshot);
+ }
+
+ void App::pickColorUnderCursor() {
+ doc_.syncBvh();
+ RayHit hit;
+ if (!pick(mouseX_, mouseY_, hit)) return;
+ if (!doc_.mesh.hasColors()) {
+ status_ = "The mesh has no polypaint to pick from";
+ return;
+ }
+ doc_.paintColor = sampleColor(doc_.mesh, hit.triangle, hit.position);
+ status_ = "Paint colour " + colorToHex(doc_.paintColor);
+ }
+
+ void App::drawPolypaintPanel() {
+ const float scale = uiScale_;
+ const ImGuiViewport* vp = ImGui::GetMainViewport();
+ ImGui::SetNextWindowPos(ImVec2(vp->Pos.x + 270 * scale, vp->Pos.y + 34 * scale), ImGuiCond_FirstUseEver);
+ ImGui::SetNextWindowSizeConstraints(ImVec2(250 * scale, 0), ImVec2(250 * scale, FLT_MAX));
+ if (!ImGui::Begin("Polypaint & Materials", &showPolypaintPanel_, ImGuiWindowFlags_AlwaysAutoResize)) {
+ ImGui::End();
+ return;
+ }
+
+ ImGui::SeparatorText("Paint");
+ const bool painting = paintMode_;
+ if (painting) ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive));
+ if (ImGui::Button("Paint brush (P)", ImVec2(-1, 0))) paintMode_ = !paintMode_;
+ if (painting) ImGui::PopStyleColor();
+ if (ImGui::IsItemHovered())
+ ImGui::SetTooltip("Paint colour without sculpting. Size, strength, pressure and symmetry come from Tools;\n"
+ "Alt paints back to white, Shift smooths the surface, masked areas are protected.");
+ ImGui::Checkbox("RGB while sculpting", &paintRgb_);
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("Sculpt brushes also paint the colour (ZBrush Rgb with Zadd)");
+
+ // The picker edits display (sRGB) values; the mesh stores linear colour.
+ glm::vec3 srgb = linearToSrgb(doc_.paintColor);
+ ImGui::SetNextItemWidth(-1);
+ if (ImGui::ColorPicker3("##paintcolor", glm::value_ptr(srgb),
+ ImGuiColorEditFlags_PickerHueWheel | ImGuiColorEditFlags_NoSidePreview |
+ ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_DisplayHex))
+ doc_.paintColor = srgbToLinear(srgb);
+ static const glm::vec3 kSwatches[] = {{1.0f, 1.0f, 1.0f}, {0.1f, 0.1f, 0.1f}, {0.85f, 0.12f, 0.1f},
+ {0.95f, 0.5f, 0.12f}, {0.95f, 0.85f, 0.2f}, {0.2f, 0.65f, 0.25f},
+ {0.15f, 0.35f, 0.85f}, {0.5f, 0.25f, 0.7f}, {0.87f, 0.66f, 0.55f},
+ {0.45f, 0.28f, 0.16f}};
+ const float swatch = (ImGui::GetContentRegionAvail().x - 4 * ImGui::GetStyle().ItemSpacing.x) / 5.0f;
+ for (int i = 0; i < int(std::size(kSwatches)); ++i) {
+ const glm::vec3& c = kSwatches[i];
+ ImGui::PushID(i);
+ if (ImGui::ColorButton("##swatch", ImVec4(c.x, c.y, c.z, 1.0f), ImGuiColorEditFlags_NoTooltip,
+ ImVec2(swatch, swatch * 0.6f)))
+ doc_.paintColor = srgbToLinear(c);
+ ImGui::PopID();
+ if (i % 5 != 4) ImGui::SameLine();
+ }
+ ImGui::TextDisabled("C over the model picks its colour");
+
+ const float half = (ImGui::GetContentRegionAvail().x - ImGui::GetStyle().ItemSpacing.x) * 0.5f;
+ const std::string hex = colorToHex(doc_.paintColor);
+ if (ImGui::Button("Fill", ImVec2(half, 0))) runCommand("paint_fill", {{"color", hex}});
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("Fill the unmasked area (the whole mesh if nothing is masked)");
+ ImGui::SameLine();
+ ImGui::BeginDisabled(!doc_.mesh.hasMask());
+ if (ImGui::Button("Fill masked", ImVec2(half, 0)))
+ runCommand("paint_fill", {{"color", hex}, {"area", std::string("masked")}});
+ ImGui::EndDisabled();
+ if (ImGui::Button("Cavity", ImVec2(half, 0))) runCommand("paint_from_cavity");
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("Darken creases and cavities (respects the mask)");
+ ImGui::SameLine();
+ ImGui::BeginDisabled(!doc_.mesh.hasColors());
+ if (ImGui::Button("Clear", ImVec2(half, 0))) runCommand("paint_clear");
+ ImGui::EndDisabled();
+ ImGui::Checkbox("Show polypaint", &renderSettings_.showPolypaint);
+
+ ImGui::SeparatorText("Material");
+ const float spacing = ImGui::GetStyle().ItemSpacing.x;
+ const float tile = (ImGui::GetContentRegionAvail().x - 3 * spacing) / 4.0f;
+ auto outline = [&](bool selected) {
+ if (!selected) return;
+ ImGui::GetWindowDrawList()->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(),
+ ImGui::GetColorU32(ImGuiCol_ButtonActive), 4.0f, 0, 2.0f * scale);
+ };
+ if (ImGui::Button("Studio", ImVec2(tile, tile))) renderSettings_.matcap = -1;
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("Studio lights with the Tools material colour");
+ outline(renderSettings_.matcap < 0);
+ for (int i = 0; i < matcapCount(); ++i) {
+ if ((i + 1) % 4 != 0) ImGui::SameLine();
+ const GLuint tex = renderer_.matcapTexture(i);
+ if (!tex) continue;
+ ImGui::PushID(i);
+ ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
+ // Textures are stored bottom-up: flip V for display.
+ if (ImGui::ImageButton("##matcap", ImTextureID(uintptr_t(tex)), ImVec2(tile, tile), ImVec2(0, 1), ImVec2(1, 0)))
+ renderSettings_.matcap = i;
+ ImGui::PopStyleVar();
+ ImGui::PopID();
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("%s", matcapName(i));
+ outline(renderSettings_.matcap == i);
+ }
+ ImGui::Text("%s", renderSettings_.matcap < 0 ? "Studio" : matcapName(renderSettings_.matcap));
+ ImGui::End();
+ }
+
+ } // namespace sf
diff --cc src/core/Commands.cpp
index 26e1541f,a32e85e5..e1466e71
--- a/src/core/Commands.cpp
+++ b/src/core/Commands.cpp
@@@ -6,8 -5,7 +6,9 @@@
#include "core/Decimate.h"
#include "core/EnginePresets.h"
#include "core/ImageTo3D.h"
+#include "core/Masking.h"
+#include "core/Polygroups.h"
+ #include "core/Polypaint.h"
#include "core/Primitives.h"
#include "core/Splats.h"
#include "core/Voxel.h"
@@@ -601,8 -598,7 +602,9 @@@ CommandRegistry::CommandRegistry()
return {true, s.str()};
}});
+ registerMaskingCommands(*this);
+ registerPolygroupCommands(*this);
+ registerPaintCommands(*this); // core/Polypaint.cpp
}
void CommandRegistry::add(Command cmd) { commands_.push_back(std::move(cmd)); }
diff --cc src/core/Document.h
index 1e84f717,70812e25..5a9d2340
--- a/src/core/Document.h
+++ b/src/core/Document.h
@@@ -28,7 -27,7 +28,8 @@@ struct Document
// Recipe brush library (built-ins, loaded .sfbrush files, AI-made brushes).
std::vector<BrushRecipe> brushes = builtinBrushRecipes();
int activeBrush = -1; // index into `brushes`, -1 = use a built-in brush type
+ BrushAlpha brushAlpha; // stamp shaping every brush's falloff (empty = round); brush_set_alpha
+ glm::vec3 paintColor{0.6f, 0.03f, 0.02f}; // current polypaint colour (linear RGB; see set_color)
// Adds or replaces (by name) a recipe and returns its index.
int addBrush(BrushRecipe recipe);
// Gaussian splats shown alongside the mesh (a capture used as reference or
diff --cc src/core/Sculpt.cpp
index 2e4662de,760390da..fbe59754
--- a/src/core/Sculpt.cpp
+++ b/src/core/Sculpt.cpp
@@@ -527,37 -431,4 +527,43 @@@ void Sculptor::grab(Mesh& mesh, BVH& bv
bvh.refit(mesh, grabVerts_);
}
+// Puts every vertex the current stroke moved back to its stroke-start position.
+void Sculptor::restoreStroke(Mesh& mesh, BVH& bvh) {
- if (!active_ || record_.positions.empty()) return;
++ if (!active_) return;
++ // Polypaint laid down by the stroke so far (Paint brush / RGB while sculpting).
++ if (!record_.colors.empty() && mesh.hasColors()) {
++ const auto& ci = record_.colors.indices;
++ for (size_t i = 0; i < ci.size(); ++i) mesh.colors[ci[i]] = record_.colors.values[i];
++ }
++ if (record_.positions.empty()) return;
+ auto& target = mesh.layerTarget(layer_);
+ const auto& idx = record_.positions.indices;
+ for (size_t i = 0; i < idx.size(); ++i) target[idx[i]] = record_.positions.values[i];
+ mesh.evaluate(idx);
+ mesh.updateNormals(idx);
+ bvh.refit(mesh, idx);
+ // The Layer brush starts over too.
+ if (layerEpoch_ != 0 && ++layerEpoch_ == 0) {
+ std::fill(layerStamp_.begin(), layerStamp_.end(), 0);
+ layerEpoch_ = 1;
+ }
+}
+
+// Sizes the Layer brush state and starts a fresh height map for each new stroke.
+void Sculptor::prepareLayer(const Mesh& mesh, float height) {
+ if (layerStamp_.size() != mesh.vertexCount()) {
+ layerStamp_.assign(mesh.vertexCount(), 0);
+ layerDone_.assign(mesh.vertexCount(), 0.0f);
+ layerDir_.assign(mesh.vertexCount(), glm::vec3(0.0f));
+ layerEpoch_ = 0;
+ }
+ if (layerEpoch_ != 0 && layerSeenStamp_ == stamp_) return;
+ layerSeenStamp_ = stamp_;
+ layerHeight_ = height;
+ if (++layerEpoch_ == 0) {
+ std::fill(layerStamp_.begin(), layerStamp_.end(), 0);
+ layerEpoch_ = 1;
+ }
+}
+
} // namespace sf
diff --cc src/core/Sculpt.h
index 1b37670b,2aa1f97c..fe069eed
--- a/src/core/Sculpt.h
+++ b/src/core/Sculpt.h
@@@ -86,10 -73,6 +86,10 @@@ public
// Mirror sign vectors for the enabled symmetry axes (always starts with (1,1,1)).
static std::vector<glm::vec3> mirrors(Symmetry sym);
- // Puts every vertex the current stroke moved back where it started (the
- // stroke stays open). DragRect re-stamps from scratch while dragging.
++ // Puts every vertex the current stroke moved (or painted) back the way it
++ // started (the stroke stays open). DragRect re-stamps from scratch while dragging.
+ void restoreStroke(Mesh& mesh, BVH& bvh);
+
private:
struct GrabEntry {
uint32_t vertex;