Skip to content

Commit

Permalink
Avoid duplicate textures from texture atlas generation in glTF
Browse files Browse the repository at this point in the history
  • Loading branch information
tordanik committed May 19, 2023
1 parent b095463 commit 488fea6
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/main/java/org/osm2world/core/target/gltf/GltfTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class GltfTarget extends MeshTarget {
private final Gltf gltf = new Gltf();

private final Map<Material, Integer> materialIndexMap = new HashMap<>();
private final Map<TextureData, Integer> textureIndexMap = new HashMap<>();
private final Map<String, Integer> imageIndexMap = new HashMap<>();

public GltfTarget(File outputFile, @Nullable SimpleClosedShapeXZ bounds) {
Expand Down Expand Up @@ -241,13 +242,9 @@ private int createMesh(Mesh mesh) throws IOException {

int materialIndex;
if (material.getNumTextureLayers() == 0) {
materialIndex = materialIndexMap.containsKey(material)
? materialIndexMap.get(material)
: createMaterial(material, null);
materialIndex = createMaterial(material, null);
} else {
materialIndex = materialIndexMap.containsKey(material)
? materialIndexMap.get(material)
: createMaterial(material, material.getTextureLayers().get(0));
materialIndex = createMaterial(material, material.getTextureLayers().get(0));
}
primitive.material = materialIndex;

Expand Down Expand Up @@ -333,6 +330,8 @@ private int createAccessor(int numComponents, List<? extends Vector3D> vs) {

private int createMaterial(Material m, @Nullable TextureLayer textureLayer) throws IOException {

if (materialIndexMap.containsKey(m)) return materialIndexMap.get(m);

GltfMaterial material = new GltfMaterial();
material.pbrMetallicRoughness = new PbrMetallicRoughness();

Expand Down Expand Up @@ -399,6 +398,8 @@ private int createMaterial(Material m, @Nullable TextureLayer textureLayer) thro

private int createTexture(TextureData textureData) throws IOException {

if (textureIndexMap.containsKey(textureData)) return textureIndexMap.get(textureData);

File outputDir = outputFile.getParentFile();
URI textureDir = new File(outputDir,FilenameUtils.removeExtension(outputFile.getName()) + "_textures").toURI();
ResourceOutputSettings resourceOutputSettings = ResourceOutputSettings.fromConfig(config, textureDir, true);
Expand Down Expand Up @@ -431,7 +432,9 @@ private int createTexture(TextureData textureData) throws IOException {
texture.sampler = samplerIndex;

gltf.textures.add(texture);
return gltf.textures.size() - 1;
int index = gltf.textures.size() - 1;
textureIndexMap.put(textureData, index);
return index;

}

Expand Down

0 comments on commit 488fea6

Please sign in to comment.