spaceship/NeonDraw.gd
2026-08-15 04:45:15 +02:00

52 lines
2.7 KiB
GDScript

class_name NeonDraw
extends RefCounted
# Cheap "poor man's glow": layered translucent strokes of increasing width
# and decreasing opacity, topped with a bright core line. No solid fill -
# gives ships/enemies/shots a light-made, neon/laser-sabre look without
# needing an HDR bloom pipeline.
#
# The core colour is a parameter rather than always white: the effect is
# built for a dark background, and a near-white core all but disappears
# against a bright one (the lunar surface, whose colours are sampled from a
# real NASA photograph, is a light grey). Callers drawing onto a bright
# backdrop pass a dark core instead, which reads as an inked outline while
# keeping the same layered-glow construction.
const DEFAULT_CORE: Color = Color(1.0, 1.0, 1.0)
static func draw_glowing_polygon(item: CanvasItem, points: PackedVector2Array, glow_color: Color, pulse: float = 1.0, core_color: Color = DEFAULT_CORE, width_scale: float = 1.0) -> void:
var closed: PackedVector2Array = points.duplicate()
closed.append(points[0])
_draw_layers(item, closed, glow_color, pulse, width_scale, core_color)
static func draw_glowing_line(item: CanvasItem, from: Vector2, to: Vector2, glow_color: Color, pulse: float = 1.0, width_scale: float = 1.0, core_color: Color = DEFAULT_CORE) -> void:
_draw_layers(item, PackedVector2Array([from, to]), glow_color, pulse, width_scale, core_color)
static func draw_glowing_circle(item: CanvasItem, center: Vector2, radius: float,
glow_color: Color, pulse: float = 1.0,
core_color: Color = DEFAULT_CORE) -> void:
var points := PackedVector2Array()
for i in range(24):
var angle := TAU * float(i) / 24.0
points.append(center + Vector2(cos(angle), sin(angle)) * radius)
draw_glowing_polygon(item, points, glow_color, pulse, core_color)
# An open run of points - unlike draw_glowing_polygon, the ends are NOT
# joined. For arcs, cradle ribs and other shapes that are deliberately open.
static func draw_glowing_path(item: CanvasItem, points: PackedVector2Array, glow_color: Color, pulse: float = 1.0, width_scale: float = 1.0, core_color: Color = DEFAULT_CORE) -> void:
if points.size() < 2:
return
_draw_layers(item, points, glow_color, pulse, width_scale, core_color)
static func _draw_layers(item: CanvasItem, poly: PackedVector2Array, glow_color: Color, pulse: float, width_scale: float, core_color: Color) -> void:
var c: Color = glow_color
item.draw_polyline(poly, Color(c.r, c.g, c.b, 0.12 * pulse), 18.0 * width_scale, true)
item.draw_polyline(poly, Color(c.r, c.g, c.b, 0.28 * pulse), 10.0 * width_scale, true)
item.draw_polyline(poly, Color(c.r, c.g, c.b, 0.60 * pulse), 5.0 * width_scale, true)
item.draw_polyline(poly, Color(core_color.r, core_color.g, core_color.b, 0.95 * pulse), 1.8 * width_scale, true)