Skip to main content

ShaderEffect

Trait ShaderEffect 

Source
pub trait ShaderEffect: 'static {
    const WGSL: &'static str;
    const INPUTS: Inputs;
    const CLOCK: Option<usize> = None;

    // Provided methods
    fn uniforms(&self, _uniforms: &mut [f32; 16]) { ... }
    fn margin(&self) -> f32 { ... }
    fn content_blur(&self) -> f32 { ... }
    fn backdrop_blur(&self) -> f32 { ... }
    fn backdrop_reach(&self) -> Option<f32> { ... }
}

Required Associated Constants§

Source

const WGSL: &'static str

Source

const INPUTS: Inputs

Provided Associated Constants§

Source

const CLOCK: Option<usize> = None

Provided Methods§

Source

fn uniforms(&self, _uniforms: &mut [f32; 16])

Source

fn margin(&self) -> f32

Source

fn content_blur(&self) -> f32

Source

fn backdrop_blur(&self) -> f32

Source

fn backdrop_reach(&self) -> Option<f32>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl ShaderEffect for BackdropBlur

Source§

const WGSL: &'static str = fn effect(in: EffectInput) -> vec4<f32> { return sample_backdrop(in.local) * in.coverage; }

Source§

const INPUTS: Inputs = Inputs::BACKDROP

Source§

impl ShaderEffect for BlendMode

Source§

const WGSL: &'static str = fn dodge(b: vec3<f32>, s: vec3<f32>) -> vec3<f32> { let lit = min(vec3<f32>(1.0), b / max(vec3<f32>(1.0) - s, vec3<f32>(1e-5))); return select(lit, vec3<f32>(0.0), b <= vec3<f32>(0.0)); } fn burn(b: vec3<f32>, s: vec3<f32>) -> vec3<f32> { let dark = vec3<f32>(1.0) - min(vec3<f32>(1.0), (vec3<f32>(1.0) - b) / max(s, vec3<f32>(1e-5))); return select(dark, vec3<f32>(1.0), b >= vec3<f32>(1.0)); } fn hard_light(b: vec3<f32>, s: vec3<f32>) -> vec3<f32> { let multiply = 2.0 * b * s; let screen = vec3<f32>(1.0) - 2.0 * (vec3<f32>(1.0) - b) * (vec3<f32>(1.0) - s); return select(screen, multiply, s <= vec3<f32>(0.5)); } fn soft_light(b: vec3<f32>, s: vec3<f32>) -> vec3<f32> { let curve = select(sqrt(b), ((16.0 * b - 12.0) * b + 4.0) * b, b <= vec3<f32>(0.25)); let dark = b - (vec3<f32>(1.0) - 2.0 * s) * b * (vec3<f32>(1.0) - b); let light = b + (2.0 * s - vec3<f32>(1.0)) * (curve - b); return select(light, dark, s <= vec3<f32>(0.5)); } fn mix_colors(mode: i32, b: vec3<f32>, s: vec3<f32>) -> vec3<f32> { switch mode { case 0 { return b * s; } case 1 { return b + s - b * s; } case 2 { return hard_light(s, b); } case 3 { return min(b, s); } case 4 { return max(b, s); } case 5 { return dodge(b, s); } case 6 { return burn(b, s); } case 7 { return hard_light(b, s); } case 8 { return soft_light(b, s); } case 9 { return abs(b - s); } case 10 { return b + s - 2.0 * b * s; } default { return s; } } } fn effect(in: EffectInput) -> vec4<f32> { let source = sample_content(in.local); if (source.a <= 0.0) { return vec4<f32>(0.0); } let behind = sample_backdrop(in.local); let mode = i32(param(0).x); if (mode == 11) { return vec4<f32>(source.rgb + behind.rgb * source.a, source.a); } let b = behind.rgb / max(behind.a, 1e-5); let s = source.rgb / source.a; let mixed = clamp(mix_colors(mode, b, s), vec3<f32>(0.0), vec3<f32>(1.0)); return vec4<f32>(source.rgb * (1.0 - behind.a) + mixed * source.a * behind.a, source.a); }

Source§

const INPUTS: Inputs = Inputs::BOTH

Source§

impl ShaderEffect for Blur

Source§

const WGSL: &'static str = PASSTHROUGH_WGSL

Source§

const INPUTS: Inputs = Inputs::CONTENT

Source§

impl ShaderEffect for ColorMatrix

Source§

