use of com.almuradev.content.model.obj.material.MalformedMaterialLibraryException in project Almura by AlmuraDev.
the class OBJModelParser method parseMaterialLibrary.
private MaterialLibrary parseMaterialLibrary(final ResourceLocation source) throws Exception {
final MaterialLibrary.Builder mtllibBuilder = MaterialLibrary.builder();
final IResource resource = this.resourceManager.getResource(source);
try (final InputStream stream = resource.getInputStream()) {
MaterialDefinition.Builder mtlBuilder = null;
String currentMaterial = null;
final List<String> lines = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines().collect(Collectors.toList());
for (int i = 0; i < lines.size(); i++) {
final String line = lines.get(i);
// Skip comments and newlines
if (line.startsWith(OBJModelConfig.COMMENT) || line.isEmpty()) {
continue;
}
final String[] combinedLineContents = line.split(" ");
final String lineHeader = combinedLineContents[0];
final String[] lineContents = Arrays.copyOfRange(combinedLineContents, 1, combinedLineContents.length);
switch(lineHeader) {
case OBJModelConfig.Material.NEW_MATERIAL:
if (mtlBuilder != null) {
mtllibBuilder.materialDefinition(mtlBuilder.build(currentMaterial));
}
mtlBuilder = MaterialDefinition.builder();
currentMaterial = lineContents[0];
break;
case OBJModelConfig.Material.DIFFUSE:
if (mtlBuilder == null) {
throw new MalformedMaterialLibraryException("Material attribute cannot occur before defining new material " + "definition! Source -> Line: " + (i + 1) + ", Content: " + Arrays.toString(combinedLineContents));
}
@Nullable final ResourceLocation parentLocation = getParent(this.source);
String parentPath = null;
if (parentLocation != null) {
// Making some assumptions here...carry on now
final int lastSlashIndex = parentLocation.getResourcePath().lastIndexOf("/");
if (lastSlashIndex != -1) {
parentPath = parentLocation.getResourcePath().substring(lastSlashIndex + 1, parentLocation.getResourcePath().length());
}
}
mtlBuilder.diffuseTexture(ResourceLocations.buildResourceLocationPath(lineContents[0], parentPath));
break;
default:
}
}
if (mtlBuilder != null) {
mtllibBuilder.materialDefinition(mtlBuilder.build(currentMaterial));
}
}
return mtllibBuilder.build(source, getFileName(source).split("\\.")[0]);
}
Aggregations