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
Drag Stamp / Slide Stamp keep the drag's firmest pen pressure at the lift (#146)
islandmed3c08b9742734
diff --git a/changes/fix-vdm-penup-strength.md b/changes/fix-vdm-penup-strength.mdnew file mode 100644index 00000000..75f47536--- /dev/null+++ b/changes/fix-vdm-penup-strength.md@@ -0,0 +1,12 @@+---+whats_new: **Drag Stamp with a pen**: a stamp (a vector displacement or any brush) dragged to size keeps its strength when+ you lift the pen, instead of going weak or vanishing.+fixed: **Drag Stamp / Slide Stamp with a pen** (#146, Huion RTP-700 on Windows): the stamp went weak or vanished when+ the pen lifted. A tablet's last samples before the lift fall towards 0, and the stamp is pressed again at every+ move; it now keeps the firmest pressure of the drag (Windows Ink, Wintab and Linux alike).+testers:+ - Windows with a pen (Windows Ink on, then off for Wintab): Brush > Vector Displacement > Horn, press and drag it to+ size, lift the pen. Good: the horn stays as it looked while dragging.+ - Same with Stroke > Slide Stamp: the stamp stays where and as strong as it was at the lift.+---+Before the fix, tests/ui/vdm_penup.sfui's lift with falling pressure left no stamp at all (moved 0).diff --git a/src/app/App.cpp b/src/app/App.cppindex d5e9a485..3dbb8649 100644--- a/src/app/App.cpp+++ b/src/app/App.cpp@@ -2225,8 +2225,10 @@ void App::beginStroke(float x, float y, const RayHit& hit) {beginAlongNormal(hit, radiusPx_ * camera_.worldPerPixel(hit.position, canvasHeight()));return;}- if (strokeType_ == StrokeType::DragRect || strokeType_ == StrokeType::DragDot)+ if (strokeType_ == StrokeType::DragRect || strokeType_ == StrokeType::DragDot) {stampJitter_ = alphaJitterAngle(alphaOptions_.jitter, stampSeed_);+ stampPressure_.reset();+ }if (strokeType_ == StrokeType::DragRect) {dragAnchor_ = glm::vec2(x, y);dragAnchorHit_ = hit.position;diff --git a/src/app/App.h b/src/app/App.hindex 0dcea562..19c9aa67 100644--- a/src/app/App.h+++ b/src/app/App.h@@ -837,6 +837,7 @@ private:float sprayScatter_ = 1.0f; // jitter radius as a fraction of brush sizeglm::vec2 dragAnchor_{0.0f}; // DragRect: where the click landed (window pixels)glm::vec3 dragAnchorHit_{0.0f};+ StampPressure stampPressure_; // DragRect / DragDot: the drag's firmest pressure (#146)float dabRadiusScale_ = 1.0f, dabStrengthScale_ = 1.0f, dabAngle_ = 0.0f; // per-dab overridesbool alphaInvert_ = false;// Alpha options (#76, core/AlphaOptions.h): the Alpha palette's Follow stroke direction, Angle, Angle jitter anddiff --git a/src/app/Demo.h b/src/app/Demo.hindex 0cf0d921..12df22cf 100644--- a/src/app/Demo.h+++ b/src/app/Demo.h@@ -124,7 +124,8 @@ private:void penTouch(bool down, bool eraser); // PEN_DOWN / PEN_UP (+ left button)void penButtonEvent(int button, bool down); // PEN_BUTTON_DOWN / UP (+ SDL 3.4's mouse button)void penStroke(std::function<ImVec2(float u)> path, float p0, float p1, bool eraser, bool leave, float seconds,- std::function<void()> afterSample = {}); // afterSample: e.g. a driver's stray mouse event+ std::function<void()> afterSample = {}, // afterSample: e.g. a driver's stray mouse event+ bool tail = false); // tail: the pressure falls to 0 over the last samples before the lift (#146)// World -> screen in window coordinates.ImVec2 project(const glm::vec3& p) const;diff --git a/src/app/PanelBrushes.cpp b/src/app/PanelBrushes.cppindex bc12f5d9..1d411d26 100644--- a/src/app/PanelBrushes.cpp+++ b/src/app/PanelBrushes.cpp@@ -167,7 +167,7 @@ void App::dragRectTo(float x, float y) {const float len = glm::length(drag);if (len < 2.0f) return;const glm::quat q = camera_.orientation();- const float pressure = strokePressure();+ const float pressure = stampPressure_.hold(strokePressure()); // (not the lift's falling pressure, #146)DabParams p;p.center = dragAnchorHit_;diff --git a/src/app/PanelVdm.cpp b/src/app/PanelVdm.cppindex 9f93433d..f12592b5 100644--- a/src/app/PanelVdm.cpp+++ b/src/app/PanelVdm.cpp@@ -132,7 +132,7 @@ void App::dragDotTo(float x, float y) {doc_.sculptor.restoreStroke(doc_.mesh, doc_.bvh);RayHit hit;if (!pick(x, y, hit)) return;- const float pressure = strokePressure();+ const float pressure = stampPressure_.hold(strokePressure()); // (not the lift's falling pressure, #146)DabParams p;p.center = hit.position;p.viewDir = camera_.forward();diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cppindex 0e26cc52..2685b41c 100644--- a/src/app/UiTest.cpp+++ b/src/app/UiTest.cpp@@ -216,8 +216,9 @@// Simulated graphics tablet: real SDL3 pen events, each followed by the mouse event SDL 3.2 synthesizes// from it (SDL_PEN_MOUSEID), in SDL's order (the app ignores those and makes its own, #95). The touch comes before its first pressure sample, as// with Windows Ink, and every frame carries 4 pen samples (a 240 Hz pen at 60 fps).-// pen_stroke U0 V0 U1 V1 P0 P1 [eraser] [leave] [S] pen stroke across the model with pressure P0 -> P1;-// eraser: the eraser end; leave: the pen leaves proximity before lifting+// pen_stroke U0 V0 U1 V1 P0 P1 [eraser] [leave] [tail] [S] pen stroke across the model with pressure P0 -> P1;+// eraser: the eraser end; leave: the pen leaves proximity before lifting; tail: as a real+// tablet lifts, the last 8 samples (2 frames) move on a few pixels with the pressure falling to 0// pen_drag_item LABEL DX DY P0 P1 [stray X Y] [lost] [S] pen stroke through the item's centre, DX DY pixels long// (a short one is a tap with a little jitter); stray: as for pen_drag; lost: SDL drops// the pen while it touches (out of proximity without lifting, then sdl_pen_mouse stuck@@ -2302,18 +2303,19 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& e} else if (verb == "pen_stroke") {if (!need(6)) return false;const float u0 = num(1, 0), v0 = num(2, 0), u1 = num(3, 0), v1 = num(4, 0), p0 = num(5, 1), p1 = num(6, 1);- bool eraser = false, leave = false;+ bool eraser = false, leave = false, tail = false;float seconds = 0.6f;for (size_t i = 7; i < w.size(); ++i) {if (w[i] == "eraser") eraser = true;else if (w[i] == "leave") leave = true;+ else if (w[i] == "tail") tail = true;else seconds = num(i, seconds);}auto ends = std::make_shared<std::pair<ImVec2, ImVec2>>();run([this, ends, u0, v0, u1, v1] { *ends = {onModel(u0, v0), onModel(u1, v1)}; });penStroke([ends](float u) {return ImVec2(ends->first.x + (ends->second.x - ends->first.x) * u, ends->first.y + (ends->second.y - ends->first.y) * u);- }, p0, p1, eraser, leave, seconds);+ }, p0, p1, eraser, leave, seconds, {}, tail);wait(0.1f);} else if (verb == "pen_drag_item") {if (!need(5)) return false;@@ -2775,7 +2777,7 @@ void Demo::penButtonEvent(int button, bool down) {}void Demo::penStroke(std::function<ImVec2(float u)> path, float p0, float p1, bool eraser, bool leave, float seconds,- std::function<void()> afterSample) {+ std::function<void()> afterSample, bool tail) {// Hover to the start, touch down, then 4 samples a frame along the path, then lift (or leave).auto from = std::make_shared<ImVec2>();steps_.push_back([this, path, from, afterSample](float t) {@@ -2790,24 +2792,40 @@ void Demo::penStroke(std::function<ImVec2(float u)> path, float p0, float p1, boreturn t >= 0.25f;});auto last = std::make_shared<float>(-1.0f);- steps_.push_back([this, path, p0, p1, eraser, leave, seconds, last, afterSample](float t) {+ auto tailFrame = std::make_shared<int>(0);+ steps_.push_back([this, path, p0, p1, eraser, leave, seconds, last, afterSample, tail, tailFrame](float t) {if (*last < 0.0f) {penTouch(true, eraser);penPressure(p0); // the first pressure sample comes after the touch*last = 0.0f;return false;}- const float u = std::min(t / seconds, 1.0f);constexpr int kSamples = 4;- for (int k = 1; k <= kSamples; ++k) {- const float uu = *last + (u - *last) * float(k) / float(kSamples);- penPressure(p0 + (p1 - p0) * uu);- const ImVec2 p = path(uu);- penMove(p.x, p.y);- if (afterSample) afterSample();+ if (*last < 1.0f) {+ const float u = std::min(t / seconds, 1.0f);+ for (int k = 1; k <= kSamples; ++k) {+ const float uu = *last + (u - *last) * float(k) / float(kSamples);+ penPressure(p0 + (p1 - p0) * uu);+ const ImVec2 p = path(uu);+ penMove(p.x, p.y);+ if (afterSample) afterSample();+ }+ *last = u;+ if (u < 1.0f) return false;+ if (tail) return false; // (the lift's samples come in the next frames)+ }+ if (tail) { // the lift: 8 more samples on past the end, the pressure falling to 0+ const ImVec2 a = path(0.98f), b = path(1.0f);+ const float dx = b.x - a.x, dy = b.y - a.y, len = std::max(std::hypot(dx, dy), 1e-3f);+ for (int k = 1; k <= kSamples; ++k) {+ const int i = *tailFrame * kSamples + k;+ penPressure(p1 * (1.0f - float(i) / float(2 * kSamples)));+ penMove(b.x + dx / len * 0.5f * float(i), b.y + dy / len * 0.5f * float(i));+ if (afterSample) afterSample();+ }+ if (++*tailFrame < 2) return false;+ *tailFrame = 0;}- *last = u;- if (u < 1.0f) return false;if (leave) {penProximity(false); // out of range without lifting first} else {diff --git a/src/core/Tablet.h b/src/core/Tablet.hindex 53b66006..6bac3132 100644--- a/src/core/Tablet.h+++ b/src/core/Tablet.h@@ -108,6 +108,22 @@ private:int silentMotions_ = 0;};+// The pressure a re-pressed stamp uses (Drag Stamp, Slide Stamp: each move undoes the stamp and presses it again):+// the firmest of the drag so far. A pen's last samples before it lifts fall towards 0 (every tablet does, whether+// the pressure comes from Windows Ink, Wintab or Linux), and the stamp re-pressed with them went weak or vanished at+// release (#146); the stamp now ends as the drag showed it.+class StampPressure {+public:+ void reset() { peak_ = 0.0f; }+ float hold(float pressure) {+ if (pressure > peak_) peak_ = pressure;+ return peak_;+ }++private:+ float peak_ = 0.0f;+};+// Tablets plugged in or out while the app runs. SDL has no pen hot-plug events: a new pen is a new SDL_PenID at// its first proximity, and the tablet itself shows up as a mouse device (SDL_EVENT_MOUSE_ADDED / _REMOVED) with// the maker's name on Linux and usually on Windows. A mouse device added after launch with a generic name may bediff --git a/tests/test_tablet.cpp b/tests/test_tablet.cppindex c1bee5e7..3ff382d5 100644--- a/tests/test_tablet.cpp+++ b/tests/test_tablet.cpp@@ -9,6 +9,7 @@#include <cmath>#include <string>+#include <vector>using namespace sf;@@ -116,6 +117,41 @@ TEST(pen_pressure_tracker_guards_stroke_start) {CHECK(near(t.raw(), 1.0f));}+TEST(stamp_pressure_keeps_the_drag_strength_at_the_lift) {+ // #146: Drag Stamp / Slide Stamp re-press the stamp at every move with the pressure then. The samples a pen+ // sends as it lifts fall to 0, and the stamp went weak or vanished at release. Each feed, as the app gets it+ // (pressure samples through PenPressureTracker, then the curve, then StampPressure::hold):+ const PressureCurve curve = PressureCurve::soft();+ auto drag = [&](const std::vector<float>& samples, bool touchUpFirst) {+ PenPressureTracker t;+ StampPressure held;+ held.reset();+ t.touchDown();+ float last = 0.0f;+ for (size_t i = 0; i < samples.size(); ++i) {+ if (touchUpFirst && i + 1 == samples.size()) t.touchUp(); // Wintab: the packet with the tip up carries 0+ t.sample(samples[i]);+ t.motion();+ last = held.hold(curve.apply(t.pressure()));+ }+ return last;+ };+ const float firm = curve.apply(0.8f);+ // Windows Ink (SDL pen events): the pressure ramps down over the last samples, then PEN_UP.+ CHECK(near(drag({0.2f, 0.6f, 0.8f, 0.8f, 0.8f, 0.5f, 0.2f, 0.05f, 0.0f}, false), firm));+ // Wintab: the driver's last packet has the tip up and pressure 0, and the mouse moves with it.+ CHECK(near(drag({0.8f, 0.8f, 0.8f, 0.3f, 0.0f}, true), firm));+ // Linux (libinput / X11 via SDL): the pressure axis reports 0 before the tip-up event.+ CHECK(near(drag({0.8f, 0.8f, 0.01f, 0.0f}, false), firm));+ // A drag that presses harder as it goes gets stronger (the stamp follows the pen up, never down).+ CHECK(near(drag({0.1f, 0.4f, 0.9f}, false), curve.apply(0.9f)));+ // A new drag starts afresh.+ StampPressure held;+ CHECK(near(held.hold(0.9f), 0.9f));+ held.reset();+ CHECK(near(held.hold(0.3f), 0.3f));+}+TEST(pen_cursor_mouse_mode_moves_relatively) {// Pen mode: the cursor is where the pen is.PenCursor c;diff --git a/tests/ui/vdm_penup.sfui b/tests/ui/vdm_penup.sfuinew file mode 100644index 00000000..f5f4b9de--- /dev/null+++ b/tests/ui/vdm_penup.sfui@@ -0,0 +1,58 @@+# #146 (Huion RTP-700, Windows): a VDM stamp dragged to size went weak on pen up. A tablet's last samples before it+# lifts fall towards 0, and Drag Stamp re-presses the stamp at every move: the stamp now keeps the drag's firmest+# pressure (StampPressure, core/Tablet.h), for Drag Stamp and Slide Stamp. The pen here is SDL's (Windows Ink and+# Linux); Wintab feeds the same pressure (test_tablet.cpp).+run ui_look look=classic layout=classic+run new_sphere subdivisions=6+click "Brush##palette.brush"+click "Brush/##totray"+wait 0.3+click "Vector Displacement##brush.vdm"+click "Horn##vdm0"+expect_value stroke_type == 1+expect_value pen_strength == 1++# Drag Stamp, lifted cleanly: the stamp as the drag showed it.+snapshot+pen_stroke 0.72 0.5 0.8 0.36 0.8 0.8+expect_value moved > 300+remember moved_sum firm+wait_idle 30+shot drag-clean+key ctrl+z++# A light pen makes a weaker stamp (pressure counts, so the checks below mean something).+snapshot+pen_stroke 0.72 0.5 0.8 0.36 0.3 0.3+expect_value moved_sum < $firm*0.6+key ctrl+z++# Lifted as a real tablet lifts, the pressure falling to 0 over the last samples: the stamp stays as strong.+snapshot+pen_stroke 0.72 0.5 0.8 0.36 0.8 0.8 tail+expect_value moved > 300+expect_value moved_sum > $firm*0.95+expect_value stroking == 0+expect_value can_undo == 1+wait_idle 30+shot drag-tail+key ctrl+z++# Slide Stamp: the same at the lift.+pen_out+click "Stroke##palette.stroke"+click "Slide Stamp##stroke"+key escape+expect_value stroke_type == 2+snapshot+pen_stroke 0.3 0.4 0.62 0.55 0.8 0.8+remember moved_sum slide+expect_value moved > 300+key ctrl+z+snapshot+pen_stroke 0.3 0.4 0.62 0.55 0.8 0.8 tail+expect_value moved_sum > $slide*0.95+wait_idle 30+shot slide-tail+key ctrl+z+pen_out