Skip to content

ToonMaterial

A cel-shaded material: each light lights it in flat steps rather than a smooth gradient, with an optional hard highlight and a rim along the edges the camera sees. The look reads as drawn rather than photographed, and it holds up on simple meshes and flat colors.

Each light’s diffuse term is cut into bands even steps, three by default, or read from a ramp_map() gradient wider than one texel. A highlight color adds a hard spot where the light glints, and a rim color marks the faces seen edge-on. Both start black, which turns them off.

base_color, base_color_map(), alpha_cutoff, alpha_mode and double_sided mean the same as they do on PbrMaterial and UnlitMaterial.

It reads the camera and lights roots PbrMaterial reads, through the same light loops, so the Lights on its pass light it as they light a PBR model, at the same brightness, and the scene’s ambient light fills its shadows. It takes the vertex layout PbrMaterial takes, so a Vertex::pbr mesh, or a mesh the glTF loader built, draws with any of the three. An outline is not a material setting: it needs a draw of its own. The toon_outline example in examples/rust draws one as a pass after the scene, from the depth and normals an earlier pass draws.

A material draws on a Mesh through a Model. Model::new takes a ToonMaterial 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 ToonMaterial::new() for an independent material.

use fragmentcolor::{Mesh, Model, ToonMaterial, Vertex};
let mesh = Mesh::new();
mesh.add_vertices([
Vertex::pbr([0.0, 0.5, 0.0]),
Vertex::pbr([-0.5, -0.5, 0.0]),
Vertex::pbr([0.5, -0.5, 0.0]),
]);
let hero = ToonMaterial::new()
.base_color([0.9, 0.3, 0.2, 1.0])?
.bands(2)?
.highlight([1.0, 1.0, 1.0])?
.rim([0.3, 0.5, 1.0])?;
let model = Model::new(mesh, hero);

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 ToonMaterial.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, ToonMaterial};
let leaves = ToonMaterial::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 leaves, hair cards 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, ToonMaterial};
let glass = ToonMaterial::new()
.base_color([0.6, 0.8, 1.0, 0.4])?
.alpha_mode(AlphaMode::Blend);

Set how many even steps each light’s diffuse term takes, from the edge of the shadow to a face turned straight at the light. A face at the cosine ndl to the light gets ceil(ndl * bands) / bands of it, and a face turned away gets none, so the scene’s ambient light alone fills it. One band lights every face toward the light fully, and many bands come close to smooth shading.

Maps to the material.bands uniform on the underlying shader. Default is 3. A ramp_map() takes over from the bands once it holds a texture wider than one texel.

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

0 returns MaterialError::Bands, such as ToonMaterial::bands takes 1 band or more, got 0, and the material keeps the bands it had. JavaScript throws an Error named MaterialError, Python raises MaterialError, a FragmentColorError that is also a ValueError, Swift throws FragmentColorError, and Kotlin throws FragmentColorException. The count is a count like any other: JavaScript throws for a fraction or a negative number, and Python takes an int, raising TypeError for a float and InvalidInputError for an int below 0 or past 2**32 - 1.

use fragmentcolor::ToonMaterial;
// Lit or in shadow, and nothing between.
let flat = ToonMaterial::new().bands(1)?;

Set the material’s color, in linear RGBA. The base color map’s sample and the vertex color multiply it, each light’s band or ramp level scales 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 ToonMaterial.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::ToonMaterial;
let red = ToonMaterial::new().base_color([1.0, 0.2, 0.2, 1.0])?;

The material’s color map, as a MaterialMap handle that sets its texture, UV set and transform. Its sample multiplies material.base_color and the vertex color, at 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::ToonMaterial;
// Raw pixels with their size upload when the scene loads.
let pixels: Vec<u8> = vec![255, 128, 0, 255];
let material = ToonMaterial::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.

Thin geometry the camera can see from both sides, such as a flag or a leaf, needs both faces. Like alpha_mode, it is pipeline state.

Seen from behind, a face lights, bands and takes the rim as the side the camera sees. On a Model mirrored by a negative scale, whose outer faces wind the other way on screen, those faces still light as the front.

use fragmentcolor::ToonMaterial;
let flag = ToonMaterial::new().double_sided(true);

Set the color of the hard highlight, in linear RGB. Each light adds it, as a spot with a hard edge, where the half vector between the light and the eye comes within highlight_size of the normal, on faces turned toward the light. The light’s color and falloff scale it, as they scale the diffuse term.

