Skip to content

Commit ec07834

Browse files
committed
Fix atmosphere not supporting multiple cameras
1 parent c040d76 commit ec07834

5 files changed

Lines changed: 52 additions & 33 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: "Atmosphere now supports multiple cameras"
3+
pull_requests: [23113]
4+
---
5+
6+
Atmosphere now works correctly with multiple cameras. No action is required for most users.
7+
8+
`init_atmosphere_buffer` has been removed, and `AtmosphereBuffer` has been changed from a `Resource` to a `Component` attached to each camera entity.
9+
10+
If you were directly accessing `AtmosphereBuffer` as a resource in a render world system, you'll need to query for it as a component on camera entities instead.

crates/bevy_pbr/src/atmosphere/mod.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ use resources::{
8585
};
8686
use tracing::warn;
8787

88-
use crate::resources::{init_atmosphere_buffer, write_atmosphere_buffer};
88+
use crate::resources::prepare_atmosphere_buffers;
8989

9090
use self::resources::{
9191
prepare_atmosphere_bind_groups, prepare_atmosphere_textures, AtmosphereBindGroupLayouts,
@@ -165,12 +165,7 @@ impl Plugin for AtmospherePlugin {
165165
.init_gpu_resource::<SpecializedRenderPipelines<RenderSkyBindGroupLayouts>>()
166166
.add_systems(
167167
RenderStartup,
168-
(
169-
init_atmosphere_probe_layout,
170-
init_atmosphere_probe_pipeline,
171-
init_atmosphere_buffer,
172-
)
173-
.chain(),
168+
(init_atmosphere_probe_layout, init_atmosphere_probe_pipeline).chain(),
174169
)
175170
.add_systems(
176171
Render,
@@ -187,7 +182,7 @@ impl Plugin for AtmospherePlugin {
187182
prepare_atmosphere_probe_bind_groups.in_set(RenderSystems::PrepareBindGroups),
188183
prepare_atmosphere_transforms.in_set(RenderSystems::PrepareResources),
189184
prepare_atmosphere_bind_groups.in_set(RenderSystems::PrepareBindGroups),
190-
write_atmosphere_buffer.in_set(RenderSystems::PrepareResources),
185+
prepare_atmosphere_buffers.in_set(RenderSystems::PrepareResources),
191186
),
192187
)
193188
.add_systems(

crates/bevy_pbr/src/atmosphere/resources.rs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -792,32 +792,44 @@ pub(super) fn prepare_atmosphere_bind_groups(
792792
Ok(())
793793
}
794794

795-
pub fn init_atmosphere_buffer(mut commands: Commands) {
796-
commands.insert_resource(AtmosphereBuffer {
797-
buffer: StorageBuffer::from(GpuAtmosphere {
798-
ground_albedo: Vec3::ZERO,
799-
inner_radius: 0.0,
800-
outer_radius: 0.0,
801-
world_to_atmosphere: Mat4::IDENTITY,
802-
}),
803-
});
795+
#[derive(ShaderType)]
796+
#[repr(C)]
797+
pub(crate) struct AtmosphereData {
798+
pub atmosphere: GpuAtmosphere,
799+
pub settings: GpuAtmosphereSettings,
804800
}
805801

806-
#[derive(Resource)]
802+
#[derive(Component)]
807803
pub struct AtmosphereBuffer {
808-
pub(crate) buffer: StorageBuffer<GpuAtmosphere>,
804+
pub(crate) buffer: StorageBuffer<AtmosphereData>,
809805
}
810806

811-
pub(crate) fn write_atmosphere_buffer(
807+
pub(crate) fn prepare_atmosphere_buffers(
812808
device: Res<RenderDevice>,
813809
queue: Res<RenderQueue>,
814-
atmosphere_entity: Query<&GpuAtmosphere, With<Camera3d>>,
815-
mut atmosphere_buffer: ResMut<AtmosphereBuffer>,
810+
mut views: Query<
811+
(
812+
Entity,
813+
&GpuAtmosphere,
814+
&GpuAtmosphereSettings,
815+
Option<&mut AtmosphereBuffer>,
816+
),
817+
With<ExtractedAtmosphere>,
818+
>,
819+
mut commands: Commands,
816820
) {
817-
let Ok(atmosphere) = atmosphere_entity.single() else {
818-
return;
819-
};
820-
821-
atmosphere_buffer.buffer.set(atmosphere.clone());
822-
atmosphere_buffer.buffer.write_buffer(&device, &queue);
821+
for (entity, atmosphere, settings, existing_buffer) in &mut views {
822+
let data = AtmosphereData {
823+
atmosphere: atmosphere.clone(),
824+
settings: settings.clone(),
825+
};
826+
if let Some(mut atmosphere_buffer) = existing_buffer {
827+
atmosphere_buffer.buffer.set(data);
828+
atmosphere_buffer.buffer.write_buffer(&device, &queue);
829+
} else {
830+
let mut buffer = StorageBuffer::from(data);
831+
buffer.write_buffer(&device, &queue);
832+
commands.entity(entity).insert(AtmosphereBuffer { buffer });
833+
}
834+
}
823835
}

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::contact_shadows::ViewContactShadowsUniformOffset;
22
use crate::{
3-
material_bind_groups::MaterialBindGroupSlot, resources::write_atmosphere_buffer,
3+
material_bind_groups::{MaterialBindGroupIndex, MaterialBindGroupSlot},
4+
resources::prepare_atmosphere_buffers,
45
skin::skin_uniforms_from_world,
56
};
67
use alloc::sync::Arc;
@@ -214,7 +215,7 @@ impl Plugin for MeshRenderPlugin {
214215
prepare_mesh_view_bind_groups
215216
.in_set(RenderSystems::PrepareBindGroups)
216217
.after(prepare_oit_buffers)
217-
.after(write_atmosphere_buffer),
218+
.after(prepare_atmosphere_buffers),
218219
no_gpu_preprocessing::clear_batched_cpu_instance_buffers::<MeshPipeline>
219220
.in_set(RenderSystems::Cleanup)
220221
.after(RenderSystems::Render),

crates/bevy_pbr/src/render/mesh_view_bindings.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ pub fn prepare_mesh_view_bind_groups(
630630
Option<&RenderViewLightProbes<IrradianceVolume>>,
631631
Has<OrderIndependentTransparencySettings>,
632632
Option<&AtmosphereTextures>,
633+
Option<&AtmosphereBuffer>,
633634
Has<ExtractedAtmosphere>,
634635
Option<&ViewContactShadowsUniformOffset>,
635636
)>,
@@ -648,10 +649,9 @@ pub fn prepare_mesh_view_bind_groups(
648649
Res<ContactShadowsBuffer>,
649650
),
650651
oit_buffers: Res<OitBuffers>,
651-
(decals_buffer, render_decals, atmosphere_buffer, atmosphere_sampler, blue_noise, ltc_luts): (
652+
(decals_buffer, render_decals, atmosphere_sampler, blue_noise, ltc_luts): (
652653
Res<DecalsBuffer>,
653654
Res<RenderClusteredDecals>,
654-
Option<Res<AtmosphereBuffer>>,
655655
Option<Res<AtmosphereSampler>>,
656656
Res<Bluenoise>,
657657
Res<LtcLuts>,
@@ -695,6 +695,7 @@ pub fn prepare_mesh_view_bind_groups(
695695
render_view_irradiance_volumes,
696696
has_oit,
697697
atmosphere_textures,
698+
atmosphere_buffer,
698699
has_atmosphere,
699700
_contact_shadows_offset,
700701
) in &views
@@ -807,7 +808,7 @@ pub fn prepare_mesh_view_bind_groups(
807808

808809
if has_atmosphere
809810
&& let Some(atmosphere_textures) = atmosphere_textures
810-
&& let Some(atmosphere_buffer) = atmosphere_buffer.as_ref()
811+
&& let Some(atmosphere_buffer) = atmosphere_buffer
811812
&& let Some(atmosphere_sampler) = atmosphere_sampler.as_ref()
812813
&& let Some(atmosphere_buffer_binding) = atmosphere_buffer.buffer.binding()
813814
{

0 commit comments

Comments
 (0)