33 private readonly List<RevealSource> _revealSources = [];
34 private readonly List<TrackedLightReveal> _trackedLightReveals = [];
35 private readonly List<TrackedLightLayerReveal> _trackedLightLayers = [];
37 private readonly SKPaint _darknessPaint =
new()
40 BlendMode = SKBlendMode.SrcOver,
41 Style = SKPaintStyle.Fill
44 private Color _darknessColor = Color.Black;
45 private byte _darknessOpacity = 190;
46 private float _innerClearRadiusRatio = 0.20f;
47 private float _midpointRadiusRatio = 0.62f;
48 private float _midpointStrength = 0.45f;
60 Rectangle worldBounds,
61 string? nickname =
null)
68 worldBounds: worldBounds,
72 RebuildDarknessPaint();
94 get => _darknessColor;
97 if (_darknessColor == value)
100 _darknessColor = value;
101 RebuildDarknessPaint();
111 get => _darknessOpacity;
114 if (_darknessOpacity == value)
117 _darknessOpacity = value;
118 RebuildDarknessPaint();
128 get => _innerClearRadiusRatio;
131 float clamped = Math.Clamp(value, 0f, 1f);
133 if (Math.Abs(_innerClearRadiusRatio - clamped) < 0.0001f)
136 _innerClearRadiusRatio = clamped;
138 if (_midpointRadiusRatio < _innerClearRadiusRatio)
139 _midpointRadiusRatio = _innerClearRadiusRatio;
150 get => _midpointRadiusRatio;
153 float clamped = Math.Clamp(value, 0f, 1f);
154 clamped = Math.Max(clamped, _innerClearRadiusRatio);
156 if (Math.Abs(_midpointRadiusRatio - clamped) < 0.0001f)
159 _midpointRadiusRatio = clamped;
169 get => _midpointStrength;
172 float clamped = Math.Clamp(value, 0f, 1f);
174 if (Math.Abs(_midpointStrength - clamped) < 0.0001f)
177 _midpointStrength = clamped;
197 _revealSources.Add(source);
219 float radiusScale = 1f,
220 float intensityScale = 1f,
221 bool trackIntensity =
true,
222 string? nickname =
null)
225 throw new ArgumentNullException(nameof(light));
227 EnsureSameSceneLayer(light);
229 if (_trackedLightReveals.Any(t => ReferenceEquals(t.Light, light)))
230 return _trackedLightReveals.First(t => ReferenceEquals(t.Light, light)).Source;
238 _revealSources.Add(source);
240 var tracked =
new TrackedLightReveal(
243 Math.Max(0f, radiusScale),
244 Math.Max(0f, intensityScale),
247 _trackedLightReveals.Add(tracked);
249 light.Changed += OnTrackedLightChanged;
250 light.Disposing += OnTrackedLightDisposing;
252 SyncTrackedLight(tracked);
270 float radiusScale = 1f,
271 float intensityScale = 1f,
272 bool trackIntensity =
true)
274 if (lightLayer is
null)
275 throw new ArgumentNullException(nameof(lightLayer));
278 throw new ArgumentException(
279 "SceneLayer-bound darkness can only track a DirectLightLayer attached to the same SceneLayer.",
282 if (_trackedLightLayers.Any(t => ReferenceEquals(t.LightLayer, lightLayer)))
285 var trackedLayer =
new TrackedLightLayerReveal(
287 Math.Max(0f, radiusScale),
288 Math.Max(0f, intensityScale),
291 _trackedLightLayers.Add(trackedLayer);
293 foreach (var light
in lightLayer.Lights)
296 trackedLayer.RadiusScale,
297 trackedLayer.IntensityScale,
298 trackedLayer.TrackIntensity);
300 lightLayer.LightAdded += OnTrackedLayerLightAdded;
301 lightLayer.LightRemoving += OnTrackedLayerLightRemoving;
311 var tracked = _trackedLightReveals.FirstOrDefault(t => ReferenceEquals(t.Light, light));
330 var tracked = _trackedLightReveals.FirstOrDefault(t => ReferenceEquals(t.Source, source));
332 if (tracked is not
null)
335 bool removed = _revealSources.Remove(source);
348 if (_revealSources.Count == 0 && _trackedLightReveals.Count == 0 && _trackedLightLayers.Count == 0)
351 foreach (var trackedLayer
in _trackedLightLayers.ToArray())
353 trackedLayer.LightLayer.LightAdded -= OnTrackedLayerLightAdded;
354 trackedLayer.LightLayer.LightRemoving -= OnTrackedLayerLightRemoving;
357 _trackedLightLayers.Clear();
359 foreach (var tracked
in _trackedLightReveals.ToArray())
362 _revealSources.Clear();
435 if (destRectScreen.Width <= 0f || destRectScreen.Height <= 0f ||
WorldBounds.Width <= 0 ||
439 var canvas = backbuffer.
Canvas;
440 var destRect = destRectScreen.ToSKRect();
444 canvas.SaveLayer(destRect,
null);
446 canvas.DrawRect(destRect, _darknessPaint);
448 for (
int i = 0; i < _revealSources.Count; i++)
450 var source = _revealSources[i];
452 if (!source.Enabled || source.RadiusWorldPx <= 0.01f || source.Intensity <= 0.001f)
455 var centerScreen = WorldPointToDrawRect(source.CenterWorldPx, destRectScreen);
456 float radiusScreen = WorldRadiusToDrawRadius(source.RadiusWorldPx, destRectScreen);
458 if (radiusScreen <= 0.01f)
461 using var revealShader = BuildRevealShader(centerScreen, radiusScreen, source.Intensity);
463 using var revealPaint =
new SKPaint
466 BlendMode = SKBlendMode.DstOut,
467 Shader = revealShader,
468 Style = SKPaintStyle.Fill
471 canvas.DrawRect(destRect, revealPaint);
477 internal void RefreshFromSourceChange()
482 private void OnTrackedLightChanged(DirectRadialLight light)
484 foreach (var tracked
in _trackedLightReveals.Where(t => ReferenceEquals(t.Light, light)).ToArray())
485 SyncTrackedLight(tracked);
488 private void OnTrackedLightDisposing(
object? sender, IDirectDrawable drawing)
490 if (drawing is DirectRadialLight light)
494 private void OnTrackedLayerLightAdded(DirectRadialLight light)
496 if (!ReferenceEquals(light.SceneLayer,
SceneLayer))
499 foreach (var trackedLayer
in _trackedLightLayers.Where(t => t.LightLayer.Lights.Contains(light)).ToArray())
502 trackedLayer.RadiusScale,
503 trackedLayer.IntensityScale,
504 trackedLayer.TrackIntensity);
507 private void OnTrackedLayerLightRemoving(DirectRadialLight light)
512 private void SyncTrackedLight(TrackedLightReveal tracked)
514 var light = tracked.Light;
516 float intensity = tracked.TrackIntensity
517 ? light.EffectiveIntensity * tracked.IntensityScale
518 : tracked.IntensityScale;
520 tracked.Source.SyncFromTrackedLight(
522 light.RadiusWorldPx * tracked.RadiusScale,
526 private void UntrackLight(TrackedLightReveal tracked,
bool removeRevealSource)
528 tracked.Light.Changed -= OnTrackedLightChanged;
529 tracked.Light.Disposing -= OnTrackedLightDisposing;
530 _trackedLightReveals.Remove(tracked);
532 if (removeRevealSource)
533 _revealSources.Remove(tracked.Source);
538 private void EnsureSameSceneLayer(DirectRadialLight light)
540 if (!ReferenceEquals(light.SceneLayer,
SceneLayer))
541 throw new ArgumentException(
542 "SceneLayer-bound darkness can only track lights attached to the same SceneLayer.",
546 private PointF WorldPointToDrawRect(PointF worldPx, RectangleF destRectScreen)
548 float scaleX = destRectScreen.Width /
WorldBounds.Width;
549 float scaleY = destRectScreen.Height /
WorldBounds.Height;
552 destRectScreen.Left + (worldPx.X -
WorldBounds.Left) * scaleX,
553 destRectScreen.Top + (worldPx.Y -
WorldBounds.Top) * scaleY);
556 private float WorldRadiusToDrawRadius(
float radiusWorldPx, RectangleF destRectScreen)
558 float scaleX = destRectScreen.Width /
WorldBounds.Width;
559 float scaleY = destRectScreen.Height /
WorldBounds.Height;
561 return Math.Max(Math.Abs(radiusWorldPx * scaleX), Math.Abs(radiusWorldPx * scaleY));
564 private void RebuildDarknessPaint()
566 _darknessPaint.Color = Color.FromArgb(_darknessOpacity, _darknessColor).ToSKColor();
569 private SKShader BuildRevealShader(PointF centerScreenPx,
float radiusScreenPx,
float intensity)
571 byte centerAlpha = (byte)Math.Clamp((
int)Math.Round(255f * Math.Clamp(intensity, 0f, 1f)), 0, 255);
573 byte midAlpha = (byte)Math.Clamp(
574 (
int)Math.Round(255f * Math.Clamp(intensity, 0f, 1f) * _midpointStrength),
578 return SKShader.CreateRadialGradient(
579 new SKPoint(centerScreenPx.X, centerScreenPx.Y),
607 _innerClearRadiusRatio,
608 _midpointRadiusRatio,
611 mode: SKShaderTileMode.Clamp);
615 protected override void Dispose(
bool disposing)
620 _darknessPaint.Dispose();
623 base.Dispose(disposing);
626 private sealed class TrackedLightReveal(
630 float intensityScale,
641 public RevealSource Source {
get; } = source;
646 public float RadiusScale {
get; } = radiusScale;
651 public float IntensityScale {
get; } = intensityScale;
656 public bool TrackIntensity {
get; } = trackIntensity;
659 private sealed class TrackedLightLayerReveal(
660 DirectLightLayer lightLayer,
662 float intensityScale,
668 public DirectLightLayer LightLayer {
get; } = lightLayer;
673 public float RadiusScale {
get; } = radiusScale;
678 public float IntensityScale {
get; } = intensityScale;
683 public bool TrackIntensity {
get; } = trackIntensity;
689 public sealed class RevealSource
692 private PointF _centerWorldPx;
693 private float _radiusWorldPx;
694 private float _intensity = 1f;
695 private bool _enabled =
true;
697 internal RevealSource(
699 PointF centerWorldPx,
704 _centerWorldPx = centerWorldPx;
705 _radiusWorldPx = Math.Max(0f, radiusWorldPx);
722 if (_enabled == value)
726 _owner.RefreshFromSourceChange();
735 get => _centerWorldPx;
738 if (_centerWorldPx == value)
741 _centerWorldPx = value;
742 _owner.RefreshFromSourceChange();
751 get => _radiusWorldPx;
754 float clamped = Math.Max(0f, value);
756 if (Math.Abs(_radiusWorldPx - clamped) < 0.0001f)
759 _radiusWorldPx = clamped;
760 _owner.RefreshFromSourceChange();
772 float clamped = Math.Clamp(value, 0f, 1f);
774 if (Math.Abs(_intensity - clamped) < 0.0001f)
777 _intensity = clamped;
778 _owner.RefreshFromSourceChange();
800 internal void SyncFromTrackedLight(PointF centerWorldPx,
float radiusWorldPx,
float intensity)
803 _centerWorldPx != centerWorldPx ||
804 Math.Abs(_radiusWorldPx - radiusWorldPx) >= 0.0001f ||
805 Math.Abs(_intensity - intensity) >= 0.0001f;
810 _centerWorldPx = centerWorldPx;
811 _radiusWorldPx = Math.Max(0f, radiusWorldPx);
812 _intensity = Math.Clamp(intensity, 0f, 1f);
813 _owner.RefreshFromSourceChange();
int ZOrder
Gets or sets the Z-order (depth) used for sorting this direct drawing relative to other drawables.
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...
Rectangle WorldBounds
Gets or sets the world-space bounds of this direct drawing in pixels.
DirectDrawingBase(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, SceneLayer? sceneLayer, View? view, Rectangle? screenBounds, Rectangle? worldBounds, string? nickname=null)
Initializes a new instance of the DirectDrawingBase class.
Convenience owner for a group of scene-layer radial lights.
SceneLayer SceneLayer
Gets the scene layer where lights are drawn.
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 single world-space reveal source used by a DirectSceneLayerDarknessOverlay.
PointF CenterWorldPx
Gets or sets the reveal center in world-space pixels.
bool Enabled
Gets or sets whether this reveal source is currently active.
string? Nickname
Optional human-readable name for debugging and lookup.
void MoveTo(PointF centerWorldPx)
Moves the reveal source to a new world-space position.
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.
DirectSceneLayerDarknessOverlay(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle worldBounds, string? nickname=null)
Initializes a new scene-layer darkness/fog 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 on this same SceneLayer.
DirectSceneLayerDarknessOverlay SetMidpointStrength(float strength)
Fluent helper to set the midpoint reveal strength.
bool RemoveRevealSource(RevealSource source)
Removes a reveal source from this overlay.
byte DarknessOpacity
Gets or sets the opacity of the darkness overlay.
void ClearRevealSources()
Removes all reveal sources and stops all automatic tracking.
Rectangle DarknessWorldBounds
Gets or sets the world-space region affected by the darkness/fog.
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.
IReadOnlyList< RevealSource > RevealSources
Gets a live, ordered list of reveal sources used by this overlay.
void TrackLightLayer(DirectLightLayer lightLayer, float radiusScale=1f, float intensityScale=1f, bool trackIntensity=true)
Tracks every current and future light in a DirectLightLayer attached to this same SceneLayer.
override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
DirectSceneLayerDarknessOverlay SetDarknessWorldBounds(Rectangle worldBounds)
Fluent helper to set the darkness/fog world bounds.
float MidpointRadiusRatio
Gets or sets the radius ratio of the midpoint alpha stop used for reveal falloff.
DirectSceneLayerDarknessOverlay SetDarknessColor(Color color)
Fluent helper to set the darkness tint.
DirectSceneLayerDarknessOverlay SetDarknessOpacity(byte opacity)
Fluent helper to set the darkness opacity.
Color DarknessColor
Gets or sets the tint color of the overlay.
override void Dispose(bool disposing)
Releases resources used by this DirectDrawingBase instance.When disposing is true,...
DirectSceneLayerDarknessOverlay SetInnerClearRadiusRatio(float ratio)
Fluent helper to set the fully-revealed radius ratio.
bool UntrackLight(DirectRadialLight light)
Stops tracking a specific light and removes its reveal source.
RevealSource AddRevealSource(PointF centerWorldPx, float radiusWorldPx, string? nickname=null)
Adds a new manually controlled reveal source in world-space.
DirectSceneLayerDarknessOverlay SetMidpointRadiusRatio(float ratio)
Fluent helper to set the midpoint radius ratio.
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 ...
Represents a single layer within a Scene, containing a grid of tiles and supporting various coordinat...
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.