Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
DirectRadialLight.cs
1using System.Drawing;
6using SkiaSharp;
7
9
32{
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;
42
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;
49
59 public event Action<DirectRadialLight>? Changed;
60
71 Color lightColor,
72 RenderSurfaceHostBase renderSurfaceHost,
73 SceneLayer sceneLayer,
74 PointF centerWorldPx,
75 float radiusWorldPx,
76 string? nickname = null)
77 : base(
78 renderSurfaceHost,
80 sceneLayer,
81 view: null,
82 screenBounds: null,
83 worldBounds: BoundsFromCenter(centerWorldPx, radiusWorldPx),
84 nickname)
85 {
86 _lightColor = lightColor;
87 _centerWorldPx = centerWorldPx;
88 _radiusWorldPx = Math.Max(1f, radiusWorldPx);
89 }
90
94 public PointF CenterWorldPx
95 {
96 get => _centerWorldPx;
97 set
98 {
99 if (ApproximatelyEqual(_centerWorldPx, value))
100 return;
101
102 _centerWorldPx = value;
103 UpdateWorldBounds();
104 }
105 }
106
113 public float RadiusWorldPx
114 {
115 get => _radiusWorldPx;
116 set
117 {
118 var clamped = Math.Max(1f, value);
119
120 if (Math.Abs(_radiusWorldPx - clamped) < 0.001f)
121 return;
122
123 _radiusWorldPx = clamped;
124 UpdateWorldBounds();
125 }
126 }
127
131 public Color LightColor
132 {
133 get => _lightColor;
134 set
135 {
136 if (_lightColor.ToArgb() == value.ToArgb())
137 return;
138
139 _lightColor = value;
140 ForceRefresh();
141 OnChanged();
142 }
143 }
144
148 public float Intensity
149 {
150 get => _intensity;
151 set
152 {
153 var clamped = Clamp01(value);
154
155 if (Math.Abs(_intensity - clamped) < 0.001f)
156 return;
157
158 _intensity = clamped;
159 ForceRefresh();
160 OnChanged();
161 }
162 }
163
167 public float EffectiveIntensity => Clamp01(_intensity * _flickerMultiplier);
168
172 public SKBlendMode BlendMode
173 {
174 get => _blendMode;
175 set
176 {
177 if (_blendMode == value)
178 return;
179
180 _blendMode = value;
181 ForceRefresh();
182 }
183 }
184
189 {
190 get => _hotspotRadiusRatio;
191 set
192 {
193 var clamped = Clamp01(value);
194
195 if (Math.Abs(_hotspotRadiusRatio - clamped) < 0.001f)
196 return;
197
198 _hotspotRadiusRatio = clamped;
199 ForceRefresh();
200 }
201 }
202
207 {
208 get => _midpointRadiusRatio;
209 set
210 {
211 var clamped = Clamp01(value);
212
213 if (Math.Abs(_midpointRadiusRatio - clamped) < 0.001f)
214 return;
215
216 _midpointRadiusRatio = clamped;
217 ForceRefresh();
218 }
219 }
220
225 {
226 get => _midpointIntensityRatio;
227 set
228 {
229 var clamped = Clamp01(value);
230
231 if (Math.Abs(_midpointIntensityRatio - clamped) < 0.001f)
232 return;
233
234 _midpointIntensityRatio = clamped;
235 ForceRefresh();
236 }
237 }
238
242 public bool IsAntialias
243 {
244 get => _isAntialias;
245 set
246 {
247 if (_isAntialias == value)
248 return;
249
250 _isAntialias = value;
251 ForceRefresh();
252 }
253 }
254
263 public bool FlickerEnabled
264 {
265 get => _flickerEnabled;
266 set
267 {
268 if (_flickerEnabled == value)
269 return;
270
271 _flickerEnabled = value;
272 _flickerMultiplier = 1f;
273 _lastFlickerRefreshTick = 0;
274 ForceRefresh();
275 OnChanged();
276 }
277 }
278
282 public float FlickerAmount
283 {
284 get => _flickerAmount;
285 set
286 {
287 var clamped = Math.Clamp(value, 0f, 1f);
288
289 if (Math.Abs(_flickerAmount - clamped) < 0.001f)
290 return;
291
292 _flickerAmount = clamped;
293 ForceRefresh();
294 OnChanged();
295 }
296 }
297
302 {
303 get => _flickerRefreshHz;
304 set => _flickerRefreshHz = Math.Max(1, value);
305 }
306
312 public DirectRadialLight MoveTo(PointF centerWorldPx)
313 {
314 CenterWorldPx = centerWorldPx;
315 return this;
316 }
317
323 public DirectRadialLight SetRadius(float radiusWorldPx)
324 {
325 RadiusWorldPx = radiusWorldPx;
326 return this;
327 }
328
334 public DirectRadialLight SetIntensity(float intensity)
335 {
336 Intensity = intensity;
337 return this;
338 }
339
341 public override void Update(long tick)
342 {
343 UpdateFlicker(tick);
344 base.Update(tick);
345 }
346
348 protected override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
349 {
350 if (destRectScreen.Width <= 0f || destRectScreen.Height <= 0f)
351 return;
352
353 var canvas = backbuffer.Canvas;
354 var worldBounds = WorldBounds;
355
356 if (worldBounds.Width <= 0 || worldBounds.Height <= 0)
357 return;
358
359 var rect = new SKRect(
360 destRectScreen.Left,
361 destRectScreen.Top,
362 destRectScreen.Right,
363 destRectScreen.Bottom);
364
365 float scaleX = destRectScreen.Width / worldBounds.Width;
366 float scaleY = destRectScreen.Height / worldBounds.Height;
367
368 // Use the actual world-space center/radius rather than the integer-rounded
369 // WorldBounds midpoint. The bounds are intentionally pixel-aligned for dirty
370 // rectangles, but the shader geometry should preserve sub-pixel light movement
371 // so a moving torch does not pop by ~0.5px as its bounds floor/ceil change.
372 var center = new SKPoint(
373 destRectScreen.Left + (_centerWorldPx.X - worldBounds.Left) * scaleX,
374 destRectScreen.Top + (_centerWorldPx.Y - worldBounds.Top) * scaleY);
375
376 var radius = Math.Max(Math.Abs(_radiusWorldPx * scaleX), Math.Abs(_radiusWorldPx * scaleY));
377
378 if (radius <= 0f)
379 return;
380
381 var hotspot = Math.Clamp(_hotspotRadiusRatio, 0f, 0.95f);
382 var midpoint = Math.Clamp(_midpointRadiusRatio, hotspot + 0.001f, 0.999f);
383 var effectiveIntensity = EffectiveIntensity;
384
385 var inner = ToSkColor(_lightColor, effectiveIntensity);
386 var middle = ToSkColor(_lightColor, effectiveIntensity * _midpointIntensityRatio);
387 var outer = ToSkColor(_lightColor, 0f);
388
389 using var shader = SKShader.CreateRadialGradient(
390 center,
391 radius,
392 new[] { inner, inner, middle, outer },
393 new[] { 0f, hotspot, midpoint, 1f },
394 SKShaderTileMode.Clamp);
395
396 using var paint = new SKPaint
397 {
398 Shader = shader,
399 Style = SKPaintStyle.Fill,
400 BlendMode = _blendMode,
401 IsAntialias = _isAntialias
402 };
403
404 canvas.DrawRect(rect, paint);
405 }
406
407 private void UpdateFlicker(long tick)
408 {
409 if (!_flickerEnabled)
410 return;
411
412 if (_lastFlickerRefreshTick == 0)
413 {
414 _lastFlickerRefreshTick = tick;
415 return;
416 }
417
418 if (tick <= _lastFlickerRefreshTick)
419 return;
420
421 var dt = HighResTimer.GetDuration(_lastFlickerRefreshTick, tick);
422 var refreshIntervalSec = 1f / _flickerRefreshHz;
423
424 if (dt < refreshIntervalSec)
425 return;
426
427 _flickerPhaseSec += dt;
428
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;
432
433 _flickerMultiplier = 1f - _flickerAmount + mixed * _flickerAmount * 2f;
434 _lastFlickerRefreshTick = tick;
435
436 ForceRefresh();
437 OnChanged();
438 }
439
440 private void UpdateWorldBounds()
441 {
442 WorldBounds = BoundsFromCenter(_centerWorldPx, _radiusWorldPx);
443 OnChanged();
444 }
445
446 private void OnChanged()
447 {
448 Changed?.Invoke(this);
449 }
450
451 private static Rectangle BoundsFromCenter(PointF centerWorldPx, float radiusWorldPx)
452 {
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);
458
459 return Rectangle.FromLTRB(
460 left,
461 top,
462 right,
463 bottom);
464 }
465
466 private static SKColor ToSkColor(Color color, float intensity)
467 {
468 var alpha = (byte)Math.Clamp(color.A * Clamp01(intensity), 0f, 255f);
469
470 return new SKColor(
471 color.R,
472 color.G,
473 color.B,
474 alpha);
475 }
476
477 private static float Clamp01(float value) => Math.Clamp(value, 0f, 1f);
478
479 private static bool ApproximatelyEqual(PointF left, PointF right)
480 {
481 return Math.Abs(left.X - right.X) < 0.001f &&
482 Math.Abs(left.Y - right.Y) < 0.001f;
483 }
484}
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.