Log inGet started
World

Builtin Library

Branchmain
Branchesmainpublish
Commit0a13e47b57

README.md

Post effect (asset type)

A post effect is a post-process shader on the screen, as an asset: one post-process shader with its values, its order and its layer in the post chain, described in postEffect.yaml. It implements applicable: applying it puts that shader in the chain for the applier.

Making one

A post effect names a post-process shader and states the values it runs with: a shader of the world's own, or any post-process shader a world can reach. A holder (a setup, a sky, a PostProcess entry) names the post effect and may state values of its own for it, which are that holder's alone:

# <setup>.sceneSetup / sceneSetup.yaml
members:
  - id: grade
    source: "./grade.postEffect"
    values: { saturation: 0.82 }

asset.list({ type = "postEffect" }) lists the post effects a world holds, and ref:inputs() names the values each takes.

Where it lives

  • Source: <name>.postEffect/
  • Identity: <name> (the .postEffect suffix strips from the identity)
  • Folder shape:
    • postEffect.yaml: the effect. Required.
    • README.md: what it does to the picture. Required.
    • .metadata: description and tags. Required.
# <name>.postEffect / postEffect.yaml
shader: "<post-process shader>"             # any post-process shader
layer: scene                                # scene | all | "" (the shader's own space)
order: 120
values: { intensity: 0.3 }

Writing its shader

The shader a post effect names is a post-process shader: one fn fragment(in: PostInput) -> vec4<f32> over the rendered frame, its properties in properties.yaml. Besides in.uv and in.frag_coord, it reads:

  • zero_sample_scene(uv): the rendered scene colour at uv;
  • zero_scene_depth(uv): the scene depth at uv (reversed: 1 is the near plane, 0 the far plane and empty sky), and zero_linear_depth(d, near, far) for it in metres; zero_scene_motion(uv), the per-pixel motion;
  • engine.time: x the elapsed seconds, y the frame's dt, z the frame index: an effect that moves (a scan line, a pulse, drifting grain) animates by it;
  • engine.resolution: x and y the target's width and height, z and w their reciprocals;
  • engine.view_proj, engine.prev_view_proj, engine.inv_view_proj: the camera the frame was drawn from, this frame and the last.

The shader type's README, "Post-process & screen shaders", has the whole surface: every helper, multi-pass shaders and the space an effect declares.

Parameters

Its parameters are its shader's properties, each with the Field descriptor of its kind, defaulting to what values states (else the property's own default):

Property kindField
float with a min or max, rangeField.range
floatField.number
float2 / float3Field.vec2 / Field.vec3
float4a four-number Field.struct (x, y, z, w)
colorField.color
boolField.bool
textureField.resource("texture")

and the chain's two inputs every post effect has: order (where it runs in the post chain, lower first) and layer (scene, all, or empty for the shader's own space). A shader property of a kind a post effect does not take (texture_array, buffer), or one named order or layer, is refused by name (postEffect.definition from asset.validate, and an error from ref:inputs() and apply).

Applying it

Each application puts the shader in the post chain under the name "<asset guid>/<owner>" (ref:effectName(owner) answers it), at its effective order and layer, with the effective values written to its properties: the asset's saved values, its live values (appliedSystem.setLive), then what the owner states. Applying it again for the same owner changes the values of that running application: ref:as("applicable"):apply(owner, { intensity = 0.5 }) writes the new value to the standing entry, and a change of order or layer registers it again. Turning the application off disables the entry; dropping it removes it. Post effects combine in chain order, so every application of every post effect draws at once.

The entry is registered under the component origin, so a scene freeze never turns an applied post effect into scene content: what puts it in force (a setup, a PostProcess entry, code) is what the scene keeps.

In a PostProcess region

An effects entry of PostProcess names either a shader (registered as a plain chain entry) or a post effect in effect, exactly one. An effect entry is an application of the post effect, owned by "<entity id>/effects/<index>", with the entry's values and textures as its stated values, its layer stated when not empty, and the region's priority added to the effect's order. A post effect applied by a setup and the same one named in a PostProcess entry draw the same, and a live change reaches both.