Skip to content

Commit ec254de

Browse files
authored
Make Skybox’s image optional so that Skybox::default() is valid. (#23691)
# Objective - Partially addresses #23688. - Prevents use of `Skybox::default()` from causing errors. ## Solution `Skybox::default()` is problematic because it contains an `Image` that is not a valid skybox. ~~This change removes the `Default` implementation and instead provides a `new()` function which takes the image as a parameter (and also the brightness, which is practically required).~~ This change makes the `image` field optional so that the default `None` renders nothing. Things we could do instead of this: * Make `Skybox` not implement `Default`. I am informed this is a bad idea. * Create a default cubemap image for `default()` to use. ## Testing Ran the `skybox` and `irradiance_volumes` examples.
1 parent 9129cb1 commit ec254de

16 files changed

Lines changed: 74 additions & 23 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: "`Skybox` `image` is now optional"
3+
pull_requests: [23691]
4+
---
5+
6+
The `image` field of the `Skybox` component now has the type `Option<Handle<Image>>` instead of `Handle<Image>`.
7+
A `Skybox` component without an image will not draw anything, just like it was not present.
8+
9+
If you were creating a skybox with an image, wrap the image handle in `Some`:
10+
11+
```rust
12+
// 0.18
13+
Skybox {
14+
image: my_skybox,
15+
brightness: 1000.0,
16+
..default()
17+
}
18+
19+
// 0.19
20+
Skybox {
21+
image: Some(my_skybox),
22+
brightness: 1000.0,
23+
..default()
24+
}
25+
```
26+
27+
If you were previously creating a `Skybox` component with a placeholder image to be changed later, you can now remove the placeholder:
28+
29+
```rust
30+
// 0.18
31+
Skybox {
32+
image: cubemap_image_that_will_not_actually_be_seen,
33+
brightness: 1000.0,
34+
..default()
35+
}
36+
37+
// 0.19
38+
Skybox {
39+
brightness: 1000.0,
40+
..default()
41+
}
42+
```

crates/bevy_core_pipeline/src/skybox/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,18 @@ fn prepare_skybox_bind_groups(
227227
views: Query<(Entity, &Skybox, &DynamicUniformIndex<SkyboxUniforms>)>,
228228
) {
229229
for (entity, skybox, skybox_uniform_index) in &views {
230-
if let (Some(skybox), Some(view_uniforms), Some(skybox_uniforms)) = (
231-
images.get(&skybox.image),
230+
if let (Some(image_handle), Some(view_uniforms), Some(skybox_uniforms)) = (
231+
&skybox.image,
232232
view_uniforms.uniforms.binding(),
233233
skybox_uniforms.binding(),
234-
) {
234+
) && let Some(image) = images.get(image_handle)
235+
{
235236
let bind_group = render_device.create_bind_group(
236237
"skybox_bind_group",
237238
&pipeline_cache.get_bind_group_layout(&pipeline.bind_group_layout),
238239
&BindGroupEntries::sequential((
239-
&skybox.texture_view,
240-
&skybox.sampler,
240+
&image.texture_view,
241+
&image.sampler,
241242
view_uniforms,
242243
skybox_uniforms,
243244
)),
@@ -246,6 +247,8 @@ fn prepare_skybox_bind_groups(
246247
commands
247248
.entity(entity)
248249
.insert(SkyboxBindGroup((bind_group, skybox_uniform_index.index())));
250+
} else {
251+
commands.entity(entity).remove::<SkyboxBindGroup>();
249252
}
250253
}
251254
}

crates/bevy_light/src/probe.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use bevy_asset::{Assets, Handle, RenderAssetUsages};
1+
use bevy_asset::{Assets, Handle, HandleTemplate, RenderAssetUsages};
22
use bevy_camera::visibility::{self, ViewVisibility, Visibility, VisibilityClass};
33
use bevy_color::{Color, ColorToComponents, Srgba};
44
use bevy_ecs::prelude::*;
5+
use bevy_ecs::template::{FromTemplate, OptionTemplate};
56
use bevy_image::Image;
67
use bevy_math::{Quat, UVec2, Vec3};
78
use bevy_reflect::prelude::*;
@@ -224,7 +225,12 @@ impl Default for EnvironmentMapLight {
224225
#[reflect(Component, Default, Clone)]
225226
pub struct Skybox {
226227
/// The cubemap to use.
227-
pub image: Handle<Image>,
228+
///
229+
/// If this is [`None`], the skybox will not be rendered, as if it does not exist.
230+
/// This allows `Skybox` to implement [`Default`].
231+
#[template(OptionTemplate<HandleTemplate<Image>>)]
232+
pub image: Option<Handle<Image>>,
233+
228234
/// Scale factor applied to the skybox image.
229235
/// After applying this multiplier to the image samples, the resulting values should
230236
/// be in units of [cd/m^2](https://en.wikipedia.org/wiki/Candela_per_square_metre).
@@ -239,7 +245,7 @@ pub struct Skybox {
239245
impl Default for Skybox {
240246
fn default() -> Self {
241247
Skybox {
242-
image: Handle::default(),
248+
image: None,
243249
brightness: 0.0,
244250
rotation: Quat::IDENTITY,
245251
}

examples/3d/anisotropy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ fn add_skybox_and_environment_map(
305305
.entity(entity)
306306
.insert(Skybox {
307307
brightness: 5000.0,
308-
image: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
308+
image: Some(asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2")),
309309
..default()
310310
})
311311
.insert(EnvironmentMapLight {

examples/3d/auto_exposure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn setup(
4646
..default()
4747
},
4848
Skybox {
49-
image: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
49+
image: Some(asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2")),
5050
brightness: light_consts::lux::DIRECT_SUNLIGHT,
5151
..default()
5252
},

examples/3d/clearcoat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn spawn_camera(commands: &mut Commands, asset_server: &AssetServer) {
203203
))
204204
.insert(Skybox {
205205
brightness: 5000.0,
206-
image: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
206+
image: Some(asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2")),
207207
..default()
208208
})
209209
.insert(EnvironmentMapLight {

examples/3d/contact_shadows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
117117
Hdr,
118118
Skybox {
119119
brightness: 1000.0,
120-
image: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
120+
image: Some(asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2")),
121121
..default()
122122
},
123123
EnvironmentMapLight {

examples/3d/irradiance_volumes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ fn spawn_camera(commands: &mut Commands, assets: &ExampleAssets) {
236236
Camera3d::default(),
237237
Transform::from_xyz(-10.012, 4.8605, 13.281).looking_at(Vec3::ZERO, Vec3::Y),
238238
Skybox {
239-
image: assets.skybox.clone(),
239+
image: Some(assets.skybox.clone()),
240240
brightness: 150.0,
241241
..default()
242242
},

examples/3d/pcss.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ fn spawn_camera(commands: &mut Commands, asset_server: &AssetServer) {
177177
.insert(MotionVectorPrepass)
178178
// Add a nice skybox.
179179
.insert(Skybox {
180-
image: asset_server.load("environment_maps/sky_skybox.ktx2"),
180+
image: Some(asset_server.load("environment_maps/sky_skybox.ktx2")),
181181
brightness: 500.0,
182182
rotation: Quat::IDENTITY,
183183
});

examples/3d/reflection_probes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ fn add_environment_map_to_camera(
197197
.entity(camera_entity)
198198
.insert(create_camera_environment_map_light(&cubemaps))
199199
.insert(Skybox {
200-
image: cubemaps.specular_environment_map.clone(),
200+
image: Some(cubemaps.specular_environment_map.clone()),
201201
brightness: ENV_MAP_INTENSITY,
202202
..default()
203203
});

0 commit comments

Comments
 (0)