diff --git a/src/main/java/tech/fastj/resources/models/MtlUtil.java b/src/main/java/tech/fastj/resources/models/MtlUtil.java index 7eeda6f9..5edbb742 100644 --- a/src/main/java/tech/fastj/resources/models/MtlUtil.java +++ b/src/main/java/tech/fastj/resources/models/MtlUtil.java @@ -52,9 +52,27 @@ public static void write(Path destinationPath, Model2D model) { } private static void writeMaterial(StringBuilder fileContents, Polygon2D polygon, Path destinationPath, int materialIndex) { + switch (polygon.getRenderStyle()) { + case Fill: { + writeFillMaterial(fileContents, polygon, destinationPath, materialIndex); + break; + } + case Outline: { + writeOutlineMaterial(fileContents, polygon, materialIndex); + break; + } + case FillAndOutline: { + writeFillMaterial(fileContents, polygon, destinationPath, materialIndex); + writeOutlineMaterial(fileContents, polygon, materialIndex); + break; + } + } + } + + private static void writeFillMaterial(StringBuilder fileContents, Polygon2D polygon, Path destinationPath, int materialIndex) { fileContents.append(ParsingKeys.NewMaterial) .append(' ') - .append("Polygon2D_material_") + .append("Polygon2D_material_fill_") .append(materialIndex) .append(LineSeparator); @@ -77,6 +95,15 @@ private static void writeMaterial(StringBuilder fileContents, Polygon2D polygon, } } + private static void writeOutlineMaterial(StringBuilder fileContents, Polygon2D polygon, int materialIndex) { + fileContents.append(ParsingKeys.NewMaterial) + .append(' ') + .append("Polygon2D_material_outline_") + .append(materialIndex) + .append(LineSeparator); + writeColorMaterial(fileContents, polygon.getOutlineColor()); + } + private static void writeGradientMaterial(StringBuilder fileContents, Polygon2D polygon, Path destinationPath, int materialIndex) { writeDefaultColorValues(fileContents); diff --git a/src/main/java/tech/fastj/resources/models/ObjUtil.java b/src/main/java/tech/fastj/resources/models/ObjUtil.java index 9dbc472f..399c5488 100644 --- a/src/main/java/tech/fastj/resources/models/ObjUtil.java +++ b/src/main/java/tech/fastj/resources/models/ObjUtil.java @@ -53,8 +53,7 @@ public static void write(Path destinationPath, Model2D model) { for (int i = 0; i < model.getPolygons().length; i++) { Polygon2D polygon = model.getPolygons()[i]; writeObject(fileContents, i + 1); - writeMaterialUsage(fileContents, i + 1); - writeFaces(fileContents, polygon, vertexCount); + writeMaterial(fileContents, polygon, i + 1, vertexCount); vertexCount += polygon.getPoints().length; } @@ -127,10 +126,41 @@ private static void writeObject(StringBuilder fileContents, int polygonIndex) { .append(LineSeparator); } - private static void writeMaterialUsage(StringBuilder fileContents, int polygonIndex) { + private static void writeMaterial(StringBuilder fileContents, Polygon2D polygon, int polygonIndex, int vertexCount) { + switch (polygon.getRenderStyle()) { + case Fill: { + writeFillMaterialUsage(fileContents, polygonIndex); + writeFaces(fileContents, polygon, vertexCount); + break; + } + case Outline: { + writeOutlineMaterialUsage(fileContents, polygonIndex); + writeLines(fileContents, polygon, vertexCount); + break; + } + case FillAndOutline: { + writeFillMaterialUsage(fileContents, polygonIndex); + writeFaces(fileContents, polygon, vertexCount); + writeOutlineMaterialUsage(fileContents, polygonIndex); + writeLines(fileContents, polygon, vertexCount); + break; + } + } + fileContents.append(LineSeparator); + } + + private static void writeFillMaterialUsage(StringBuilder fileContents, int polygonIndex) { + fileContents.append(ParsingKeys.UseMaterial) + .append(' ') + .append("Polygon2D_material_fill_") + .append(polygonIndex) + .append(LineSeparator); + } + + private static void writeOutlineMaterialUsage(StringBuilder fileContents, int polygonIndex) { fileContents.append(ParsingKeys.UseMaterial) .append(' ') - .append("Polygon2D_material_") + .append("Polygon2D_material_outline_") .append(polygonIndex) .append(LineSeparator); } @@ -143,7 +173,15 @@ private static void writeFaces(StringBuilder fileContents, Polygon2D polygon, in .append('/') .append(vertexCount + i); } - fileContents.append(LineSeparator).append(LineSeparator); + fileContents.append(LineSeparator); + } + + private static void writeLines(StringBuilder fileContents, Polygon2D polygon, int vertexCount) { + fileContents.append(ParsingKeys.ObjectLine); + for (int i = 1; i <= polygon.getPoints().length; i++) { + fileContents.append(' ').append(vertexCount + i); + } + fileContents.append(LineSeparator); } public static class ParsingKeys { @@ -158,5 +196,6 @@ private ParsingKeys() { public static final String ObjectName = "g"; public static final String UseMaterial = "usemtl"; public static final String ObjectFace = "f"; + public static final String ObjectLine = "l"; } }