Skip to content

Commit 0f6bb31

Browse files
authored
Allow changing the created skybox size when using detection (#76)
1 parent da8c1e3 commit 0f6bb31

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

src/lib.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,28 @@
5252
//!
5353
//! Use the [`AtmosphereSettings`](crate::settings::AtmosphereSettings) resource to change how the sky is rendered.
5454
//! ```no_run
55-
//! # use bevy_atmosphere::settings::AtmosphereSettings;
55+
//! # use bevy::utils::default;
56+
//! use bevy_atmosphere::settings::AtmosphereSettings;
5657
//! # let _ =
5758
//! AtmosphereSettings {
5859
//! // changes the resolution (should be a multiple of 8)
5960
//! resolution: 1024,
6061
//! // turns off dithering
6162
//! dithering: false,
63+
//! ..default()
64+
//! }
65+
//! # ;
66+
//! ```
67+
//!
68+
//! When using the `detection` feature, you can use [`SkyboxCreationMode`](crate::settings::SkyboxCreationMode) to control the size of the generated skybox.
69+
//! ```no_run
70+
//! # use bevy::utils::default;
71+
//! use bevy_atmosphere::settings::{AtmosphereSettings, SkyboxCreationMode};
72+
//! # let _ =
73+
//! AtmosphereSettings {
74+
//! // Will use the camera projections `far` value or `1000.0` as a fallback
75+
//! skybox_creation_mode: SkyboxCreationMode::FromProjectionFarWithFallback(1000.0),
76+
//! ..default()
6277
//! }
6378
//! # ;
6479
//! ```

src/plugin.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ use bevy::{
1414
use crate::{
1515
model::AddAtmosphereModel,
1616
pipeline::*,
17+
settings::AtmosphereSettings,
1718
skybox::{AtmosphereSkyBoxMaterial, SkyBoxMaterial, ATMOSPHERE_SKYBOX_SHADER_HANDLE},
1819
};
1920

21+
#[cfg(feature = "detection")]
22+
use crate::settings::SkyboxCreationMode;
23+
2024
/// A `Plugin` that adds the prerequisites for a procedural sky.
2125
#[derive(Debug, Clone, Copy)]
2226
pub struct AtmospherePlugin;
@@ -101,17 +105,29 @@ pub struct AtmosphereSkyBox;
101105
fn atmosphere_insert(
102106
mut commands: Commands,
103107
mut mesh_assets: ResMut<Assets<Mesh>>,
108+
settings: Res<AtmosphereSettings>,
104109
material: Res<AtmosphereSkyBoxMaterial>,
105110
atmosphere_cameras: Query<(Entity, &Projection, &AtmosphereCamera), Added<AtmosphereCamera>>,
106111
) {
107112
for (camera, projection, atmosphere_camera) in &atmosphere_cameras {
108-
trace!("Adding skybox to camera entity (ID:{:?})", camera);
113+
let far_size = match settings.skybox_creation_mode {
114+
// TODO: Use `unwrap_or(fallback)` when `projection.far()` becomes an `Option<f32>`
115+
SkyboxCreationMode::FromProjectionFarWithFallback(_fallback) => projection.far(),
116+
SkyboxCreationMode::FromSpecifiedFar(specified) => specified,
117+
};
118+
119+
trace!(
120+
"Adding skybox to camera entity (ID:{:?}) with far_size {}",
121+
camera,
122+
far_size
123+
);
124+
109125
commands
110126
.entity(camera)
111127
.insert(Visibility::Visible)
112128
.with_children(|c| {
113129
let mut child = c.spawn((
114-
Mesh3d(mesh_assets.add(crate::skybox::mesh(projection.far()))),
130+
Mesh3d(mesh_assets.add(crate::skybox::mesh(far_size))),
115131
MeshMaterial3d(material.0.clone()),
116132
AtmosphereSkyBox,
117133
NotShadowCaster,

src/settings.rs

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22
33
use bevy::{prelude::Resource, render::extract_resource::ExtractResource};
44

5+
/// Available methods for determining the size of the auto created skybox
6+
#[cfg(feature = "detection")]
7+
#[derive(Debug, Clone, Copy)]
8+
pub enum SkyboxCreationMode {
9+
/// Uses the camera projection `far` value or the specified fallback if `far` is `None`.
10+
FromProjectionFarWithFallback(f32),
11+
/// Ignores any camera projection `far` value and always uses the specified value.
12+
FromSpecifiedFar(f32),
13+
}
14+
15+
#[cfg(feature = "detection")]
16+
impl Default for SkyboxCreationMode {
17+
fn default() -> Self {
18+
Self::FromProjectionFarWithFallback(1000.0)
19+
}
20+
}
21+
522
/// Provides settings for how the sky is rendered.
623
#[derive(Resource, ExtractResource, Debug, Clone, Copy)]
724
pub struct AtmosphereSettings {
@@ -16,6 +33,13 @@ pub struct AtmosphereSettings {
1633
/// This option can be removed by disabling the "dithering" feature
1734
#[cfg(feature = "dithering")]
1835
pub dithering: bool,
36+
/// Method used to determine the size of the auto created skybox.
37+
///
38+
/// See [`SkyboxCreationMode`]
39+
///
40+
/// Default: `FromProjectionFarWithFallback(1000.0)`.
41+
#[cfg(feature = "detection")]
42+
pub skybox_creation_mode: SkyboxCreationMode,
1943
}
2044

2145
impl Default for AtmosphereSettings {
@@ -24,6 +48,8 @@ impl Default for AtmosphereSettings {
2448
resolution: 512,
2549
#[cfg(feature = "dithering")]
2650
dithering: true,
51+
#[cfg(feature = "detection")]
52+
skybox_creation_mode: SkyboxCreationMode::default(),
2753
}
2854
}
2955
}

0 commit comments

Comments
 (0)