material method

GpuMaterial material(
  1. MaterialData data, {
  2. (GpuTexture, GpuSampler)? baseColor,
  3. List<(GpuTexture, GpuSampler)?>? maps,
})

A material of data drawing baseColor (its texture and sampler) where it has one, and the default maps everywhere else: a material made in code (a shape's), sharing textures it was given.

maps gives any of the five — base colour, metal-rough, normal, occlusion, emissive, in that order; a null one is the default — and wins over baseColor. A layered material (MaterialData.layered) passes its four colour layers as the first four.

Implementation

GpuMaterial material(
  MaterialData data, {
  (GpuTexture, GpuSampler)? baseColor,
  List<(GpuTexture, GpuSampler)?>? maps,
}) {
  (GpuTexture, GpuSampler) at(int i, (GpuTexture, GpuSampler) fallback) =>
      (maps != null && i < maps.length ? maps[i] : null) ?? fallback;
  // A layer missing from a layered material is plain white, not a
  // normal map's flat blue.
  final layered = data.layered;
  return GpuMaterial._(data, [
    at(0, baseColor ?? (defaults.whiteSrgb, GpuSampler.linearRepeat)),
    at(1, (defaults.white, GpuSampler.linearRepeat)),
    at(
      2,
      (layered ? defaults.whiteSrgb : defaults.flatNormal, GpuSampler.linearRepeat),
    ),
    at(3, (defaults.white, GpuSampler.linearRepeat)),
    at(4, (defaults.whiteSrgb, GpuSampler.linearRepeat)),
  ]);
}