Custom shaders
Write a WGSL shader for Lightair's Custom shader effect, with main_image, the functions the prelude gives you, and the effect's Amount setting.
In short
- Add Custom shader from an object's Effects in the Inspector and pick a
.wgslfile. - The file defines
fn main_image(uv: vec2<f32>) -> vec4<f32>, which returns each pixel's colour. img(uv)reads the picture,time()gives seconds,u.sizethe size in pixels. Colours are premultiplied.- Lightair compiles the file again whenever it changes on disk.
In the appInspector › Effects › Add effect › Custom shader
The Custom shader effect runs your own WGSL shader on an object’s picture, in its place among the object’s other effects.
Add a shader
Section titled “Add a shader”- Write a
.wgslfile (see The file). - Select the object on the canvas.
- In the Inspector’s Effects, click Add effect and choose Custom shader (under Stylize).
- Set Shader file to your
.wgslfile. - Set Amount: 0% leaves the picture unchanged, 100% shows the shader’s result alone.
Keep the file open in your editor: each time you save it, Lightair compiles it again on the next frame.
The file
Section titled “The file”Define one function:
fn main_image(uv: vec2<f32>) -> vec4<f32> { let c = img(uv); let g = luma(unpremul(c)); return vec4<f32>(vec3<f32>(g) * c.a, c.a);}It runs for every pixel of the object’s picture and returns that pixel’s colour.
uvgoes from 0 to 1, from the top left of the picture.- Pictures are premultiplied RGBA: colours already multiplied by alpha. Return premultiplied colour too.
- The result is written as 8 bits per channel, so values outside 0 to 1 are clipped.
The file may define its own helper functions and constants. It mustn’t declare bindings, entry
points (@vertex, @fragment), or any name the prelude already uses.
The prelude
Section titled “The prelude”These come before your file:
| Name | What it is |
|---|---|
img(uv) |
The picture as it reaches the shader, after the effects before it, sampled smoothly |
img_point(uv) |
The same, nearest pixel |
src(uv) |
The object’s picture before any of its effects |
time() |
Seconds, for movement |
u.size |
The picture’s size in pixels (vec2<f32>) |
u.texel |
One pixel in uv units (1 / u.size) |
luma(rgb) |
The brightness of a colour |
unpremul(c) |
The straight (not premultiplied) colour of a sample, as vec3<f32> |
hash12(p) |
A pseudo-random number from 0 to 1 for a vec2<f32> |
to_square(uv), from_square(p) |
uv to a space centred on the picture with square pixels (x scaled by the aspect ratio), and back |
gauss9(uv, dir) |
A 9-tap Gaussian blur of img along dir, in pixels per step |
sat(x), sat3(x), sat4(x) |
x clamped to 0…1, for a f32, vec3<f32> or vec4<f32> |
Names already taken: u, U, image, source, linear_clamp, point_clamp, vs, VOut,
fs_custom, and the functions above.
Examples
Section titled “Examples”A wave that moves across the picture:
fn main_image(uv: vec2<f32>) -> vec4<f32> { let shift = sin(uv.y * 40.0 + time() * 3.0) * 4.0 * u.texel.x; return img(vec2<f32>(uv.x + shift, uv.y));}Scanlines, with a constant of its own:
const LINE_PX: f32 = 3.0;
fn main_image(uv: vec2<f32>) -> vec4<f32> { let c = img(uv); let row = floor(uv.y * u.size.y / LINE_PX); let shade = select(1.0, 0.7, row % 2.0 == 0.0); return vec4<f32>(c.rgb * shade, c.a);}A soft glow, blurring across and down and adding the blur back:
fn main_image(uv: vec2<f32>) -> vec4<f32> { let blur = (gauss9(uv, vec2<f32>(3.0, 0.0)) + gauss9(uv, vec2<f32>(0.0, 3.0))) * 0.5; let c = img(uv); let a = max(c.a, blur.a); return vec4<f32>(min(c.rgb + blur.rgb * 0.6, vec3<f32>(a)), a);}When it doesn’t compile
Section titled “When it doesn’t compile”The effect is left out, so the picture shows as if it were off, and the compiler’s error is written to Lightair’s log, once each time the file changes. Fix the file and save it: the next frame compiles it again.
Only .wgsl files are drawn. OBS Shaderfilter’s .shader and .effect files aren’t.
Settings
Section titled “Settings”The effect has two settings:
| Setting | Key | What it is |
|---|---|---|
| Shader file | path |
The .wgsl file’s path on this computer |
| Amount | amount |
0 to 1: how much of the shader’s result replaces the picture |
The effect’s kind is lightair_shader. A shader can’t have settings of its own beyond Amount,
read another object’s picture, or run in more than one pass.