Maps to the material.highlight uniform on the underlying shader. Default is [0, 0, 0], which adds nothing.

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. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

use fragmentcolor::ToonMaterial;
let glossy = ToonMaterial::new().highlight([1.0, 1.0, 1.0])?;

Set how large the highlight’s spot is, from 0, which hides it, to 1, which spreads it over every face turned toward the light. The spot covers the faces where the cosine between the normal and the half vector, between the light and the eye, tops 1 - size. At the default 0.05, the spot reaches about 18 degrees from the glint.

Maps to the material.highlight_size uniform on the underlying shader.

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

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. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

use fragmentcolor::ToonMaterial;
let wet = ToonMaterial::new()
.highlight([1.0, 1.0, 1.0])?
.highlight_size(0.15)?;

Construct a white, opaque, single-sided ToonMaterial that lights in three bands, with no highlight and no rim. Its two maps start unbound: the renderer binds a 1×1 white default to each, so the material draws base_color in bands until you set a map through base_color_map() or ramp_map(). ToonMaterial::new can’t fail: the shader parts it composes are compiled into the library.

The shader adds up each light the fragment receives, the scene’s ambient light times the albedo, and the rim, 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(1) normal : vec3<f32>, set as .set(Vertex::NORMAL, ...).
  • @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 base color 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.bands 3
material.highlight [0, 0, 0]
material.highlight_size 0.05
material.rim [0, 0, 0]
material.rim_width 0.2
material.alpha_cutoff 0.5
base_color_map 1×1 white
ramp_map 1×1, no ramp
camera.view_proj identity 4×4
camera.position [0, 0, 0]
lights.ambient [0.03; 3]
placeholder light white, directional, pointing down [0, -1, 0]

The placeholder light lights a fresh material until the first Light reaches its shader, and returns once no Light does, as on PbrMaterial.

use fragmentcolor::ToonMaterial;
let grass = ToonMaterial::new().base_color([0.3, 0.7, 0.2, 1.0])?;

The gradient that lights the material in place of its bands, as a MaterialMap handle that sets its texture. Each light reads it left to right by the cosine ndl between the normal and the light, at ndl * 0.5 + 0.5: the left half colors the faces turned away from the light, the middle the edge of the shadow, and the right edge the faces turned straight at it. The ramp’s color times the base color is that light’s diffuse term, so a colored ramp tints the shadows.

The ramp takes over once it holds a texture wider than one texel. A texture one texel wide reads as no ramp, so the bands draw, with no error. The ramp samples its middle row, so a ramp one texel tall is enough. Its texture’s own sampler applies: set smooth: false for hard steps between its texels. A source with no format of its own reads as sRGB, as a painted gradient is.

The ramp reads the light, not the mesh’s UVs, so its uv and transform return MaterialError::NoUv, which names the call, and change nothing.

use fragmentcolor::ToonMaterial;
// Deep blue shadows, a warm middle tone and full light, one texel each.
let ramp: Vec<u8> = vec![
20, 30, 90, 255, 20, 30, 90, 255,
200, 140, 110, 255, 255, 255, 255, 255,
];
let material = ToonMaterial::new();
let map = material.ramp_map().texture((ramp, [4, 1]))?;

Set the color of the rim, in linear RGB, which the material adds to the faces the camera sees edge-on, along the silhouette. It needs no light, so it shows in shadow too. rim_width sets how far in from the edge it reaches.

Maps to the material.rim uniform on the underlying shader. Default is [0, 0, 0], which adds nothing.

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. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

use fragmentcolor::ToonMaterial;
let ghost = ToonMaterial::new().rim([0.4, 0.8, 1.0])?;

Set how far in from the silhouette the rim reaches, from 0, which hides it, to 1, which covers every face not turned straight at the camera. The rim covers the faces where the cosine between the normal and the direction to the camera stays under the width. At the default 0.2, it covers the faces turned more than about 78 degrees from the camera.

Maps to the material.rim_width uniform on the underlying shader.

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

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. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

use fragmentcolor::ToonMaterial;
let glow = ToonMaterial::new()
.rim([1.0, 0.6, 0.2])?
.rim_width(0.35)?;

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 and the lights, add a Camera and Lights to the Pass that draws the material, rather than setting camera.* by hand. They keep the shader up to date.

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