dataOf static method

MaterialData dataOf(
  1. MaterialFile file, {
  2. List<MaterialFile?> layers = const [],
})

file's values as the renderer takes them: colours made linear, the maps' tiling, offset and turn on each; a layered one's layers tiled as each layer file says (layers, read or null).

Implementation

static MaterialData dataOf(
  MaterialFile file, {
  List<MaterialFile?> layers = const [],
}) {
  final color = Color(file.color);
  final (r, g, b) = GraphicsShapes.linear(color);
  final (er, eg, eb) = GraphicsShapes.linear(Color(file.emission));
  final blend = file.alpha == MaterialAlpha.blend;
  final alpha = color.a * (blend ? file.opacity.clamp(0.0, 1.0) : 1);
  TextureRef ref(int slot) => TextureRef(
    slot,
    scale: (file.tilingX, file.tilingY),
    offset: (file.offsetX, file.offsetY),
    rotation: file.rotation * math.pi / 180,
  );
  double tiling(int i) => i < layers.length ? (layers[i]?.tilingX ?? 1) : 1;
  return MaterialData(
    baseColor: (r, g, b, alpha),
    baseColorTexture: ref(0),
    metallic: file.metallic,
    roughness: file.roughness,
    metallicRoughnessTexture: file.metalRoughMap.isEmpty ? null : ref(1),
    normalTexture: file.normalMap.isEmpty ? null : ref(2),
    normalScale: file.normalScale,
    occlusionTexture: file.occlusionMap.isEmpty ? null : ref(3),
    occlusionStrength: file.occlusionStrength,
    emissive: (er, eg, eb),
    emissiveTexture: file.emissionMap.isEmpty ? null : ref(4),
    emissiveStrength: file.emissionStrength,
    alphaMode: switch (file.alpha) {
      MaterialAlpha.opaque => AlphaMode.opaque,
      MaterialAlpha.cutout => AlphaMode.mask,
      MaterialAlpha.blend => AlphaMode.blend,
    },
    alphaCutoff: file.alphaCutoff,
    doubleSided: file.doubleSided,
    unlit: file.unlit,
    uvMode: file.uv == MaterialUv.world ? UvMode.world : UvMode.natural,
    layered: file.kind == MaterialKind.layered,
    layerTiling: (tiling(0), tiling(1), tiling(2), tiling(3)),
  );
}