Randoloot/scripts/game/star_lighting.gd

54 lines
1.8 KiB
GDScript

extends RefCounted
const LIGHT_RADIUS := 3400.0
const LIGHT_ENERGY := 0.28
const SHIP_LIGHT_ENERGY := 0.16
const LIGHT_COLOR := Color(1.0, 0.93, 0.82)
static func attach_to(star: Node2D) -> void:
var gradient := Gradient.new()
gradient.offsets = PackedFloat32Array([0.0, 0.25, 0.65, 1.0])
gradient.colors = PackedColorArray([Color.WHITE, Color(1, 1, 1, 0.9), Color(1, 1, 1, 0.5), Color(1, 1, 1, 0)])
var texture := GradientTexture2D.new()
texture.gradient = gradient
texture.width = 512
texture.height = 512
texture.fill = GradientTexture2D.FILL_RADIAL
texture.fill_from = Vector2(0.5, 0.5)
texture.fill_to = Vector2(0.5, 0.0)
var light := PointLight2D.new()
light.name = "StarLight"
light.texture = texture
light.texture_scale = LIGHT_RADIUS * 2.0 / texture.width
light.color = LIGHT_COLOR
light.energy = LIGHT_ENERGY
light.range_item_cull_mask = 1
light.shadow_enabled = true
light.shadow_item_cull_mask = 1
light.shadow_filter = Light2D.SHADOW_FILTER_PCF5
light.shadow_filter_smooth = 2.0
star.add_child(light)
# A separate mask lights hulls without their own silhouettes darkening them.
var hull_light := PointLight2D.new()
hull_light.name = "HullLight"
hull_light.texture = texture
hull_light.texture_scale = light.texture_scale
hull_light.color = LIGHT_COLOR
hull_light.energy = SHIP_LIGHT_ENERGY
hull_light.range_item_cull_mask = 2
star.add_child(hull_light)
var glow := Sprite2D.new()
glow.name = "StarGlow"
glow.texture = texture
glow.scale = Vector2.ONE * (float(star.get("radius")) * 5.0 / texture.width)
glow.modulate = Color(LIGHT_COLOR, 0.09)
glow.z_index = -1
var material := CanvasItemMaterial.new()
material.blend_mode = CanvasItemMaterial.BLEND_MODE_ADD
material.light_mode = CanvasItemMaterial.LIGHT_MODE_UNSHADED
glow.material = material
star.add_child(glow)