Skip to content

Commit

Permalink
Solve issue jMonkeyEngine#2127 (excessive memory use while loading GL…
Browse files Browse the repository at this point in the history
…B) (jMonkeyEngine#2128)

* Cache the materials

* Try with resources to make sure we close the stream

* Conform cache naming to the other cache usages

* Just use the cache in the read method as the other caches do

* Clone the material

* Cache textures locally to avoid embedded textures to be read many times
  • Loading branch information
tonihele authored Oct 28, 2023
1 parent 8fb016c commit 300d2a7
Showing 1 changed file with 20 additions and 5 deletions.
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

0 comments on commit 300d2a7

Please sign in to comment.