Path: blob/main/crates/bevy_post_process/src/effect_stack/chromatic_aberration.rs
9356 views
use bevy_asset::Handle;1use bevy_camera::Camera;2use bevy_ecs::{3component::Component,4query::{QueryItem, With},5reflect::ReflectComponent,6resource::Resource,7system::lifetimeless::Read,8};9use bevy_image::Image;10use bevy_reflect::{std_traits::ReflectDefault, Reflect};11use bevy_render::{12extract_component::ExtractComponent, render_resource::ShaderType, sync_component::SyncComponent,13};1415/// The raw RGBA data for the default chromatic aberration gradient.16///17/// This consists of one red pixel, one green pixel, and one blue pixel, in that18/// order.19pub(super) static DEFAULT_CHROMATIC_ABERRATION_LUT_DATA: [u8; 12] =20[255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255];2122/// The default chromatic aberration intensity amount, in a fraction of the23/// window size.24const DEFAULT_CHROMATIC_ABERRATION_INTENSITY: f32 = 0.02;2526/// The default maximum number of samples for chromatic aberration.27const DEFAULT_CHROMATIC_ABERRATION_MAX_SAMPLES: u32 = 8;2829#[derive(Resource)]30pub(crate) struct DefaultChromaticAberrationLut(pub(crate) Handle<Image>);3132/// Adds colored fringes to the edges of objects in the scene.33///34/// [Chromatic aberration] simulates the effect when lenses fail to focus all35/// colors of light toward a single point. It causes rainbow-colored streaks to36/// appear, which are especially apparent on the edges of objects. Chromatic37/// aberration is commonly used for collision effects, especially in horror38/// games.39///40/// Bevy's implementation is based on that of *Inside* ([Gjøl & Svendsen 2016]).41/// It's based on a customizable lookup texture, which allows for changing the42/// color pattern. By default, the color pattern is simply a 3×1 pixel texture43/// consisting of red, green, and blue, in that order, but you can change it to44/// any image in order to achieve different effects.45///46/// [Chromatic aberration]: https://en.wikipedia.org/wiki/Chromatic_aberration47///48/// [Gjøl & Svendsen 2016]: https://github.com/playdeadgames/publications/blob/master/INSIDE/rendering_inside_gdc2016.pdf49#[derive(Reflect, Component, Clone)]50#[reflect(Component, Default, Clone)]51pub struct ChromaticAberration {52/// The lookup texture that determines the color gradient.53///54/// By default (if None), this is a 3×1 texel texture consisting of one red55/// pixel, one green pixel, and one blue texel, in that order. This56/// recreates the most typical chromatic aberration pattern. However, you57/// can change it to achieve different artistic effects.58///59/// The texture is always sampled in its vertical center, so it should60/// ordinarily have a height of 1 texel.61pub color_lut: Option<Handle<Image>>,6263/// The size of the streaks around the edges of objects, as a fraction of64/// the window size.65///66/// The default value is 0.02.67pub intensity: f32,6869/// A cap on the number of texture samples that will be performed.70///71/// Higher values result in smoother-looking streaks but are slower.72///73/// The default value is 8.74pub max_samples: u32,75}7677impl Default for ChromaticAberration {78fn default() -> Self {79Self {80color_lut: None,81intensity: DEFAULT_CHROMATIC_ABERRATION_INTENSITY,82max_samples: DEFAULT_CHROMATIC_ABERRATION_MAX_SAMPLES,83}84}85}8687impl SyncComponent for ChromaticAberration {88type Out = Self;89}9091impl ExtractComponent for ChromaticAberration {92type QueryData = Read<ChromaticAberration>;93type QueryFilter = With<Camera>;9495fn extract_component(96chromatic_aberration: QueryItem<'_, '_, Self::QueryData>,97) -> Option<Self::Out> {98// Skip the postprocessing phase entirely if the intensity is zero.99if chromatic_aberration.intensity > 0.0 {100Some(chromatic_aberration.clone())101} else {102None103}104}105}106107/// The on-GPU version of the [`ChromaticAberration`] settings.108///109/// See the documentation for [`ChromaticAberration`] for more information on110/// each of these fields.111#[derive(ShaderType, Default)]112pub struct ChromaticAberrationUniform {113/// The intensity of the effect, in a fraction of the screen.114pub(super) intensity: f32,115/// A cap on the number of samples of the source texture that the shader116/// will perform.117pub(super) max_samples: u32,118/// Padding data.119pub(super) unused_1: u32,120/// Padding data.121pub(super) unused_2: u32,122}123124125