Skip to content

Materials & Textures

Thermion uses Filament’s material system. A material is a shader program compiled from a .mat file; a material instance is a runtime binding of parameters to that material. Every renderable entity has one or more material instances.

FilamentApp.instance (the shared engine facade) can create a couple of ready-to-use material instances without compiling anything:

final unlit = await FilamentApp.instance!.createUnlitMaterialInstance();
final lit = await FilamentApp.instance!.createUbershaderMaterialInstance();
  • Unlit — flat-shaded, ignores lighting. Good for UI overlays and debug geometry.
  • Ubershader — the full PBR lit material (the default glTF material).

Material instances expose typed setters for shader parameters:

await material.setParameterFloat4("baseColorFactor", 0.0, 1.0, 0.0, 1.0); // RGBA green
await material.setParameterInt("baseColorIndex", -1); // -1 = use baseColorFactor, not a texture
await material.setParameterFloat("metallic", 0.8);
await material.setParameterBool("doubleSided", true);

A loaded glTF asset exposes its material instances, and you can replace them:

// Read the current instance at primitive 0
final current = await asset.getMaterialInstanceAt();
// Replace the material on every primitive
await asset.setMaterialInstanceForAll(unlit);
// Or replace at a specific primitive index
await asset.setMaterialInstanceAt(unlit);

Load an image, create a sampler, and bind both to a material parameter:

final image = await FilamentApp.instance!.decodeImage(byteData);
final texture = await FilamentApp.instance!.createTexture(...);
final sampler = TextureSampler(
minFilter: SamplerFilterParams.linear(),
magFilter: SamplerFilterParams.linear(),
);
await material.setParameterInt("baseColorIndex", 0); // use the texture
await material.setParameterTexture("baseColorMap", texture, sampler);

Remember to dispose() textures, samplers, and LinearImages when you’re done with them to avoid GPU memory leaks.

You don’t need a glTF file to render something. viewer.createGeometry builds a renderable from a Geometry and a list of material instances:

final asset = await viewer.createGeometry(
CubeGeometry.cube(),
materialInstances: [unlit],
);

Built-in geometries include CubeGeometry, SphereGeometry, CylinderGeometry, ConeGeometry, CapsuleGeometry, PyramidGeometry, QuadGeometry, and PlaneGeometry.

For custom shaders, write a Filament .mat file. You have two ways to get it into a loadable .filamat package: let the build hook compile it automatically (recommended), or run matc yourself.

Section titled “Automatic compilation in the build hook (recommended)”

Point Thermion at your own .mat sources with the material_compilation user define in your app’s pubspec.yaml:

hooks:
user_defines:
thermion_dart:
material_compilation:
inputs: # files, directories or globs, relative to the app root
- materials/skin.mat
- materials/eye
output: build/materials # any directory, relative to the app root; created if missing
backends: all # optional: all (default) | native | web | explicit matc -a list
resgen: false # optional: also emit .c/.h
link_generated: false # optional: also compile the emitted .c into libthermion_dart (native only)

On every build the hook:

  1. Expands inputs to .mat files (a directory is scanned recursively, a glob like materials/**/*.mat is matched against files; anything that resolves to nothing fails the build with the input named).
  2. Reads build/materials/.thermion-materials.json, the manifest recording what was compiled, by which toolchain, from which inputs.
  3. Compiles exactly what is stale with the version-matched matc — and skips everything (no download, no tool run) when nothing changed.

The result is one blended .filamat per material — a single package carrying every requested backend, with one stable filename, so loading it at runtime (FilamentApp.createMaterial(Uint8List data) after reading the file) works on every platform.

The manifest records a matcId: a SHA-256 over the pipeline version, the Filament tag pinned in filament.version, the thermion_dart package version, and the host platform/arch. The sha256 of the downloaded matc binary is recorded separately as matcSha256. When either changes — for example a Filament or Thermion bump ships a new matc — every material is recompiled, even if no .mat file changed. That is the whole point: a blob built by an older matc is never silently kept. Otherwise:

  • a material or transitive quoted include changed, a material was added, or a recorded output is missing or has different bytes → that material recompiles;
  • an input was removed from the config → its outputs are deleted;
  • everything matches → the hook logs why it skipped and does nothing else.

