Skip to content

Materials

The MaterialService manages the lifecycle and data of materials. A Material is an instance of a shader with specific property values.

A lightweight object holding:

  • Reference to a ShaderVariantSet.
  • Property Block: Uniform data (colors, floats) matching the shader’s uniform buffer.
  • Texture Bindings: References to GpuTexture objects.

Materials use a Handle-Slot pattern.

  • Materials are stored in a fixed-size pool.
  • References (RefObject<Material>) store a handle with a Generation ID.
  • This prevents use-after-free bugs if a material is destroyed and its slot is reused.
  1. Texture Loading: Texture AssetIDs are resolved asynchronously via TextureService.
graph TD
    Data["MaterialData (Canvas)"] --> Service[MaterialService]
    Service --> Create["Allocate Material Slot"]
    Create --> Resolve["Resolve Textures"]

    Resolve --> Check{"In Cache?"}
    Check -->|Yes| Bind[Bind GpuTexture]
    Check -->|No| Fetch["AssetLoader (Details)"]
    Fetch --> Decode["Decode & Upload"]
    Decode --> Bind