Skip to content

Commit 300d2a7

Browse files
authored
Solve issue #2127 (excessive memory use while loading GLB) (#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
1 parent 8fb016c commit 300d2a7

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,9 @@ protected byte[] getBytes(int bufferIndex, String uri, Integer bufferLength) thr
605605
BinDataKey key = new BinDataKey(info.getKey().getFolder() + decoded);
606606
InputStream input = (InputStream) info.getManager().loadAsset(key);
607607
data = new byte[bufferLength];
608-
DataInputStream dataStream = new DataInputStream(input);
609-
dataStream.readFully(data);
610-
dataStream.close();
608+
try (DataInputStream dataStream = new DataInputStream(input)) {
609+
dataStream.readFully(data);
610+
}
611611
}
612612
} else {
613613
// no URI, this should not happen in a gltf file, only in glb files.
@@ -619,6 +619,11 @@ protected byte[] getBytes(int bufferIndex, String uri, Integer bufferLength) thr
619619
public Material readMaterial(int materialIndex) throws IOException {
620620
assertNotNull(materials, "There is no material defined yet a mesh references one");
621621

622+
Material material = fetchFromCache("materials", materialIndex, Material.class);
623+
if (material != null) {
624+
return material.clone();
625+
}
626+
622627
JsonObject matData = materials.get(materialIndex).getAsJsonObject();
623628
JsonObject pbrMat = matData.getAsJsonObject("pbrMetallicRoughness");
624629

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

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

691-
return adapter.getMaterial();
696+
material = adapter.getMaterial();
697+
addToCache("materials", materialIndex, material, materials.size());
698+
699+
return material;
692700
}
693701

694702
public void readCameras() throws IOException {
@@ -746,12 +754,17 @@ public Texture2D readTexture(JsonObject texture, boolean flip) throws IOExceptio
746754
Integer textureIndex = getAsInteger(texture, "index");
747755
assertNotNull(textureIndex, "Texture has no index");
748756
assertNotNull(textures, "There are no textures, yet one is referenced by a material");
757+
758+
Texture2D texture2d = fetchFromCache("textures", textureIndex, Texture2D.class);
759+
if (texture2d != null) {
760+
return texture2d;
761+
}
749762

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

754-
Texture2D texture2d = readImage(sourceIndex, flip);
767+
texture2d = readImage(sourceIndex, flip);
755768

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

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

777+
addToCache("textures", textureIndex, texture2d, textures.size());
778+
764779
return texture2d;
765780
}
766781

0 commit comments

Comments
 (0)