> For the complete documentation index, see [llms.txt](https://docs.aethercube.fr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aethercube.fr/developers/content-packs/shaders.md). Agents can also search and read this documentation through its [MCP server](https://docs.aethercube.fr/~gitbook/mcp).

# Shaders

This page is for the pack author who wants to change how the world looks. A pack can do it with two kinds of shader, both written in WGSL, the shading language of WebGPU.

| Kind          | Declared in `pack.json` | Effect                                                            |
| ------------- | ----------------------- | ----------------------------------------------------------------- |
| Terrain hook  | `terrain_shader`        | Changes the colour of terrain and of every voxel model.           |
| Screen effect | `screen_effects`        | Draws over the whole 3D view, when a plugin shows it to a player. |

## They are functions

A pack shader is not a full shader. It is one or two WGSL **functions**. The client wraps them in its own code, which provides the bindings, the entry point and the types. You write the function bodies and any helpers.

Before it uses a shader, the client compiles the wrapped result the way the renderer does: through WESL, then through naga validation with no optional GPU capability. A shader that does not parse or validate is skipped with a warning in the client log. The rest of the pack still applies.

## Terrain hook

`terrain_shader` points to a file that defines both functions with exactly these signatures:

```wgsl
// Before lighting: the surface colour.
fn surface(color: vec4<f32>, world_position: vec3<f32>, world_normal: vec3<f32>, time: f32) -> vec4<f32> {
    return color;
}

// After lighting: the lit colour.
fn lit(color: vec4<f32>, world_position: vec3<f32>, time: f32) -> vec4<f32> {
    return color;
}
```

* `time` is in seconds and wraps every hour.
* `lit` receives the colour after lighting and exposure, before tone mapping, so it can exceed 1.
* Both functions are required. Return `color` unchanged from the one you do not need.
* The hook applies to terrain and to every voxel model.
* When several packs declare a terrain hook, the last one wins, with a warning.

## Screen effect

A file listed under `screen_effects` defines one function:

```wgsl
fn effect(uv: vec2<f32>, color: vec4<f32>, fx: Effect) -> vec4<f32> {
    return color;
}
```

The client provides the struct:

```wgsl
struct Effect {
    intensity: f32,
    time: f32,
    resolution: vec2<f32>,
    params: array<vec4<f32>, 2>,
}
```

| Part            | Meaning                                                                          |
| --------------- | -------------------------------------------------------------------------------- |
| `uv`            | Position on the screen, 0 to 1.                                                  |
| `color`         | The scene colour at `uv`.                                                        |
| `fx.intensity`  | The effect's strength, already faded.                                            |
| `fx.time`       | Seconds.                                                                         |
| `fx.resolution` | The view size in pixels.                                                         |
| `fx.params`     | The eight numbers of `ScreenEffect.params`, from `params[0].x` to `params[1].w`. |

The function may call `ac_scene(uv)` to sample the scene at another point, for a blur or a distortion. The client blends your result with the scene by `intensity`.

One screen effect shows at a time. A new one fades from the old one over `fade_seconds`. An unknown or empty effect id turns the effect off, and leaving the server turns it off. A plugin shows an effect with `ScreenEffects.show`. See [UI](https://docs.aethercube.fr/developers/plugins/ui.md).

A complete example is `examples/skyglass/src/main/resources/content/shaders/updraft.wgsl`:

```wgsl
fn effect(uv: vec2<f32>, color: vec4<f32>, fx: Effect) -> vec4<f32> {
    let strength = select(1.0, fx.params[0].x, fx.params[0].x > 0.0);
    let top = clamp(1.0 - uv.y, 0.0, 1.0);
    let ripple = sin(uv.x * 38.0 + fx.time * 1.7) + sin(uv.x * 17.0 - fx.time * 1.1);
    let shifted = vec2<f32>(uv.x + ripple * 0.0012 * top * strength, uv.y + 0.004 * top * strength);
    let scene = ac_scene(shifted);
    var rgb = mix(color.rgb, scene.rgb, 0.6);
    rgb = rgb * (1.0 + 0.12 * top * top * strength);
    return vec4<f32>(rgb, color.a);
}
```

## Rules

The same rules apply to both kinds. They are there so that a server cannot ship a shader that hangs the player's GPU or reaches outside the hook I provide.

| Forbidden                                                           | Why                                                                 |
| ------------------------------------------------------------------- | ------------------------------------------------------------------- |
| `@group`, `@binding`                                                | The client owns the bindings.                                       |
| `@vertex`, `@fragment`, `@compute`                                  | No entry point.                                                     |
| `import`, `enable`, `requires`, `diagnostic`                        | No imports and no directives.                                       |
| `@if`, `@elif`, `@else`, `@publish`                                 | No WESL conditionals or visibility.                                 |
| `loop`, `while`                                                     | Loops must end.                                                     |
| `ptr`, and `&` used to take an address (not `&&`, not a binary `&`) | So that the loop counter cannot be written through a pointer.       |
| `f16`, `f64`, `i64`, `u64`                                          | Only the number types every GPU has.                                |
| A declared name that starts with `ac_`                              | Reserved for the wrapper. Only a screen effect may call `ac_scene`. |

Limits:

* At most 128 KiB of source, and at most 16 384 tokens, comments not counted.
* Brackets (`{`, `(`, `[` and the `<` of type arguments) nest at most 32 deep.
* A statement, between `;`, `{` and `}`, holds at most 1024 tokens.
* At most 32 `else if` in a row, and at most 4 prefix operators in a row (`- - - - x` is too many).
* A `for` loop must read `for (var i = 0; i < 16; i++)`, or step with `i += K` where `K` is a whole number of at least 1, and may use `<=`. The start and end are whole-number literals of at most 64.
* The counter is written in the `for` header only. The body may read it anywhere, but any assignment to it is refused: `i = 0`, `i += 1`, `i <<= 1`, `i++`.
* `for` loops nest at most 3 deep. A loop inside a function that is called counts too: two loops around a call to a function that holds two loops are 4 deep, and refused.
* The estimated work of the entry function must stay under a fixed budget. Calls count as inlined and a loop counts 64 times. A shader that is too heavy is skipped.

The client checks these rules against the tokens, with comments removed, before WESL or naga read the text. A hostile source is refused without being compiled.

## What the server checks

When it loads a pack, the server runs a textual check of the same rules: the file must exist and end in `.wgsl`, stay under 128 KiB, and follow the rules above: the forbidden attributes and words, `ptr` and `&`, the token limits, the loop header, the counter and the nesting of the `for` loops, and no `ac_` name. The server reads the same tokens with the same rules as the client. A pack that fails is refused with a log line, so you see the error and your players do not. A file that several effects name is checked once.

The server does not compile the shader. What only the compiled form shows is left to the client: the types, the cost, and loops hidden behind calls. A shader can pass the server's check and still be skipped by a client that validates it, with a warning in the client log.

To test a shader, load the pack on a server, join with a client and read the client log.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.aethercube.fr/developers/content-packs/shaders.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