const WGSL: &'static str = fn effect(in: EffectInput) -> vec4<f32> { let color = sample_content(in.local); if (color.a <= 0.0) { return vec4<f32>(0.0); } let straight = vec4<f32>(color.rgb / color.a, 1.0); let mapped = vec3<f32>(dot(param(0), straight), dot(param(1), straight), dot(param(2), straight)); return vec4<f32>(clamp(mapped, vec3<f32>(0.0), vec3<f32>(1.0)) * color.a, color.a); }

Source§

const INPUTS: Inputs = Inputs::CONTENT

Source§

impl ShaderEffect for Glass

Source§

const WGSL: &'static str = const RIM_WIDTH: f32 = 1.5; const SHADOW_REACH: f32 = 22.0; const SHADOW_DROP: f32 = 6.0; const CONTACT_REACH: f32 = 5.0; const CONTACT_DROP: f32 = 1.5; const LUMA: vec3<f32> = vec3<f32>(0.2126, 0.7152, 0.0722); fn effect(in: EffectInput) -> vec4<f32> { let tint = param(0); let optics = param(1); let finish = param(2); let pointer = param(3); let shadow_amount = finish.x; let texel = 1.0 / scale_factor(); var shadow = 0.0; if (shadow_amount > 0.0) { let dropped = shape_sdf(in.local - vec2<f32>(0.0, SHADOW_DROP)); let soft = clamp(1.0 - dropped / SHADOW_REACH, 0.0, 1.0); let grounded = shape_sdf(in.local - vec2<f32>(0.0, CONTACT_DROP)); let contact = clamp(1.0 - grounded / CONTACT_REACH, 0.0, 1.0); shadow = shadow_amount * (0.75 * soft * soft * soft + 0.25 * contact * contact) * (1.0 - in.coverage); } if (in.coverage <= 0.0 && shadow <= 0.0) { discard; } let e = vec2<f32>(texel, 0.0); let gradient = vec2<f32>( shape_sdf(in.local + e.xy) - shape_sdf(in.local - e.xy), shape_sdf(in.local + e.yx) - shape_sdf(in.local - e.yx), ); let normal = gradient / max(length(gradient), 1e-4 * texel); let edge = max(optics.w, texel); let depth = max(-in.distance, 0.0); let rim = clamp(1.0 - depth / edge, 0.0, 1.0); let bend = 1.0 - sqrt(1.0 - rim * rim); let offset = -normal * bend * optics.x * edge * 2.0; let spread = offset * optics.y; let r = sample_backdrop(in.local + offset + spread).r; let g = sample_backdrop(in.local + offset).g; let b = sample_backdrop(in.local + offset - spread).b; var color = vec3<f32>(r, g, b); color = mix(vec3<f32>(dot(color, LUMA)), color, finish.y); color = mix(color, tint.rgb, tint.a); let light = vec2<f32>(sin(pointer.z), -cos(pointer.z)); let facing = dot(normal, light); let key = max(facing, 0.0); let fill = max(-facing, 0.0); let glint = 0.15 + 0.85 * key * key + pointer.w * fill * fill; let line = clamp(1.0 - depth / RIM_WIDTH, 0.0, 1.0); let bloom = rim * rim * rim; let sheen = line * glint + bloom * glint * 0.22 + bend * 0.12; let away = (in.local - finish.zw) / max(pointer.y, 1.0); let spot = exp(-dot(away, away)) * pointer.x; let caught = spot * (line * 0.8 + bloom * 0.3 + 0.05); color += vec3<f32>(sheen * optics.z + caught); let glass = clamp(color, vec3<f32>(0.0), vec3<f32>(1.0)) * in.coverage; return vec4<f32>(glass, in.coverage + shadow); }

Source§

const INPUTS: Inputs = Inputs::BACKDROP

Source§

impl ShaderEffect for Mask

Source§

const WGSL: &'static str = fn effect(in: EffectInput) -> vec4<f32> { let options = param(0); let geometry = param(1); var along = 0.0; if (options.x < 0.5) { let axis = (geometry.zw - geometry.xy) * in.size; let offset = (in.uv - geometry.xy) * in.size; along = dot(offset, axis) / max(dot(axis, axis), 1e-6); } else if (options.x < 1.5) { let reach = max(geometry.z * min(in.size.x, in.size.y), 1e-6); along = length((in.uv - geometry.xy) * in.size) / reach; } else { along = 1.0 - in.coverage; } let span = max(options.w - options.z, 1e-6); var alpha = 1.0 - clamp((along - options.z) / span, 0.0, 1.0); if (options.y > 0.5) { alpha = 1.0 - alpha; } return sample_content(in.local) * alpha; }

Source§

const INPUTS: Inputs = Inputs::CONTENT