Шейдеры на лампочки (#655)
* +light shader * fixed * toggling logic * nice * add option to settings --------- Co-authored-by: CaYpeN1 <artem7771art@gmail.com>
This commit is contained in:
95
Content.Client/_White/Lighting/Shaders/LightingOverlay.cs
Normal file
95
Content.Client/_White/Lighting/Shaders/LightingOverlay.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System.Numerics;
|
||||
using Content.Shared._White;
|
||||
using Content.Shared._White.Lighting.Shaders;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Utility;
|
||||
using DrawDepth = Content.Shared.DrawDepth.DrawDepth;
|
||||
|
||||
namespace Content.Client._White.Lighting.Shaders;
|
||||
|
||||
public sealed class LightingOverlay : Overlay
|
||||
{
|
||||
private readonly IPrototypeManager _prototypeManager;
|
||||
private readonly EntityManager _entityManager;
|
||||
private readonly SpriteSystem _spriteSystem;
|
||||
private readonly TransformSystem _transformSystem;
|
||||
private readonly IConfigurationManager _cfg;
|
||||
|
||||
public override OverlaySpace Space => OverlaySpace.WorldSpaceEntities;
|
||||
public override bool RequestScreenTexture => true;
|
||||
|
||||
private readonly ShaderInstance _shader;
|
||||
private bool _enableGlowing;
|
||||
|
||||
public LightingOverlay(EntityManager entityManager, IPrototypeManager prototypeManager)
|
||||
{
|
||||
_entityManager = entityManager;
|
||||
_spriteSystem = entityManager.EntitySysManager.GetEntitySystem<SpriteSystem>();
|
||||
_prototypeManager = prototypeManager;
|
||||
_transformSystem = entityManager.EntitySysManager.GetEntitySystem<TransformSystem>();
|
||||
_cfg = IoCManager.Resolve<IConfigurationManager>();
|
||||
_cfg.OnValueChanged(WhiteCVars.EnableLightsGlowing, val => _enableGlowing = val, true);
|
||||
|
||||
IoCManager.InjectDependencies(this);
|
||||
|
||||
_shader = _prototypeManager.Index<ShaderPrototype>("LightingOverlay").InstanceUnique();
|
||||
ZIndex = (int) DrawDepth.Overdoors;
|
||||
}
|
||||
|
||||
protected override void Draw(in OverlayDrawArgs args)
|
||||
{
|
||||
if (!_enableGlowing)
|
||||
return;
|
||||
|
||||
if (ScreenTexture == null)
|
||||
return;
|
||||
|
||||
var xformCompQuery = _entityManager.GetEntityQuery<TransformComponent>();
|
||||
|
||||
var handle = args.WorldHandle;
|
||||
var bounds = args.WorldAABB.Enlarged(5f);
|
||||
|
||||
_shader.SetParameter("SCREEN_TEXTURE", ScreenTexture);
|
||||
|
||||
var query = _entityManager.AllEntityQueryEnumerator<LightingOverlayComponent, TransformComponent>();
|
||||
while (query.MoveNext(out var uid, out var component, out var xform))
|
||||
{
|
||||
if (xform.MapID != args.MapId)
|
||||
continue;
|
||||
|
||||
if (!component.Enabled)
|
||||
continue;
|
||||
|
||||
var worldPos = _transformSystem.GetWorldPosition(xform, xformCompQuery);
|
||||
|
||||
if (!bounds.Contains(worldPos))
|
||||
continue;
|
||||
|
||||
var color = component.Color;
|
||||
|
||||
if (color == null && _entityManager.TryGetComponent<PointLightComponent>(uid, out var pointLight))
|
||||
color = pointLight.Color;
|
||||
|
||||
var (_, _, worldMatrix) = xform.GetWorldPositionRotationMatrix(xformCompQuery);
|
||||
handle.SetTransform(worldMatrix);
|
||||
|
||||
var mask = _spriteSystem.Frame0(component.Sprite); // mask
|
||||
|
||||
var xOffset = component.Offsetx - (mask.Width / 2) / EyeManager.PixelsPerMeter;
|
||||
var yOffset = component.Offsety - (mask.Height / 2) / EyeManager.PixelsPerMeter;
|
||||
|
||||
var textureVector = new Vector2(xOffset, yOffset);
|
||||
|
||||
handle.DrawTexture(mask, textureVector, color);
|
||||
|
||||
handle.UseShader(_shader);
|
||||
}
|
||||
|
||||
handle.UseShader(null);
|
||||
handle.SetTransform(Matrix3.Identity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Client._White.Lighting.Shaders;
|
||||
|
||||
public sealed class LightingOverlaySystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IOverlayManager _overlayManager = default!;
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
private LightingOverlay _lightingOverlay = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_lightingOverlay = new LightingOverlay(EntityManager, _prototypeManager);
|
||||
_overlayManager.AddOverlay(_lightingOverlay);
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
_overlayManager.RemoveOverlay(_lightingOverlay);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using Content.Shared._White.Lighting.Shaders;
|
||||
using Content.Shared.Power;
|
||||
using Robust.Client.GameObjects;
|
||||
|
||||
namespace Content.Client._White.Lighting.Shaders;
|
||||
|
||||
public sealed class TogglingLightOverlaySystem : EntitySystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeLocalEvent<LightingOverlayComponent, AppearanceChangeEvent>(OnAppearanceChange);
|
||||
}
|
||||
|
||||
private void OnAppearanceChange(EntityUid uid, LightingOverlayComponent component, AppearanceChangeEvent args)
|
||||
{
|
||||
if (!args.AppearanceData.TryGetValue(PowerDeviceVisuals.Powered, out var state))
|
||||
return;
|
||||
|
||||
component.Enabled = (bool) state;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user