Skip to main content

nightshade_api/
environment.rs

1//! Background, grid, fog, bloom, time of day, and other scene wide settings.
2
3use crate::runner::{SUN_NAME, lookup_named};
4use nightshade::prelude::*;
5
6/// What fills the space behind your scene.
7pub enum Background {
8    /// The procedural sky gradient. The default.
9    Sky,
10    /// A solid color, linear RGBA.
11    Color([f32; 4]),
12    /// An equirectangular hdr image used as a skybox.
13    Hdr(Vec<u8>),
14}
15
16/// Sets the background. [`Background::Color`] switches the atmosphere off and
17/// sets the clear color together, which is the pairing the engine needs.
18pub fn set_background(world: &mut World, background: Background) {
19    match background {
20        Background::Sky => {
21            world.resources.render_settings.atmosphere = Atmosphere::Sky;
22        }
23        Background::Color(color) => {
24            world.resources.render_settings.atmosphere = Atmosphere::None;
25            world.resources.render_settings.clear_color = color;
26        }
27        Background::Hdr(bytes) => {
28            load_hdr_skybox(world, bytes);
29        }
30    }
31}
32
33/// Shows or hides the reference grid. On by default.
34#[inline]
35pub fn show_grid(world: &mut World, enabled: bool) {
36    world.resources.debug_draw.show_grid = enabled;
37}
38
39/// Sets the ambient light color, linear RGBA.
40#[inline]
41pub fn set_ambient(world: &mut World, color: [f32; 4]) {
42    world.resources.render_settings.ambient_light = color;
43}
44
45/// Enables distance fog between `Fog::start` and `Fog::end`, or disables it
46/// with `None`.
47#[inline]
48pub fn set_fog(world: &mut World, fog: Option<Fog>) {
49    world.resources.render_settings.fog = fog;
50}
51
52/// Toggles bloom. Emissive materials glow when this is on.
53#[inline]
54pub fn set_bloom(world: &mut World, enabled: bool) {
55    world.resources.render_settings.bloom_enabled = enabled;
56}
57
58/// Sets the hour of the day from 0.0 to 24.0. Switches the sky to the day
59/// and night atmosphere and puts the default sun under its control, so the
60/// sun arcs and the light warms and cools with the hour. The hour you pass is
61/// authoritative, so call this every frame to animate time.
62pub fn set_time_of_day(world: &mut World, hour: f32) {
63    if world
64        .resources
65        .renderer_state
66        .day_night
67        .sun_entity
68        .is_none()
69    {
70        world.resources.renderer_state.day_night.sun_entity = lookup_named(world, SUN_NAME);
71    }
72    world.resources.render_settings.atmosphere = Atmosphere::DayNight;
73    world.resources.renderer_state.day_night.auto_cycle = true;
74    world.resources.renderer_state.day_night.speed = 0.0;
75    world.resources.renderer_state.day_night.hour = hour;
76}
77
78/// Sets the window title.
79#[inline]
80pub fn set_title(world: &mut World, title: &str) {
81    world.resources.window.title = title.to_string();
82}
83
84/// Saves a screenshot of the next rendered frame to `path` as a png.
85pub fn screenshot(world: &mut World, path: std::path::PathBuf) {
86    queue_render_command(
87        world,
88        RenderCommand::CaptureScreenshot {
89            path: Some(path),
90            max_dimension: None,
91        },
92    );
93}