Add Mimir scene editor control surface
This commit is contained in:
parent
f868ddf18d
commit
4108f000b8
4 changed files with 786 additions and 12 deletions
53
docs/scene-editor-control-surface.md
Normal file
53
docs/scene-editor-control-surface.md
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
# Mimir Scene Editor Control Surface
|
||||
|
||||
Mimir now has a runtime-owned scene editor state for arranging the visible
|
||||
presentation inputs inside the Fensalir-hosted Mimir window. This editor is not
|
||||
the OBS program output. It is an operator surface for placing source panels,
|
||||
text panels, and imported-model placeholders before later renderer cuts turn
|
||||
those nodes into final composited pixels or volumetric field claims.
|
||||
|
||||
## Authority Map
|
||||
|
||||
- Owner: `MimirSceneEditorState` owns editor camera, scene nodes, selected node,
|
||||
visibility, lock state, 2D-plane transform, and active gizmo mode.
|
||||
- Inputs: rolling video buffers provide sensor-feed nodes; Fensalir `InputState`
|
||||
provides mouse/key edits; the Mimir UI provides visibility, reset, transform,
|
||||
text, and model-path commands.
|
||||
- Outputs: `MimirRuntime` reads the editor state into the Fensalir frame camera,
|
||||
spline outlines, selection gizmos, and SDF handle markers.
|
||||
- Derived state: the hierarchy text, selected-node readout, editor splines, and
|
||||
handle SDF objects are derived from the editor graph. They do not own node
|
||||
truth.
|
||||
- Forbidden writers: program-composite controls, OBS source endpoints, bridge
|
||||
scripts, and FieldEvidence surface intents do not decide editor transforms.
|
||||
- Shared paths: UI buttons/sliders and mouse-drag gizmos all commit through
|
||||
`MimirSceneEditorState`; reset returns to each node's stored default
|
||||
transform.
|
||||
- Deletion line: do not add a separate transform cache in the renderer or OBS
|
||||
path. Fensalir may later render richer node types, but it should consume this
|
||||
graph or a deliberate successor, not invent a competing editor graph.
|
||||
|
||||
## Current Controls
|
||||
|
||||
- `Mimir Editor` panel exposes the scene graph, selected-node visibility and
|
||||
lock toggles, transform reset, camera reset, and transform sliders.
|
||||
- Active gizmo modes are `Grab`, `Rotate`, and `Resize`; keys `1`, `2`, and `3`
|
||||
switch modes, and left-drag applies the active mode to the selected node.
|
||||
- Sensor-feed nodes are created from live video rolling buffers.
|
||||
- SDF text panels and model placeholders can be added from the panel.
|
||||
- `--scene-editor-smoke` proves graph creation, visibility toggles, transform
|
||||
reset, mouse-drag transform edits, text-node creation, model placeholder
|
||||
import, and derived gizmo geometry.
|
||||
|
||||
## Renderer Hooks Still Open
|
||||
|
||||
- World SDF text: Fensalir currently has DirectWrite overlay text and SDF object
|
||||
proxies. Scene text needs a renderer-owned SDF/MSDF glyph atlas plus billboard
|
||||
or mesh text lowering before text glyphs are truly drawn inside the scene.
|
||||
- Imported models: Fensalir resource contracts already know mesh packages, but
|
||||
selected mesh draw/material lowerings are still future work. The current
|
||||
import control creates graph nodes with paths; it does not decode ASSIMP
|
||||
meshes or upload vertex/index resources yet.
|
||||
- Pixel-accurate gizmo hit-testing: current gizmo edits apply to the selected
|
||||
node. Fensalir needs an editor hit-test path before handles can be clicked
|
||||
directly in world space.
|
||||
|
|
@ -84,6 +84,11 @@ if (args.Any(arg => string.Equals(arg, "--presentation-control-smoke", StringCom
|
|||
return RunPresentationControlSmoke();
|
||||
}
|
||||
|
||||
if (args.Any(arg => string.Equals(arg, "--scene-editor-smoke", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return RunSceneEditorSmoke();
|
||||
}
|
||||
|
||||
if (args.Any(arg => string.Equals(arg, "--audio-stem-publication-smoke", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return RunAudioStemPublicationSmoke();
|
||||
|
|
@ -1980,6 +1985,59 @@ static int RunPresentationControlSmoke()
|
|||
: 1;
|
||||
}
|
||||
|
||||
static int RunSceneEditorSmoke()
|
||||
{
|
||||
var editor = new MimirSceneEditorState();
|
||||
var camera = NewVideoBuffer("camera-main", MimirStreamOrigin.LocalDevice, 1_000_000_000, 1280, 720, MimirVideoPixelFormat.Bgra8, 1);
|
||||
var display = NewVideoBuffer("raven-display", MimirStreamOrigin.Network, 1_000_000_000, 1920, 1080, MimirVideoPixelFormat.Bgra8, 2, "raven-sync");
|
||||
editor.SyncSensorFeeds([camera, display]);
|
||||
|
||||
var displayNode = editor.Nodes.First(node => string.Equals(node.SourceId, "raven-display", StringComparison.Ordinal));
|
||||
editor.SelectNode(displayNode.Id);
|
||||
editor.SetSelectedVisible(false);
|
||||
editor.SetSelectedVisible(true);
|
||||
editor.SetSelectedX(1.25f);
|
||||
editor.SetSelectedY(-0.5f);
|
||||
editor.SetSelectedRotation(0.75f);
|
||||
editor.SetSelectedScaleX(3.0f);
|
||||
editor.ResetSelectedTransform();
|
||||
|
||||
var input = new InputState();
|
||||
input.SetMousePosition(new Vector2(100.0f, 100.0f));
|
||||
input.SetMouseButton(MouseButton.Left, true);
|
||||
editor.UpdateInput(0.016f, input);
|
||||
input.BeginFrame();
|
||||
input.SetMousePosition(new Vector2(130.0f, 82.0f));
|
||||
editor.UpdateInput(0.016f, input);
|
||||
|
||||
editor.PendingText = "Signal";
|
||||
editor.AddSdfTextPanel();
|
||||
editor.PendingModelPath = "assets/models/raven-stage.glb";
|
||||
editor.ImportModelPlaceholder();
|
||||
|
||||
var hierarchy = editor.DescribeHierarchy();
|
||||
var splineFrame = editor.BuildEditorSplineFrame();
|
||||
var handles = editor.BuildEditorSdfObjects();
|
||||
var hasDisplay = hierarchy.Contains("raven-display", StringComparison.Ordinal);
|
||||
var hasText = editor.Nodes.Any(node => node.Kind == MimirSceneEditorNodeKind.SdfTextPanel && string.Equals(node.Text, "Signal", StringComparison.Ordinal));
|
||||
var hasModel = editor.Nodes.Any(node => node.Kind == MimirSceneEditorNodeKind.Model && string.Equals(node.ModelPath, "assets/models/raven-stage.glb", StringComparison.Ordinal));
|
||||
var reset = Math.Abs(displayNode.Transform.Position.X - displayNode.DefaultTransform.Position.X) > 0.001f;
|
||||
|
||||
Console.WriteLine(
|
||||
$"scene-editor-smoke nodes={editor.Nodes.Count} feeds={editor.Nodes.Count(node => node.Kind == MimirSceneEditorNodeKind.SensorFeedPanel)} " +
|
||||
$"splines={splineFrame.Splines.Count} handles={handles.Count} selected={editor.SelectedNode?.DisplayName} hasDisplay={hasDisplay} hasText={hasText} hasModel={hasModel} movedAfterReset={reset}");
|
||||
|
||||
return editor.Nodes.Count >= 5 &&
|
||||
hasDisplay &&
|
||||
hasText &&
|
||||
hasModel &&
|
||||
reset &&
|
||||
splineFrame.HasInput &&
|
||||
handles.Count == 4
|
||||
? 0
|
||||
: 1;
|
||||
}
|
||||
|
||||
static int RunAudioStemPublicationSmoke()
|
||||
{
|
||||
var publication = new MimirObsStemPublicationState(MimirObsPublicationConfigurations.AlignmentActuatorStemBus);
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
private readonly MimirAudioSpectrumAnalyzer spectrumAnalyzer;
|
||||
private readonly MimirAlignmentActuatorBank audioActuatorBank = new();
|
||||
private readonly MimirPresentationControlState presentationControls = new();
|
||||
private readonly MimirSceneEditorState sceneEditor = new();
|
||||
private readonly MimirObsStemPublicationState obsStemPublication = new(MimirObsPublicationConfigurations.AlignmentActuatorStemBus);
|
||||
private readonly MimirObsStemSharedMemoryPublisher? obsStemPublisher;
|
||||
private readonly IReadOnlyList<MimirStreamSourceFactory> sourceFactories;
|
||||
|
|
@ -144,7 +145,8 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
BloomIntensity: 0.075f,
|
||||
BloomVeilIntensity: 0.0f,
|
||||
FieldReservoirMode: GraphicsSettings.FieldReservoirModeNativeDomain,
|
||||
FieldReservoirScale: 0.5f);
|
||||
FieldReservoirScale: 0.5f,
|
||||
FieldReservoirSpatialReuseBudget: GraphicsSettings.Default.FieldReservoirSpatialReuseBudget);
|
||||
}
|
||||
|
||||
obsStemPublisher = CreateObsStemPublisher();
|
||||
|
|
@ -182,25 +184,43 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
{
|
||||
var channelCount = Math.Max(1, lastAudioSpectra.Count);
|
||||
var fieldEvidenceFrame = BuildFieldEvidenceFrame();
|
||||
var editorSplineFrame = sceneEditor.Enabled
|
||||
? sceneEditor.BuildEditorSplineFrame()
|
||||
: AquariumSplineFrame.Empty;
|
||||
var editorSdfObjects = sceneEditor.Enabled
|
||||
? sceneEditor.BuildEditorSdfObjects()
|
||||
: [];
|
||||
var editorSdfLights = sceneEditor.Enabled
|
||||
? sceneEditor.BuildEditorSdfLights()
|
||||
: [];
|
||||
var windowCount = Math.Max(1, spectrumHistory.Count);
|
||||
var (splineMin, splineMax) = syntheticSingleTubePreview
|
||||
? (new Vector3(-5.76643f, -1.0f, 0.0f), new Vector3(5.76643f, 1.0f, 0.0f))
|
||||
: SpectrumSplineAabb(channelCount, windowCount);
|
||||
var cameraTarget = (splineMin + splineMax) * 0.5f;
|
||||
var cameraPosition = syntheticSingleTubePreview
|
||||
var spectrumCameraTarget = (splineMin + splineMax) * 0.5f;
|
||||
var spectrumCameraPosition = syntheticSingleTubePreview
|
||||
? new Vector3(0.0f, 0.0f, -18.367355f)
|
||||
: SpectrumCameraPosition(
|
||||
channelCount,
|
||||
windowCount,
|
||||
cameraTarget,
|
||||
spectrumCameraTarget,
|
||||
spectrumCameraDistanceMultiplier,
|
||||
spectrumCameraAngleDegrees);
|
||||
var cameraFrustum = syntheticSingleTubePreview
|
||||
var spectrumCameraFrustum = syntheticSingleTubePreview
|
||||
? new AquariumCameraFrustum(-0.036f, 0.036f, -0.02025f, 0.02025f, 0.1f, 100.0f)
|
||||
: FitSpectrumCameraFrustum(splineMin, splineMax, cameraPosition, cameraTarget);
|
||||
lastSpectrumFrustum = cameraFrustum;
|
||||
lastSpectrumCameraPosition = cameraPosition;
|
||||
lastSpectrumCameraTarget = cameraTarget;
|
||||
: FitSpectrumCameraFrustum(splineMin, splineMax, spectrumCameraPosition, spectrumCameraTarget);
|
||||
var cameraTarget = sceneEditor.Enabled
|
||||
? sceneEditor.CameraTarget
|
||||
: spectrumCameraTarget;
|
||||
var cameraPosition = sceneEditor.Enabled
|
||||
? sceneEditor.CameraPosition
|
||||
: spectrumCameraPosition;
|
||||
var cameraFrustum = sceneEditor.Enabled
|
||||
? sceneEditor.CameraFrustum
|
||||
: spectrumCameraFrustum;
|
||||
lastSpectrumFrustum = spectrumCameraFrustum;
|
||||
lastSpectrumCameraPosition = spectrumCameraPosition;
|
||||
lastSpectrumCameraTarget = spectrumCameraTarget;
|
||||
lastSpectrumAabb = (splineMin, splineMax);
|
||||
return new AquariumFrame(
|
||||
new ViewFrame(Vector2.Zero, 24.0f) { Frustum = cameraFrustum },
|
||||
|
|
@ -213,14 +233,28 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
TraceHeightFieldSurface = false,
|
||||
UseStudioBackground = !syntheticSingleTubePreview,
|
||||
UseStarfieldBackground = obsProofVisualEnabled,
|
||||
SdfObjects = obsProofVisualEnabled ? BuildObsProofSdfObjects(runtimeSeconds) : [],
|
||||
SdfLights = obsProofVisualEnabled ? BuildObsProofSdfLights(runtimeSeconds) : [],
|
||||
SdfObjects = MergeSdfObjects(
|
||||
obsProofVisualEnabled ? BuildObsProofSdfObjects(runtimeSeconds) : [],
|
||||
editorSdfObjects),
|
||||
SdfLights = MergeSdfLights(
|
||||
obsProofVisualEnabled ? BuildObsProofSdfLights(runtimeSeconds) : [],
|
||||
editorSdfLights),
|
||||
FieldEvidenceFrame = fieldEvidenceFrame,
|
||||
BufferFieldFrame = AquariumBufferFieldFrame.Empty,
|
||||
SplineFrame = AquariumSplineFrame.Empty,
|
||||
SplineFrame = editorSplineFrame,
|
||||
});
|
||||
}
|
||||
|
||||
private static IReadOnlyList<AquariumSdfObject> MergeSdfObjects(
|
||||
IReadOnlyList<AquariumSdfObject> first,
|
||||
IReadOnlyList<AquariumSdfObject> second) =>
|
||||
first.Count == 0 ? second : second.Count == 0 ? first : first.Concat(second).ToArray();
|
||||
|
||||
private static IReadOnlyList<AquariumSdfLight> MergeSdfLights(
|
||||
IReadOnlyList<AquariumSdfLight> first,
|
||||
IReadOnlyList<AquariumSdfLight> second) =>
|
||||
first.Count == 0 ? second : second.Count == 0 ? first : first.Concat(second).ToArray();
|
||||
|
||||
private static AquariumSdfObject[] BuildObsProofSdfObjects(float timeSeconds)
|
||||
{
|
||||
var center = new Vector3(MathF.Sin(timeSeconds * 0.8f) * 1.2f, 0.25f, 0.0f);
|
||||
|
|
@ -726,6 +760,8 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
runtimeSeconds += Math.Max(deltaSeconds, 0.0f);
|
||||
lastPollCount = synchronization.PollSources();
|
||||
presentationControls.SyncFromBuffers(synchronization.Buffers.Buffers);
|
||||
sceneEditor.SyncSensorFeeds(synchronization.Buffers.Buffers);
|
||||
sceneEditor.UpdateInput(deltaSeconds, input);
|
||||
ApplyPresentationPostprocess();
|
||||
UpdateObsStemPublication();
|
||||
QueueCalibrationTimeline();
|
||||
|
|
@ -830,6 +866,39 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
}, 0.0f, 1.0f, "0.00");
|
||||
panel.Readout("Post", DescribePostprocess);
|
||||
})
|
||||
.Panel("Mimir Editor", 18.0f, 508.0f, 430.0f, fadeWhenMouseDistant: true, panel =>
|
||||
{
|
||||
panel.Section("Scene Graph");
|
||||
panel.Toggle("Editor View", () => sceneEditor.Enabled, value => sceneEditor.Enabled = value, "The Mimir window shows the editor camera and gizmos. This is not the OBS program output.");
|
||||
panel.TreeItem("Scene", 0, true, () => sceneEditor.IsExpanded("scene"), value => sceneEditor.SetExpanded("scene", value), () => false, () => { }, "editor root");
|
||||
panel.TreeItem("Editor Camera", 1, false, () => false, _ => { }, () => sceneEditor.SelectedNodeId == "editor-camera", () => sceneEditor.SelectNode("editor-camera"), "view camera", isVisible: () => sceneEditor.IsExpanded("scene"));
|
||||
panel.TreeItem("Sensor Feeds", 1, true, () => sceneEditor.IsExpanded("feeds"), value => sceneEditor.SetExpanded("feeds", value), () => false, () => { }, "video panels", isVisible: () => sceneEditor.IsExpanded("scene"));
|
||||
panel.TreeItem("SDF Text", 1, true, () => sceneEditor.IsExpanded("text"), value => sceneEditor.SetExpanded("text", value), () => false, () => { }, "text panels", isVisible: () => sceneEditor.IsExpanded("scene"));
|
||||
panel.TreeItem("Models", 1, true, () => sceneEditor.IsExpanded("models"), value => sceneEditor.SetExpanded("models", value), () => false, () => { }, "import placeholders", isVisible: () => sceneEditor.IsExpanded("scene"));
|
||||
panel.TextBox("Hierarchy", sceneEditor.DescribeHierarchy, _ => { }, lines: 9, acceptsReturn: false, monospace: true);
|
||||
panel.Button("Previous", sceneEditor.SelectPrevious);
|
||||
panel.Button("Next", sceneEditor.SelectNext);
|
||||
panel.Readout("Selected", sceneEditor.DescribeSelection);
|
||||
panel.Toggle("Visible", () => sceneEditor.SelectedNode?.Visible ?? false, sceneEditor.SetSelectedVisible);
|
||||
panel.Toggle("Locked", () => sceneEditor.SelectedNode?.Locked ?? false, sceneEditor.SetSelectedLocked);
|
||||
panel.Button("Reset Transform", sceneEditor.ResetSelectedTransform);
|
||||
panel.Button("Reset Camera", sceneEditor.ResetCamera);
|
||||
|
||||
panel.Section("Gizmo");
|
||||
panel.Options("Mode", () => (int)sceneEditor.GizmoMode, value => sceneEditor.GizmoMode = (MimirSceneEditorGizmoMode)value, SceneEditorGizmoOptions(), "1 translate, 2 rotate, 3 resize. Left-drag applies the active gizmo to the selected node.");
|
||||
panel.Slider("X", () => sceneEditor.SelectedNode?.Transform.Position.X ?? 0.0f, sceneEditor.SetSelectedX, -12.0f, 12.0f, "0.00");
|
||||
panel.Slider("Y", () => sceneEditor.SelectedNode?.Transform.Position.Y ?? 0.0f, sceneEditor.SetSelectedY, -8.0f, 8.0f, "0.00");
|
||||
panel.Slider("Z", () => sceneEditor.SelectedNode?.Transform.Position.Z ?? 0.0f, sceneEditor.SetSelectedZ, -8.0f, 8.0f, "0.00");
|
||||
panel.Slider("Rotation", () => sceneEditor.SelectedNode?.Transform.RotationRadians ?? 0.0f, sceneEditor.SetSelectedRotation, -MathF.PI, MathF.PI, "0.00");
|
||||
panel.Slider("Scale X", () => sceneEditor.SelectedNode?.Transform.Scale.X ?? 1.0f, sceneEditor.SetSelectedScaleX, 0.05f, 8.0f, "0.00");
|
||||
panel.Slider("Scale Y", () => sceneEditor.SelectedNode?.Transform.Scale.Y ?? 1.0f, sceneEditor.SetSelectedScaleY, 0.05f, 8.0f, "0.00");
|
||||
|
||||
panel.Section("Create");
|
||||
panel.TextBox("Text", () => sceneEditor.PendingText, value => sceneEditor.PendingText = value, lines: 2, acceptsReturn: true);
|
||||
panel.Button("Add SDF Text", sceneEditor.AddSdfTextPanel, "Creates a scene-owned text panel. Fensalir still needs a world SDF text renderer before glyphs are drawn in the scene.");
|
||||
panel.TextBox("Model Path", () => sceneEditor.PendingModelPath, value => sceneEditor.PendingModelPath = value, lines: 1, acceptsReturn: false);
|
||||
panel.Button("Import Model", sceneEditor.ImportModelPlaceholder, "Creates a model node now. Mesh decoding and draw resource ownership are the pending Fensalir/ASSIMP cut.");
|
||||
})
|
||||
.Panel("Mimir Sync", 392.0f, 82.0f, 390.0f, fadeWhenMouseDistant: true, panel =>
|
||||
{
|
||||
panel.Section("Rolling Buffers");
|
||||
|
|
@ -1058,6 +1127,13 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
.Select((preset, index) => new AquariumUiOption(index, preset.DisplayName))
|
||||
.ToArray();
|
||||
|
||||
private static IReadOnlyList<AquariumUiOption> SceneEditorGizmoOptions() =>
|
||||
[
|
||||
new AquariumUiOption((int)MimirSceneEditorGizmoMode.Translate, "Grab"),
|
||||
new AquariumUiOption((int)MimirSceneEditorGizmoMode.Rotate, "Rotate"),
|
||||
new AquariumUiOption((int)MimirSceneEditorGizmoMode.Scale, "Resize"),
|
||||
];
|
||||
|
||||
private string DescribeProgramVideo()
|
||||
{
|
||||
var feeds = presentationControls.VideoFeeds;
|
||||
|
|
|
|||
587
src/Mimir.Runtime/Synchronization/MimirSceneEditorState.cs
Normal file
587
src/Mimir.Runtime/Synchronization/MimirSceneEditorState.cs
Normal file
|
|
@ -0,0 +1,587 @@
|
|||
using System.Numerics;
|
||||
using Aquarium.Engine;
|
||||
using Aquarium.Engine.Input;
|
||||
using Aquarium.Engine.Render;
|
||||
|
||||
namespace Mimir.Runtime.Synchronization;
|
||||
|
||||
public sealed class MimirSceneEditorState
|
||||
{
|
||||
private readonly List<MimirSceneEditorNode> nodes = [];
|
||||
private readonly Dictionary<string, bool> expandedGroups = new(StringComparer.Ordinal)
|
||||
{
|
||||
["scene"] = true,
|
||||
["feeds"] = true,
|
||||
["text"] = true,
|
||||
["models"] = true,
|
||||
};
|
||||
|
||||
private string pendingModelPath = "assets/models/example.glb";
|
||||
private string pendingText = "Mimir";
|
||||
private Vector2? previousMousePosition;
|
||||
|
||||
public MimirSceneEditorState()
|
||||
{
|
||||
nodes.Add(new MimirSceneEditorNode(
|
||||
"editor-camera",
|
||||
"",
|
||||
"Editor Camera",
|
||||
MimirSceneEditorNodeKind.Camera,
|
||||
new MimirSceneEditorTransform(new Vector3(0.0f, 0.0f, -18.0f), 0.0f, Vector2.One)));
|
||||
nodes.Add(new MimirSceneEditorNode(
|
||||
"text-title",
|
||||
"",
|
||||
"SDF Text Panel",
|
||||
MimirSceneEditorNodeKind.SdfTextPanel,
|
||||
new MimirSceneEditorTransform(new Vector3(-3.2f, 2.1f, 0.0f), 0.0f, new Vector2(2.4f, 0.8f)))
|
||||
{
|
||||
Text = pendingText,
|
||||
});
|
||||
SelectedNodeId = "text-title";
|
||||
}
|
||||
|
||||
public bool Enabled { get; set; } = true;
|
||||
|
||||
public MimirSceneEditorGizmoMode GizmoMode { get; set; } = MimirSceneEditorGizmoMode.Translate;
|
||||
|
||||
public string SelectedNodeId { get; private set; }
|
||||
|
||||
public string PendingModelPath
|
||||
{
|
||||
get => pendingModelPath;
|
||||
set => pendingModelPath = value.Trim();
|
||||
}
|
||||
|
||||
public string PendingText
|
||||
{
|
||||
get => pendingText;
|
||||
set => pendingText = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<MimirSceneEditorNode> Nodes => nodes;
|
||||
|
||||
public MimirSceneEditorNode? SelectedNode =>
|
||||
nodes.FirstOrDefault(node => string.Equals(node.Id, SelectedNodeId, StringComparison.Ordinal));
|
||||
|
||||
public IReadOnlyList<MimirSceneEditorNode> VisibleNodes =>
|
||||
nodes.Where(node => node.Visible).ToArray();
|
||||
|
||||
public Vector3 CameraPosition => nodes
|
||||
.First(node => node.Kind == MimirSceneEditorNodeKind.Camera)
|
||||
.Transform.Position;
|
||||
|
||||
public Vector3 CameraTarget { get; private set; } = Vector3.Zero;
|
||||
|
||||
public AquariumCameraFrustum CameraFrustum { get; private set; } =
|
||||
new(-0.85f, 0.85f, -0.48f, 0.48f, 0.1f, 120.0f);
|
||||
|
||||
public void SyncSensorFeeds(IEnumerable<MimirRollingStreamBuffer> buffers)
|
||||
{
|
||||
var feeds = buffers
|
||||
.Where(static buffer => buffer.Descriptor.Kind == MimirStreamKind.Video)
|
||||
.OrderBy(static buffer => buffer.Descriptor.SourceId, StringComparer.Ordinal)
|
||||
.ToArray();
|
||||
for (var index = 0; index < feeds.Length; index++)
|
||||
{
|
||||
var buffer = feeds[index];
|
||||
var id = FeedNodeId(buffer.Descriptor.SourceId);
|
||||
if (nodes.Any(node => string.Equals(node.Id, id, StringComparison.Ordinal)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var row = index / 3;
|
||||
var column = index % 3;
|
||||
nodes.Add(new MimirSceneEditorNode(
|
||||
id,
|
||||
"feeds",
|
||||
buffer.Descriptor.Label,
|
||||
MimirSceneEditorNodeKind.SensorFeedPanel,
|
||||
new MimirSceneEditorTransform(
|
||||
new Vector3(-3.6f + column * 3.6f, 0.9f - row * 2.2f, 0.0f),
|
||||
0.0f,
|
||||
new Vector2(2.8f, 1.6f)))
|
||||
{
|
||||
SourceId = buffer.Descriptor.SourceId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateInput(float deltaSeconds, InputState input)
|
||||
{
|
||||
if (!Enabled)
|
||||
{
|
||||
previousMousePosition = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (input.IsKeyPressed(KeyCode.Digit1))
|
||||
{
|
||||
GizmoMode = MimirSceneEditorGizmoMode.Translate;
|
||||
}
|
||||
else if (input.IsKeyPressed(KeyCode.Digit2))
|
||||
{
|
||||
GizmoMode = MimirSceneEditorGizmoMode.Rotate;
|
||||
}
|
||||
else if (input.IsKeyPressed(KeyCode.Digit3))
|
||||
{
|
||||
GizmoMode = MimirSceneEditorGizmoMode.Scale;
|
||||
}
|
||||
|
||||
if (input.IsKeyPressed(KeyCode.LeftArrow))
|
||||
{
|
||||
SelectPrevious();
|
||||
}
|
||||
else if (input.IsKeyPressed(KeyCode.RightArrow))
|
||||
{
|
||||
SelectNext();
|
||||
}
|
||||
|
||||
if (input.IsKeyPressed(KeyCode.Home))
|
||||
{
|
||||
ResetSelectedTransform();
|
||||
}
|
||||
|
||||
if (input.IsKeyPressed(KeyCode.Delete))
|
||||
{
|
||||
RemoveSelectedMutableNode();
|
||||
}
|
||||
|
||||
if (MathF.Abs(input.WheelDelta) > 0.0f)
|
||||
{
|
||||
var camera = nodes.First(node => node.Kind == MimirSceneEditorNodeKind.Camera);
|
||||
var cameraPosition = camera.Transform.Position;
|
||||
camera.Transform = camera.Transform with
|
||||
{
|
||||
Position = new Vector3(
|
||||
cameraPosition.X,
|
||||
cameraPosition.Y,
|
||||
Math.Clamp(cameraPosition.Z + input.WheelDelta * 0.75f, -60.0f, -4.0f)),
|
||||
};
|
||||
}
|
||||
|
||||
if (input.LeftMouseDown && previousMousePosition.HasValue && SelectedNode is { } selected && !selected.Locked)
|
||||
{
|
||||
var delta = input.MousePosition - previousMousePosition.Value;
|
||||
ApplyScreenDelta(selected, delta, Math.Max(deltaSeconds, 0.0001f));
|
||||
}
|
||||
|
||||
previousMousePosition = input.LeftMouseDown
|
||||
? input.MousePosition
|
||||
: null;
|
||||
}
|
||||
|
||||
public bool IsExpanded(string groupId) =>
|
||||
expandedGroups.TryGetValue(groupId, out var expanded) && expanded;
|
||||
|
||||
public void SetExpanded(string groupId, bool expanded)
|
||||
{
|
||||
expandedGroups[groupId] = expanded;
|
||||
}
|
||||
|
||||
public void SelectNode(string nodeId)
|
||||
{
|
||||
if (nodes.Any(node => string.Equals(node.Id, nodeId, StringComparison.Ordinal)))
|
||||
{
|
||||
SelectedNodeId = nodeId;
|
||||
}
|
||||
}
|
||||
|
||||
public void SelectNext() => SelectRelative(1);
|
||||
|
||||
public void SelectPrevious() => SelectRelative(-1);
|
||||
|
||||
public void SetSelectedVisible(bool visible)
|
||||
{
|
||||
if (SelectedNode is { } selected)
|
||||
{
|
||||
selected.Visible = visible;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetSelectedLocked(bool locked)
|
||||
{
|
||||
if (SelectedNode is { } selected)
|
||||
{
|
||||
selected.Locked = locked;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetSelectedX(float value) => SetSelectedPosition(axis: 0, value);
|
||||
|
||||
public void SetSelectedY(float value) => SetSelectedPosition(axis: 1, value);
|
||||
|
||||
public void SetSelectedZ(float value) => SetSelectedPosition(axis: 2, value);
|
||||
|
||||
public void SetSelectedRotation(float value)
|
||||
{
|
||||
if (SelectedNode is { } selected && !selected.Locked)
|
||||
{
|
||||
selected.Transform = selected.Transform with { RotationRadians = value };
|
||||
}
|
||||
}
|
||||
|
||||
public void SetSelectedScaleX(float value)
|
||||
{
|
||||
if (SelectedNode is { } selected && !selected.Locked)
|
||||
{
|
||||
selected.Transform = selected.Transform with
|
||||
{
|
||||
Scale = selected.Transform.Scale with { X = Math.Clamp(value, 0.05f, 8.0f) },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public void SetSelectedScaleY(float value)
|
||||
{
|
||||
if (SelectedNode is { } selected && !selected.Locked)
|
||||
{
|
||||
selected.Transform = selected.Transform with
|
||||
{
|
||||
Scale = selected.Transform.Scale with { Y = Math.Clamp(value, 0.05f, 8.0f) },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetSelectedTransform()
|
||||
{
|
||||
if (SelectedNode is { } selected && !selected.Locked)
|
||||
{
|
||||
selected.Transform = selected.DefaultTransform;
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetCamera()
|
||||
{
|
||||
var camera = nodes.First(node => node.Kind == MimirSceneEditorNodeKind.Camera);
|
||||
camera.Transform = camera.DefaultTransform;
|
||||
CameraTarget = Vector3.Zero;
|
||||
}
|
||||
|
||||
public void AddSdfTextPanel()
|
||||
{
|
||||
var index = nodes.Count(node => node.Kind == MimirSceneEditorNodeKind.SdfTextPanel) + 1;
|
||||
var node = new MimirSceneEditorNode(
|
||||
$"text-{index}",
|
||||
"text",
|
||||
$"SDF Text {index}",
|
||||
MimirSceneEditorNodeKind.SdfTextPanel,
|
||||
new MimirSceneEditorTransform(new Vector3(-2.8f + index * 0.25f, 1.7f - index * 0.25f, 0.0f), 0.0f, new Vector2(2.4f, 0.8f)))
|
||||
{
|
||||
Text = string.IsNullOrWhiteSpace(pendingText) ? "Text" : pendingText,
|
||||
};
|
||||
nodes.Add(node);
|
||||
SelectedNodeId = node.Id;
|
||||
}
|
||||
|
||||
public void ImportModelPlaceholder()
|
||||
{
|
||||
var modelPath = string.IsNullOrWhiteSpace(pendingModelPath)
|
||||
? "assets/models/model.glb"
|
||||
: pendingModelPath;
|
||||
var index = nodes.Count(node => node.Kind == MimirSceneEditorNodeKind.Model) + 1;
|
||||
var node = new MimirSceneEditorNode(
|
||||
$"model-{index}",
|
||||
"models",
|
||||
Path.GetFileNameWithoutExtension(modelPath),
|
||||
MimirSceneEditorNodeKind.Model,
|
||||
new MimirSceneEditorTransform(new Vector3(2.4f, -1.5f + index * 0.2f, 0.0f), 0.0f, new Vector2(1.2f, 1.2f)))
|
||||
{
|
||||
ModelPath = modelPath,
|
||||
};
|
||||
nodes.Add(node);
|
||||
SelectedNodeId = node.Id;
|
||||
}
|
||||
|
||||
public string DescribeHierarchy()
|
||||
{
|
||||
var builder = new System.Text.StringBuilder();
|
||||
builder.AppendLine("Scene");
|
||||
AppendNode(builder, nodes.First(node => node.Kind == MimirSceneEditorNodeKind.Camera), 1);
|
||||
AppendGroup(builder, "feeds", "Sensor Feeds", MimirSceneEditorNodeKind.SensorFeedPanel);
|
||||
AppendGroup(builder, "text", "SDF Text", MimirSceneEditorNodeKind.SdfTextPanel);
|
||||
AppendGroup(builder, "models", "Models", MimirSceneEditorNodeKind.Model);
|
||||
return builder.ToString().TrimEnd();
|
||||
}
|
||||
|
||||
public string DescribeSelection()
|
||||
{
|
||||
if (SelectedNode is not { } selected)
|
||||
{
|
||||
return "no selection";
|
||||
}
|
||||
|
||||
var source = string.IsNullOrWhiteSpace(selected.SourceId) ? "" : $" source={selected.SourceId}";
|
||||
var model = string.IsNullOrWhiteSpace(selected.ModelPath) ? "" : $" model={selected.ModelPath}";
|
||||
return $"{selected.Kind} {selected.DisplayName}{source}{model} pos={selected.Transform.Position.X:0.00},{selected.Transform.Position.Y:0.00},{selected.Transform.Position.Z:0.00} rot={selected.Transform.RotationRadians:0.00} scale={selected.Transform.Scale.X:0.00},{selected.Transform.Scale.Y:0.00}";
|
||||
}
|
||||
|
||||
public AquariumSplineFrame BuildEditorSplineFrame()
|
||||
{
|
||||
var splines = new List<AquariumSpline3D>();
|
||||
foreach (var node in nodes.Where(static node => node.Visible && node.Kind != MimirSceneEditorNodeKind.Camera))
|
||||
{
|
||||
AppendNodeFrame(splines, node, IsSelected(node));
|
||||
}
|
||||
|
||||
if (SelectedNode is { Visible: true } selected && selected.Kind != MimirSceneEditorNodeKind.Camera)
|
||||
{
|
||||
AppendGizmo(splines, selected);
|
||||
}
|
||||
|
||||
return splines.Count == 0
|
||||
? AquariumSplineFrame.Empty
|
||||
: new AquariumSplineFrame { Splines = splines };
|
||||
}
|
||||
|
||||
public IReadOnlyList<AquariumSdfObject> BuildEditorSdfObjects()
|
||||
{
|
||||
if (SelectedNode is not { Visible: true } selected || selected.Kind == MimirSceneEditorNodeKind.Camera)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var corners = Corners(selected.Transform);
|
||||
return corners
|
||||
.Select((corner, index) => new AquariumSdfObject(
|
||||
new Vector4(corner, 0.075f),
|
||||
new Vector4(corner, 0.0f),
|
||||
new Vector4(0.2f + index * 0.02f, 0.0f, 0.0f, 0.0f)))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
public IReadOnlyList<AquariumSdfLight> BuildEditorSdfLights() =>
|
||||
[
|
||||
new AquariumSdfLight(new Vector4(-3.5f, 3.0f, -4.5f, 4.0f), new Vector4(1.0f, 1.0f, 1.0f, 4.0f)),
|
||||
new AquariumSdfLight(new Vector4(3.0f, -2.0f, -2.5f, 3.0f), new Vector4(0.2f, 0.7f, 1.0f, 2.5f)),
|
||||
];
|
||||
|
||||
private void AppendGroup(System.Text.StringBuilder builder, string groupId, string label, MimirSceneEditorNodeKind kind)
|
||||
{
|
||||
var groupNodes = nodes.Where(node => node.Kind == kind).OrderBy(node => node.DisplayName, StringComparer.Ordinal).ToArray();
|
||||
builder.Append(" ")
|
||||
.Append(IsExpanded(groupId) ? "v " : "> ")
|
||||
.Append(label)
|
||||
.Append(" (")
|
||||
.Append(groupNodes.Length)
|
||||
.AppendLine(")");
|
||||
if (!IsExpanded(groupId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var node in groupNodes)
|
||||
{
|
||||
AppendNode(builder, node, 2);
|
||||
}
|
||||
}
|
||||
|
||||
private void AppendNode(System.Text.StringBuilder builder, MimirSceneEditorNode node, int depth)
|
||||
{
|
||||
builder
|
||||
.Append(' ', depth * 2)
|
||||
.Append(IsSelected(node) ? "* " : "- ")
|
||||
.Append(node.Visible ? "[eye] " : "[off] ")
|
||||
.Append(node.DisplayName)
|
||||
.Append(" <")
|
||||
.Append(node.Kind)
|
||||
.AppendLine(">");
|
||||
}
|
||||
|
||||
private void SelectRelative(int delta)
|
||||
{
|
||||
if (nodes.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var index = Math.Max(0, nodes.FindIndex(node => string.Equals(node.Id, SelectedNodeId, StringComparison.Ordinal)));
|
||||
var next = (index + delta + nodes.Count) % nodes.Count;
|
||||
SelectedNodeId = nodes[next].Id;
|
||||
}
|
||||
|
||||
private void RemoveSelectedMutableNode()
|
||||
{
|
||||
if (SelectedNode is not { } selected || selected.Kind is MimirSceneEditorNodeKind.Camera or MimirSceneEditorNodeKind.SensorFeedPanel)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
nodes.Remove(selected);
|
||||
SelectedNodeId = nodes.First().Id;
|
||||
}
|
||||
|
||||
private void SetSelectedPosition(int axis, float value)
|
||||
{
|
||||
if (SelectedNode is not { } selected || selected.Locked)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var position = selected.Transform.Position;
|
||||
position = axis switch
|
||||
{
|
||||
0 => position with { X = value },
|
||||
1 => position with { Y = value },
|
||||
2 => position with { Z = value },
|
||||
_ => position,
|
||||
};
|
||||
selected.Transform = selected.Transform with { Position = position };
|
||||
}
|
||||
|
||||
private void ApplyScreenDelta(MimirSceneEditorNode selected, Vector2 screenDelta, float deltaSeconds)
|
||||
{
|
||||
var worldDelta = new Vector2(screenDelta.X, -screenDelta.Y) * 0.012f;
|
||||
switch (GizmoMode)
|
||||
{
|
||||
case MimirSceneEditorGizmoMode.Translate:
|
||||
selected.Transform = selected.Transform with
|
||||
{
|
||||
Position = selected.Transform.Position + new Vector3(worldDelta, 0.0f),
|
||||
};
|
||||
break;
|
||||
case MimirSceneEditorGizmoMode.Rotate:
|
||||
selected.Transform = selected.Transform with
|
||||
{
|
||||
RotationRadians = selected.Transform.RotationRadians + screenDelta.X * 0.012f,
|
||||
};
|
||||
break;
|
||||
case MimirSceneEditorGizmoMode.Scale:
|
||||
var scaleDelta = MathF.Exp((screenDelta.X - screenDelta.Y) * 0.006f);
|
||||
selected.Transform = selected.Transform with
|
||||
{
|
||||
Scale = Vector2.Clamp(selected.Transform.Scale * scaleDelta, new Vector2(0.05f), new Vector2(8.0f)),
|
||||
};
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsSelected(MimirSceneEditorNode node) =>
|
||||
string.Equals(node.Id, SelectedNodeId, StringComparison.Ordinal);
|
||||
|
||||
private static void AppendNodeFrame(List<AquariumSpline3D> splines, MimirSceneEditorNode node, bool selected)
|
||||
{
|
||||
var corners = Corners(node.Transform);
|
||||
var color = node.Kind switch
|
||||
{
|
||||
MimirSceneEditorNodeKind.SensorFeedPanel => new Vector4(0.25f, 0.85f, 1.0f, selected ? 1.0f : 0.62f),
|
||||
MimirSceneEditorNodeKind.SdfTextPanel => new Vector4(1.0f, 0.78f, 0.28f, selected ? 1.0f : 0.64f),
|
||||
MimirSceneEditorNodeKind.Model => new Vector4(0.8f, 0.6f, 1.0f, selected ? 1.0f : 0.58f),
|
||||
_ => new Vector4(0.8f, 0.8f, 0.8f, 0.6f),
|
||||
};
|
||||
var vertices = new[]
|
||||
{
|
||||
new AquariumSplineVertex(corners[0], color),
|
||||
new AquariumSplineVertex(corners[1], color),
|
||||
new AquariumSplineVertex(corners[2], color),
|
||||
new AquariumSplineVertex(corners[3], color),
|
||||
new AquariumSplineVertex(corners[0], color),
|
||||
};
|
||||
splines.Add(new AquariumSpline3D(
|
||||
$"mimir-editor-node:{node.Id}",
|
||||
vertices,
|
||||
new AquariumSplineStyle(selected ? 0.028f : 0.018f, selected ? 2.1f : 1.1f, color.W, 1.0f, 0.08f),
|
||||
CatmullRomSubdivisions: 1));
|
||||
}
|
||||
|
||||
private static void AppendGizmo(List<AquariumSpline3D> splines, MimirSceneEditorNode node)
|
||||
{
|
||||
var center = node.Transform.Position;
|
||||
splines.Add(Line($"mimir-editor-gizmo:{node.Id}:x", center, center + new Vector3(1.1f, 0.0f, 0.0f), new Vector4(1.0f, 0.22f, 0.18f, 1.0f)));
|
||||
splines.Add(Line($"mimir-editor-gizmo:{node.Id}:y", center, center + new Vector3(0.0f, 1.1f, 0.0f), new Vector4(0.25f, 1.0f, 0.32f, 1.0f)));
|
||||
var ring = new List<AquariumSplineVertex>();
|
||||
for (var index = 0; index <= 32; index++)
|
||||
{
|
||||
var angle = index / 32.0f * MathF.Tau;
|
||||
ring.Add(new AquariumSplineVertex(
|
||||
center + new Vector3(MathF.Cos(angle), MathF.Sin(angle), 0.0f) * 0.85f,
|
||||
new Vector4(0.95f, 0.72f, 0.2f, 0.72f)));
|
||||
}
|
||||
|
||||
splines.Add(new AquariumSpline3D(
|
||||
$"mimir-editor-gizmo:{node.Id}:rotate",
|
||||
ring,
|
||||
new AquariumSplineStyle(0.012f, 1.4f, 0.72f, 1.0f, 0.08f),
|
||||
CatmullRomSubdivisions: 1));
|
||||
}
|
||||
|
||||
private static AquariumSpline3D Line(string id, Vector3 start, Vector3 end, Vector4 color) =>
|
||||
new(
|
||||
id,
|
||||
[new AquariumSplineVertex(start, color), new AquariumSplineVertex(end, color)],
|
||||
new AquariumSplineStyle(0.022f, 2.0f, color.W, 1.0f, 0.08f),
|
||||
CatmullRomSubdivisions: 1);
|
||||
|
||||
private static Vector3[] Corners(MimirSceneEditorTransform transform)
|
||||
{
|
||||
var half = transform.Scale * 0.5f;
|
||||
var local = new[]
|
||||
{
|
||||
new Vector2(-half.X, -half.Y),
|
||||
new Vector2(half.X, -half.Y),
|
||||
new Vector2(half.X, half.Y),
|
||||
new Vector2(-half.X, half.Y),
|
||||
};
|
||||
var sine = MathF.Sin(transform.RotationRadians);
|
||||
var cosine = MathF.Cos(transform.RotationRadians);
|
||||
return local
|
||||
.Select(point => new Vector3(
|
||||
transform.Position.X + point.X * cosine - point.Y * sine,
|
||||
transform.Position.Y + point.X * sine + point.Y * cosine,
|
||||
transform.Position.Z))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
private static string FeedNodeId(string sourceId) =>
|
||||
$"feed:{sourceId}";
|
||||
}
|
||||
|
||||
public sealed class MimirSceneEditorNode(
|
||||
string id,
|
||||
string parentId,
|
||||
string displayName,
|
||||
MimirSceneEditorNodeKind kind,
|
||||
MimirSceneEditorTransform defaultTransform)
|
||||
{
|
||||
public string Id { get; } = id;
|
||||
|
||||
public string ParentId { get; } = parentId;
|
||||
|
||||
public string DisplayName { get; } = displayName;
|
||||
|
||||
public MimirSceneEditorNodeKind Kind { get; } = kind;
|
||||
|
||||
public MimirSceneEditorTransform DefaultTransform { get; } = defaultTransform;
|
||||
|
||||
public MimirSceneEditorTransform Transform { get; set; } = defaultTransform;
|
||||
|
||||
public bool Visible { get; set; } = true;
|
||||
|
||||
public bool Locked { get; set; }
|
||||
|
||||
public string SourceId { get; init; } = "";
|
||||
|
||||
public string Text { get; set; } = "";
|
||||
|
||||
public string ModelPath { get; init; } = "";
|
||||
}
|
||||
|
||||
public readonly record struct MimirSceneEditorTransform(
|
||||
Vector3 Position,
|
||||
float RotationRadians,
|
||||
Vector2 Scale);
|
||||
|
||||
public enum MimirSceneEditorNodeKind
|
||||
{
|
||||
Camera,
|
||||
SensorFeedPanel,
|
||||
SdfTextPanel,
|
||||
Model,
|
||||
}
|
||||
|
||||
public enum MimirSceneEditorGizmoMode
|
||||
{
|
||||
Translate,
|
||||
Rotate,
|
||||
Scale,
|
||||
}
|
||||
Loading…
Reference in a new issue