Skip to content

UnlitMaterial

A material that draws its base color as it is, with no lighting: glTF’s KHR_materials_unlit. Each fragment is base_color times its map’s sample times the vertex color, encoded for the target. Reach for it for UI, sprites, models whose lighting is baked into their textures, and debug views. The glTF loader builds one for every material that carries KHR_materials_unlit.

An UnlitMaterial has two number settings, base_color and alpha_cutoff, and one map, base_color_map(), a MaterialMap that sets its texture, UV set and transform. alpha_mode and double_sided set how it blends and which faces it draws. These calls mean the same as they do on PbrMaterial.

It reads the camera root and no lights, so a Light on its pass costs it nothing and changes nothing. It takes the vertex layout PbrMaterial takes, so a Vertex::pbr mesh, or a mesh the glTF loader built, draws with either material.

A material draws on a Mesh through a Model. Model::new takes an UnlitMaterial as it takes any Material.

Clones share one shader, as with PbrMaterial: a setter on one handle changes what every handle draws. Build a fresh UnlitMaterial::new() for an independent material.

use fragmentcolor::{Mesh, Model, UnlitMaterial, Vertex};
let mesh = Mesh::new();
mesh.add_vertices([
Vertex::pbr([0.0, 0.5, 0.0]).set(Vertex::UV0, [0.5, 1.0]),
Vertex::pbr([-0.5, -0.5, 0.0]).set(Vertex::UV0, [0.0, 0.0]),
Vertex::pbr([0.5, -0.5, 0.0]).set(Vertex::UV0, [1.0, 0.0]),
]);
let badge = UnlitMaterial::new().base_color([1.0, 0.8, 0.1, 1.0])?;
let model = Model::new(mesh, badge);

Sets the alpha threshold AlphaMode::Mask uses. The fragment shader discards a fragment whose alpha, the base color’s times the map’s and the vertex color’s, falls below it. Stored on material.alpha_cutoff; default 0.5, matching glTF 2.0. Opaque and Blend ignore it.

NaN or an infinity returns MaterialError::NotFinite, which names the call, and the material keeps the value it had. The other languages also refuse a number past the 32-bit float range, such as 1e39, with a message like UnlitMaterial.alphaCutoff: value must be a finite number that fits a 32-bit float, got 1e39. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

use fragmentcolor::{AlphaMode, UnlitMaterial};
let sprite = UnlitMaterial::new()
.alpha_mode(AlphaMode::Mask)
.alpha_cutoff(0.3)?;

Set how the renderer treats the alpha of the material’s output, as glTF 2.0’s alphaMode does. It works as PbrMaterial::alpha_mode does:

  • AlphaMode::Opaque (default): blending is off and alpha is ignored.
  • AlphaMode::Mask: the fragment shader discards a fragment whose alpha falls below alpha_cutoff, and the rest draw as opaque. It suits sprites and cut-outs with hard edges.
  • AlphaMode::Blend: depth writes turn off and the color blends over what is behind it. The renderer sorts blended Models back to front by the pass’s Camera.

It is pipeline state, so each mode builds its own pipeline once and reuses it.

use fragmentcolor::{AlphaMode, UnlitMaterial};
let overlay = UnlitMaterial::new()
.base_color([0.0, 0.0, 0.0, 0.5])?
.alpha_mode(AlphaMode::Blend);

Set the color the material draws, in linear RGBA. The base color map’s sample and the vertex color multiply it, and alpha goes through the fragment output unchanged.

Maps to the material.base_color uniform on the underlying shader. Default is [1, 1, 1, 1].

Returns a handle to the same material (Arc-shared), so calls chain.

An item that is NaN or an infinity returns MaterialError::NotFinite, which names the call and the item, and the material keeps the value it had. The other languages also refuse an item past the 32-bit float range, such as 1e39, with a message like UnlitMaterial.baseColor: item 3 must be a finite number that fits a 32-bit float, got 1e39. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

use fragmentcolor::UnlitMaterial;
let red = UnlitMaterial::new().base_color([1.0, 0.2, 0.2, 1.0])?;

The material’s one map, as a MaterialMap handle that sets its texture, UV set and transform. Each fragment draws material.base_color * textureSample(base_color_map, unlit_sampler, uv) * vertex_color, where uv is the UV set the map’s uv picks, through its transform.

A source with no format of its own reads as sRGB in this map, as glTF’s base color does. Unset, the map draws a 1×1 white default, so the material draws its base color alone.

use fragmentcolor::UnlitMaterial;
// Raw pixels with their size upload when the scene loads.
let pixels: Vec<u8> = vec![255, 128, 0, 255];
let material = UnlitMaterial::new();
let map = material.base_color_map().texture((pixels, [1, 1]))?;

Draw both faces of every triangle when true. When false (the default), the renderer culls back faces, the ones whose vertices run clockwise on screen. Mirrors glTF 2.0’s doubleSided.

A sprite or a label on a single quad that the camera can see from behind needs both faces. Like alpha_mode, it is pipeline state.

use fragmentcolor::UnlitMaterial;
let card = UnlitMaterial::new().double_sided(true);

Construct a white, opaque, single-sided UnlitMaterial. Its base color map starts unbound, and the renderer binds a 1×1 white default in its place, so the material draws base_color alone until you set a map through base_color_map(). UnlitMaterial::new can’t fail: the shader parts it composes are compiled into the library.

The shader multiplies the base color, the map’s sample and the vertex color, and encodes the result as sRGB on the Rgba8Unorm and Bgra8Unorm targets FragmentColor creates, as PbrMaterial does. It reads these vertex inputs from the mesh:

  • @location(0) position : vec3<f32>, from Vertex::new([...]) or Vertex::pbr([...]).
  • @location(2) uv0 : vec2<f32>, set as .set(Vertex::UV0, ...).
  • @location(7) color0 : vec4<f32>, set as .set(Vertex::COLOR0, ...), which tints the base color.
  • @location(8) uv1 : vec2<f32>, set as .set(Vertex::UV1, ...), which the map samples on UV set 1.

Vertex::pbr sets each of them to a neutral value. Locations 3 to 6 hold model_0 to model_3, the columns of the Model’s transform.

key value
material.base_color [1, 1, 1, 1]
material.alpha_cutoff 0.5
base_color_map 1×1 white
camera.view_proj identity 4×4
camera.position [0, 0, 0]
use fragmentcolor::UnlitMaterial;
let label = UnlitMaterial::new().base_color([0.1, 0.6, 1.0, 1.0])?;

Borrow the underlying Shader, for a uniform the material’s setters don’t cover. It is the shader the material draws with, so a change reaches every Model that uses the material, and every clone of it.

For the camera, add a Camera to the Pass that draws the material, rather than setting camera.* by hand. The Camera keeps the shader up to date as it moves.

use fragmentcolor::UnlitMaterial;
let material = UnlitMaterial::new();
material.shader().set("material.alpha_cutoff", 0.25)?;