Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_post_process/src/effect_stack/chromatic_aberration.rs
9356 views
1
use bevy_asset::Handle;
2
use bevy_camera::Camera;
3
use bevy_ecs::{
4
component::Component,
5
query::{QueryItem, With},
6
reflect::ReflectComponent,
7
resource::Resource,
8
system::lifetimeless::Read,
9
};
10
use bevy_image::Image;
11
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
12
use bevy_render::{
13
extract_component::ExtractComponent, render_resource::ShaderType, sync_component::SyncComponent,
14
};
15
16
/// The raw RGBA data for the default chromatic aberration gradient.
17
///
18
/// This consists of one red pixel, one green pixel, and one blue pixel, in that
19
/// order.
20
pub(super) static DEFAULT_CHROMATIC_ABERRATION_LUT_DATA: [u8; 12] =
21
[255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255];
22
23
/// The default chromatic aberration intensity amount, in a fraction of the
24
/// window size.
25
const DEFAULT_CHROMATIC_ABERRATION_INTENSITY: f32 = 0.02;
26
27
/// The default maximum number of samples for chromatic aberration.
28
const DEFAULT_CHROMATIC_ABERRATION_MAX_SAMPLES: u32 = 8;
29
30
#[derive(Resource)]
31
pub(crate) struct DefaultChromaticAberrationLut(pub(crate) Handle<Image>);
32
33
/// Adds colored fringes to the edges of objects in the scene.
34
///
35
/// [Chromatic aberration] simulates the effect when lenses fail to focus all
36
/// colors of light toward a single point. It causes rainbow-colored streaks to
37
/// appear, which are especially apparent on the edges of objects. Chromatic
38
/// aberration is commonly used for collision effects, especially in horror
39
/// games.
40
///
41
/// Bevy's implementation is based on that of *Inside* ([Gjøl & Svendsen 2016]).
42
/// It's based on a customizable lookup texture, which allows for changing the
43
/// color pattern. By default, the color pattern is simply a 3×1 pixel texture
44
/// consisting of red, green, and blue, in that order, but you can change it to
45
/// any image in order to achieve different effects.
46
///
47
/// [Chromatic aberration]: https://en.wikipedia.org/wiki/Chromatic_aberration
48
///
49
/// [Gjøl & Svendsen 2016]: https://github.com/playdeadgames/publications/blob/master/INSIDE/rendering_inside_gdc2016.pdf
50
#[derive(Reflect, Component, Clone)]
51
#[reflect(Component, Default, Clone)]
52
pub struct ChromaticAberration {
53
/// The lookup texture that determines the color gradient.
54
///
55
/// By default (if None), this is a 3×1 texel texture consisting of one red
56
/// pixel, one green pixel, and one blue texel, in that order. This
57
/// recreates the most typical chromatic aberration pattern. However, you
58
/// can change it to achieve different artistic effects.
59
///
60
/// The texture is always sampled in its vertical center, so it should
61
/// ordinarily have a height of 1 texel.
62
pub color_lut: Option<Handle<Image>>,
63
64
/// The size of the streaks around the edges of objects, as a fraction of
65
/// the window size.
66
///
67
/// The default value is 0.02.
68
pub intensity: f32,
69
70
/// A cap on the number of texture samples that will be performed.
71
///
72
/// Higher values result in smoother-looking streaks but are slower.
73
///
74
/// The default value is 8.
75
pub max_samples: u32,
76
}
77
78
impl Default for ChromaticAberration {
79
fn default() -> Self {
80
Self {
81
color_lut: None,
82
intensity: DEFAULT_CHROMATIC_ABERRATION_INTENSITY,
83
max_samples: DEFAULT_CHROMATIC_ABERRATION_MAX_SAMPLES,
84
}
85
}
86
}
87
88
impl SyncComponent for ChromaticAberration {
89
type Out = Self;
90
}
91
92
impl ExtractComponent for ChromaticAberration {
93
type QueryData = Read<ChromaticAberration>;
94
type QueryFilter = With<Camera>;
95
96
fn extract_component(
97
chromatic_aberration: QueryItem<'_, '_, Self::QueryData>,
98
) -> Option<Self::Out> {
99
// Skip the postprocessing phase entirely if the intensity is zero.
100
if chromatic_aberration.intensity > 0.0 {
101
Some(chromatic_aberration.clone())
102
} else {
103
None
104
}
105
}
106
}
107
108
/// The on-GPU version of the [`ChromaticAberration`] settings.
109
///
110
/// See the documentation for [`ChromaticAberration`] for more information on
111
/// each of these fields.
112
#[derive(ShaderType, Default)]
113
pub struct ChromaticAberrationUniform {
114
/// The intensity of the effect, in a fraction of the screen.
115
pub(super) intensity: f32,
116
/// A cap on the number of samples of the source texture that the shader
117
/// will perform.
118
pub(super) max_samples: u32,
119
/// Padding data.
120
pub(super) unused_1: u32,
121
/// Padding data.
122
pub(super) unused_2: u32,
123
}
124
125