Skip to content

Commit

Permalink
Merge environment_map and light_probe into one
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed Dec 17, 2023
1 parent 5dc6874 commit 35b8ad2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 40 deletions.
11 changes: 5 additions & 6 deletions crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ pub mod wireframe;
mod alpha;
mod bundle;
pub mod deferred;
mod environment_map;
mod extended_material;
mod fog;
mod light;
mod light_probe;
pub mod light_probe;
mod material;
mod parallax;
mod pbr_material;
Expand All @@ -17,7 +16,6 @@ mod ssao;

pub use alpha::*;
pub use bundle::*;
pub use environment_map::{EnvironmentMapLight, EnvironmentMapPlugin};
pub use extended_material::*;
pub use fog::*;
pub use light::*;
Expand All @@ -37,10 +35,12 @@ pub mod prelude {
DirectionalLightBundle, MaterialMeshBundle, PbrBundle, PointLightBundle,
SpotLightBundle,
},
environment_map::{EnvironmentMapLight, ReflectionProbeBundle},
fog::{FogFalloff, FogSettings},
light::{AmbientLight, DirectionalLight, PointLight, SpotLight},
light_probe::LightProbe,
light_probe::{
environment_map::{EnvironmentMapLight, ReflectionProbeBundle},
LightProbe,
},
material::{Material, MaterialPlugin},
parallax::ParallaxMappingMethod,
pbr_material::StandardMaterial,
Expand Down Expand Up @@ -255,7 +255,6 @@ impl Plugin for PbrPlugin {
..Default::default()
},
ScreenSpaceAmbientOcclusionPlugin,
EnvironmentMapPlugin,
ExtractResourcePlugin::<AmbientLight>::default(),
FogPlugin,
ExtractResourcePlugin::<DefaultOpaqueRendererMethod>::default(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,20 @@
//!
//! [glTF IBL Sampler]: https://github.com/KhronosGroup/glTF-IBL-Sampler
use bevy_app::{App, Plugin};
use bevy_asset::{load_internal_asset, AssetId, Handle};
use bevy_asset::{AssetId, Handle};
use bevy_ecs::{
bundle::Bundle, component::Component, query::QueryItem, system::lifetimeless::Read,
};
use bevy_reflect::Reflect;
use bevy_render::{
extract_instances::{ExtractInstance, ExtractInstancesPlugin},
extract_instances::ExtractInstance,
prelude::SpatialBundle,
render_asset::RenderAssets,
render_resource::{
binding_types, BindGroupLayoutEntryBuilder, Sampler, SamplerBindingType, Shader,
TextureSampleType, TextureView,
},
texture::{FallbackImage, Image},
RenderApp,
};

#[cfg(all(not(feature = "shader_format_glsl"), not(target_arch = "wasm32")))]
Expand All @@ -58,12 +56,6 @@ use crate::LightProbe;
pub const ENVIRONMENT_MAP_SHADER_HANDLE: Handle<Shader> =
Handle::weak_from_u128(154476556247605696);

/// Adds support for environment maps.
///
/// See the documentation in [`crate::environment_map`] for detailed information
/// on environment maps.
pub struct EnvironmentMapPlugin;

/// A pair of cubemap textures that represent the surroundings of a specific
/// area in space.
///
Expand Down Expand Up @@ -174,25 +166,6 @@ pub(crate) struct RenderViewBindGroupEntries<'a> {
pub(crate) sampler: &'a Sampler,
}

impl Plugin for EnvironmentMapPlugin {
fn build(&self, app: &mut App) {
load_internal_asset!(
app,
ENVIRONMENT_MAP_SHADER_HANDLE,
"environment_map.wgsl",
Shader::from_wgsl
);
}

fn finish(&self, app: &mut App) {
let Ok(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};

render_app.add_plugins(ExtractInstancesPlugin::<EnvironmentMapIds>::new());
}
}

impl ExtractInstance for EnvironmentMapIds {
type Data = Read<EnvironmentMapLight>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Light probes for baked global illumination.
use bevy_app::{App, Plugin};
use bevy_asset::load_internal_asset;
use bevy_core_pipeline::core_3d::Camera3d;
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{
Expand All @@ -14,21 +15,24 @@ use bevy_ecs::{
use bevy_math::{Affine3A, Mat4, Vec3A, Vec4};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::{
extract_instances::ExtractInstancesPlugin,
primitives::{Aabb, Frustum},
render_asset::RenderAssets,
render_resource::{DynamicUniformBuffer, ShaderType},
render_resource::{DynamicUniformBuffer, Shader, ShaderType},
renderer::{RenderDevice, RenderQueue},
texture::Image,
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_transform::prelude::GlobalTransform;
use bevy_utils::{EntityHashMap, FloatOrd};

use crate::{
environment_map::{EnvironmentMapIds, RenderViewEnvironmentMaps},
EnvironmentMapLight,
use crate::light_probe::environment_map::{
EnvironmentMapIds, EnvironmentMapLight, RenderViewEnvironmentMaps,
ENVIRONMENT_MAP_SHADER_HANDLE,
};

pub mod environment_map;

/// The maximum number of reflection probes that each view will consider.
///
/// Because the fragment shader does a linear search through the list for each
Expand All @@ -37,6 +41,9 @@ pub const MAX_VIEW_REFLECTION_PROBES: usize = 8;

/// Adds support for light probes: cuboid bounding regions that apply global
/// illumination to objects within them.
///
/// This also adds support for view environment maps: diffuse and specular
/// cubemaps applied to all objects that a view renders.
pub struct LightProbePlugin;

/// A cuboid region that provides global illumination to all fragments inside it.
Expand Down Expand Up @@ -118,6 +125,13 @@ impl LightProbe {

impl Plugin for LightProbePlugin {
fn build(&self, app: &mut App) {
load_internal_asset!(
app,
ENVIRONMENT_MAP_SHADER_HANDLE,
"environment_map.wgsl",
Shader::from_wgsl
);

app.register_type::<LightProbe>();
}

Expand All @@ -127,6 +141,7 @@ impl Plugin for LightProbePlugin {
};

render_app
.add_plugins(ExtractInstancesPlugin::<EnvironmentMapIds>::new())
.init_resource::<LightProbesBuffer>()
.init_resource::<RenderLightProbes>()
.add_systems(ExtractSchedule, gather_light_probes)
Expand Down
1 change: 0 additions & 1 deletion examples/3d/reflection_probes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//! Reflection probes don't work on WebGL 2 or WebGPU.
use bevy::core_pipeline::Skybox;
use bevy::pbr::EnvironmentMapLight;
use bevy::prelude::*;

use std::fmt::{Display, Formatter, Result as FmtResult};
Expand Down

0 comments on commit 35b8ad2

Please sign in to comment.