load static method

Future<Map<String, Color>> load(
  1. String assetPath
)

Loads an MTL file and returns a Map of MaterialName -> Color

Implementation

static Future<Map<String, Color>> load(String assetPath) async {
  final String content = await rootBundle.loadString(assetPath);
  final Map<String, Color> materials = {};
  String currentMat = "";

  LineSplitter.split(content).forEach((line) {
    line = line.trim();
    if (line.isEmpty || line.startsWith('#')) return;
    final parts = line.split(RegExp(r'\s+'));

    if (parts[0] == 'newmtl') {
      currentMat = parts[1];
    } else if (parts[0] == 'Kd') {
      // Diffuse Color: Kd 0.8 0.8 0.8
      if (currentMat.isNotEmpty && parts.length >= 4) {
        final r = double.parse(parts[1]);
        final g = double.parse(parts[2]);
        final b = double.parse(parts[3]);
        materials[currentMat] = Color.fromARGB(
          255,
          (r * 255).toInt(),
          (g * 255).toInt(),
          (b * 255).toInt()
        );
      }
    }
  });
  return materials;
}