tiramisu/material

Types

Material types for rendering objects.

Materials define how surfaces appear when rendered. Different materials have different performance characteristics and visual properties.

Performance

  • BasicMaterial: Fastest, no lighting calculations
  • LambertMaterial, ToonMaterial: Fast, simple lighting
  • PhongMaterial: Medium, specular highlights
  • StandardMaterial: Physically-based, most realistic but slower
pub opaque type Material
pub type MaterialError {
  OutOfBoundsColor(Int)
  OutOfBoundsOpacity(Float)
  OutOfBoundsRoughness(Float)
  OutOfBoundsMetalness(Float)
  NonPositiveLinewidth(Float)
  NonPositiveShininess(Float)
}

Constructors

  • OutOfBoundsColor(Int)
  • OutOfBoundsOpacity(Float)
  • OutOfBoundsRoughness(Float)
  • OutOfBoundsMetalness(Float)
  • NonPositiveLinewidth(Float)
  • NonPositiveShininess(Float)

Builder for standard (PBR) materials with sensible defaults.

Start with new_standard_material(), chain setter methods, then call build().

Example

let material = material.new()
  |> material.with_color(0xff0000)
  |> material.with_metalness(0.8)
  |> material.with_roughness(0.3)
  |> material.build()
pub opaque type StandardMaterialBuilder

Values

pub fn basic(
  color color: Int,
  transparent transparent: Bool,
  opacity opacity: Float,
  map map: option.Option(asset.Texture),
) -> Result(Material, MaterialError)

Create a validated basic (unlit) material.

Basic materials don’t react to lights, making them very fast to render. Opacity must be between 0.0 (fully transparent) and 1.0 (fully opaque).

Example

let assert Ok(red) = material.basic(color: 0xff0000, transparent: False, opacity: 1.0, map: option.None)
let assert Ok(glass) = material.basic(color: 0x88ccff, transparent: True, opacity: 0.5, map: option.None)
pub fn build(
  builder: StandardMaterialBuilder,
) -> Result(Material, MaterialError)
pub fn lambert(
  color color: Int,
  map map: option.Option(asset.Texture),
  normal_map normal_map: option.Option(asset.Texture),
  ambient_oclusion_map ambient_oclusion_map: option.Option(
    asset.Texture,
  ),
) -> Result(Material, MaterialError)
pub fn line(
  color color: Int,
  linewidth linewidth: Float,
) -> Result(Material, MaterialError)

Create a validated line material for rendering lines.

Example

let assert Ok(line_mat) = scene.line_material(color: 0xff0000, linewidth: 2.0)
pub fn phong(
  color color: Int,
  shininess shininess: Float,
  map map: option.Option(asset.Texture),
  normal_map normal_map: option.Option(asset.Texture),
  ambient_oclusion_map ambient_oclusion_map: option.Option(
    asset.Texture,
  ),
) -> Result(Material, MaterialError)
pub fn sprite(
  color color: Int,
  transparent transparent: Bool,
  opacity opacity: Float,
  map map: option.Option(asset.Texture),
) -> Result(Material, MaterialError)

Create a validated sprite material for 2D billboards.

Sprites always face the camera and are useful for particles, UI elements, etc.

Example

let assert Ok(sprite_mat) = material.sprite(color: 0xffffff, transparent: True, opacity: 0.8, map: option.None)
pub fn standard(
  color color: Int,
  metalness metalness: Float,
  roughness roughness: Float,
  transparent transparent: Bool,
  opacity opacity: Float,
  map map: option.Option(asset.Texture),
  normal_map normal_map: option.Option(asset.Texture),
  ambient_oclusion_map ambient_oclusion_map: option.Option(
    asset.Texture,
  ),
  roughness_map roughness_map: option.Option(asset.Texture),
  metalness_map metalness_map: option.Option(asset.Texture),
) -> Result(Material, MaterialError)

Create a validated physically-based (PBR) standard material.

Standard materials use metalness/roughness workflow for realistic rendering.

  • Metalness: 0.0 = dielectric (plastic, wood), 1.0 = metal
  • Roughness: 0.0 = mirror-smooth, 1.0 = completely rough

Example

let assert Ok(gold) = scene.standard_material(color: 0xffd700, metalness: 1.0, roughness: 0.3, transparent: False, opacity: 1.0, map: option.None, normal_map: option.None, ao_map: option.None, roughness_map: option.None, metalness_map: option.None)
let assert Ok(plastic) = scene.standard_material(color: 0xff0000, metalness: 0.0, roughness: 0.5, transparent: False, opacity: 1.0, map: option.None, normal_map: option.None, ao_map: option.None, roughness_map: option.None, metalness_map: option.None)
pub fn toon(
  color color: Int,
  map map: option.Option(asset.Texture),
  normal_map normal_map: option.Option(asset.Texture),
  ambient_oclusion_map ambient_oclusion_map: option.Option(
    asset.Texture,
  ),
) -> Result(Material, MaterialError)
pub fn with_ambient_oclusion_map(
  builder: StandardMaterialBuilder,
  ambient_oclusion_map: asset.Texture,
) -> StandardMaterialBuilder
pub fn with_color(
  builder: StandardMaterialBuilder,
  color: Int,
) -> StandardMaterialBuilder
pub fn with_metalness(
  builder: StandardMaterialBuilder,
  metalness: Float,
) -> StandardMaterialBuilder
pub fn with_metalness_map(
  builder: StandardMaterialBuilder,
  metalness_map: asset.Texture,
) -> StandardMaterialBuilder
pub fn with_normal_map(
  builder: StandardMaterialBuilder,
  normal_map: asset.Texture,
) -> StandardMaterialBuilder
pub fn with_opacity(
  builder: StandardMaterialBuilder,
  opacity: Float,
) -> StandardMaterialBuilder
pub fn with_roughness(
  builder: StandardMaterialBuilder,
  roughness: Float,
) -> StandardMaterialBuilder
pub fn with_roughness_map(
  builder: StandardMaterialBuilder,
  roughness_map: asset.Texture,
) -> StandardMaterialBuilder
pub fn with_transparent(
  builder: StandardMaterialBuilder,
  transparent: Bool,
) -> StandardMaterialBuilder
Search Document