From e789efacac694603ada9b925679b51b6448cbfe3 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 20 Nov 2026 15:03:21 +0100 Subject: [PATCH] OrbitCamera navigates as Blender's viewport does: MMB orbit about the puppet, Shift pan, Ctrl and wheel zoom, Alt+LMB as MMB The right-drag orbit, the yaw and pitch fields and the LookAt placement are gone; the camera's transform is the state, written once per gesture by one Commit. Startup view unchanged. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 2 +- client/Main.cs | 2 +- client/OrbitCamera.cs | 125 +++++++++++++++++++++++++++++++++++------- 3 files changed, 107 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 3410def..5cb27ac 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ checkouts (`..\Lodur`, `..\Ymir`, `..\CultLib`, or `LODUR_ROOT`, `YMIR_ROOT` and ``` dotnet build client/BadDay.Client.csproj # CultLib Geometry, Lodur, Ymir (cmake) and BDS, plus ymir_box3d.dll, into client/.godot/mono/temp/bin/Debug - --path client # play: right-drag orbits, the wheel zooms + --path client # play: Blender's navigation: middle-drag orbits the puppet, Shift+middle-drag pans, Ctrl+middle-drag and the wheel zoom; Alt+left-drag stands for the middle button (OrbitCamera.Emulate3ButtonMouse) --headless --path client res://tests/Smoke.tscn # the smoke; exit code 0 passes --path client res://tests/RenderCheck.tscn # the render check, in a window (not headless); exit code 0 passes ``` diff --git a/client/Main.cs b/client/Main.cs index 52f3b75..b9f5b51 100644 --- a/client/Main.cs +++ b/client/Main.cs @@ -54,7 +54,7 @@ public partial class Main : Node3D AddChild(_bodies); _chamber = new Chamber { Name = "Chamber" }; AddChild(_chamber); - _camera = new OrbitCamera { Name = "Camera" }; + _camera = new OrbitCamera { Name = "Camera", Pivot = () => _bodies.PuppetCentre }; AddChild(_camera); AddChild(Look.Studio(_camera)); _hud = new Hud(this) { Name = "Hud" }; diff --git a/client/OrbitCamera.cs b/client/OrbitCamera.cs index 276db99..24654f6 100644 --- a/client/OrbitCamera.cs +++ b/client/OrbitCamera.cs @@ -4,46 +4,131 @@ using Godot; namespace BadDay.Client; -/// A camera orbiting the chamber: right-drag orbits, the wheel zooms. +/// +/// A camera that navigates as Blender's viewport does: middle-drag orbits (turntable, 0.4 degrees per pixel, about the puppet), Shift+middle-drag +/// pans, Ctrl+middle-drag and the wheel zoom. With (on by default) Alt+left-drag stands for the middle button +/// in each combination. Plain left, right and every other combination pass through unhandled. The camera's own transform is the state: the orientation is +/// its basis, the view centre lies ahead of it, and every gesture writes the transform once, on input. +/// public partial class OrbitCamera : Camera3D { - private Vector3 _target = new(BodyPainBox.HoldPoint.X, BodyPainBox.HoldPoint.Y, BodyPainBox.HoldPoint.Z); - private float _yaw = 0.35f; - private float _pitch = 0.2f; + private enum Gesture { Orbit, Pan, Zoom } + + private readonly record struct Binding(MouseButton Button, bool Alt, bool Shift, bool Ctrl, Gesture Gesture, bool Emulated); + + /// The gesture the drag in progress runs, fixed at the press: the button that holds it, where an orbit turns and which way up the view started. + private readonly record struct Drag(Gesture Gesture, MouseButton Button, Vector3 Pivot, float Reverse); + + private static readonly Binding[] Bindings = + { + new(MouseButton.Middle, false, false, false, Gesture.Orbit, false), + new(MouseButton.Middle, false, true, false, Gesture.Pan, false), + new(MouseButton.Middle, false, false, true, Gesture.Zoom, false), + new(MouseButton.Left, true, false, false, Gesture.Orbit, true), + new(MouseButton.Left, true, true, false, Gesture.Pan, true), + new(MouseButton.Left, true, false, true, Gesture.Zoom, true), + }; + + /// Blender's default turntable sensitivity. + private const float DegreesPerPixel = 0.4f; + /// One wheel notch, or 16 pixels of zoom drag, scales the distance by this. + private const float ZoomStep = 1.2f; + private const float ZoomDragPixels = 16f; + private const float MinDistance = 0.6f; + private const float MaxDistance = 12f; + private float _distance = 2.4f; + private Drag? _drag; + + /// Alt+left-drag stands for the middle button (Blender's Emulate 3 Button Mouse). + public bool Emulate3ButtonMouse { get; set; } = true; + + /// Where an orbit turns, read once at each orbit press; null, or a null result, means the view centre. + public Func? Pivot { get; set; } + + /// How far the view centre lies ahead of the camera, in metres. Setting it zooms about the view centre, within the clamp. + public float Distance + { + get => _distance; + set => Commit(Basis, ViewCentre, Math.Clamp(value, MinDistance, MaxDistance)); + } + + /// The point the camera looks at and the view pans and zooms about. + public Vector3 ViewCentre => Position - Basis.Z * _distance; public override void _Ready() { Fov = 55f; - PhysicsInterpolationMode = PhysicsInterpolationModeEnum.Off; // moved in _Process, not on the physics tick + PhysicsInterpolationMode = PhysicsInterpolationModeEnum.Off; // moved on input, not on the physics tick Current = true; - Place(); + // The startup view: yaw 0.35, pitch 0.2, 2.4 m from the hold point. + var centre = new Vector3(BodyPainBox.HoldPoint.X, BodyPainBox.HoldPoint.Y, BodyPainBox.HoldPoint.Z); + const float yaw = 0.35f, pitch = 0.2f; + var offset = new Vector3(MathF.Sin(yaw) * MathF.Cos(pitch), MathF.Sin(pitch), MathF.Cos(yaw) * MathF.Cos(pitch)); + Commit(Basis.LookingAt(-offset, Vector3.Up), centre, _distance); } public override void _UnhandledInput(InputEvent e) { switch (e) { - case InputEventMouseMotion m when (m.ButtonMask & MouseButtonMask.Right) != 0: - _yaw -= m.Relative.X * 0.006f; - _pitch = Math.Clamp(_pitch + m.Relative.Y * 0.006f, -0.2f, 1.4f); - Place(); + case InputEventMouseButton { Pressed: true, ButtonIndex: MouseButton.WheelUp or MouseButton.WheelDown } wheel: + if (wheel.AltPressed || wheel.ShiftPressed || wheel.CtrlPressed) return; + Distance = wheel.ButtonIndex == MouseButton.WheelUp ? _distance / MathF.Pow(ZoomStep, wheel.Factor) : _distance * MathF.Pow(ZoomStep, wheel.Factor); + Handle(); break; - case InputEventMouseButton { Pressed: true, ButtonIndex: MouseButton.WheelUp }: - _distance = Math.Max(0.6f, _distance * 0.9f); - Place(); + case InputEventMouseButton { Pressed: true } press: + foreach (var row in Bindings) + { + if (row.Button != press.ButtonIndex || row.Alt != press.AltPressed || row.Shift != press.ShiftPressed || row.Ctrl != press.CtrlPressed) continue; + if (row.Emulated && !Emulate3ButtonMouse) continue; + var pivot = row.Gesture == Gesture.Orbit ? Pivot?.Invoke() ?? ViewCentre : ViewCentre; + _drag = new Drag(row.Gesture, row.Button, pivot, Basis.Y.Dot(Vector3.Up) < 0 ? -1f : 1f); + Handle(); + break; + } break; - case InputEventMouseButton { Pressed: true, ButtonIndex: MouseButton.WheelDown }: - _distance = Math.Min(12f, _distance * 1.1f); - Place(); + case InputEventMouseButton { Pressed: false } release when _drag is { } drag && drag.Button == release.ButtonIndex: + _drag = null; + Handle(); + break; + case InputEventMouseMotion motion when _drag is { } drag: + var held = drag.Button == MouseButton.Left ? MouseButtonMask.Left : MouseButtonMask.Middle; + if ((motion.ButtonMask & held) == 0) { _drag = null; return; } // the release was lost, to a focus change say + Apply(drag, motion.Relative); + Handle(); break; } } - private void Place() + private void Handle() => GetViewport().SetInputAsHandled(); + + private void Apply(Drag drag, Vector2 move) { - var offset = new Vector3(MathF.Sin(_yaw) * MathF.Cos(_pitch), MathF.Sin(_pitch), MathF.Cos(_yaw) * MathF.Cos(_pitch)) * _distance; - Position = _target + offset; - LookAt(_target, Vector3.Up); + switch (drag.Gesture) + { + case Gesture.Orbit: + // Turntable: yaw about the world's up, pitch about the view's right, both about the pivot, unclamped. + float step = Mathf.DegToRad(DegreesPerPixel); + var turn = new Basis(Vector3.Up, -move.X * step * drag.Reverse) * new Basis(Basis.X, -move.Y * step); + Commit(turn * Basis, drag.Pivot + turn * (ViewCentre - drag.Pivot), _distance); + break; + case Gesture.Pan: + // The view-centre plane follows the cursor. + float perPixel = 2f * _distance * MathF.Tan(Mathf.DegToRad(Fov) / 2f) / GetViewport().GetVisibleRect().Size.Y; + Commit(Basis, ViewCentre + (-move.X * Basis.X + move.Y * Basis.Y) * perPixel, _distance); + break; + case Gesture.Zoom: + Distance = _distance * MathF.Pow(ZoomStep, move.Y / ZoomDragPixels); + break; + } + } + + /// The one writer of the camera's transform: looking at from away. + private void Commit(Basis basis, Vector3 centre, float distance) + { + _distance = distance; + basis = basis.Orthonormalized(); + Transform = new Transform3D(basis, centre + basis.Z * distance); } }