* Implements a Dynamic Lighting System on maps. * Edit: the night should be a little bit brighter and blue now. * Major edit: everything must be done on the client side now, with certain datafield replicated. Changes were outlined in the salvage to accommodate the new lighting system. * Edit: The offset is now serverside, this makes the time accurate in all situations. * Removing ununsed import * Minor tweaks * Tweak in time precision * Minor tweak + Unused import removed * Edit: apparently RealTime is better for what I'm looking for * Fix: Now the time is calculated correctly. * Minor tweaks * Adds condition for when the light should be updated * Add planet lighting * she * close-ish * c * bittersweat * Fixes * Revert "Merge branch '22719' into 2024-09-29-planet-lighting" This reverts commit 9f2785bb16aee47d794aa3eed8ae15004f97fc35, reversing changes made to 19649c07a5fb625423e08fc18d91c9cb101daa86. * Europa and day-night * weh * rooves working * Clean * Remove Europa * Fixes * fix * Update * Fix caves * Update for engine * Add sun shadows (planet lighting v2) For now mostly targeting walls and having the shadows change over time. Got the basic proof-of-concept working just needs a hell of a lot of polish. * Documentation * a * Fixes * Move blur to an overlay * Slughands * Fixes * Apply RoofOverlay per-grid not per-map * Fix light render scales * sangas * Juice it a bit * Better angle * Fixes * Add color support * Rounding bandaid * Wehs * Better * Remember I forgot to do this when writing docs --------- Co-authored-by: DoutorWhite <thedoctorwhite@gmail.com>
155 lines
6.1 KiB
C#
155 lines
6.1 KiB
C#
using System.Numerics;
|
|
using Content.Shared.Light.Components;
|
|
using Robust.Client.Graphics;
|
|
using Robust.Shared.Enums;
|
|
using Robust.Shared.Map;
|
|
using Robust.Shared.Map.Components;
|
|
using Robust.Shared.Physics;
|
|
using Robust.Shared.Prototypes;
|
|
|
|
namespace Content.Client.Light;
|
|
|
|
public sealed class SunShadowOverlay : Overlay
|
|
{
|
|
public override OverlaySpace Space => OverlaySpace.BeforeLighting;
|
|
|
|
[Dependency] private readonly IClyde _clyde = default!;
|
|
[Dependency] private readonly IEntityManager _entManager = default!;
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
|
[Dependency] private readonly IPrototypeManager _protoManager = default!;
|
|
private readonly EntityLookupSystem _lookup;
|
|
private readonly SharedTransformSystem _xformSys;
|
|
|
|
private readonly HashSet<Entity<SunShadowCastComponent>> _shadows = new();
|
|
|
|
private IRenderTexture? _blurTarget;
|
|
private IRenderTexture? _target;
|
|
|
|
public SunShadowOverlay()
|
|
{
|
|
IoCManager.InjectDependencies(this);
|
|
_xformSys = _entManager.System<SharedTransformSystem>();
|
|
_lookup = _entManager.System<EntityLookupSystem>();
|
|
ZIndex = AfterLightTargetOverlay.ContentZIndex + 1;
|
|
}
|
|
|
|
private List<Entity<MapGridComponent>> _grids = new();
|
|
|
|
protected override void Draw(in OverlayDrawArgs args)
|
|
{
|
|
var viewport = args.Viewport;
|
|
var eye = viewport.Eye;
|
|
|
|
if (eye == null)
|
|
return;
|
|
|
|
_grids.Clear();
|
|
_mapManager.FindGridsIntersecting(args.MapId,
|
|
args.WorldBounds.Enlarged(SunShadowComponent.MaxLength),
|
|
ref _grids);
|
|
|
|
var worldHandle = args.WorldHandle;
|
|
var mapId = args.MapId;
|
|
var worldBounds = args.WorldBounds;
|
|
var targetSize = viewport.LightRenderTarget.Size;
|
|
|
|
if (_target?.Size != targetSize)
|
|
{
|
|
_target = _clyde
|
|
.CreateRenderTarget(targetSize,
|
|
new RenderTargetFormatParameters(RenderTargetColorFormat.Rgba8Srgb),
|
|
name: "sun-shadow-target");
|
|
|
|
if (_blurTarget?.Size != targetSize)
|
|
{
|
|
_blurTarget = _clyde
|
|
.CreateRenderTarget(targetSize, new RenderTargetFormatParameters(RenderTargetColorFormat.Rgba8Srgb), name: "sun-shadow-blur");
|
|
}
|
|
}
|
|
|
|
var lightScale = viewport.LightRenderTarget.Size / (Vector2)viewport.Size;
|
|
var scale = viewport.RenderScale / (Vector2.One / lightScale);
|
|
|
|
foreach (var grid in _grids)
|
|
{
|
|
if (!_entManager.TryGetComponent(grid.Owner, out SunShadowComponent? sun))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var direction = sun.Direction;
|
|
var alpha = Math.Clamp(sun.Alpha, 0f, 1f);
|
|
|
|
// Nowhere to cast to so ignore it.
|
|
if (direction.Equals(Vector2.Zero) || alpha == 0f)
|
|
continue;
|
|
|
|
// Feature todo: dynamic shadows for mobs and trees. Also ideally remove the fake tree shadows.
|
|
// TODO: Jittering still not quite perfect
|
|
|
|
var expandedBounds = worldBounds.Enlarged(direction.Length() + 0.01f);
|
|
_shadows.Clear();
|
|
|
|
// Draw shadow polys to stencil
|
|
args.WorldHandle.RenderInRenderTarget(_target,
|
|
() =>
|
|
{
|
|
var invMatrix =
|
|
_target.GetWorldToLocalMatrix(eye, scale);
|
|
var indices = new Vector2[PhysicsConstants.MaxPolygonVertices * 2];
|
|
|
|
// Go through shadows in range.
|
|
|
|
// For each one we:
|
|
// - Get the original vertices.
|
|
// - Extrapolate these along the sun direction.
|
|
// - Combine the above into 1 single polygon to draw.
|
|
|
|
// Note that this is range-limited for accuracy; if you set it too high it will clip through walls or other undesirable entities.
|
|
// This is probably not noticeable most of the time but if you want something "accurate" you'll want to code a solution.
|
|
// Ideally the CPU would have its own shadow-map copy that we could just ray-cast each vert into though
|
|
// You might need to batch verts or the likes as this could get expensive.
|
|
_lookup.GetEntitiesIntersecting(mapId, expandedBounds, _shadows);
|
|
|
|
foreach (var ent in _shadows)
|
|
{
|
|
var xform = _entManager.GetComponent<TransformComponent>(ent.Owner);
|
|
var worldMatrix = _xformSys.GetWorldMatrix(xform);
|
|
var renderMatrix = Matrix3x2.Multiply(worldMatrix, invMatrix);
|
|
var pointCount = ent.Comp.Points.Length;
|
|
|
|
Array.Copy(ent.Comp.Points, indices, pointCount);
|
|
|
|
for (var i = 0; i < pointCount; i++)
|
|
{
|
|
indices[pointCount + i] = indices[i] + direction;
|
|
}
|
|
|
|
var points = PhysicsHull.ComputePoints(indices, pointCount * 2);
|
|
worldHandle.SetTransform(renderMatrix);
|
|
|
|
worldHandle.DrawPrimitives(DrawPrimitiveTopology.TriangleFan, points, Color.White);
|
|
}
|
|
},
|
|
Color.Transparent);
|
|
|
|
// Slightly blur it just to avoid aliasing issues on the later viewport-wide blur.
|
|
_clyde.BlurRenderTarget(viewport, _target, _target, eye, 1f);
|
|
|
|
// Draw stencil (see roofoverlay).
|
|
args.WorldHandle.RenderInRenderTarget(viewport.LightRenderTarget,
|
|
() =>
|
|
{
|
|
var invMatrix =
|
|
viewport.LightRenderTarget.GetWorldToLocalMatrix(eye, scale);
|
|
worldHandle.SetTransform(invMatrix);
|
|
|
|
var maskShader = _protoManager.Index<ShaderPrototype>("Mix").Instance();
|
|
worldHandle.UseShader(maskShader);
|
|
|
|
worldHandle.DrawTextureRect(_target.Texture, worldBounds, Color.Black.WithAlpha(alpha));
|
|
}, null);
|
|
}
|
|
}
|
|
}
|