Use managed RTS viewport document handles

This commit is contained in:
Meta Vens 2026-06-29 12:54:07 +01:00
commit ab6387fe1e
7 changed files with 199 additions and 205 deletions

View file

@ -1,6 +1,5 @@
using System;
using GameCult.Aetheria.State.Verse;
using GameCult.Mesh;
using UnityEngine;
public sealed class AetheriaUnityRenderSplatViewportSource : MonoBehaviour
@ -27,8 +26,7 @@ public sealed class AetheriaUnityRenderSplatViewportSource : MonoBehaviour
private bool renderInLateUpdate = true;
private float _nextRefreshTime;
private AetheriaRuntimeRtsViewportBounds _viewport;
private CultMeshReactiveDocument<AetheriaRuntimeRenderSplatsViewportDocument> _renderSplatsViewport;
private AetheriaUnityRtsViewportDocuments _viewportDocuments;
public RenderTexture TargetTexture
{
@ -65,7 +63,7 @@ public sealed class AetheriaUnityRenderSplatViewportSource : MonoBehaviour
return false;
RefreshDocument(false);
var document = _renderSplatsViewport?.Current;
var document = _viewportDocuments?.CurrentRenderSplatsViewport;
if (document == null)
return false;
@ -107,7 +105,7 @@ public sealed class AetheriaUnityRenderSplatViewportSource : MonoBehaviour
private void RefreshDocument(bool force)
{
var viewport = ResolveViewportBounds();
if (!force && _renderSplatsViewport != null && SameViewport(_viewport, viewport))
if (!force && _viewportDocuments != null && _viewportDocuments.Matches(viewport))
return;
if (!force && Time.unscaledTime < _nextRefreshTime)
@ -116,13 +114,10 @@ public sealed class AetheriaUnityRenderSplatViewportSource : MonoBehaviour
_nextRefreshTime = Time.unscaledTime + Mathf.Max(0.02f, refreshIntervalSeconds);
try
{
var nextRenderSplatsViewport = AetheriaUnityRuntimeClientProvider
.ReactiveRenderSplatsViewport(
viewport,
"unity-render-splat-viewport");
var nextDocuments = AetheriaUnityRuntimeClientProvider
.RenderSplatViewportDocuments(viewport, "unity-render-splat-viewport");
ClearViewportDocument();
_viewport = viewport;
_renderSplatsViewport = nextRenderSplatsViewport;
_viewportDocuments = nextDocuments;
}
catch (Exception ex)
{
@ -144,21 +139,8 @@ public sealed class AetheriaUnityRenderSplatViewportSource : MonoBehaviour
private void ClearViewportDocument()
{
_renderSplatsViewport?.Dispose();
_renderSplatsViewport = null;
}
private static bool SameViewport(
AetheriaRuntimeRtsViewportBounds left,
AetheriaRuntimeRtsViewportBounds right)
{
if (left == null || right == null)
return false;
return Mathf.Approximately(left.MinX, right.MinX) &&
Mathf.Approximately(left.MinY, right.MinY) &&
Mathf.Approximately(left.MaxX, right.MaxX) &&
Mathf.Approximately(left.MaxY, right.MaxY);
_viewportDocuments?.Dispose();
_viewportDocuments = null;
}
}

View file

@ -0,0 +1,100 @@
using System;
using GameCult.Aetheria.State.Verse;
using GameCult.Mesh;
public sealed class AetheriaUnityRtsViewportDocuments : IDisposable
{
private AetheriaUnityRtsViewportDocuments(
AetheriaClientState state,
AetheriaRuntimeRtsViewportBounds viewport,
bool bindContacts,
bool bindObjects,
bool bindRenderSplats)
{
if (state == null)
throw new ArgumentNullException(nameof(state));
if (viewport == null)
throw new ArgumentNullException(nameof(viewport));
Viewport = viewport;
Contacts = bindContacts ? state.ReactiveZoneContacts() : null;
ObjectsViewport = bindObjects ? state.ReactiveObjectsViewport(viewport) : null;
RenderSplatsViewport = bindRenderSplats ? state.ReactiveRenderSplatsViewport(viewport) : null;
}
public AetheriaRuntimeRtsViewportBounds Viewport { get; }
public CultMeshReactiveDocument<AetheriaRuntimeZoneContactsDocument> Contacts { get; }
public CultMeshReactiveDocument<AetheriaRuntimeObjectsViewportDocument> ObjectsViewport { get; }
public CultMeshReactiveDocument<AetheriaRuntimeRenderSplatsViewportDocument> RenderSplatsViewport { get; }
public AetheriaRuntimeZoneContactsDocument CurrentContacts => Contacts?.Current;
public AetheriaRuntimeObjectsViewportDocument CurrentObjectsViewport => ObjectsViewport?.Current;
public AetheriaRuntimeRenderSplatsViewportDocument CurrentRenderSplatsViewport => RenderSplatsViewport?.Current;
public static AetheriaUnityRtsViewportDocuments OpenMap(
AetheriaClientState state,
AetheriaRuntimeRtsViewportBounds viewport)
{
return new AetheriaUnityRtsViewportDocuments(
state,
viewport,
bindContacts: false,
bindObjects: true,
bindRenderSplats: true);
}
public static AetheriaUnityRtsViewportDocuments OpenZonePresentation(
AetheriaClientState state,
AetheriaRuntimeRtsViewportBounds viewport)
{
return new AetheriaUnityRtsViewportDocuments(
state,
viewport,
bindContacts: true,
bindObjects: true,
bindRenderSplats: false);
}
public static AetheriaUnityRtsViewportDocuments OpenRenderSplats(
AetheriaClientState state,
AetheriaRuntimeRtsViewportBounds viewport)
{
return new AetheriaUnityRtsViewportDocuments(
state,
viewport,
bindContacts: false,
bindObjects: false,
bindRenderSplats: true);
}
public bool Matches(AetheriaRuntimeRtsViewportBounds viewport)
{
return SameViewport(Viewport, viewport);
}
public void Dispose()
{
Contacts?.Dispose();
ObjectsViewport?.Dispose();
RenderSplatsViewport?.Dispose();
}
public static bool SameViewport(
AetheriaRuntimeRtsViewportBounds left,
AetheriaRuntimeRtsViewportBounds right)
{
if (left == null || right == null)
return false;
return Approximately(left.MinX, right.MinX) &&
Approximately(left.MinY, right.MinY) &&
Approximately(left.MaxX, right.MaxX) &&
Approximately(left.MaxY, right.MaxY);
}
private static bool Approximately(double left, double right)
{
var scale = Math.Max(1.0, Math.Max(Math.Abs(left), Math.Abs(right)));
return Math.Abs(left - right) <= scale * 1e-6;
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e763d94f902249349a0f897ef12bd185

View file

@ -175,6 +175,27 @@ public static class AetheriaUnityRuntimeClientProvider
return RuntimeState(runtimeId).ReactiveRenderSplatsViewport(viewport);
}
public static AetheriaUnityRtsViewportDocuments MapViewportDocuments(
AetheriaRuntimeRtsViewportBounds viewport,
string runtimeId = "")
{
return AetheriaUnityRtsViewportDocuments.OpenMap(RuntimeState(runtimeId), viewport);
}
public static AetheriaUnityRtsViewportDocuments ZonePresentationDocuments(
AetheriaRuntimeRtsViewportBounds viewport,
string runtimeId = "")
{
return AetheriaUnityRtsViewportDocuments.OpenZonePresentation(RuntimeState(runtimeId), viewport);
}
public static AetheriaUnityRtsViewportDocuments RenderSplatViewportDocuments(
AetheriaRuntimeRtsViewportBounds viewport,
string runtimeId = "")
{
return AetheriaUnityRtsViewportDocuments.OpenRenderSplats(RuntimeState(runtimeId), viewport);
}
public static AetheriaRuntimeDaemonFrameDocument CurrentDaemonFrame(string runtimeId = "")
{
return RuntimeState(runtimeId).CurrentDaemonFrame();

View file

@ -7,7 +7,6 @@ using System.Collections;
using System.Collections.Generic;
using System.Linq;
using GameCult.Aetheria.State.Verse;
using GameCult.Mesh;
using TMPro;
using UnityEngine;
using Unity.Mathematics;
@ -40,8 +39,7 @@ public class MapRenderer : MonoBehaviour
private RenderTexture _mapTexture;
private int2 _size;
private bool _init;
private CultMeshReactiveDocument<AetheriaRuntimeObjectsViewportDocument> _objectsViewport;
private CultMeshReactiveDocument<AetheriaRuntimeRenderSplatsViewportDocument> _renderSplatsViewport;
private AetheriaUnityRtsViewportDocuments _viewportDocuments;
private float _nextViewportRefreshTime;
private readonly List<RawImage> _rtsIconPool = new List<RawImage>();
@ -126,12 +124,10 @@ public class MapRenderer : MonoBehaviour
{
var viewport = ResolveViewportBounds();
ClearViewportCaches();
_objectsViewport = AetheriaUnityRuntimeClientProvider
.ReactiveObjectsViewport(viewport, "unity-map-renderer");
_renderSplatsViewport = AetheriaUnityRuntimeClientProvider
.ReactiveRenderSplatsViewport(viewport, "unity-map-renderer");
_viewportDocuments = AetheriaUnityRuntimeClientProvider
.MapViewportDocuments(viewport, "unity-map-renderer");
var objectsViewport = _objectsViewport?.Current;
var objectsViewport = _viewportDocuments?.CurrentObjectsViewport;
var zoneName = string.IsNullOrWhiteSpace(objectsViewport?.ZoneName)
? "Unknown"
: objectsViewport.ZoneName;
@ -146,7 +142,7 @@ public class MapRenderer : MonoBehaviour
private void RenderSplatLayers()
{
var renderSplatsViewport = _renderSplatsViewport?.Current;
var renderSplatsViewport = _viewportDocuments?.CurrentRenderSplatsViewport;
if (renderSplatsViewport == null)
return;
@ -185,7 +181,7 @@ public class MapRenderer : MonoBehaviour
if (RtsIconRoot == null)
return;
var objects = _objectsViewport?.Current?.Objects ?? Array.Empty<AetheriaRuntimeRtsViewportObject>();
var objects = _viewportDocuments?.CurrentObjectsViewport?.Objects ?? Array.Empty<AetheriaRuntimeRtsViewportObject>();
for (var i = 0; i < objects.Count; i++)
{
var icon = ResolveRtsIcon(i);
@ -244,7 +240,7 @@ public class MapRenderer : MonoBehaviour
private Vector2 WorldToMapPosition(double worldX, double worldY)
{
var viewport = _objectsViewport?.Current?.Viewport ?? ResolveViewportBounds();
var viewport = _viewportDocuments?.CurrentObjectsViewport?.Viewport ?? ResolveViewportBounds();
var minX = Math.Min(viewport.MinX, viewport.MaxX);
var maxX = Math.Max(viewport.MinX, viewport.MaxX);
var minY = Math.Min(viewport.MinY, viewport.MaxY);
@ -329,10 +325,8 @@ public class MapRenderer : MonoBehaviour
private void ClearViewportCaches()
{
_objectsViewport?.Dispose();
_renderSplatsViewport?.Dispose();
_objectsViewport = null;
_renderSplatsViewport = null;
_viewportDocuments?.Dispose();
_viewportDocuments = null;
}
private void OnDestroy()

View file

@ -8,7 +8,6 @@ using System.Collections.Generic;
using System.Linq;
using Cinemachine;
using GameCult.Aetheria.State.Verse;
using GameCult.Mesh;
using UnityEngine;
using UnityEngine.Serialization;
using Object = UnityEngine.Object;
@ -119,9 +118,7 @@ public class ZoneRenderer : MonoBehaviour
Array.Empty<AetheriaRuntimeZoneRenderAsteroidBeltPose>();
private IReadOnlyList<AetheriaRuntimeBodySnapshotCommit> _zoneRenderBodies =
Array.Empty<AetheriaRuntimeBodySnapshotCommit>();
private CultMeshReactiveDocument<AetheriaRuntimeZoneContactsDocument> _zoneContacts;
private AetheriaRuntimeRtsViewportBounds _objectsViewportBounds;
private CultMeshReactiveDocument<AetheriaRuntimeObjectsViewportDocument> _objectsViewport;
private AetheriaUnityRtsViewportDocuments _zonePresentationDocuments;
public Dictionary<int, (GameObject gravity, CompassIcon icon)> WormholeInstances = new Dictionary<int, (GameObject, CompassIcon)>();
private List<ItemPickup> _loot = new List<ItemPickup>();
@ -1204,8 +1201,7 @@ public class ZoneRenderer : MonoBehaviour
try
{
return AetheriaUnityRuntimeClientProvider
.RuntimeState("unity-zone-renderer")
.CurrentCatalog();
.CurrentCatalog("unity-zone-renderer");
}
catch (Exception ex)
{
@ -1217,65 +1213,47 @@ public class ZoneRenderer : MonoBehaviour
private AetheriaRuntimeZoneContactsDocument ResolveZoneContacts()
{
if (_zoneContacts != null)
return _zoneContacts.Current;
if (_zonePresentationDocuments != null)
return _zonePresentationDocuments.CurrentContacts;
try
{
_zoneContacts = AetheriaUnityRuntimeClientProvider
.ReactiveZoneContacts("unity-zone-renderer");
_zonePresentationDocuments = AetheriaUnityRuntimeClientProvider
.ZonePresentationDocuments(ToViewportBounds(ResolveDaemonRenderViewport()), "unity-zone-renderer");
}
catch (Exception ex)
{
Debug.LogWarning($"Failed to bind Aetheria zone contacts for renderer target distances: {ex.Message}");
}
return _zoneContacts?.Current;
return _zonePresentationDocuments?.CurrentContacts;
}
private AetheriaRuntimeObjectsViewportDocument ResolveObjectsViewport(AetheriaRuntimeXzRect viewport)
{
var viewportBounds = ToViewportBounds(viewport);
if (_objectsViewport != null && SameViewport(_objectsViewportBounds, viewportBounds))
return _objectsViewport.Current;
if (_zonePresentationDocuments != null && _zonePresentationDocuments.Matches(viewportBounds))
return _zonePresentationDocuments.CurrentObjectsViewport;
try
{
var nextObjectsViewport = AetheriaUnityRuntimeClientProvider
.ReactiveObjectsViewport(
viewportBounds,
"unity-zone-renderer");
_objectsViewport?.Dispose();
_objectsViewportBounds = viewportBounds;
_objectsViewport = nextObjectsViewport;
var nextDocuments = AetheriaUnityRuntimeClientProvider
.ZonePresentationDocuments(viewportBounds, "unity-zone-renderer");
_zonePresentationDocuments?.Dispose();
_zonePresentationDocuments = nextDocuments;
}
catch (Exception ex)
{
Debug.LogWarning($"Failed to bind Aetheria objects viewport for zone renderer presentation: {ex.Message}");
}
return _objectsViewport?.Current;
return _zonePresentationDocuments?.CurrentObjectsViewport;
}
private void ClearClientCaches()
{
_zoneContacts?.Dispose();
_objectsViewport?.Dispose();
_zoneContacts = null;
_objectsViewport = null;
}
private static bool SameViewport(
AetheriaRuntimeRtsViewportBounds left,
AetheriaRuntimeRtsViewportBounds right)
{
if (left == null || right == null)
return false;
return Mathf.Approximately(left.MinX, right.MinX) &&
Mathf.Approximately(left.MinY, right.MinY) &&
Mathf.Approximately(left.MaxX, right.MaxX) &&
Mathf.Approximately(left.MaxY, right.MaxY);
_zonePresentationDocuments?.Dispose();
_zonePresentationDocuments = null;
}
}