44 lines
1.5 KiB
GDScript
44 lines
1.5 KiB
GDScript
extends Node
|
|
class_name CloudSunSync
|
|
|
|
## Keeps the fullscreen sky composite tied to the real DirectionalLight3D.
|
|
##
|
|
## The Environment sky shader receives LIGHT0_DIRECTION automatically, but the
|
|
## ink pass has to repaint background pixels after the screen copy. This bridge
|
|
## gives that second shader the same moving direction and color every frame.
|
|
|
|
var _sun: DirectionalLight3D
|
|
var _ink_material: ShaderMaterial
|
|
var _palette_sun_color := Color.WHITE
|
|
|
|
|
|
func configure(sun: DirectionalLight3D, ink_material: ShaderMaterial) -> void:
|
|
_sun = sun
|
|
_ink_material = ink_material
|
|
var authored = ink_material.get_shader_parameter("sky_sun_color")
|
|
if authored is Color:
|
|
_palette_sun_color = authored
|
|
_sync()
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
_sync()
|
|
|
|
|
|
func _sync() -> void:
|
|
if not is_instance_valid(_sun) or not is_instance_valid(_ink_material):
|
|
set_process(false)
|
|
return
|
|
|
|
# DirectionalLight3D shines down its local -Z axis, so +Z points back
|
|
# toward the apparent sun—the direction a sky pixel uses for illumination.
|
|
var toward_sun := _sun.global_transform.basis.z.normalized()
|
|
_ink_material.set_shader_parameter("sky_sun_direction", toward_sun)
|
|
|
|
# Preserve the authored day/sunset palette while allowing a dynamically
|
|
# recolored sun to tint the cloud's lit face.
|
|
var live_color := _palette_sun_color.lerp(_sun.light_color, 0.35)
|
|
var energy_scale: float = clampf(_sun.light_energy / 1.4, 0.75, 1.25)
|
|
_ink_material.set_shader_parameter(
|
|
"sky_sun_color", live_color * energy_scale)
|