ย้อนกลับ
· อ่าน 2 นาที
Shaders and materials
Shaders in WESL linked to WGSL at run time, material assets whose params are read off the shader, and relinking on save.
- Dragos Balaur
Shaders are written in WESL and linked to WGSL at run time. A material asset names one and its params are read off the shader, saving a shader relinks it in place, and there are debug channels to see what a pass is doing.
- WESL is WGSL plus imports,
@ifvariants and dead-code elimination, linked to WGSL at run time. A 2D shader importspackage::sprite; a 3D one importspackage::meshand callsshade(in)for the scene's lights. - A
materialasset names a shader and its[params], which are read off the linked shader.sprite,shape2d,shape3dandmeshtake one, and a node without one draws with the built-in material. - Saving a shader relinks every material naming it and rebuilds every node drawing with it. A shader that will not link is reported with its source line, and the node keeps the built-in material.
render::set_channel("uv")draws the scene's UVs.normals,depthandalbedoare the other channels.
// shaders/glow.wesl
struct Params { speed: f32, rings: f32, tint: vec4<f32> }
@group(3) @binding(0) var<uniform> params: Params;
@vertex fn vs_main(in: VertexInput) -> VertexOutput {
return vertex(in);
}
@fragment fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let uv: vec2<f32> = in.uv;
let dist: f32 = length(uv - vec2<f32>(0.5, 0.5)) * 2.0;
let wave: f32 = 0.5 + 0.5 * sin(dist * params.rings * 6.2832 - time() * params.speed);
let ramp: vec4<f32> = params.tint * wave * wave;
let albedo: vec4<f32> = sample_albedo(uv) * tint(in);
return albedo + ramp * albedo.a;
}
# materials/glow.toml
type = "material"
shader = "shaders/glow.wesl"
[params]
speed = 2.0
rings = 5.0
tint = "#ffb347"
A shader is outside the digest. time() is render time, not the tick. A
function marked @const runs on the CPU in headless tests.
examples/shaders is a glow material on a sprite beside a plain one. It
runs in the browser.
Shaders and materials in the manual.