bevy_core_pipeline/skybox/
mod.rs

1use bevy_app::{App, Plugin};
2use bevy_asset::{embedded_asset, load_embedded_asset, AssetServer, Handle};
3use bevy_camera::Exposure;
4use bevy_ecs::{
5    prelude::{Component, Entity},
6    query::{QueryItem, With},
7    reflect::ReflectComponent,
8    resource::Resource,
9    schedule::IntoScheduleConfigs,
10    system::{Commands, Query, Res, ResMut},
11};
12use bevy_image::{BevyDefault, Image};
13use bevy_math::{Mat4, Quat};
14use bevy_reflect::{std_traits::ReflectDefault, Reflect};
15use bevy_render::{
16    extract_component::{
17        ComponentUniforms, DynamicUniformIndex, ExtractComponent, ExtractComponentPlugin,
18        UniformComponentPlugin,
19    },
20    render_asset::RenderAssets,
21    render_resource::{
22        binding_types::{sampler, texture_cube, uniform_buffer},
23        *,
24    },
25    renderer::RenderDevice,
26    texture::GpuImage,
27    view::{ExtractedView, Msaa, ViewTarget, ViewUniform, ViewUniforms},
28    Render, RenderApp, RenderStartup, RenderSystems,
29};
30use bevy_shader::Shader;
31use bevy_transform::components::Transform;
32use bevy_utils::default;
33use prepass::SkyboxPrepassPipeline;
34
35use crate::{
36    core_3d::CORE_3D_DEPTH_FORMAT, prepass::PreviousViewUniforms,
37    skybox::prepass::init_skybox_prepass_pipeline,
38};
39
40pub mod prepass;
41
42pub struct SkyboxPlugin;
43
44impl Plugin for SkyboxPlugin {
45    fn build(&self, app: &mut App) {
46        embedded_asset!(app, "skybox.wgsl");
47        embedded_asset!(app, "skybox_prepass.wgsl");
48
49        app.add_plugins((
50            ExtractComponentPlugin::<Skybox>::default(),
51            UniformComponentPlugin::<SkyboxUniforms>::default(),
52        ));
53
54        let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
55            return;
56        };
57        render_app
58            .init_resource::<SpecializedRenderPipelines<SkyboxPipeline>>()
59            .init_resource::<SpecializedRenderPipelines<SkyboxPrepassPipeline>>()
60            .init_resource::<PreviousViewUniforms>()
61            .add_systems(
62                RenderStartup,
63                (init_skybox_pipeline, init_skybox_prepass_pipeline),
64            )
65            .add_systems(
66                Render,
67                (
68                    prepare_skybox_pipelines.in_set(RenderSystems::Prepare),
69                    prepass::prepare_skybox_prepass_pipelines.in_set(RenderSystems::Prepare),
70                    prepare_skybox_bind_groups.in_set(RenderSystems::PrepareBindGroups),
71                    prepass::prepare_skybox_prepass_bind_groups
72                        .in_set(RenderSystems::PrepareBindGroups),
73                ),
74            );
75    }
76}
77
78/// Adds a skybox to a 3D camera, based on a cubemap texture.
79///
80/// Note that this component does not (currently) affect the scene's lighting.
81/// To do so, use `EnvironmentMapLight` alongside this component.
82///
83/// See also <https://en.wikipedia.org/wiki/Skybox_(video_games)>.
84#[derive(Component, Clone, Reflect)]
85#[reflect(Component, Default, Clone)]
86pub struct Skybox {
87    pub image: Handle<Image>,
88    /// Scale factor applied to the skybox image.
89    /// After applying this multiplier to the image samples, the resulting values should
90    /// be in units of [cd/m^2](https://en.wikipedia.org/wiki/Candela_per_square_metre).
91    pub brightness: f32,
92
93    /// View space rotation applied to the skybox cubemap.
94    /// This is useful for users who require a different axis, such as the Z-axis, to serve
95    /// as the vertical axis.
96    pub rotation: Quat,
97}
98
99impl Default for Skybox {
100    fn default() -> Self {
101        Skybox {
102            image: Handle::default(),
103            brightness: 0.0,
104            rotation: Quat::IDENTITY,
105        }
106    }
107}
108
109impl ExtractComponent for Skybox {
110    type QueryData = (&'static Self, Option<&'static Exposure>);
111    type QueryFilter = ();
112    type Out = (Self, SkyboxUniforms);
113
114    fn extract_component(
115        (skybox, exposure): QueryItem<'_, '_, Self::QueryData>,
116    ) -> Option<Self::Out> {
117        let exposure = exposure
118            .map(Exposure::exposure)
119            .unwrap_or_else(|| Exposure::default().exposure());
120
121        Some((
122            skybox.clone(),
123            SkyboxUniforms {
124                brightness: skybox.brightness * exposure,
125                transform: Transform::from_rotation(skybox.rotation.inverse()).to_matrix(),
126                #[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
127                _wasm_padding_8b: 0,
128                #[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
129                _wasm_padding_12b: 0,
130                #[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
131                _wasm_padding_16b: 0,
132            },
133        ))
134    }
135}
136
137// TODO: Replace with a push constant once WebGPU gets support for that
138#[derive(Component, ShaderType, Clone)]
139pub struct SkyboxUniforms {
140    brightness: f32,
141    transform: Mat4,
142    #[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
143    _wasm_padding_8b: u32,
144    #[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
145    _wasm_padding_12b: u32,
146    #[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
147    _wasm_padding_16b: u32,
148}
149
150#[derive(Resource)]
151struct SkyboxPipeline {
152    bind_group_layout: BindGroupLayoutDescriptor,
153    shader: Handle<Shader>,
154}
155
156impl SkyboxPipeline {
157    fn new(shader: Handle<Shader>) -> Self {
158        Self {
159            bind_group_layout: BindGroupLayoutDescriptor::new(
160                "skybox_bind_group_layout",
161                &BindGroupLayoutEntries::sequential(
162                    ShaderStages::FRAGMENT,
163                    (
164                        texture_cube(TextureSampleType::Float { filterable: true }),
165                        sampler(SamplerBindingType::Filtering),
166                        uniform_buffer::<ViewUniform>(true)
167                            .visibility(ShaderStages::VERTEX_FRAGMENT),
168                        uniform_buffer::<SkyboxUniforms>(true),
169                    ),
170                ),
171            ),
172            shader,
173        }
174    }
175}
176
177fn init_skybox_pipeline(mut commands: Commands, asset_server: Res<AssetServer>) {
178    let shader = load_embedded_asset!(asset_server.as_ref(), "skybox.wgsl");
179    commands.insert_resource(SkyboxPipeline::new(shader));
180}
181
182#[derive(PartialEq, Eq, Hash, Clone, Copy)]
183struct SkyboxPipelineKey {
184    hdr: bool,
185    samples: u32,
186    depth_format: TextureFormat,
187}
188
189impl SpecializedRenderPipeline for SkyboxPipeline {
190    type Key = SkyboxPipelineKey;
191
192    fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
193        RenderPipelineDescriptor {
194            label: Some("skybox_pipeline".into()),
195            layout: vec![self.bind_group_layout.clone()],
196            vertex: VertexState {
197                shader: self.shader.clone(),
198                ..default()
199            },
200            depth_stencil: Some(DepthStencilState {
201                format: key.depth_format,
202                depth_write_enabled: false,
203                depth_compare: CompareFunction::GreaterEqual,
204                stencil: StencilState {
205                    front: StencilFaceState::IGNORE,
206                    back: StencilFaceState::IGNORE,
207                    read_mask: 0,
208                    write_mask: 0,
209                },
210                bias: DepthBiasState {
211                    constant: 0,
212                    slope_scale: 0.0,
213                    clamp: 0.0,
214                },
215            }),
216            multisample: MultisampleState {
217                count: key.samples,
218                mask: !0,
219                alpha_to_coverage_enabled: false,
220            },
221            fragment: Some(FragmentState {
222                shader: self.shader.clone(),
223                targets: vec![Some(ColorTargetState {
224                    format: if key.hdr {
225                        ViewTarget::TEXTURE_FORMAT_HDR
226                    } else {
227                        TextureFormat::bevy_default()
228                    },
229                    // BlendState::REPLACE is not needed here, and None will be potentially much faster in some cases.
230                    blend: None,
231                    write_mask: ColorWrites::ALL,
232                })],
233                ..default()
234            }),
235            ..default()
236        }
237    }
238}
239
240#[derive(Component)]
241pub struct SkyboxPipelineId(pub CachedRenderPipelineId);
242
243fn prepare_skybox_pipelines(
244    mut commands: Commands,
245    pipeline_cache: Res<PipelineCache>,
246    mut pipelines: ResMut<SpecializedRenderPipelines<SkyboxPipeline>>,
247    pipeline: Res<SkyboxPipeline>,
248    views: Query<(Entity, &ExtractedView, &Msaa), With<Skybox>>,
249) {
250    for (entity, view, msaa) in &views {
251        let pipeline_id = pipelines.specialize(
252            &pipeline_cache,
253            &pipeline,
254            SkyboxPipelineKey {
255                hdr: view.hdr,
256                samples: msaa.samples(),
257                depth_format: CORE_3D_DEPTH_FORMAT,
258            },
259        );
260
261        commands
262            .entity(entity)
263            .insert(SkyboxPipelineId(pipeline_id));
264    }
265}
266
267#[derive(Component)]
268pub struct SkyboxBindGroup(pub (BindGroup, u32));
269
270fn prepare_skybox_bind_groups(
271    mut commands: Commands,
272    pipeline: Res<SkyboxPipeline>,
273    view_uniforms: Res<ViewUniforms>,
274    skybox_uniforms: Res<ComponentUniforms<SkyboxUniforms>>,
275    images: Res<RenderAssets<GpuImage>>,
276    render_device: Res<RenderDevice>,
277    pipeline_cache: Res<PipelineCache>,
278    views: Query<(Entity, &Skybox, &DynamicUniformIndex<SkyboxUniforms>)>,
279) {
280    for (entity, skybox, skybox_uniform_index) in &views {
281        if let (Some(skybox), Some(view_uniforms), Some(skybox_uniforms)) = (
282            images.get(&skybox.image),
283            view_uniforms.uniforms.binding(),
284            skybox_uniforms.binding(),
285        ) {
286            let bind_group = render_device.create_bind_group(
287                "skybox_bind_group",
288                &pipeline_cache.get_bind_group_layout(&pipeline.bind_group_layout),
289                &BindGroupEntries::sequential((
290                    &skybox.texture_view,
291                    &skybox.sampler,
292                    view_uniforms,
293                    skybox_uniforms,
294                )),
295            );
296
297            commands
298                .entity(entity)
299                .insert(SkyboxBindGroup((bind_group, skybox_uniform_index.index())));
300        }
301    }
302}