33 private PointF _centerWorldPx;
34 private float _radiusWorldPx;
35 private Color _lightColor;
36 private float _intensity = 1f;
37 private float _hotspotRadiusRatio = 0.06f;
38 private float _midpointRadiusRatio = 0.55f;
39 private float _midpointIntensityRatio = 0.35f;
40 private bool _isAntialias =
true;
41 private SKBlendMode _blendMode = SKBlendMode.Screen;
43 private bool _flickerEnabled;
44 private float _flickerAmount = 0.08f;
45 private int _flickerRefreshHz = 12;
46 private float _flickerPhaseSec;
47 private float _flickerMultiplier = 1f;
48 private long _lastFlickerRefreshTick;
59 public event Action<DirectRadialLight>?
Changed;
76 string? nickname =
null)
83 worldBounds: BoundsFromCenter(centerWorldPx, radiusWorldPx),
86 _lightColor = lightColor;
87 _centerWorldPx = centerWorldPx;
88 _radiusWorldPx = Math.Max(1f, radiusWorldPx);
96 get => _centerWorldPx;
99 if (ApproximatelyEqual(_centerWorldPx, value))
102 _centerWorldPx = value;
115 get => _radiusWorldPx;
118 var clamped = Math.Max(1f, value);
120 if (Math.Abs(_radiusWorldPx - clamped) < 0.001f)
123 _radiusWorldPx = clamped;
136 if (_lightColor.ToArgb() == value.ToArgb())
153 var clamped = Clamp01(value);
155 if (Math.Abs(_intensity - clamped) < 0.001f)
158 _intensity = clamped;
177 if (_blendMode == value)
190 get => _hotspotRadiusRatio;
193 var clamped = Clamp01(value);
195 if (Math.Abs(_hotspotRadiusRatio - clamped) < 0.001f)
198 _hotspotRadiusRatio = clamped;
208 get => _midpointRadiusRatio;
211 var clamped = Clamp01(value);
213 if (Math.Abs(_midpointRadiusRatio - clamped) < 0.001f)
216 _midpointRadiusRatio = clamped;
226 get => _midpointIntensityRatio;
229 var clamped = Clamp01(value);
231 if (Math.Abs(_midpointIntensityRatio - clamped) < 0.001f)
234 _midpointIntensityRatio = clamped;
247 if (_isAntialias == value)
250 _isAntialias = value;
265 get => _flickerEnabled;
268 if (_flickerEnabled == value)
271 _flickerEnabled = value;
272 _flickerMultiplier = 1f;
273 _lastFlickerRefreshTick = 0;
284 get => _flickerAmount;
287 var clamped = Math.Clamp(value, 0f, 1f);
289 if (Math.Abs(_flickerAmount - clamped) < 0.001f)
292 _flickerAmount = clamped;
303 get => _flickerRefreshHz;
304 set => _flickerRefreshHz = Math.Max(1, value);
350 if (destRectScreen.Width <= 0f || destRectScreen.Height <= 0f)
353 var canvas = backbuffer.
Canvas;
356 if (worldBounds.Width <= 0 || worldBounds.Height <= 0)
359 var rect =
new SKRect(
362 destRectScreen.Right,
363 destRectScreen.Bottom);
365 float scaleX = destRectScreen.Width / worldBounds.Width;
366 float scaleY = destRectScreen.Height / worldBounds.Height;
372 var center =
new SKPoint(
373 destRectScreen.Left + (_centerWorldPx.X - worldBounds.Left) * scaleX,
374 destRectScreen.Top + (_centerWorldPx.Y - worldBounds.Top) * scaleY);
376 var radius = Math.Max(Math.Abs(_radiusWorldPx * scaleX), Math.Abs(_radiusWorldPx * scaleY));
381 var hotspot = Math.Clamp(_hotspotRadiusRatio, 0f, 0.95f);
382 var midpoint = Math.Clamp(_midpointRadiusRatio, hotspot + 0.001f, 0.999f);
385 var inner = ToSkColor(_lightColor, effectiveIntensity);
386 var middle = ToSkColor(_lightColor, effectiveIntensity * _midpointIntensityRatio);
387 var outer = ToSkColor(_lightColor, 0f);
389 using var shader = SKShader.CreateRadialGradient(
392 new[] { inner, inner, middle, outer },
393 new[] { 0f, hotspot, midpoint, 1f },
394 SKShaderTileMode.Clamp);
396 using var paint =
new SKPaint
399 Style = SKPaintStyle.Fill,
404 canvas.DrawRect(rect, paint);
407 private void UpdateFlicker(
long tick)
409 if (!_flickerEnabled)
412 if (_lastFlickerRefreshTick == 0)
414 _lastFlickerRefreshTick = tick;
418 if (tick <= _lastFlickerRefreshTick)
421 var dt = HighResTimer.GetDuration(_lastFlickerRefreshTick, tick);
422 var refreshIntervalSec = 1f / _flickerRefreshHz;
424 if (dt < refreshIntervalSec)
427 _flickerPhaseSec += dt;
429 var primary = 0.5f + 0.5f * MathF.Sin(_flickerPhaseSec * MathF.Tau * _flickerRefreshHz);
430 var secondary = 0.5f + 0.5f * MathF.Sin(_flickerPhaseSec * MathF.Tau * (_flickerRefreshHz * 0.37f + 0.73f));
431 var mixed = primary * 0.65f + secondary * 0.35f;
433 _flickerMultiplier = 1f - _flickerAmount + mixed * _flickerAmount * 2f;
434 _lastFlickerRefreshTick = tick;
440 private void UpdateWorldBounds()
442 WorldBounds = BoundsFromCenter(_centerWorldPx, _radiusWorldPx);
446 private void OnChanged()
451 private static Rectangle BoundsFromCenter(PointF centerWorldPx,
float radiusWorldPx)
453 var radius = Math.Max(1f, radiusWorldPx);
454 var left = (int)MathF.Floor(centerWorldPx.X - radius);
455 var top = (int)MathF.Floor(centerWorldPx.Y - radius);
456 var right = (int)MathF.Ceiling(centerWorldPx.X + radius);
457 var bottom = (int)MathF.Ceiling(centerWorldPx.Y + radius);
466 private static SKColor ToSkColor(Color color,
float intensity)
468 var alpha = (byte)Math.Clamp(color.A * Clamp01(intensity), 0f, 255f);
477 private static float Clamp01(
float value) => Math.Clamp(value, 0f, 1f);
479 private static bool ApproximatelyEqual(PointF left, PointF right)
481 return Math.Abs(left.X - right.X) < 0.001f &&
482 Math.Abs(left.Y - right.Y) < 0.001f;
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.
DirectRadialLight SetRadius(float radiusWorldPx)
Sets the light radius and returns this instance for chaining.
float FlickerAmount
Gets or sets how much flicker may alter the base intensity. Recommended range: 0 to 0....
int FlickerRefreshHz
Gets or sets how often flicker is recomputed and dirtied.
float MidpointIntensityRatio
Gets or sets the middle stop intensity multiplier from 0 to 1.
Action< DirectRadialLight >? Changed
Occurs when this light changes in a way tracked darkness overlays care about.
float EffectiveIntensity
Gets the current intensity after flicker has been applied.
bool IsAntialias
Gets or sets whether Skia antialiasing is enabled for the light draw.
float RadiusWorldPx
Gets the light radius in world pixels.
DirectRadialLight SetIntensity(float intensity)
Sets the light intensity and returns this instance for chaining.
override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
Color LightColor
Gets or sets the color and base alpha of the light.
DirectRadialLight(Color lightColor, RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, PointF centerWorldPx, float radiusWorldPx, string? nickname=null)
Initializes a new scene-layer radial light.
SKBlendMode BlendMode
Gets or sets the blend mode used when compositing the light over scene content.
float MidpointRadiusRatio
Gets or sets the radius ratio for the middle gradient stop.
float HotspotRadiusRatio
Gets or sets the radius ratio for the hottest inner portion of the gradient.
bool FlickerEnabled
Gets or sets whether the light should periodically vary its intensity.
override void Update(long tick)
Performs per-frame update logic for this direct drawing, including fade and reveal animations....
float Intensity
Gets or sets the overall intensity multiplier from 0 to 1.
PointF CenterWorldPx
Gets the light center in world pixels.
DirectRadialLight MoveTo(PointF centerWorldPx)
Moves the light center and returns this instance for chaining.
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 ...
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.