Apply RoofOverlay per-grid not per-map + fix lighting quality (#35207)

* Apply RoofOverlay per-grid not per-map

* Fix light render scales
This commit is contained in:
metalgearsloth
2025-02-17 00:29:03 +11:00
committed by GitHub
parent a9f900c397
commit ead32b5770
4 changed files with 71 additions and 42 deletions

View File

@@ -3,13 +3,16 @@ using Content.Shared.Light.Components;
using Content.Shared.Maps;
using Robust.Client.Graphics;
using Robust.Shared.Enums;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Physics;
namespace Content.Client.Light;
public sealed class RoofOverlay : Overlay
{
private readonly IEntityManager _entManager;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IOverlayManager _overlay = default!;
private readonly EntityLookupSystem _lookup;
@@ -17,6 +20,7 @@ public sealed class RoofOverlay : Overlay
private readonly SharedTransformSystem _xformSystem;
private readonly HashSet<Entity<OccluderComponent>> _occluders = new();
private List<Entity<MapGridComponent>> _grids = new();
public override OverlaySpace Space => OverlaySpace.BeforeLighting;
@@ -39,14 +43,6 @@ public sealed class RoofOverlay : Overlay
if (args.Viewport.Eye == null)
return;
var mapEnt = _mapSystem.GetMap(args.MapId);
if (!_entManager.TryGetComponent(mapEnt, out RoofComponent? roofComp) ||
!_entManager.TryGetComponent(mapEnt, out MapGridComponent? grid))
{
return;
}
var viewport = args.Viewport;
var eye = args.Viewport.Eye;
@@ -55,46 +51,72 @@ public sealed class RoofOverlay : Overlay
var bounds = lightoverlay.EnlargedBounds;
var target = lightoverlay.EnlargedLightTarget;
_grids.Clear();
_mapManager.FindGridsIntersecting(args.MapId, bounds, ref _grids);
for (var i = _grids.Count - 1; i >= 0; i--)
{
var grid = _grids[i];
if (_entManager.HasComponent<RoofComponent>(grid.Owner))
continue;
_grids.RemoveAt(i);
}
if (_grids.Count == 0)
return;
var lightScale = viewport.LightRenderTarget.Size / (Vector2) viewport.Size;
var scale = viewport.RenderScale / (Vector2.One / lightScale);
worldHandle.RenderInRenderTarget(target,
() =>
{
var invMatrix = target.GetWorldToLocalMatrix(eye, viewport.RenderScale / 2f);
var gridMatrix = _xformSystem.GetWorldMatrix(mapEnt);
var matty = Matrix3x2.Multiply(gridMatrix, invMatrix);
worldHandle.SetTransform(matty);
var tileEnumerator = _mapSystem.GetTilesEnumerator(mapEnt, grid, bounds);
// Due to stencilling we essentially draw on unrooved tiles
while (tileEnumerator.MoveNext(out var tileRef))
foreach (var grid in _grids)
{
if ((tileRef.Tile.Flags & (byte) TileFlag.Roof) == 0x0)
if (!_entManager.TryGetComponent(grid.Owner, out RoofComponent? roof))
continue;
var invMatrix = target.GetWorldToLocalMatrix(eye, scale);
var gridMatrix = _xformSystem.GetWorldMatrix(grid.Owner);
var matty = Matrix3x2.Multiply(gridMatrix, invMatrix);
worldHandle.SetTransform(matty);
var tileEnumerator = _mapSystem.GetTilesEnumerator(grid.Owner, grid, bounds);
// Due to stencilling we essentially draw on unrooved tiles
while (tileEnumerator.MoveNext(out var tileRef))
{
// Check if the tile is occluded in which case hide it anyway.
// This is to avoid lit walls bleeding over to unlit tiles.
_occluders.Clear();
_lookup.GetLocalEntitiesIntersecting(mapEnt, tileRef.GridIndices, _occluders);
var found = false;
foreach (var occluder in _occluders)
if ((tileRef.Tile.Flags & (byte) TileFlag.Roof) == 0x0)
{
if (!occluder.Comp.Enabled)
continue;
// Check if the tile is occluded in which case hide it anyway.
// This is to avoid lit walls bleeding over to unlit tiles.
_occluders.Clear();
_lookup.GetLocalEntitiesIntersecting(grid.Owner, tileRef.GridIndices, _occluders);
var found = false;
found = true;
break;
foreach (var occluder in _occluders)
{
if (!occluder.Comp.Enabled)
continue;
found = true;
break;
}
if (!found)
continue;
}
if (!found)
continue;
var local = _lookup.GetLocalBounds(tileRef, grid.Comp.TileSize);
worldHandle.DrawRect(local, roof.Color);
}
var local = _lookup.GetLocalBounds(tileRef, grid.TileSize);
worldHandle.DrawRect(local, roofComp.Color);
}
}, null);
worldHandle.SetTransform(Matrix3x2.Identity);
}
}