# Materials and shaders

> Aego 0.12.5 · development; docs build `4a32b816933e02930662f6a443cb25abe6050d090c55e237cbff5c44ae9b25f8`. [Build manifest](/docs-build.json).

A material is a shared asset. Assign it to [SpriteRenderer](/ref/engine.SpriteRenderer.md) or [Mesh2D](/ref/engine.Mesh2D.md), then add [MaterialOverride](/ref/engine.MaterialOverride.md) for values that belong to one node. The effective value is the shader default, then an explicit material value, then an explicit node override. Play and preview own separate node state.

## Create and edit

1. Choose **Shader**, **Material**, or **Material from Shader** in the searchable **Create** palette. The editable sources are [.aeshader](/formats/aeshader.md) and [.aemat](/formats/aemat.md).
2. Select the material in **Assets** and choose its shader, variant, blend, parameters and textures. Saving a shared material affects every consumer of that asset.
3. Assign it to the intended SpriteRenderer or Mesh2D. Save the material source and the scene containing that assignment separately.
4. Add **Material Override** when only this node should differ. Change a declared instance parameter, compare a second node using the same material, then use **Reset** to restore inheritance.

The material source and its Inspector share one undo history. Dragging one parameter produces one undo step. Reset restores inheritance even when an explicit value happened to equal the inherited value; it is not an assignment of zero.

Select a sprite, assign its Material field and add **Material Override**. Only numerical parameters marked `@instance` can be overridden. Textures, variants and blend belong to the material asset. Removed fields remain serialized as unknown values; unresolved references keep their AssetID.

## Portable shader

Author GLSL 330 with Aego metadata. The importer prepares and validates GLSL 330 and GLSL ES 3.00 sources. It resolves `engine/` includes and project includes below `assets/shaders/include/`. Public fields come from declarations, so an optimized-out parameter stays in the Inspector.

```glsl
#pragma aego abi 1
#pragma domain sprite
#include "engine/sprite.glsl"
#pragma material
uniform float Opacity; // @instance @default(1) @range(0, 1)
#pragma end
void fragment() {
    aego_out = aego_sample() * Opacity;
}
```

`aego_sample()` already returns linear, premultiplied color. Additional opacity scales the entire returned `vec4` once. Ordinary `vec4` values are numerical; only parameters marked `@color` use Color semantics. Sample a separate noise texture with `aego_local`, independent of the sprite's atlas location. `aego_sample_at` clamps to this sprite's region and returns transparent outside it.

Sprite and Mesh2D are separate shader domains. A material must match its consumer's vertex layout. Frame time belongs to the current world, View data to the current camera, and material values to their committed revision. A custom vertex stage disables CPU culling for its Sprite or Mesh2D consumer. The CPU does not analyze GLSL to guess vertex displacement bounds.

## Animate and reload

Tween and Animation address a declared instance parameter through a property path. The final segment is the parameter's name from the shader schema, for example:

```text
MaterialOverride.Flash
MaterialOverride.Threshold
```

Bindings refresh when a committed layout generation changes. Numeric integer animation keys retain signed integer precision; float tweens use float interpolation.

Save a shader or an include to import a candidate. Compile, link, layout validation and dependent material migration must all succeed before the next generation is committed at a frame boundary. An error retains the previous program, schema and values. The Problems panel opens the original source or include line; a column is omitted when the compiler did not provide one.

A failed current shader blocks strict export even when the scene still renders its last-good version. Save dirty shader/material/include buffers before exporting. Exports contain prepared shader and material artifacts; players do not need the importer or glslang.

## Demonstration

`examples/materials` contains the full project workflow: translucent A/B/A sprites sharing an atlas, adjacent instances with different overrides, a Mesh2D, two cameras, a Flash tween and a Dissolve animation. Its README gives the edit, undo, reload and native/web export steps. The Inspector preview uses a separate material and transparent offscreen target; a session without a GPU displays its reason instead of pretending a render succeeded.