matc (and resgen when resgen: true) are downloaded from Thermion’s R2 bucket for the host platform. The hook selects the release archive through the shipped filament.libs.json pin and verifies its full SHA-256 before extracting bin/. Pinned archives use immutable hash-suffixed URLs; a pin change selects a separate tool cache and rebuilds the app’s material packages. The cache lives under:

${XDG_CACHE_HOME:-$HOME/.cache}/thermion/filament-tools/filament-<tag>-webgpu-<host>-<hash16>/

Without a pin entry, the hook falls back to the legacy archive URL and omits the hash suffix from the cache directory.

The cache is shared by all projects on the machine and is trusted only after matc passes a -a webgpu smoke test. Installation holds a shared cache lock across app builds. Downloads and extraction use a private temporary directory; only a complete installation that passes the smoke test is renamed into the live cache. A failed download leaves no partial installation. These builds of matc support WebGPU (WGSL) — the official Filament release binaries do not.

  • all (default): -a metal -a vulkan -a opengl -a webgpu — one package that works everywhere Thermion runs.
  • native: metal + vulkan + opengl (no WebGPU).
  • web: opengl + webgpu (what a browser can load).
  • an explicit list, e.g. [vulkan], passed straight to matc -a.

With resgen: true, each .filamat is additionally converted to C:

resgen -c -p <prefix> -x <output>/ <stem>.filamat

producing <prefix>.c and <prefix>.h beside the .filamat (plus whatever else resgen emits). The prefix defaults to the sanitised material name (non [A-Za-z0-9_] characters become _, so my-eye.mat → my_eye); override it per material with:

resgen_prefixes:
skin: APP_SKIN

Because every material is one blended package, there is exactly one .c/.h per material and no per-variant symbol collision. Like Thermion’s own materials pipeline, the emitted .c gets a prepended #include of its own header (so the package symbol has external C linkage when the file is compiled as C++) and the header’s extern "C" block gets __cplusplus guards (so it is also valid C) — without the include, a C++ compile gives the const array internal linkage and the optimiser drops the unreferenced symbol entirely. Public declarations also get explicit export annotations: default visibility on Android/ELF and dllexport on Windows.

