Renders a WGSL fragment shader with WebGPU.
Put your fragment shader in a <script type="wgsl"> child. Because script content is
opaque to the HTML parser, angle brackets and generics like mat2x2<f32> need no
escaping. A uniforms binding and a full-screen vertex stage are injected for you, so
you only write the fragment:
<orb-shader-canvas>
<script type="wgsl">
@fragment
fn main(@builtin(position) pos: vec4f) -> @location(0) vec4f {
let uv = pos.xy / vec2f(800.0, 600.0);
return vec4f(uv, 0.5 + 0.5 * sin(uniforms.time), 1.0);
}
</script>
</orb-shader-canvas>
HTML
WGSL
<orb-shader-canvas>
<script src="/stars.wgsl" type="wgsl"></script>
</orb-shader-canvas>
fn rot(a: f32) -> mat2x2f {
let c = cos(a);
let s = sin(a);
return mat2x2f(c, s, -s, c);
}
fn modv(x: vec3f, y: vec3f) -> vec3f {
return x - y * floor(x / y);
}
@fragment
fn main(@builtin(position) fragCoord: vec4f) -> @location(0) vec4f {
let res = uniforms.resolution;
var uv = fragCoord.xy / res - vec2f(0.5);
uv.y *= res.y / res.x;
var dir = vec3f(uv * 0.8, 1.0);
let t = uniforms.time * 0.01 + 0.25;
let a1 = 0.5 + uniforms.mouse.x / res.x * 2.0;
let a2 = 0.8 + uniforms.mouse.y / res.y * 2.0;
let r1 = rot(a1);
let r2 = rot(a2);
let dxz = r1 * dir.xz;
dir.x = dxz.x; dir.z = dxz.y;
let dxy = r2 * dir.xy;
dir.x = dxy.x; dir.y = dxy.y;
var origin = vec3f(1.0, 0.5, 0.5) + vec3f(t * 2.0, t, -2.0);
let fxz = r1 * origin.xz;
origin.x = fxz.x; origin.z = fxz.y;
let fxy = r2 * origin.xy;
origin.x = fxy.x; origin.y = fxy.y;
let formuparam = 0.53;
let tile = 0.85;
let brightness = 0.0015;
let darkmatter = 0.3;
let distfading = 0.73;
let saturation = 0.85;
var s = 0.1;
var fade = 1.0;
var v = vec3f(0.0);
for (var r = 0; r < 20; r = r + 1) {
var p = origin + dir * (s * 0.5);
p = abs(vec3f(tile) - modv(p, vec3f(tile * 2.0)));
var pa = 0.0;
var a = 0.0;
for (var i = 0; i < 17; i = i + 1) {
p = abs(p) / dot(p, p) - formuparam;
a += abs(length(p) - pa);
pa = length(p);
}
let dm = max(0.0, darkmatter - a * a * 0.001);
a = a * a * a;
if (r > 6) { fade *= 1.0 - dm; }
v += vec3f(fade);
v += vec3f(s, s * s, s * s * s * s) * a * brightness * fade;
fade *= distfading;
s += 0.1;
}
let vlen = length(v);
v = mix(vec3f(vlen), v, saturation);
return vec4f(v * 0.01, 1.0);
}
Available uniforms (WGSL struct Uniforms, bound at @group(0) @binding(0)):
resolution: vec2f — framebuffer size in pixels
time: f32 — seconds elapsed (pauses with paused)
timeDelta: f32 — seconds since the previous frame
mouse: vec4f — xy current pointer, zw last press position (framebuffer pixels, top-left origin)
frame: f32 — frames rendered
| Description |
Pixel density multiplier applied to the backing store. Increase for sharper output. |
| Attribute |
dpr |
| Type |
number |
| Default |
1 |
| Description |
Name of the fragment entry point in the supplied shader. |
| Attribute |
fragment-entry |
| Type |
string |
| Default |
"main" |
| Description |
Backing-store height in pixels. When unset, the canvas fills its container. |
| Attribute |
height |
| Type |
number |
| Default |
undefined |
| Description |
Freezes time and stops the render loop while keeping the last frame on screen. |
| Attribute |
paused |
| Type |
boolean |
| Default |
false |
| Description |
Backing-store width in pixels. When unset, the canvas fills its container. |
| Attribute |
width |
| Type |
number |
| Default |
undefined |
| Description |
Fires once the GPU device is ready and the first frame has rendered. |
| Type |
CustomEvent<void> |
| Description |
Fires when the shader fails to compile/validate or WebGPU is unavailable. |
| Type |
CustomEvent<ShaderErrorDetail> |
| Description |
Read the shader source currently in use. |
| Signature |
getShader() => Promise<string> |
| Description |
Pause the render loop, keeping the last frame on screen. |
| Signature |
pause() => Promise<void> |
| Description |
Resume the render loop (equivalent to paused = false). |
| Signature |
play() => Promise<void> |
| Description |
Reset elapsed time and the frame counter to zero. |
| Signature |
reset() => Promise<void> |
| Description |
Replace the shader source at runtime, recompiling the pipeline. |
| Signature |
setShader(source: string) => Promise<void> |
| Parameters |
source (string) |
| Part |
Description |
"canvas" |
|
"error" |
|