parseMtlLib method

Future<Map<String, ObjMaterial>?> parseMtlLib(
  1. String? materialData
)

Gets map of materials from materialData (mtl file content).

Implementation

Future<Map<String, ObjMaterial>?> parseMtlLib(String? materialData) async {
  final materials = <String, ObjMaterial>{};

  if (materialData == null || materialData.isEmpty) return null;

  final lines = _lineSplitter.convert(materialData);
  String name = '';
  double r = 0, g = 0, b = 0;
  for (var line in lines) {
    List<String> chars = line.trim().split(_whiteSpace);
    if (chars[0] == newmtl) {
      name = chars[1];
    }
    if (chars[0] == _kd) {
      r = double.parse(chars[1]);
      g = double.parse(chars[2]);
      b = double.parse(chars[3]);
      materials[name] = ObjMaterial(
        color: Color.fromARGB(
          255,
          (255 * r).toInt(),
          (255 * g).toInt(),
          (255 * b).toInt(),
        ),
      );
    } else if (chars[0] == _tr) {
      final a = double.parse(chars[1]);
      materials[name] = ObjMaterial(
        color: Color.fromARGB(
          (255 * a).toInt(),
          (255 * r).toInt(),
          (255 * g).toInt(),
          (255 * b).toInt(),
        ),
      );
    }
  }
  return materials;
}