Decal system cleanup (#13493)

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Leon Friedrich
2023-01-16 13:46:22 +13:00
committed by GitHub
parent 3cfd814503
commit dbe99f9fa6
7 changed files with 141 additions and 283 deletions

View File

@@ -15,15 +15,11 @@ namespace Content.Client.Decals
private DecalOverlay _overlay = default!;
// TODO move this data to the component
public readonly Dictionary<EntityUid, SortedDictionary<int, SortedDictionary<uint, Decal>>> DecalRenderIndex = new();
private readonly Dictionary<EntityUid, Dictionary<uint, int>> _decalZIndexIndex = new();
public override void Initialize()
{
base.Initialize();
_overlay = new DecalOverlay(this, _sprites, EntityManager, PrototypeManager);
_overlay = new DecalOverlay(_sprites, EntityManager, PrototypeManager);
_overlayManager.AddOverlay(_overlay);
SubscribeLocalEvent<DecalGridComponent, ComponentHandleState>(OnHandleState);
@@ -42,41 +38,25 @@ namespace Content.Client.Decals
}
}
protected override void OnCompRemove(EntityUid uid, DecalGridComponent component, ComponentRemove args)
{
DecalRenderIndex.Remove(uid);
_decalZIndexIndex.Remove(uid);
base.OnCompRemove(uid, component, args);
}
protected override void OnCompAdd(EntityUid uid, DecalGridComponent component, ComponentAdd args)
{
DecalRenderIndex[uid] = new();
_decalZIndexIndex[uid] = new();
base.OnCompAdd(uid, component, args);
}
public override void Shutdown()
{
base.Shutdown();
_overlayManager.RemoveOverlay(_overlay);
}
protected override bool RemoveDecalHook(EntityUid gridId, uint uid)
protected override void OnDecalRemoved(EntityUid gridId, uint decalId, DecalGridComponent component, Vector2i indices, DecalChunk chunk)
{
RemoveDecalFromRenderIndex(gridId, uid);
return base.RemoveDecalHook(gridId, uid);
}
base.OnDecalRemoved(gridId, decalId, component, indices, chunk);
private void RemoveDecalFromRenderIndex(EntityUid gridId, uint uid)
{
var zIndex = _decalZIndexIndex[gridId][uid];
if (!component.DecalZIndexIndex.Remove(decalId, out var zIndex))
return;
DecalRenderIndex[gridId][zIndex].Remove(uid);
if (DecalRenderIndex[gridId][zIndex].Count == 0)
DecalRenderIndex[gridId].Remove(zIndex);
if (!component.DecalRenderIndex.TryGetValue(zIndex, out var renderIndex))
return;
_decalZIndexIndex[gridId].Remove(uid);
renderIndex.Remove(decalId);
if (renderIndex.Count == 0)
component.DecalRenderIndex.Remove(zIndex);
}
private void OnHandleState(EntityUid gridUid, DecalGridComponent gridComp, ref ComponentHandleState args)
@@ -143,14 +123,8 @@ namespace Content.Client.Decals
private void UpdateChunks(EntityUid gridId, DecalGridComponent gridComp, Dictionary<Vector2i, DecalChunk> updatedGridChunks)
{
var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
if (!ChunkIndex.TryGetValue(gridId, out var chunkIndex) ||
!DecalRenderIndex.TryGetValue(gridId, out var renderIndex) ||
!_decalZIndexIndex.TryGetValue(gridId, out var zIndexIndex))
{
Logger.Error($"Grid missing from dictionaries while updating decal chunks for grid {ToPrettyString(gridId)}");
return;
}
var renderIndex = gridComp.DecalRenderIndex;
var zIndexIndex = gridComp.DecalZIndexIndex;
// Update any existing data / remove decals we didn't receive data for.
foreach (var (indices, newChunkData) in updatedGridChunks)
@@ -161,8 +135,8 @@ namespace Content.Client.Decals
removedUids.ExceptWith(newChunkData.Decals.Keys);
foreach (var removedUid in removedUids)
{
RemoveDecalHook(gridId, removedUid);
chunkIndex.Remove(removedUid);
OnDecalRemoved(gridId, removedUid, gridComp, indices, chunk);
gridComp.DecalIndex.Remove(removedUid);
}
}
@@ -175,7 +149,7 @@ namespace Content.Client.Decals
renderIndex.GetOrNew(decal.ZIndex)[uid] = decal;
zIndexIndex[uid] = decal.ZIndex;
chunkIndex[uid] = indices;
gridComp.DecalIndex[uid] = indices;
}
}
}
@@ -184,20 +158,14 @@ namespace Content.Client.Decals
{
var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
if (!ChunkIndex.TryGetValue(gridId, out var chunkIndex))
{
Logger.Error($"Missing grid in ChunkIndex dictionary while removing chunks from grid {ToPrettyString(gridId)}");
return;
}
foreach (var index in chunks)
{
if (!chunkCollection.TryGetValue(index, out var chunk)) continue;
foreach (var uid in chunk.Decals.Keys)
foreach (var decalId in chunk.Decals.Keys)
{
RemoveDecalHook(gridId, uid);
chunkIndex.Remove(uid);
OnDecalRemoved(gridId, decalId, gridComp, index, chunk);
gridComp.DecalIndex.Remove(decalId);
}
chunkCollection.Remove(index);

View File

@@ -3,13 +3,11 @@ using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Shared.Enums;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Client.Decals.Overlays
{
public sealed class DecalOverlay : Overlay
{
private readonly DecalSystem _decals;
private readonly SpriteSystem _sprites;
private readonly IEntityManager _entManager;
private readonly IPrototypeManager _prototypeManager;
@@ -19,12 +17,10 @@ namespace Content.Client.Decals.Overlays
private readonly Dictionary<string, (Texture Texture, bool SnapCardinals)> _cachedTextures = new(64);
public DecalOverlay(
DecalSystem decals,
SpriteSystem sprites,
IEntityManager entManager,
IPrototypeManager prototypeManager)
{
_decals = decals;
_sprites = sprites;
_entManager = entManager;
_prototypeManager = prototypeManager;
@@ -37,17 +33,14 @@ namespace Content.Client.Decals.Overlays
var xformQuery = _entManager.GetEntityQuery<TransformComponent>();
var eyeAngle = args.Viewport.Eye?.Rotation ?? Angle.Zero;
foreach (var (gridId, zIndexDictionary) in _decals.DecalRenderIndex)
foreach (var (decalGrid, xform) in _entManager.EntityQuery<DecalGridComponent, TransformComponent>(true))
{
var gridId = decalGrid.Owner;
var zIndexDictionary = decalGrid.DecalRenderIndex;
if (zIndexDictionary.Count == 0)
continue;
if (!xformQuery.TryGetComponent(gridId, out var xform))
{
Logger.Error($"Tried to draw decals on a non-existent grid. GridUid: {gridId}");
continue;
}
if (xform.MapID != args.MapId)
continue;
@@ -61,8 +54,7 @@ namespace Content.Client.Decals.Overlays
{
if (!_cachedTextures.TryGetValue(decal.Id, out var cache) && _prototypeManager.TryIndex<DecalPrototype>(decal.Id, out var decalProto))
{
var sprite = GetDecalSprite(decal.Id);
cache = (_sprites.Frame0(sprite), decalProto.SnapCardinals);
cache = (_sprites.Frame0(decalProto.Sprite), decalProto.SnapCardinals);
_cachedTextures[decal.Id] = cache;
}
@@ -86,14 +78,5 @@ namespace Content.Client.Decals.Overlays
handle.SetTransform(Matrix3.Identity);
}
public SpriteSpecifier GetDecalSprite(string id)
{
if (_prototypeManager.TryIndex<DecalPrototype>(id, out var proto))
return proto.Sprite;
Logger.Error($"Unknown decal prototype: {id}");
return new SpriteSpecifier.Texture(new ResourcePath("/Textures/noSprite.png"));
}
}
}