Decal rendering improvements (#6704)
Co-authored-by: metalgearsloth <metalgearsloth@gmail.com>
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using Content.Shared.Decals;
|
using Content.Shared.Decals;
|
||||||
|
using Robust.Client.GameObjects;
|
||||||
using Robust.Client.Graphics;
|
using Robust.Client.Graphics;
|
||||||
using Robust.Client.Utility;
|
|
||||||
using Robust.Shared.Enums;
|
using Robust.Shared.Enums;
|
||||||
using Robust.Shared.Map;
|
using Robust.Shared.Map;
|
||||||
using Robust.Shared.Prototypes;
|
using Robust.Shared.Prototypes;
|
||||||
@@ -11,15 +10,27 @@ namespace Content.Client.Decals
|
|||||||
{
|
{
|
||||||
public sealed class DecalOverlay : Overlay
|
public sealed class DecalOverlay : Overlay
|
||||||
{
|
{
|
||||||
private readonly DecalSystem _system;
|
private readonly DecalSystem _decals;
|
||||||
|
private readonly SharedTransformSystem _transform;
|
||||||
|
private readonly SpriteSystem _sprites;
|
||||||
|
private readonly IEntityManager _entManager;
|
||||||
private readonly IMapManager _mapManager;
|
private readonly IMapManager _mapManager;
|
||||||
private readonly IPrototypeManager _prototypeManager;
|
private readonly IPrototypeManager _prototypeManager;
|
||||||
|
|
||||||
public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowEntities;
|
public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowEntities;
|
||||||
|
|
||||||
public DecalOverlay(DecalSystem system, IMapManager mapManager, IPrototypeManager prototypeManager)
|
public DecalOverlay(
|
||||||
|
DecalSystem decals,
|
||||||
|
SharedTransformSystem transforms,
|
||||||
|
SpriteSystem sprites,
|
||||||
|
IEntityManager entManager,
|
||||||
|
IMapManager mapManager,
|
||||||
|
IPrototypeManager prototypeManager)
|
||||||
{
|
{
|
||||||
_system = system;
|
_decals = decals;
|
||||||
|
_transform = transforms;
|
||||||
|
_sprites = sprites;
|
||||||
|
_entManager = entManager;
|
||||||
_mapManager = mapManager;
|
_mapManager = mapManager;
|
||||||
_prototypeManager = prototypeManager;
|
_prototypeManager = prototypeManager;
|
||||||
}
|
}
|
||||||
@@ -40,16 +51,25 @@ namespace Content.Client.Decals
|
|||||||
return spriteSpecifier;
|
return spriteSpecifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var (gridId, zIndexDictionary) in _system.DecalRenderIndex)
|
var xformQuery = _entManager.GetEntityQuery<TransformComponent>();
|
||||||
|
|
||||||
|
foreach (var (gridId, zIndexDictionary) in _decals.DecalRenderIndex)
|
||||||
{
|
{
|
||||||
var grid = _mapManager.GetGrid(gridId);
|
var gridUid = _mapManager.GetGridEuid(gridId);
|
||||||
handle.SetTransform(grid.WorldMatrix);
|
var xform = xformQuery.GetComponent(gridUid);
|
||||||
|
|
||||||
|
handle.SetTransform(_transform.GetWorldMatrix(xform));
|
||||||
|
|
||||||
foreach (var (_, decals) in zIndexDictionary)
|
foreach (var (_, decals) in zIndexDictionary)
|
||||||
{
|
{
|
||||||
foreach (var (_, decal) in decals)
|
foreach (var (_, decal) in decals)
|
||||||
{
|
{
|
||||||
var spriteSpecifier = GetSpriteSpecifier(decal.Id);
|
var spriteSpecifier = GetSpriteSpecifier(decal.Id);
|
||||||
handle.DrawTexture(spriteSpecifier.Frame0(), decal.Coordinates, decal.Angle, decal.Color);
|
|
||||||
|
if (decal.Angle.Equals(Angle.Zero))
|
||||||
|
handle.DrawTexture(_sprites.Frame0(spriteSpecifier), decal.Coordinates, decal.Color);
|
||||||
|
else
|
||||||
|
handle.DrawTexture(_sprites.Frame0(spriteSpecifier), decal.Coordinates, decal.Angle, decal.Color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using Content.Shared.Decals;
|
using Content.Shared.Decals;
|
||||||
|
using Robust.Client.GameObjects;
|
||||||
using Robust.Client.Graphics;
|
using Robust.Client.Graphics;
|
||||||
using Robust.Shared.GameObjects;
|
|
||||||
using Robust.Shared.IoC;
|
|
||||||
using Robust.Shared.Map;
|
using Robust.Shared.Map;
|
||||||
|
using Robust.Shared.Utility;
|
||||||
|
|
||||||
namespace Content.Client.Decals
|
namespace Content.Client.Decals
|
||||||
{
|
{
|
||||||
public sealed class DecalSystem : SharedDecalSystem
|
public sealed class DecalSystem : SharedDecalSystem
|
||||||
{
|
{
|
||||||
[Dependency] private readonly IOverlayManager _overlayManager = default!;
|
[Dependency] private readonly IOverlayManager _overlayManager = default!;
|
||||||
|
[Dependency] private readonly SharedTransformSystem _transforms = default!;
|
||||||
|
[Dependency] private readonly SpriteSystem _sprites = default!;
|
||||||
|
|
||||||
private DecalOverlay _overlay = default!;
|
private DecalOverlay _overlay = default!;
|
||||||
public Dictionary<GridId, SortedDictionary<int, SortedDictionary<uint, Decal>>> DecalRenderIndex = new();
|
public Dictionary<GridId, SortedDictionary<int, SortedDictionary<uint, Decal>>> DecalRenderIndex = new();
|
||||||
@@ -19,7 +20,7 @@ namespace Content.Client.Decals
|
|||||||
{
|
{
|
||||||
base.Initialize();
|
base.Initialize();
|
||||||
|
|
||||||
_overlay = new DecalOverlay(this, MapManager, PrototypeManager);
|
_overlay = new DecalOverlay(this, _transforms, _sprites, EntityManager, MapManager, PrototypeManager);
|
||||||
_overlayManager.AddOverlay(_overlay);
|
_overlayManager.AddOverlay(_overlay);
|
||||||
|
|
||||||
SubscribeNetworkEvent<DecalChunkUpdateEvent>(OnChunkUpdate);
|
SubscribeNetworkEvent<DecalChunkUpdateEvent>(OnChunkUpdate);
|
||||||
@@ -79,9 +80,10 @@ namespace Content.Client.Decals
|
|||||||
{
|
{
|
||||||
foreach (var (gridId, gridChunks) in ev.Data)
|
foreach (var (gridId, gridChunks) in ev.Data)
|
||||||
{
|
{
|
||||||
|
var chunkCollection = ChunkCollection(gridId);
|
||||||
|
|
||||||
foreach (var (indices, newChunkData) in gridChunks)
|
foreach (var (indices, newChunkData) in gridChunks)
|
||||||
{
|
{
|
||||||
var chunkCollection = ChunkCollection(gridId);
|
|
||||||
if (chunkCollection.TryGetValue(indices, out var chunk))
|
if (chunkCollection.TryGetValue(indices, out var chunk))
|
||||||
{
|
{
|
||||||
var removedUids = new HashSet<uint>(chunk.Keys);
|
var removedUids = new HashSet<uint>(chunk.Keys);
|
||||||
@@ -91,12 +93,14 @@ namespace Content.Client.Decals
|
|||||||
RemoveDecalFromRenderIndex(gridId, removedUid);
|
RemoveDecalFromRenderIndex(gridId, removedUid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var (uid, decal) in newChunkData)
|
foreach (var (uid, decal) in newChunkData)
|
||||||
{
|
{
|
||||||
if(!DecalRenderIndex[gridId].ContainsKey(decal.ZIndex))
|
if (!DecalRenderIndex[gridId].ContainsKey(decal.ZIndex))
|
||||||
DecalRenderIndex[gridId][decal.ZIndex] = new();
|
DecalRenderIndex[gridId][decal.ZIndex] = new();
|
||||||
|
|
||||||
if (DecalZIndexIndex.TryGetValue(gridId, out var values) && values.TryGetValue(uid, out var zIndex))
|
if (DecalZIndexIndex.TryGetValue(gridId, out var values) &&
|
||||||
|
values.TryGetValue(uid, out var zIndex))
|
||||||
{
|
{
|
||||||
DecalRenderIndex[gridId][zIndex].Remove(uid);
|
DecalRenderIndex[gridId][zIndex].Remove(uid);
|
||||||
}
|
}
|
||||||
@@ -106,8 +110,24 @@ namespace Content.Client.Decals
|
|||||||
|
|
||||||
ChunkIndex[gridId][uid] = indices;
|
ChunkIndex[gridId][uid] = indices;
|
||||||
}
|
}
|
||||||
|
|
||||||
chunkCollection[indices] = newChunkData;
|
chunkCollection[indices] = newChunkData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Now we'll cull old chunks out of range as the server will send them to us anyway.
|
||||||
|
var toRemove = new RemQueue<Vector2i>();
|
||||||
|
|
||||||
|
foreach (var (index, _) in chunkCollection)
|
||||||
|
{
|
||||||
|
if (gridChunks.ContainsKey(index)) continue;
|
||||||
|
|
||||||
|
toRemove.Add(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var index in toRemove)
|
||||||
|
{
|
||||||
|
chunkCollection.Remove(index);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,7 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using Robust.Shared;
|
using Robust.Shared;
|
||||||
using Robust.Shared.Configuration;
|
using Robust.Shared.Configuration;
|
||||||
using Robust.Shared.GameObjects;
|
|
||||||
using Robust.Shared.IoC;
|
|
||||||
using Robust.Shared.Map;
|
using Robust.Shared.Map;
|
||||||
using Robust.Shared.Maths;
|
|
||||||
using Robust.Shared.Prototypes;
|
using Robust.Shared.Prototypes;
|
||||||
using Robust.Shared.Serialization;
|
using Robust.Shared.Serialization;
|
||||||
|
|
||||||
@@ -17,6 +12,7 @@ namespace Content.Shared.Decals
|
|||||||
[Dependency] protected readonly IPrototypeManager PrototypeManager = default!;
|
[Dependency] protected readonly IPrototypeManager PrototypeManager = default!;
|
||||||
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
|
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
|
||||||
[Dependency] protected readonly IMapManager MapManager = default!;
|
[Dependency] protected readonly IMapManager MapManager = default!;
|
||||||
|
[Dependency] protected readonly SharedTransformSystem Transforms = default!;
|
||||||
|
|
||||||
protected readonly Dictionary<GridId, Dictionary<uint, Vector2i>> ChunkIndex = new();
|
protected readonly Dictionary<GridId, Dictionary<uint, Vector2i>> ChunkIndex = new();
|
||||||
|
|
||||||
@@ -101,15 +97,19 @@ namespace Content.Shared.Decals
|
|||||||
protected Dictionary<GridId, HashSet<Vector2i>> GetChunksForViewers(HashSet<EntityUid> viewers)
|
protected Dictionary<GridId, HashSet<Vector2i>> GetChunksForViewers(HashSet<EntityUid> viewers)
|
||||||
{
|
{
|
||||||
var chunks = new Dictionary<GridId, HashSet<Vector2i>>();
|
var chunks = new Dictionary<GridId, HashSet<Vector2i>>();
|
||||||
|
var xformQuery = GetEntityQuery<TransformComponent>();
|
||||||
|
|
||||||
foreach (var viewerUid in viewers)
|
foreach (var viewerUid in viewers)
|
||||||
{
|
{
|
||||||
var (bounds, mapId) = CalcViewBounds(viewerUid);
|
var (bounds, mapId) = CalcViewBounds(viewerUid);
|
||||||
MapManager.FindGridsIntersectingEnumerator(mapId, bounds, out var gridsEnumerator, true);
|
|
||||||
while(gridsEnumerator.MoveNext(out var grid))
|
foreach (var grid in MapManager.FindGridsIntersecting(mapId, bounds))
|
||||||
{
|
{
|
||||||
if(!chunks.ContainsKey(grid.Index))
|
if (!chunks.ContainsKey(grid.Index))
|
||||||
chunks[grid.Index] = new();
|
chunks[grid.Index] = new HashSet<Vector2i>();
|
||||||
var enumerator = new ChunkIndicesEnumerator(grid.InvWorldMatrix.TransformBox(bounds), ChunkSize);
|
|
||||||
|
var enumerator = new ChunkIndicesEnumerator(Transforms.GetInvWorldMatrix(grid.GridEntityId, xformQuery).TransformBox(bounds), ChunkSize);
|
||||||
|
|
||||||
while (enumerator.MoveNext(out var indices))
|
while (enumerator.MoveNext(out var indices))
|
||||||
{
|
{
|
||||||
chunks[grid.Index].Add(indices.Value);
|
chunks[grid.Index].Add(indices.Value);
|
||||||
@@ -120,7 +120,8 @@ namespace Content.Shared.Decals
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal struct ChunkIndicesEnumerator
|
// TODO: Pretty sure paul was moving this somewhere but just so people know
|
||||||
|
public struct ChunkIndicesEnumerator
|
||||||
{
|
{
|
||||||
private Vector2i _chunkLB;
|
private Vector2i _chunkLB;
|
||||||
private Vector2i _chunkRT;
|
private Vector2i _chunkRT;
|
||||||
@@ -128,7 +129,7 @@ namespace Content.Shared.Decals
|
|||||||
private int _xIndex;
|
private int _xIndex;
|
||||||
private int _yIndex;
|
private int _yIndex;
|
||||||
|
|
||||||
internal ChunkIndicesEnumerator(Box2 localAABB, int chunkSize)
|
public ChunkIndicesEnumerator(Box2 localAABB, int chunkSize)
|
||||||
{
|
{
|
||||||
_chunkLB = new Vector2i((int)Math.Floor(localAABB.Left / chunkSize), (int)Math.Floor(localAABB.Bottom / chunkSize));
|
_chunkLB = new Vector2i((int)Math.Floor(localAABB.Left / chunkSize), (int)Math.Floor(localAABB.Bottom / chunkSize));
|
||||||
_chunkRT = new Vector2i((int)Math.Floor(localAABB.Right / chunkSize), (int)Math.Floor(localAABB.Top / chunkSize));
|
_chunkRT = new Vector2i((int)Math.Floor(localAABB.Right / chunkSize), (int)Math.Floor(localAABB.Top / chunkSize));
|
||||||
|
|||||||
Reference in New Issue
Block a user