54 private readonly List<RevealSource> _revealSources = [];
55 private readonly List<TrackedLightReveal> _trackedLightReveals = [];
56 private readonly List<TrackedLightLayerReveal> _trackedLightLayers = [];
58 private readonly SKPaint _darknessPaint =
new()
61 BlendMode = SKBlendMode.SrcOver,
62 Style = SKPaintStyle.Fill
65 private Color _darknessColor = Color.Black;
66 private byte _darknessOpacity = 190;
67 private float _innerClearRadiusRatio = 0.20f;
68 private float _midpointRadiusRatio = 0.62f;
69 private float _midpointStrength = 0.45f;
85 string? nickname =
null)
91 screenBounds: view.
Viewport.TargetRectPx,
95 ProjectionLayer = projectionLayer ??
throw new ArgumentNullException(nameof(projectionLayer));
97 RebuildDarknessPaint();
115 get => _darknessColor;
118 if (_darknessColor == value)
121 _darknessColor = value;
122 RebuildDarknessPaint();
132 get => _darknessOpacity;
135 if (_darknessOpacity == value)
138 _darknessOpacity = value;
139 RebuildDarknessPaint();
153 get => _innerClearRadiusRatio;
156 float clamped = Math.Clamp(value, 0f, 1f);
158 if (Math.Abs(_innerClearRadiusRatio - clamped) < 0.0001f)
161 _innerClearRadiusRatio = clamped;
163 if (_midpointRadiusRatio < _innerClearRadiusRatio)
164 _midpointRadiusRatio = _innerClearRadiusRatio;
175 get => _midpointRadiusRatio;
178 float clamped = Math.Clamp(value, 0f, 1f);
179 clamped = Math.Max(clamped, _innerClearRadiusRatio);
181 if (Math.Abs(_midpointRadiusRatio - clamped) < 0.0001f)
184 _midpointRadiusRatio = clamped;
197 get => _midpointStrength;
200 float clamped = Math.Clamp(value, 0f, 1f);
202 if (Math.Abs(_midpointStrength - clamped) < 0.0001f)
205 _midpointStrength = clamped;
225 _revealSources.Add(source);
251 float radiusScale = 1f,
252 float intensityScale = 1f,
253 bool trackIntensity =
true,
254 string? nickname =
null)
257 throw new ArgumentNullException(nameof(light));
259 if (_trackedLightReveals.Any(t => ReferenceEquals(t.Light, light)))
260 return _trackedLightReveals.First(t => ReferenceEquals(t.Light, light)).Source;
268 _revealSources.Add(source);
270 var tracked =
new TrackedLightReveal(
273 Math.Max(0f, radiusScale),
274 Math.Max(0f, intensityScale),
277 _trackedLightReveals.Add(tracked);
279 light.Changed += OnTrackedLightChanged;
280 light.Disposing += OnTrackedLightDisposing;
282 SyncTrackedLight(tracked);
306 float radiusScale = 1f,
307 float intensityScale = 1f,
308 bool trackIntensity =
true)
310 if (lightLayer is
null)
311 throw new ArgumentNullException(nameof(lightLayer));
313 if (_trackedLightLayers.Any(t => ReferenceEquals(t.LightLayer, lightLayer)))
316 var trackedLayer =
new TrackedLightLayerReveal(
318 Math.Max(0f, radiusScale),
319 Math.Max(0f, intensityScale),
322 _trackedLightLayers.Add(trackedLayer);
324 foreach (var light
in lightLayer.Lights)
327 trackedLayer.RadiusScale,
328 trackedLayer.IntensityScale,
329 trackedLayer.TrackIntensity);
331 lightLayer.LightAdded += OnTrackedLayerLightAdded;
332 lightLayer.LightRemoving += OnTrackedLayerLightRemoving;
342 var tracked = _trackedLightReveals.FirstOrDefault(t => ReferenceEquals(t.Light, light));
361 var tracked = _trackedLightReveals.FirstOrDefault(t => ReferenceEquals(t.Source, source));
363 if (tracked is not
null)
366 bool removed = _revealSources.Remove(source);
379 if (_revealSources.Count == 0 && _trackedLightReveals.Count == 0 && _trackedLightLayers.Count == 0)
382 foreach (var trackedLayer
in _trackedLightLayers.ToArray())
384 trackedLayer.LightLayer.LightAdded -= OnTrackedLayerLightAdded;
385 trackedLayer.LightLayer.LightRemoving -= OnTrackedLayerLightRemoving;
388 _trackedLightLayers.Clear();
390 foreach (var tracked
in _trackedLightReveals.ToArray())
393 _revealSources.Clear();
456 var targetRect =
View!.Viewport.TargetRectPx;
467 var canvas = backbuffer.
Canvas;
468 var destRect = destRectScreen.ToSKRect();
472 canvas.SaveLayer(destRect,
null);
474 canvas.DrawRect(destRect, _darknessPaint);
476 for (
int i = 0; i < _revealSources.Count; i++)
478 var source = _revealSources[i];
480 if (!source.Enabled || source.RadiusWorldPx <= 0.01f || source.Intensity <= 0.001f)
485 var radiusProbeWorld =
new PointF(source.CenterWorldPx.X + source.RadiusWorldPx, source.CenterWorldPx.Y);
487 float screenRadius = Math.Abs(radiusProbeScreen.X - centerScreen.X);
489 if (screenRadius <= 0.01f)
492 using var revealShader = BuildRevealShader(centerScreen, screenRadius, source.Intensity);
494 using var revealPaint =
new SKPaint
497 BlendMode = SKBlendMode.DstOut,
498 Shader = revealShader,
499 Style = SKPaintStyle.Fill
502 canvas.DrawRect(destRect, revealPaint);
508 internal void RefreshFromSourceChange()
513 private void OnTrackedLightChanged(DirectRadialLight light)
515 foreach (var tracked
in _trackedLightReveals.Where(t => ReferenceEquals(t.Light, light)).ToArray())
516 SyncTrackedLight(tracked);
519 private void OnTrackedLightDisposing(
object? sender, IDirectDrawable drawing)
521 if (drawing is DirectRadialLight light)
525 private void OnTrackedLayerLightAdded(DirectRadialLight light)
527 foreach (var trackedLayer
in _trackedLightLayers.Where(t => t.LightLayer.Lights.Contains(light)).ToArray())
530 trackedLayer.RadiusScale,
531 trackedLayer.IntensityScale,
532 trackedLayer.TrackIntensity);
535 private void OnTrackedLayerLightRemoving(DirectRadialLight light)
540 private void SyncTrackedLight(TrackedLightReveal tracked)
542 var light = tracked.Light;
544 float intensity = tracked.TrackIntensity
545 ? light.EffectiveIntensity * tracked.IntensityScale
546 : tracked.IntensityScale;
548 tracked.Source.SyncFromTrackedLight(
550 light.RadiusWorldPx * tracked.RadiusScale,
554 private void UntrackLight(TrackedLightReveal tracked,
bool removeRevealSource)
556 tracked.Light.Changed -= OnTrackedLightChanged;
557 tracked.Light.Disposing -= OnTrackedLightDisposing;
558 _trackedLightReveals.Remove(tracked);
560 if (removeRevealSource)
561 _revealSources.Remove(tracked.Source);
566 private void RebuildDarknessPaint()
568 _darknessPaint.Color = Color.FromArgb(_darknessOpacity, _darknessColor).ToSKColor();
571 private SKShader BuildRevealShader(PointF centerScreenPx,
float radiusScreenPx,
float intensity)
573 byte centerAlpha = (byte)Math.Clamp((
int)Math.Round(255f * Math.Clamp(intensity, 0f, 1f)), 0, 255);
575 byte midAlpha = (byte)Math.Clamp(
576 (
int)Math.Round(255f * Math.Clamp(intensity, 0f, 1f) * _midpointStrength),
580 return SKShader.CreateRadialGradient(
581 new SKPoint(centerScreenPx.X, centerScreenPx.Y),
609 _innerClearRadiusRatio,
610 _midpointRadiusRatio,
613 mode: SKShaderTileMode.Clamp);
617 protected override void Dispose(
bool disposing)
622 _darknessPaint.Dispose();
625 base.Dispose(disposing);
628 private sealed class TrackedLightReveal(
632 float intensityScale,
643 public RevealSource Source {
get; } = source;
648 public float RadiusScale {
get; } = radiusScale;
653 public float IntensityScale {
get; } = intensityScale;
658 public bool TrackIntensity {
get; } = trackIntensity;
661 private sealed class TrackedLightLayerReveal(
662 DirectLightLayer lightLayer,
664 float intensityScale,
670 public DirectLightLayer LightLayer {
get; } = lightLayer;
675 public float RadiusScale {
get; } = radiusScale;
680 public float IntensityScale {
get; } = intensityScale;
685 public bool TrackIntensity {
get; } = trackIntensity;
691 public sealed class RevealSource
694 private PointF _centerWorldPx;
695 private float _radiusWorldPx;
696 private float _intensity = 1f;
697 private bool _enabled =
true;
699 internal RevealSource(
701 PointF centerWorldPx,
706 _centerWorldPx = centerWorldPx;
707 _radiusWorldPx = Math.Max(0f, radiusWorldPx);
724 if (_enabled == value)
728 _owner.RefreshFromSourceChange();
737 get => _centerWorldPx;
740 if (_centerWorldPx == value)
743 _centerWorldPx = value;
744 _owner.RefreshFromSourceChange();
753 get => _radiusWorldPx;
756 float clamped = Math.Max(0f, value);
758 if (Math.Abs(_radiusWorldPx - clamped) < 0.0001f)
761 _radiusWorldPx = clamped;
762 _owner.RefreshFromSourceChange();
777 float clamped = Math.Clamp(value, 0f, 1f);
779 if (Math.Abs(_intensity - clamped) < 0.0001f)
782 _intensity = clamped;
783 _owner.RefreshFromSourceChange();
805 internal void SyncFromTrackedLight(PointF centerWorldPx,
float radiusWorldPx,
float intensity)
808 _centerWorldPx != centerWorldPx ||
809 Math.Abs(_radiusWorldPx - radiusWorldPx) >= 0.0001f ||
810 Math.Abs(_intensity - intensity) >= 0.0001f;
815 _centerWorldPx = centerWorldPx;
816 _radiusWorldPx = Math.Max(0f, radiusWorldPx);
817 _intensity = Math.Clamp(intensity, 0f, 1f);
818 _owner.RefreshFromSourceChange();
Represents a single world-space reveal source used by a DirectDarknessOverlay.
void MoveTo(PointF centerWorldPx)
Moves the reveal source to a new world-space position.
bool Enabled
Gets or sets whether this reveal source is currently active.
void SetRadius(float radiusWorldPx)
Sets the reveal radius in world-space pixels.
float RadiusWorldPx
Gets or sets the reveal radius in world-space pixels.
float Intensity
Gets or sets the reveal intensity from 0..1.
string? Nickname
Optional human-readable name for debugging and lookup.
PointF CenterWorldPx
Gets or sets the reveal center in world-space pixels.
byte DarknessOpacity
Gets or sets the opacity of the darkness overlay.
void ClearRevealSources()
Removes all reveal sources and stops all automatic tracking.
RevealSource AddRevealSource(PointF centerWorldPx, float radiusWorldPx, string? nickname=null)
Adds a new manually controlled reveal source in world-space.
DirectDarknessOverlay(RenderSurfaceHostBase renderSurfaceHost, View view, SceneLayer projectionLayer, string? nickname=null)
Initializes a new darkness/fog overlay that covers a specific view.
float MidpointRadiusRatio
Gets or sets the radius ratio of the midpoint alpha stop used for reveal falloff.
float MidpointStrength
Gets or sets the removal strength at the midpoint stop.
float InnerClearRadiusRatio
Gets or sets the radius ratio that remains fully revealed before falloff begins.
DirectDarknessOverlay SetDarknessColor(Color color)
Fluent helper to set the darkness tint.
bool UntrackLight(DirectRadialLight light)
Stops tracking a specific light and removes its reveal source.
void TrackLightLayer(DirectLightLayer lightLayer, float radiusScale=1f, float intensityScale=1f, bool trackIntensity=true)
Tracks every current and future light in a DirectLightLayer.
DirectDarknessOverlay SetInnerClearRadiusRatio(float ratio)
Fluent helper to set the fully-revealed radius ratio.
SceneLayer ProjectionLayer
Gets the scene layer used to project reveal source world positions into screen coordinates.
bool RemoveRevealSource(RevealSource source)
Removes a reveal source from this overlay.
RevealSource TrackLight(DirectRadialLight light, float radiusScale=1f, float intensityScale=1f, bool trackIntensity=true, string? nickname=null)
Adds a reveal source that automatically tracks a DirectRadialLight.
Color DarknessColor
Gets or sets the tint color of the overlay.
DirectDarknessOverlay SetDarknessOpacity(byte opacity)
Fluent helper to set the darkness opacity.
override void Update(long tick)
Performs per-frame update logic for this direct drawing, including fade and reveal animations....
IReadOnlyList< RevealSource > RevealSources
Gets a live, ordered list of reveal sources used by this overlay.
override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
DirectDarknessOverlay SetMidpointStrength(float strength)
Fluent helper to set the midpoint reveal strength.
override void Dispose(bool disposing)
Releases resources used by this DirectDrawingBase instance.When disposing is true,...
DirectDarknessOverlay SetMidpointRadiusRatio(float ratio)
Fluent helper to set the midpoint radius ratio.
int ZOrder
Gets or sets the Z-order (depth) used for sorting this direct drawing relative to other drawables.
View? View
Gets the view to which this direct drawing is attached, or null if the drawing is in scene-layer mode...
string? Nickname
Gets the optional human-readable name for this direct drawing.
SceneLayer? SceneLayer
Gets the scene layer to which this direct drawing is attached, or null if the drawing is in view mode...
DirectDrawingBase(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, SceneLayer? sceneLayer, View? view, Rectangle? screenBounds, Rectangle? worldBounds, string? nickname=null)
Initializes a new instance of the DirectDrawingBase class.
Rectangle ScreenBounds
Gets or sets the screen-space bounds of this direct drawing in pixels.
Convenience owner for a group of scene-layer radial lights.
Draws a bounded, radial light effect as a scene-layer direct drawing.
float RadiusWorldPx
Gets the light radius in world pixels.
PointF CenterWorldPx
Gets the light center in world pixels.
Represents a base class for managing a graphical backbuffer, and is the in-memory surface where rende...
SKCanvas Canvas
Gets the SkiaSharp canvas used for drawing operations on this backbuffer.
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
A rectangular window on the render target with its own zoom and placement. Viewport never moves the w...
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.