use bevy_camera::Camera;
use bevy_color::Color;
use bevy_ecs::{
component::Component,
query::{QueryItem, With},
reflect::ReflectComponent,
system::lifetimeless::Read,
};
use bevy_math::{Vec2, Vec4};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::{
extract_component::ExtractComponent, render_resource::ShaderType, sync_component::SyncComponent,
};
#[derive(Reflect, Component, Clone)]
#[reflect(Component, Default, Clone)]
pub struct Vignette {
pub intensity: f32,
pub radius: f32,
pub smoothness: f32,
pub roundness: f32,
pub center: Vec2,
pub edge_compensation: f32,
pub color: Color,
}
impl Default for Vignette {
fn default() -> Self {
Self {
intensity: 1.0,
radius: 0.75,
smoothness: 5.0,
roundness: 1.0,
color: Color::BLACK,
edge_compensation: 1.0,
center: Vec2::new(0.5, 0.5),
}
}
}
impl SyncComponent for Vignette {
type Target = Self;
}
impl ExtractComponent for Vignette {
type QueryData = Read<Vignette>;
type QueryFilter = With<Camera>;
type Out = Self;
fn extract_component(vignette: QueryItem<'_, '_, Self::QueryData>) -> Option<Self::Out> {
if vignette.intensity > 1e-4 {
Some(vignette.clone())
} else {
None
}
}
}
#[derive(ShaderType, Default)]
pub struct VignetteUniform {
pub(super) intensity: f32,
pub(super) radius: f32,
pub(super) smoothness: f32,
pub(super) roundness: f32,
pub(super) center: Vec2,
pub(super) edge_compensation: f32,
pub(super) unused: u32,
pub(super) color: Vec4,
}