Path: blob/main/crates/bevy_post_process/src/bloom/settings.rs
6596 views
use super::downsampling_pipeline::BloomUniforms;1use bevy_camera::Camera;2use bevy_ecs::{3prelude::Component,4query::{QueryItem, With},5reflect::ReflectComponent,6};7use bevy_math::{AspectRatio, URect, UVec4, Vec2, Vec4};8use bevy_reflect::{std_traits::ReflectDefault, Reflect};9use bevy_render::{extract_component::ExtractComponent, view::Hdr};1011/// Applies a bloom effect to an HDR-enabled 2d or 3d camera.12///13/// Bloom emulates an effect found in real cameras and the human eye,14/// causing halos to appear around very bright parts of the scene.15///16/// See also <https://en.wikipedia.org/wiki/Bloom_(shader_effect)>.17///18/// # Usage Notes19///20/// **Bloom is currently not compatible with WebGL2.**21///22/// Often used in conjunction with `bevy_pbr::StandardMaterial::emissive` for 3d meshes.23///24/// Bloom is best used alongside a tonemapping function that desaturates bright colors,25/// such as [`bevy_core_pipeline::tonemapping::Tonemapping::TonyMcMapface`].26///27/// Bevy's implementation uses a parametric curve to blend between a set of28/// blurred (lower frequency) images generated from the camera's view.29/// See <https://starlederer.github.io/bloom/> for a visualization of the parametric curve30/// used in Bevy as well as a visualization of the curve's respective scattering profile.31#[derive(Component, Reflect, Clone)]32#[reflect(Component, Default, Clone)]33#[require(Hdr)]34pub struct Bloom {35/// Controls the baseline of how much the image is scattered (default: 0.15).36///37/// This parameter should be used only to control the strength of the bloom38/// for the scene as a whole. Increasing it too much will make the scene appear39/// blurry and over-exposed.40///41/// To make a mesh glow brighter, rather than increase the bloom intensity,42/// you should increase the mesh's `emissive` value.43///44/// # In energy-conserving mode45/// The value represents how likely the light is to scatter.46///47/// The value should be between 0.0 and 1.0 where:48/// * 0.0 means no bloom49/// * 1.0 means the light is scattered as much as possible50///51/// # In additive mode52/// The value represents how much scattered light is added to53/// the image to create the glow effect.54///55/// In this configuration:56/// * 0.0 means no bloom57/// * Greater than 0.0 means a proportionate amount of scattered light is added58pub intensity: f32,5960/// Low frequency contribution boost.61/// Controls how much more likely the light62/// is to scatter completely sideways (low frequency image).63///64/// Comparable to a low shelf boost on an equalizer.65///66/// # In energy-conserving mode67/// The value should be between 0.0 and 1.0 where:68/// * 0.0 means low frequency light uses base intensity for blend factor calculation69/// * 1.0 means low frequency light contributes at full power70///71/// # In additive mode72/// The value represents how much scattered light is added to73/// the image to create the glow effect.74///75/// In this configuration:76/// * 0.0 means no bloom77/// * Greater than 0.0 means a proportionate amount of scattered light is added78pub low_frequency_boost: f32,7980/// Low frequency contribution boost curve.81/// Controls the curvature of the blend factor function82/// making frequencies next to the lowest ones contribute more.83///84/// Somewhat comparable to the Q factor of an equalizer node.85///86/// Valid range:87/// * 0.0 - base intensity and boosted intensity are linearly interpolated88/// * 1.0 - all frequencies below maximum are at boosted intensity level89pub low_frequency_boost_curvature: f32,9091/// Tightens how much the light scatters (default: 1.0).92///93/// Valid range:94/// * 0.0 - maximum scattering angle is 0 degrees (no scattering)95/// * 1.0 - maximum scattering angle is 90 degrees96pub high_pass_frequency: f32,9798/// Controls the threshold filter used for extracting the brightest regions from the input image99/// before blurring them and compositing back onto the original image.100///101/// Changing these settings creates a physically inaccurate image and makes it easy to make102/// the final result look worse. However, they can be useful when emulating the 1990s-2000s game look.103/// See [`BloomPrefilter`] for more information.104pub prefilter: BloomPrefilter,105106/// Controls whether bloom textures107/// are blended between or added to each other. Useful108/// if image brightening is desired and a must-change109/// if `prefilter` is used.110///111/// # Recommendation112/// Set to [`BloomCompositeMode::Additive`] if `prefilter` is113/// configured in a non-energy-conserving way,114/// otherwise set to [`BloomCompositeMode::EnergyConserving`].115pub composite_mode: BloomCompositeMode,116117/// Maximum size of each dimension for the largest mipchain texture used in downscaling/upscaling.118/// Only tweak if you are seeing visual artifacts.119pub max_mip_dimension: u32,120121/// Amount to stretch the bloom on each axis. Artistic control, can be used to emulate122/// anamorphic blur by using a large x-value. For large values, you may need to increase123/// [`Bloom::max_mip_dimension`] to reduce sampling artifacts.124pub scale: Vec2,125}126127impl Bloom {128const DEFAULT_MAX_MIP_DIMENSION: u32 = 512;129130/// The default bloom preset.131///132/// This uses the [`EnergyConserving`](BloomCompositeMode::EnergyConserving) composite mode.133pub const NATURAL: Self = Self {134intensity: 0.15,135low_frequency_boost: 0.7,136low_frequency_boost_curvature: 0.95,137high_pass_frequency: 1.0,138prefilter: BloomPrefilter {139threshold: 0.0,140threshold_softness: 0.0,141},142composite_mode: BloomCompositeMode::EnergyConserving,143max_mip_dimension: Self::DEFAULT_MAX_MIP_DIMENSION,144scale: Vec2::ONE,145};146147/// Emulates the look of stylized anamorphic bloom, stretched horizontally.148pub const ANAMORPHIC: Self = Self {149// The larger scale necessitates a larger resolution to reduce artifacts:150max_mip_dimension: Self::DEFAULT_MAX_MIP_DIMENSION * 2,151scale: Vec2::new(4.0, 1.0),152..Self::NATURAL153};154155/// A preset that's similar to how older games did bloom.156pub const OLD_SCHOOL: Self = Self {157intensity: 0.05,158low_frequency_boost: 0.7,159low_frequency_boost_curvature: 0.95,160high_pass_frequency: 1.0,161prefilter: BloomPrefilter {162threshold: 0.6,163threshold_softness: 0.2,164},165composite_mode: BloomCompositeMode::Additive,166max_mip_dimension: Self::DEFAULT_MAX_MIP_DIMENSION,167scale: Vec2::ONE,168};169170/// A preset that applies a very strong bloom, and blurs the whole screen.171pub const SCREEN_BLUR: Self = Self {172intensity: 1.0,173low_frequency_boost: 0.0,174low_frequency_boost_curvature: 0.0,175high_pass_frequency: 1.0 / 3.0,176prefilter: BloomPrefilter {177threshold: 0.0,178threshold_softness: 0.0,179},180composite_mode: BloomCompositeMode::EnergyConserving,181max_mip_dimension: Self::DEFAULT_MAX_MIP_DIMENSION,182scale: Vec2::ONE,183};184}185186impl Default for Bloom {187fn default() -> Self {188Self::NATURAL189}190}191192/// Applies a threshold filter to the input image to extract the brightest193/// regions before blurring them and compositing back onto the original image.194/// These settings are useful when emulating the 1990s-2000s game look.195///196/// # Considerations197/// * Changing these settings creates a physically inaccurate image198/// * Changing these settings makes it easy to make the final result look worse199/// * Non-default prefilter settings should be used in conjunction with [`BloomCompositeMode::Additive`]200#[derive(Default, Clone, Reflect)]201#[reflect(Clone, Default)]202pub struct BloomPrefilter {203/// Baseline of the quadratic threshold curve (default: 0.0).204///205/// RGB values under the threshold curve will not contribute to the effect.206pub threshold: f32,207208/// Controls how much to blend between the thresholded and non-thresholded colors (default: 0.0).209///210/// 0.0 = Abrupt threshold, no blending211/// 1.0 = Fully soft threshold212///213/// Values outside of the range [0.0, 1.0] will be clamped.214pub threshold_softness: f32,215}216217#[derive(Debug, Clone, Reflect, PartialEq, Eq, Hash, Copy)]218#[reflect(Clone, Hash, PartialEq)]219pub enum BloomCompositeMode {220EnergyConserving,221Additive,222}223224impl ExtractComponent for Bloom {225type QueryData = (&'static Self, &'static Camera);226227type QueryFilter = With<Hdr>;228type Out = (Self, BloomUniforms);229230fn extract_component((bloom, camera): QueryItem<'_, '_, Self::QueryData>) -> Option<Self::Out> {231match (232camera.physical_viewport_rect(),233camera.physical_viewport_size(),234camera.physical_target_size(),235camera.is_active,236) {237(Some(URect { min: origin, .. }), Some(size), Some(target_size), true)238if size.x != 0 && size.y != 0 =>239{240let threshold = bloom.prefilter.threshold;241let threshold_softness = bloom.prefilter.threshold_softness;242let knee = threshold * threshold_softness.clamp(0.0, 1.0);243244let uniform = BloomUniforms {245threshold_precomputations: Vec4::new(246threshold,247threshold - knee,2482.0 * knee,2490.25 / (knee + 0.00001),250),251viewport: UVec4::new(origin.x, origin.y, size.x, size.y).as_vec4()252/ UVec4::new(target_size.x, target_size.y, target_size.x, target_size.y)253.as_vec4(),254aspect: AspectRatio::try_from_pixels(size.x, size.y)255.expect("Valid screen size values for Bloom settings")256.ratio(),257scale: bloom.scale,258};259260Some((bloom.clone(), uniform))261}262_ => None,263}264}265}266267268