use bevy_ecs::{
component::Component,
query::{QueryItem, With},
system::lifetimeless::Read,
};
use bevy_math::{ops::abs, Vec2};
use bevy_reflect::Reflect;
use bevy_render::{
extract_component::ExtractComponent, render_resource::ShaderType, sync_component::SyncComponent,
};
#[derive(Reflect, Component, Clone)]
pub struct LensDistortion {
pub intensity: f32,
pub scale: f32,
pub multiplier: Vec2,
pub center: Vec2,
pub edge_curvature: f32,
}
impl Default for LensDistortion {
fn default() -> Self {
Self {
intensity: 0.5,
scale: 1.0,
multiplier: Vec2::ONE,
center: Vec2::splat(0.5),
edge_curvature: 0.0,
}
}
}
impl SyncComponent for LensDistortion {
type Target = Self;
}
impl ExtractComponent for LensDistortion {
type QueryData = Read<LensDistortion>;
type QueryFilter = With<LensDistortion>;
type Out = Self;
fn extract_component(lens_distortion: QueryItem<'_, '_, Self::QueryData>) -> Option<Self::Out> {
if abs(lens_distortion.intensity) > 1e-4 {
Some(lens_distortion.clone())
} else {
None
}
}
}
#[derive(ShaderType, Default)]
pub struct LensDistortionUniform {
pub(super) intensity: f32,
pub(super) scale: f32,
pub(super) multiplier: Vec2,
pub(super) center: Vec2,
pub(super) edge_curvature: f32,
pub(super) unused: u32,
}