resgen: true only writes the .c/.h files; nothing compiles them, so they are only usable if you build your own native library. With link_generated: true the build hook additionally compiles the emitted .c files straight into libthermion_dart — the exact same mechanism Thermion uses for its own materials under native/include/material/. Each generated .c is appended to the native build’s source list and the output directory is added to its include path (so skin.c resolves #include "skin.h"), before the native library is compiled and linked in the same build.

Notes:

  • link_generated: true implies resgen emission — you never set both.
  • The generated symbols (<PREFIX>_PACKAGE[], e.g. SKIN_PACKAGE) are exported by libthermion_dart, so they are resolvable from Dart FFI through the default code asset — no separate asset id or loading step.
  • Native only. Web builds compile their material C sources into the wasm engine via CMakeLists, and a web build with link_generated: true fails with an error explaining this — remove the flag for web targets.
  • Symbol collisions fail the build at plan time. Two materials whose prefixes sanitise to the same symbol (or a material whose prefix matches one of Thermion’s built-in materials — grid, image, gizmo, …) would define the same <PREFIX>_PACKAGE symbol and fail at link time with an obscure error; the hook catches this before compiling anything and names both inputs. Give one of them a distinct resgen_prefixes entry.
  • Only the .c files the materials manifest vouches for are linked — stale leftovers in the output directory are never picked up, and the manifest records whether the generated sources were linked.

All materials compile in staging before any output is published. If a tool fails, the previous generation remains intact. Publication keeps backups and writes the manifest last; a publication error restores the previous generation, and the next hook run recovers any publication interrupted by process exit. Stale cleanup never deletes a filename owned by a current material. Before compiling, the hook rejects output filenames shared by different inputs, including collisions introduced by resgen_prefixes with cached materials.

The hook registers discovery directories, transitive include files, the manifest and generated files as build dependencies. New directory/glob matches and deleted or changed outputs therefore invalidate the build-runner cache. Quoted includes resolve relative to the file that contains them, including nested includes. Failures are loud, never silent skips:

  • No network and no cached tools: the build fails naming the R2 URL and the Filament version.
  • matc/resgen exit non-zero: the build fails with the tool’s stderr.
  • Bad configuration (unknown key values, an input that resolves to nothing, two inputs with the same output name or the same resgen symbol, link_generated on a web build): the build fails with an actionable message.

If material_compilation is absent, the hook does nothing new.

matc is part of the Filament SDK; a version-matched pair for Linux, macOS and Windows is also what the hook downloads above. From the project root:

make materials

This requires the FILAMENT_PATH environment variable to point at a local Filament build’s out/release/bin (see the Filament page). That target is for Thermion’s own bundled materials (materials/, e.g. image.mat, grid.mat, silhouette.mat), whose compiled C variants are committed under thermion_dart/native/include/material/ — it is unrelated to your app’s materials, which are runtime assets unless you opt into link_generated.

The hook’s getLibDir() downloads the prebuilt Filament libraries. CBuilder builds libthermion_dart locally, linking those libraries and compiling Thermion’s committed material arrays under native/include/material/.

Instead of precompiling .filamat/.uberz files, apps that need to generate materials at runtime (user-authored or procedural shaders) can compile GLSL material() source directly on the device with FilamentApp.compileMaterialSource:

final material = await FilamentApp.instance!.compileMaterialSource(
'my_material',
'''
void material(inout MaterialInputs material) {
prepareMaterial(material);
material.baseColor.rgb = float3(1.0, 0.0, 0.0);
}
''',
shading: MaterialShading.lit,
);

Full .mat packages with parameters and includes

Section titled “Full .mat packages with parameters and includes”

compileMaterialSource only accepts the bare material() function. For a complete .mat source — the same format matc accepts, with a material { ... } configuration block and optional vertex { ... } code — use FilamentApp.compileMaterialPackage:

final material = await FilamentApp.instance!.compileMaterialPackage(
'my_material',
'''
material {
name : "MyMaterial",
shadingModel : unlit,
parameters : [ { type : float4, name : baseColor } ]
}
fragment {
#include "common.glsl"
void material(inout MaterialInputs material) {
prepareMaterial(material);
material.baseColor = tint(materialParams.baseColor);
}
}
''',
includes: {
'common.glsl': 'vec4 tint(vec4 c) { return c * vec4(1.0, 0.9, 0.8, 1.0); }',
},
);
final instance = await material.createInstance();
await instance.setParameterFloat4('baseColor', 1.0, 0.0, 0.0, 1.0);

#include "name" directives — in the source and in included files — are resolved against the includes map by exact name; there is no filesystem access, and an unresolvable include fails the compile. Everything else (shading model, parameters, blending, variants, …) comes from the .mat source itself. This uses matp, the same parser that backs matc.

Both APIs are an opt-in build feature: they link Filament’s libfilamat (which bakes in glslang and SPIRV-Tools, adding roughly 8–45 MB) and, for compileMaterialPackage, libmatp. Enable them with a runtime_materials: true user define under hooks.user_defines.thermion_dart in your app’s pubspec.yaml; FilamentApp.runtimeMaterialsSupported reports whether the loaded build has it (otherwise both APIs throw UnsupportedError). With WebGL2 the flag selects an engine with filamat (~20 MB larger than the default wasm). WebGPU and hybrid builds use precompiled materials. See Choose a backend.

iOS device and simulator builds support the same flag and target Metal with the mobile shader model. Native compilation runs on Thermion’s job thread; web compilation runs inline on the calling thread. Compilation is CPU-intensive (hundreds of milliseconds).

A runnable web example — a live GLSL material editor that compiles and applies materials in the browser — is in examples/dart/material_editor. Material swapping, parameter setting, and texture binding with precompiled materials: examples/flutter/materials_and_textures.