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> {\n return sample_backdrop(in.local) * in.coverage;\n}\n"

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

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> {\n let color = sample_content(in.local);\n if (color.a <= 0.0) {\n return vec4<f32>(0.0);\n }\n let straight = vec4<f32>(color.rgb / color.a, 1.0);\n let mapped = vec3<f32>(dot(param(0), straight), dot(param(1), straight), dot(param(2), straight));\n return vec4<f32>(clamp(mapped, vec3<f32>(0.0), vec3<f32>(1.0)) * color.a, color.a);\n}\n"

Source§

const INPUTS: Inputs = Inputs::CONTENT

Source§

impl ShaderEffect for Glass

Source§

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

Source§

const INPUTS: Inputs = Inputs::BACKDROP

Source§

impl ShaderEffect for Mask

Source§

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

Source§

const INPUTS: Inputs = Inputs::CONTENT