rustial-renderer-bevy 0.0.1

Bevy Engine renderer for the rustial 2.5D map engine
//! Synchronise engine sky configuration to Bevy camera & light entities.
//!
//! When a [`SkyConfig`](rustial_engine::SkyConfig) is active on the
//! engine [`MapState`](rustial_engine::MapState), this system adjusts
//! the directional-light transform so the light direction matches the
//! configured sun position.
//!
//! Full procedural atmospheric scattering is handled by the WGPU
//! renderer's own sky shader.  For the Bevy renderer the same effect
//! can be achieved by enabling Bevy's built-in `Atmosphere` component
//! on the camera (requires HDR).  This system provides the engine-side
//! wiring so the sun direction stays in sync regardless.

use bevy::prelude::*;

use crate::systems::light_sync::RustialDirectionalLight;
use crate::MapStateResource;

/// Synchronise sun direction from engine's `ComputedSky` to the
/// Bevy directional-light transform.
///
/// This system runs in the `SkyAtmosphere` set, after
/// [`sync_lights`](crate::systems::light_sync::sync_lights) so the
/// directional-light entity already exists.
pub fn sync_sky(
    map: Res<MapStateResource>,
    mut q_light: Query<&mut Transform, With<RustialDirectionalLight>>,
) {
    let sky = map.0.computed_sky();
    if sky.sky_enabled < 0.5 {
        return;
    }

    let dir = sky.sun_direction;
    let incoming = Vec3::new(-dir[0], -dir[1], -dir[2]);

    if incoming.length_squared() < 1e-6 {
        return;
    }

    let rotation = Transform::default().looking_to(incoming, Vec3::Z).rotation;

    for mut transform in &mut q_light {
        transform.rotation = rotation;
    }
}