Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,9 @@ protected byte[] getBytes(int bufferIndex, String uri, Integer bufferLength) thr
BinDataKey key = new BinDataKey(info.getKey().getFolder() + decoded);
InputStream input = (InputStream) info.getManager().loadAsset(key);
data = new byte[bufferLength];
DataInputStream dataStream = new DataInputStream(input);
dataStream.readFully(data);
dataStream.close();
try (DataInputStream dataStream = new DataInputStream(input)) {
dataStream.readFully(data);
}
}
} else {
// no URI, this should not happen in a gltf file, only in glb files.
Expand All @@ -619,6 +619,11 @@ protected byte[] getBytes(int bufferIndex, String uri, Integer bufferLength) thr
public Material readMaterial(int materialIndex) throws IOException {
assertNotNull(materials, "There is no material defined yet a mesh references one");

Material material = fetchFromCache("materials", materialIndex, Material.class);
if (material != null) {
return material.clone();
}

JsonObject matData = materials.get(materialIndex).getAsJsonObject();
JsonObject pbrMat = matData.getAsJsonObject("pbrMetallicRoughness");

Expand Down Expand Up @@ -688,7 +693,10 @@ public Material readMaterial(int materialIndex) throws IOException {

adapter.setParam("emissiveTexture", readTexture(matData.getAsJsonObject("emissiveTexture")));

return adapter.getMaterial();
material = adapter.getMaterial();
addToCache("materials", materialIndex, material, materials.size());

return material;
}

public void readCameras() throws IOException {
Expand Down Expand Up @@ -746,12 +754,17 @@ public Texture2D readTexture(JsonObject texture, boolean flip) throws IOExceptio
Integer textureIndex = getAsInteger(texture, "index");
assertNotNull(textureIndex, "Texture has no index");
assertNotNull(textures, "There are no textures, yet one is referenced by a material");

Texture2D texture2d = fetchFromCache("textures", textureIndex, Texture2D.class);
if (texture2d != null) {
return texture2d;
}

JsonObject textureData = textures.get(textureIndex).getAsJsonObject();
Integer sourceIndex = getAsInteger(textureData, "source");
Integer samplerIndex = getAsInteger(textureData, "sampler");

Texture2D texture2d = readImage(sourceIndex, flip);
texture2d = readImage(sourceIndex, flip);

if (samplerIndex != null) {
texture2d = readSampler(samplerIndex, texture2d);
Expand All @@ -761,6 +774,8 @@ public Texture2D readTexture(JsonObject texture, boolean flip) throws IOExceptio

texture2d = customContentManager.readExtensionAndExtras("texture", texture, texture2d);

addToCache("textures", textureIndex, texture2d, textures.size());

return texture2d;
}

Expand Down