loadMtl function
Loading material from Material Library File (.mtl). Reference:http://paulbourke.net/dataformats/mtl/
Implementation
Future<Map<String, Material>> loadMtl(String fileName, {bool isAsset = true}) async {
final materials = <String, Material>{};
String data;
try {
if (isAsset) {
data = await rootBundle.loadString(fileName);
} else {
data = await File(fileName).readAsString();
}
} catch (_) {
return materials;
}
final List<String> lines = data.split('\n');
Material material = Material();
for (String line in lines) {
List<String> parts = line.trim().split(RegExp(r"\s+"));
switch (parts[0]) {
case 'newmtl':
material = Material();
if (parts.length >= 2) {
material.name = parts[1];
materials[material.name] = material;
}
break;
case 'Ka':
if (parts.length >= 4) {
final v = Vector3(double.parse(parts[1]), double.parse(parts[2]), double.parse(parts[3]));
material.ambient = v;
}
break;
case 'Kd':
if (parts.length >= 4) {
final v = Vector3(double.parse(parts[1]), double.parse(parts[2]), double.parse(parts[3]));
material.diffuse = v;
}
break;
case 'Ks':
if (parts.length >= 4) {
final v = Vector3(double.parse(parts[1]), double.parse(parts[2]), double.parse(parts[3]));
material.specular = v;
}
break;
case 'Ke':
if (parts.length >= 4) {
final v = Vector3(double.parse(parts[1]), double.parse(parts[2]), double.parse(parts[3]));
material.ke = v;
}
break;
case 'map_Ka':
if (parts.length >= 2) {
material.mapKa = parts.last;
}
break;
case 'map_Kd':
if (parts.length >= 2) {
material.mapKd = parts.last;
}
break;
case 'Ns':
if (parts.length >= 2) {
material.shininess = double.parse(parts[1]);
}
break;
case 'Ni':
if (parts.length >= 2) {
material.ni = double.parse(parts[1]);
}
break;
case 'd':
if (parts.length >= 2) {
material.opacity = double.parse(parts[1]);
}
break;
case 'illum':
if (parts.length >= 2) {
material.illum = int.parse(parts[1]);
}
break;
default:
}
}
return materials;
}