Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1//! The lens distortion postprocessing effect.
3/// See `package::effect_stack::LensDistortion` for more
4/// information on these fields.
5struct LensDistortionSettings {
6 intensity: f32,
7 scale: f32,
8 multiplier: vec2<f32>,
9 center: vec2<f32>,
10 edge_curvature: f32,
11 unused: u32,
14const VISUAL_THRESHOLD: f32 = 1e-4;
15const MATH_EPSILON: f32 = 1e-6;
17/// The settings supplied by the developer.
18@group(0) @binding(5) var<uniform> lens_distortion_settings: LensDistortionSettings;
20fn lens_distortion(uv: vec2<f32>) -> vec2<f32> {
21 let intensity = lens_distortion_settings.intensity;
22 if (abs(intensity) < VISUAL_THRESHOLD) {
23 return uv;
24 }
25 let multiplier = lens_distortion_settings.multiplier;
26 let center = lens_distortion_settings.center;
28 let uv_centered = uv - center;
29 // Prevent division by zero.
30 let radius = max(length(uv_centered), MATH_EPSILON);
32 let direction = uv_centered / radius;
33 let adjust = dot(abs(direction), multiplier);
35 // Maintains the correlation between k2 and k1, while ensuring the sign of k2
36 // is determined solely by `edge_curvature` rather than being influenced by intensity.
37 let k1 = intensity * adjust;
38 let k2 = k1 * intensity * lens_distortion_settings.edge_curvature;
40 let r2 = radius * radius;
41 let r_distorted = radius * (1.0 + (k1 + k2 * r2) * r2);
43 let uv_distorted = direction * r_distorted + center;
45 // Compensates for the distortion pushing pixels outside the [0,1] UV bounds.
46 let uv_scaled = (uv_distorted - center) / lens_distortion_settings.scale + center;
48 // Discard out-of-bounds pixels to prevent edge bleeding artifacts.
49 let uv_safe = clamp(uv_scaled, vec2<f32>(0.0), vec2<f32>(1.0));
51 return uv_safe;
Contents