nightshade-api 0.38.0

Procedural high level API for the nightshade game engine
Documentation
//! Background, grid, fog, bloom, time of day, and other scene wide settings.

use crate::runner::{SUN_NAME, lookup_named};
use nightshade::prelude::*;

/// What fills the space behind your scene.
pub enum Background {
    /// The procedural sky gradient. The default.
    Sky,
    /// A solid color, linear RGBA.
    Color([f32; 4]),
    /// An equirectangular hdr image used as a skybox.
    Hdr(Vec<u8>),
}

/// Sets the background. [`Background::Color`] switches the atmosphere off and
/// sets the clear color together, which is the pairing the engine needs.
pub fn set_background(world: &mut World, background: Background) {
    match background {
        Background::Sky => {
            world.resources.render_settings.atmosphere = Atmosphere::Sky;
        }
        Background::Color(color) => {
            world.resources.render_settings.atmosphere = Atmosphere::None;
            world.resources.render_settings.clear_color = color;
        }
        Background::Hdr(bytes) => {
            load_hdr_skybox(world, bytes);
        }
    }
}

/// Shows or hides the reference grid. On by default.
#[inline]
pub fn show_grid(world: &mut World, enabled: bool) {
    world.resources.debug_draw.show_grid = enabled;
}

/// Sets the ambient light color, linear RGBA.
#[inline]
pub fn set_ambient(world: &mut World, color: [f32; 4]) {
    world.resources.render_settings.ambient_light = color;
}

/// Enables distance fog between `Fog::start` and `Fog::end`, or disables it
/// with `None`.
#[inline]
pub fn set_fog(world: &mut World, fog: Option<Fog>) {
    world.resources.render_settings.fog = fog;
}

/// Toggles bloom. Emissive materials glow when this is on.
#[inline]
pub fn set_bloom(world: &mut World, enabled: bool) {
    world.resources.render_settings.bloom_enabled = enabled;
}

/// Sets the hour of the day from 0.0 to 24.0. Switches the sky to the day
/// and night atmosphere and puts the default sun under its control, so the
/// sun arcs and the light warms and cools with the hour. The hour you pass is
/// authoritative, so call this every frame to animate time.
pub fn set_time_of_day(world: &mut World, hour: f32) {
    if world
        .resources
        .renderer_state
        .day_night
        .sun_entity
        .is_none()
    {
        world.resources.renderer_state.day_night.sun_entity = lookup_named(world, SUN_NAME);
    }
    world.resources.render_settings.atmosphere = Atmosphere::DayNight;
    world.resources.renderer_state.day_night.auto_cycle = true;
    world.resources.renderer_state.day_night.speed = 0.0;
    world.resources.renderer_state.day_night.hour = hour;
}

/// Sets the window title.
#[inline]
pub fn set_title(world: &mut World, title: &str) {
    world.resources.window.title = title.to_string();
}

/// Saves a screenshot of the next rendered frame to `path` as a png.
pub fn screenshot(world: &mut World, path: std::path::PathBuf) {
    queue_render_command(
        world,
        RenderCommand::CaptureScreenshot {
            path: Some(path),
            max_dimension: None,
        },
    );
}