nightshade-api 0.43.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::ecs::graphics::resources::DepthOfField;
use nightshade::prelude::*;

/// What fills the space behind your scene.
pub enum Background {
    /// The procedural sky gradient. The default.
    Sky,
    /// The sky with volumetric clouds.
    CloudySky,
    /// A procedural starfield.
    Space,
    /// A procedural nebula with stars.
    Nebula,
    /// A procedural sunset gradient.
    Sunset,
    /// 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. The
/// procedural skies also recapture image based lighting so reflective
/// surfaces pick up the new sky.
pub fn set_background(world: &mut World, background: Background) {
    match background {
        Background::Sky => set_atmosphere(world, Atmosphere::Sky),
        Background::CloudySky => set_atmosphere(world, Atmosphere::CloudySky),
        Background::Space => set_atmosphere(world, Atmosphere::Space),
        Background::Nebula => set_atmosphere(world, Atmosphere::Nebula),
        Background::Sunset => set_atmosphere(world, Atmosphere::Sunset),
        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);
        }
    }
}

fn set_atmosphere(world: &mut World, atmosphere: Atmosphere) {
    world.resources.render_settings.atmosphere = atmosphere;
    capture_procedural_atmosphere_ibl(world, atmosphere, 0.0);
}

/// 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 manual exposure multiplier. 1.0 is neutral, above brightens,
/// below darkens.
#[inline]
pub fn set_exposure(world: &mut World, exposure: f32) {
    world.resources.render_settings.color_grading.exposure = exposure;
}

/// Sets depth of field. The engine ships presets: `DepthOfField::portrait()`,
/// `cinematic()`, `macro_shot()`, `landscape()`, and `tilt_shift()`. Disable
/// it by passing a default with `enabled` false.
#[inline]
pub fn set_depth_of_field(world: &mut World, depth_of_field: DepthOfField) {
    world.resources.render_settings.depth_of_field = depth_of_field;
}

/// 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,
        },
    );
}