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.

SpaceMouse: a slow turn of the cap no longer zooms the view away (fenix, @fenixpoints)

islandmedb359a407ba44
diff --git a/changes/fix-spacemouse-tilt.md b/changes/fix-spacemouse-tilt.md
new file mode 100644
index 00000000..45dbe480
--- /dev/null
+++ b/changes/fix-spacemouse-tilt.md
@@ -0,0 +1,9 @@
+---
+fixed: **SpaceMouse**: turning the cap slowly no longer ends in a zoom that flies the view away (3DxWare's scrolling got
+ through while the cap was inside the dead zone, and its momentum after the cap was let go). Thanks @fenixpoints.
+testers:
+ - macOS with 3DxWare installed: twist and tilt the cap slowly, then let go; the view only turns, never zooms.
+ - Any OS: right after letting go of the cap the mouse cursor moves at once; the mouse wheel zooms again after about 1.5 s.
+---
+fenix (Discord 11-06, SpaceMouse Compact, macOS): a slow rotation of the cap first turned the view, then zoomed it
+away. tests/ui/spacemouse_tilt.sfui fails without the fix (distance 6.34 -> 2.98).
diff --git a/src/app/App.h b/src/app/App.h
index 3a40e705..e9678149 100644
--- a/src/app/App.h
+++ b/src/app/App.h
@@ -2557,6 +2557,10 @@ private:
// The cap is pushed past the dead zone (SpaceMouse on) at the last read, held or not: the system pointer's motion
// and wheel meanwhile are the driver's echo of it (3DxWare's mouse emulation), not the user (spaceMouseEcho).
bool spaceMousePushed_ = false;
+ // Seconds since the cap was last stirred (spaceMouseStirred: inside the dead zone too) at the last read, and when
+ // that read was: the driver's scrolling (momentum) lasts kSpaceMouseEchoHold past it.
+ float spaceMouseQuiet_ = 1e9f;
+ Uint64 spaceMouseReadNs_ = 0;
bool spaceMouseEcho(const SDL_Event& e) const;
// ---- end SpaceMouse ----
// ---- Touch (Touch.cpp, core/TouchGesture.h, docs/guide/navigation.md) ----
diff --git a/src/app/Demo.cpp b/src/app/Demo.cpp
index ad7d87ad..4380eae0 100644
--- a/src/app/Demo.cpp
+++ b/src/app/Demo.cpp
@@ -216,7 +216,7 @@ void Demo::update(float dt) {
mouse_.y, int(buttonDown_));
stride_ = 1;
if (penResting_) penRestNoise(); // (UiTest.cpp)
- if (spaceMouseEcho_) spaceMouseEchoFrame();
+ if (spaceMouseEcho_ || spaceMouseScrollEcho_) spaceMouseEchoFrame(dt);
{
const auto now = std::chrono::steady_clock::now();
if (lastFrame_.time_since_epoch().count() != 0)
diff --git a/src/app/Demo.h b/src/app/Demo.h
index 7bfe771f..d3ae6f48 100644
--- a/src/app/Demo.h
+++ b/src/app/Demo.h
@@ -206,9 +206,13 @@ private:
void penRestNoise();
// spacemouse_echo on: while the virtual cap is pushed, a SpaceMouse driver that doesn't know the app (3DxWare's
// defaults) moves the system pointer and scrolls the wheel every frame (fenix, Discord 11-06).
- bool spaceMouseEcho_ = false;
+ // spacemouse_echo scroll: as macOS 3DxWare scrolls for an app it doesn't know: the wheel from the cap's first stir
+ // (inside the app's dead zone), by the cap's push, and on with the scroll's momentum for a second after it is let
+ // go; no pointer motion (a slow tilt zoomed the view away, fenix 11-06).
+ bool spaceMouseEcho_ = false, spaceMouseScrollEcho_ = false;
+ float scrollMomentum_ = 0.0f, scrollMomentumLeft_ = 0.0f;
std::array<float, 6> virtualAxes_{};
- void spaceMouseEchoFrame();
+ void spaceMouseEchoFrame(float dt);
static constexpr SDL_MouseID kDriverMouse = 7; // the driver's own mouse device
// `snapshot` saves the active mesh; moved* values compare against it. `remember` keeps values for $NAME.
std::vector<glm::vec3> basePositions_, baseNormals_;
diff --git a/src/app/SpaceMouse.cpp b/src/app/SpaceMouse.cpp
index 3b7519c9..db7e9963 100644
--- a/src/app/SpaceMouse.cpp
+++ b/src/app/SpaceMouse.cpp
@@ -107,12 +107,18 @@ void App::spaceMouseClose() {
spaceMouseMoving_ = spaceMousePushed_ = false;
}
-bool App::spaceMouseDeflected() const {
- if (!spaceMouse_) return false;
+namespace {
+std::array<float, kSmAxes> readAxes(SDL_Joystick* j) {
std::array<float, kSmAxes> axes{};
for (int i = 0; i < kSmAxes; ++i)
- axes[size_t(i)] = std::clamp(float(SDL_GetJoystickAxis(spaceMouse_, i)) / 32767.0f, -1.0f, 1.0f);
- return spaceMouseMotion(axes, 1.0f / 60.0f, uiPrefs_.spaceMouse).any();
+ axes[size_t(i)] = std::clamp(float(SDL_GetJoystickAxis(j, i)) / 32767.0f, -1.0f, 1.0f);
+ return axes;
+}
+} // namespace
+
+bool App::spaceMouseDeflected() const {
+ if (!spaceMouse_) return false;
+ return spaceMouseMotion(readAxes(spaceMouse_), 1.0f / 60.0f, uiPrefs_.spaceMouse).any();
}
void App::spaceMouseEvent(const SDL_Event& e) {
@@ -146,11 +152,13 @@ void App::spaceMouseEvent(const SDL_Event& e) {
void App::updateSpaceMouse(float dt) {
// The first frame of a move counts as one frame, not the time since the last frame drawn (a loop that sleeps
// while idle wakes on the axis event: the cap has only just left the centre).
+ spaceMouseQuiet_ = std::min(spaceMouseQuiet_ + std::max(dt, 0.0f), 1e9f);
+ spaceMouseReadNs_ = SDL_GetTicksNS();
if (!spaceMouseMoving_) dt = std::min(dt, 1.0f / 60.0f);
spaceMouseMoving_ = spaceMousePushed_ = false;
if (!spaceMouse_) return;
- for (int i = 0; i < kSmAxes; ++i)
- spaceMouseAxes_[size_t(i)] = std::clamp(float(SDL_GetJoystickAxis(spaceMouse_, i)) / 32767.0f, -1.0f, 1.0f);
+ spaceMouseAxes_ = readAxes(spaceMouse_);
+ if (spaceMouseStirred(spaceMouseAxes_, uiPrefs_.spaceMouse)) spaceMouseQuiet_ = 0.0f;
for (float v : spaceMouseAxes_) spaceMouseReported_ = spaceMouseReported_ || v != 0.0f;
const SpaceMouseSettings& s = uiPrefs_.spaceMouse;
const SpaceMouseMotion m = spaceMouseMotion(spaceMouseAxes_, dt, s);
@@ -219,7 +227,9 @@ void App::regrabAfterViewMove() {
// A SpaceMouse driver that doesn't know SculptForge (3DxWare's defaults) also moves the system pointer or scrolls the
// wheel while the cap is pushed: the cursor jumped from the pen to wherever the driver put it, and the wheel zoomed
// toward it (fenix, Discord 11-06). While the cap moves the view, only the pen and touch move the cursor; a mouse's
-// buttons still click.
+// buttons still click. It starts inside the dead zone and its scrolling's momentum outlasts the cap: a slow tilt
+// zoomed the view away (fenix, 11-06), so the echo counts from the cap's first stir, its wheel until kSpaceMouseEchoHold
+// after.
bool App::spaceMouseEcho(const SDL_Event& e) const {
SDL_MouseID which = 0;
if (e.type == SDL_EVENT_MOUSE_MOTION) which = e.motion.which;
@@ -228,7 +238,13 @@ bool App::spaceMouseEcho(const SDL_Event& e) const {
if (which == SDL_PEN_MOUSEID || which == SDL_TOUCH_MOUSEID) return false;
// (The cap as it is now, too: the echo of its first push comes in the same batch of events as the push, before
// the frame reads the axes.)
- return spaceMousePushed_ || spaceMouseDeflected();
+ if (!spaceMouse_) return false;
+ if (spaceMousePushed_ || spaceMouseStirred(readAxes(spaceMouse_), uiPrefs_.spaceMouse)) return true;
+ // Only the scrolling outlasts the cap (momentum): a mouse picked up as the cap is let go moves the cursor at once.
+ if (e.type != SDL_EVENT_MOUSE_WHEEL) return false;
+ // (Plus the time since that read: a loop that slept while idle reads the cap again only after this event.)
+ const float sinceRead = spaceMouseReadNs_ ? float(double(SDL_GetTicksNS() - spaceMouseReadNs_) * 1e-9) : 0.0f;
+ return spaceMouseQuiet_ + sinceRead < kSpaceMouseEchoHold;
}
bool App::sixDofPointerEvent(const SDL_Event& e) {
diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cpp
index 6faf0f48..bbb57a30 100644
--- a/src/app/UiTest.cpp
+++ b/src/app/UiTest.cpp
@@ -285,7 +285,9 @@
// e.g. a pen stroke while the view turns); spacemouse_hold 0 0 0 0 0 0 lets go
// spacemouse_button N press and release button N (1 = left side button, 2 = right)
// spacemouse_echo on|off while the cap is pushed, the system pointer drifts and the wheel scrolls every frame, as
-// a SpaceMouse driver does for an app it doesn't know (3DxWare's defaults; fenix 11-06)
+// a SpaceMouse driver does for an app it doesn't know (3DxWare's defaults; fenix 11-06);
+// scroll: only the wheel, from the cap's first stir (inside the dead zone) and on for a
+// second after it is let go (macOS momentum; fenix's slow tilt, 11-06)
// spacemouse_unplug unplug it
// expect_value spacemouse_connected (1 when the app opened it), spacemouse_moving (1 while it moves the view)
// ui_scale PERCENT | auto Preferences > Interface > UI scale (applied live, #87); display_scale PERCENT: the display's
@@ -2840,7 +2842,11 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& e
wait(0.1f);
} else if (verb == "spacemouse_echo") {
if (!need(1)) return false;
- run([this, on = w[1] != "off"] { spaceMouseEcho_ = on; });
+ run([this, mode = w[1]] {
+ spaceMouseEcho_ = mode == "on";
+ spaceMouseScrollEcho_ = mode == "scroll";
+ scrollMomentumLeft_ = 0.0f;
+ });
} else if (verb == "pen_rest") {
if (!need(1)) return false;
run([this, on = w[1] != "off"] {
@@ -2957,10 +2963,33 @@ void Demo::penRestNoise() {
app_.handleEvent(e);
}
-void Demo::spaceMouseEchoFrame() {
+void Demo::spaceMouseEchoFrame(float dt) {
+ if (!virtualSpaceMouseHandle_) return;
+ if (spaceMouseScrollEcho_) {
+ float push = 0.0f; // the cap's largest push, signed
+ for (float a : virtualAxes_)
+ if (std::abs(a) > std::abs(push)) push = a;
+ if (std::abs(push) > 0.01f) {
+ scrollMomentum_ = push * 3.0f; // wheel steps per frame, as the driver's scrolling accelerates
+ scrollMomentumLeft_ = 1.0f;
+ } else {
+ scrollMomentumLeft_ -= dt;
+ if (scrollMomentumLeft_ <= 0.0f) return;
+ scrollMomentum_ *= 0.95f;
+ }
+ SDL_Event e{};
+ e.type = SDL_EVENT_MOUSE_WHEEL;
+ e.wheel.windowID = SDL_GetWindowID(app_.window_);
+ e.wheel.which = kDriverMouse;
+ e.wheel.y = scrollMomentum_;
+ e.wheel.mouse_x = mouse_.x;
+ e.wheel.mouse_y = mouse_.y;
+ app_.handleEvent(e);
+ return;
+ }
bool pushed = false;
for (float a : virtualAxes_) pushed = pushed || std::abs(a) > 0.1f;
- if (!pushed || !virtualSpaceMouseHandle_) return;
+ if (!pushed) return;
// The system pointer (no device: injected input) drifts away from the pen, and the wheel zooms.
mouseMotion(mouse_.x + 7.0f, mouse_.y + 3.0f);
SDL_Event e{};
diff --git a/src/core/SpaceMouse.cpp b/src/core/SpaceMouse.cpp
index 246f0dda..4eca9b18 100644
--- a/src/core/SpaceMouse.cpp
+++ b/src/core/SpaceMouse.cpp
@@ -110,6 +110,14 @@ float spaceMouseDeadZone(float value, float deadZone) {
return std::copysign((a - deadZone) / (1.0f - deadZone), value);
}
+bool spaceMouseStirred(const std::array<float, kSmAxes>& axes, const SpaceMouseSettings& s) {
+ if (!s.enabled) return false;
+ const float threshold = std::max(s.deadZone * 0.5f, 0.01f);
+ for (float a : axes)
+ if (std::abs(a) > threshold) return true;
+ return false;
+}
+
SpaceMouseMotion spaceMouseMotion(const std::array<float, kSmAxes>& axes, float dt, const SpaceMouseSettings& s) {
SpaceMouseMotion m;
if (!s.enabled || !(dt > 0.0f)) return m;
diff --git a/src/core/SpaceMouse.h b/src/core/SpaceMouse.h
index 3e282eff..72acc6f8 100644
--- a/src/core/SpaceMouse.h
+++ b/src/core/SpaceMouse.h
@@ -69,6 +69,14 @@ constexpr float kSpaceMousePanRate = 1.0f; // viewport heights
constexpr float kSpaceMouseZoomRate = 6.0f; // Camera::zoom steps (x0.46 of the distance)
constexpr float kSpaceMouseMaxStep = 0.1f; // seconds: a stalled frame doesn't throw the view
+// A SpaceMouse driver that doesn't know the app (3DxWare's defaults) scrolls the wheel and moves the system pointer
+// from the cap's first stir, well inside our dead zone, and macOS carries its scrolling on (momentum) after the cap
+// is let go: a slow tilt zoomed the view away (fenix, Discord 11-06). The cap counts as stirred past half the dead
+// zone (a cap that rests a little off centre has its dead zone raised, so its own noise never counts): pointer and
+// wheel events from anything but the pen are the driver's echo meanwhile, the wheel also for this long after.
+constexpr float kSpaceMouseEchoHold = 1.5f; // seconds
+bool spaceMouseStirred(const std::array<float, kSmAxes>& axes, const SpaceMouseSettings& s);
+
// An axis value (-1 .. 1) after the dead zone: 0 inside it, rescaled so the rest of the travel still reaches 1.
float spaceMouseDeadZone(float value, float deadZone);
diff --git a/tests/test_spacemouse.cpp b/tests/test_spacemouse.cpp
index db198539..d8c10b8c 100644
--- a/tests/test_spacemouse.cpp
+++ b/tests/test_spacemouse.cpp
@@ -185,3 +185,38 @@ TEST(spacemouse_settings_json) {
fromJson(Json(), d); // not an object: untouched
CHECK(near(d.speed, 4.0f));
}
+
+// fenix (Discord 11-06): slowly turning the cap about a horizontal axis first turned the view, then zoomed it away.
+// A turn alone only turns (no zoom or pan from Rx or Ry, free or turntable), and the driver's echo counts from the cap's first stir, inside the
+// dead zone, so its scrolling is dropped from the start.
+TEST(spacemouse_tilt_only_turns_and_stirs_early) {
+ SpaceMouseSettings s;
+ for (bool turntable : {false, true}) {
+ s.turntable = turntable;
+ for (float r : {0.1f, 0.3f, -0.6f, 1.0f}) {
+ std::array<float, kSmAxes> a{};
+ a[kSmRx] = r; // forward / back
+ SpaceMouseMotion m = spaceMouseMotion(a, 1.0f / 60.0f, s);
+ CHECK(m.tip != 0.0f && m.zoom == 0.0f && m.panX == 0.0f && m.panY == 0.0f && m.spin == 0.0f && m.roll == 0.0f);
+ a = {};
+ a[kSmRy] = r; // sideways: a roll, none in turntable
+ m = spaceMouseMotion(a, 1.0f / 60.0f, s);
+ CHECK((m.roll != 0.0f) != turntable && m.zoom == 0.0f && m.panX == 0.0f && m.panY == 0.0f && m.spin == 0.0f &&
+ m.tip == 0.0f);
+ }
+ }
+ s = SpaceMouseSettings(); // dead zone 8%: stirred past 4%
+ std::array<float, kSmAxes> a{};
+ CHECK(!spaceMouseStirred(a, s));
+ a[kSmRx] = 0.03f;
+ CHECK(!spaceMouseStirred(a, s)); // a cap resting a little off centre
+ a[kSmRx] = -0.05f;
+ CHECK(spaceMouseStirred(a, s) && !spaceMouseMotion(a, 1.0f / 60.0f, s).any()); // stirred, not yet moving the view
+ s.deadZone = 0.0f;
+ a[kSmRx] = 0.005f;
+ CHECK(!spaceMouseStirred(a, s));
+ a[kSmRx] = 0.02f;
+ CHECK(spaceMouseStirred(a, s));
+ s.enabled = false; // SpaceMouse off: its driver's scrolling is the user's to keep
+ CHECK(!spaceMouseStirred(a, s));
+}
diff --git a/tests/ui/spacemouse_pen.sfui b/tests/ui/spacemouse_pen.sfui
index 72108c61..ebf9233a 100644
--- a/tests/ui/spacemouse_pen.sfui
+++ b/tests/ui/spacemouse_pen.sfui
@@ -112,9 +112,11 @@ pen_lift
pen_rest off
key ctrl+z
-# Without the cap pushed the mouse and its wheel work as ever.
+# Without the cap pushed the mouse and its wheel work as ever (the wheel once the driver's scroll momentum would be
+# over, kSpaceMouseEchoHold after the cap last moved: spacemouse_tilt.sfui).
pen_out
spacemouse_echo off
+wait 1.5
key f
remember camera_distance d3
wheel 0.5 0.5 2
diff --git a/tests/ui/spacemouse_tilt.sfui b/tests/ui/spacemouse_tilt.sfui
new file mode 100644
index 00000000..95b77728
--- /dev/null
+++ b/tests/ui/spacemouse_tilt.sfui
@@ -0,0 +1,70 @@
+# A slow turn of the SpaceMouse cap about a horizontal axis, forward / back or sideways (fenix, Discord 11-06, a real
+# SpaceMouse Compact on macOS with 3DxWare): it first
+# turned the view, then turned into a zoom that flew the camera into the model or away to infinity. 3DxWare scrolls
+# the wheel for an app it doesn't know from the cap's first stir, inside our dead zone, and macOS carries the scroll
+# on after the cap is let go; the 0.11.10 echo filter dropped it only while the cap was past the dead zone. Replayed
+# here: the view only turns, keeps its distance and its pivot, and the mouse wheel works again once the echo ends.
+run new_sphere subdivisions=4
+key f
+camera 0 0
+spacemouse 0 0 0 0 0 0 0.1 # plugged in, cap at rest
+spacemouse_echo scroll
+remember camera_distance d0
+remember camera_pan p0
+
+# A slow tilt: first inside the dead zone (the driver scrolls already, the view holds still)...
+spacemouse 0 0 0 0.06 0 0 1.0
+expect_value camera_pitch == 0
+expect_value camera_distance >= $d0-0.0001
+expect_value camera_distance <= $d0+0.0001
+# ... then past it, held, and let go; the driver's scroll carries on for a second after.
+spacemouse 0 0 0 0.3 0 0 2.0
+wait 1.2
+expect_value camera_pitch < -10
+expect_value camera_distance >= $d0-0.0001
+expect_value camera_distance <= $d0+0.0001
+expect_value camera_pan >= $p0-0.0001
+expect_value camera_pan <= $p0+0.0001
+shot spacemouse_tilt
+
+# Tilted the other way, the same.
+camera 0 0
+spacemouse 0 0 0 -0.3 0 0 2.0
+wait 1.2
+expect_value camera_pitch > 10
+expect_value camera_distance >= $d0-0.0001
+expect_value camera_distance <= $d0+0.0001
+
+# Rolled sideways (the cap turned about the other horizontal axis), both ways: the view rolls, the same.
+camera 0 0
+spacemouse 0 0 0 0 0.3 0 2.0
+wait 1.2
+expect_value camera_roll > 10
+expect_value camera_distance >= $d0-0.0001
+expect_value camera_distance <= $d0+0.0001
+expect_value camera_pan >= $p0-0.0001
+expect_value camera_pan <= $p0+0.0001
+camera 0 0
+spacemouse 0 0 0 0 -0.3 0 2.0
+wait 1.2
+expect_value camera_roll < -10
+expect_value camera_distance >= $d0-0.0001
+expect_value camera_distance <= $d0+0.0001
+
+# Twisted slowly in the horizontal plane (fenix's screenshot: the Spin bar full, the Zoom bar a little): the view
+# turns, the same.
+camera 0 0
+spacemouse 0 0 0 0 0 0.05 1.0
+spacemouse 0 0 0 0 0 0.3 2.0
+wait 1.2
+expect_value camera_yaw > 10
+expect_value camera_distance >= $d0-0.0001
+expect_value camera_distance <= $d0+0.0001
+expect_value camera_pan >= $p0-0.0001
+expect_value camera_pan <= $p0+0.0001
+
+# The echo is over: the mouse wheel zooms again.
+spacemouse_echo off
+wait 0.5
+wheel 0.5 0.5 3
+expect_value camera_distance < $d0-0.01