GPU shaders

Loading Ebi…
fn<sig> rings(ctx: ShaderContext, sig phase: Number, shadow: Color, tint: Color): Color {
    let x = ctx.aspect * (2 * ctx.u - 1) - 0.26 * cos(phase)
    let y = 2 * ctx.v - 1 - 0.26 * sin(phase)
    let distance = hypot(x, y)
    let wave = 0.5 + 0.5 * cos(20 * distance - 3 * phase)
    let glow = wave * wave / (1 + 1.15 * distance * distance)
    return Rgb(
        lerp(shadow.to_rgb().red, tint.to_rgb().red, glow),
        lerp(shadow.to_rgb().green, tint.to_rgb().green, glow),
        lerp(shadow.to_rgb().blue, tint.to_rgb().blue, glow),
    )
}

fn<sig> plasma(ctx: ShaderContext, sig phase: Number): Color {
    let x = ctx.aspect * (2 * ctx.u - 1)
    let y = 2 * ctx.v - 1
    let flow_x = x + 0.38 * sin(2 * y + 0.95 * cos(3 * x + phase))
    let flow_y = y + 0.33 * cos(2.4 * x - 0.95 * sin(3 * y - phase))
    let current = sin(3.8 * flow_x + 2.6 * flow_y + 1.4 * sin(2.7 * flow_y - phase))
    let shear = cos(1.7 * flow_x - 4.6 * flow_y + 1.2 * sin(2.1 * flow_x + phase))
    let seam = sin(4.5 * flow_y + 2.4 * flow_x + 1.6 * current)
    let glow = 0.5 + 0.24 * current + 0.18 * shear + 0.08 * seam
    let rim = 1 - abs(seam)
    let hue = TAU * (0.56 + 0.89 * glow - 0.21 * shear)
    let highlight = glow * glow * (0.6 * glow + 0.18 * rim * rim)
    let chroma = 0.5 * glow * (1 - highlight)
    return Rgb(
        highlight + chroma * (1 + cos(hue)),
        highlight + chroma * (1 + cos(hue - TAU / 3)),
        highlight + chroma * (1 + cos(hue + TAU / 3)),
    )
}

mut progress = 0
sig phase = TAU * progress
let ripple_shader = rings(phase = phase, shadow = Color("dark4"), tint = Color("blue1"))
let plasma_shader = plasma(phase = phase)

<Box w-full h-full p-14 gap-10 flex-row fill="#11141D">
    <Box flex-1 h-full p-8 rounded-lg fill="#202638">
        <Image width={100%} height={100%} rounded src={ShaderTexture(shader = ripple_shader)}/>
    </Box>
    <Box flex-1 h-full p-8 rounded-lg fill="#302331">
        <Image width={100%} height={100%} rounded src={ShaderTexture(shader = plasma_shader)}/>
    </Box>
</Box>
animate(progress, to = 1, duration = 6s, ease = "linear")

Shader inputs

ShaderTexture calls a function that takes ctx: ShaderContext and returns a Color for each texel. ctx.u and ctx.v are normalized coordinates from the top-left. ctx.uv contains both. ctx.resolution gives the texture size in pixels. ctx.aspect is the texture width divided by its height. ctx.dpi gives the viewport DPI.

Bind extra parameters by name, as in plasma(phase = phase). Ebi checks each argument against its parameter type. A bound sig parameter stays live. Ebi samples it each frame and updates the texture on the GPU.

Omit size to render at the image frame's pixel dimensions. Set the image's width and height in the layout. Pass size when you need a fixed-resolution Texture for pixel operations.

Shader inputs support unitless Number, Bool, Color, and fixed-size numeric lists. Shader bodies support arithmetic, comparisons, let, if, return, constant list indices, Rgb, literal Color, RGB channel reads, and direct calls to supported math functions and pure helpers. GPU computations use 32-bit floats. Return normalized finite color channels.

Export WGSL

shader_wgsl(shader = ripple_shader) returns the generated compute source without starting a GPU pass. A WebGPU host can bind an RGBA8 storage texture at group 0 binding 0 and dispatch in 8×8 workgroups. Binding 1 is a read-only buffer of packed 32-bit floats. Start with the DPI, followed by the bound arguments at the offsets named in the generated source. Boolean values use 0 or 1 and colors use straight sRGBA channels.