diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/BinDataKey.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/BinDataKey.java index 520e7ce0ee..32a63c7b74 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/BinDataKey.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/BinDataKey.java @@ -40,6 +40,12 @@ class BinDataKey extends AssetKey { public BinDataKey(String name) { super(name); + + // References to external data in glTF are not required to have a + // specific file extension. But the 'General.cfg' contains + // LOADER com.jme3.scene.plugins.gltf.BinLoader : bin + // So always set the extension to be "bin" to find the loader. + this.extension = "bin"; } @Override diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java index 0a39689bd7..996789a5e8 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java @@ -773,33 +773,45 @@ public ByteBuffer readData(int bufferIndex) throws IOException { return data; } + /** + * Reads the data from the given buffer uri and returns it as a byte buffer. + * + * The data is read from the given URI. If the URI is a data: URI, then the data is decoded + * and returned. Otherwise, the data is read from the given URI via the asset manager. + * + * The buffer index is not used (except for possible error messages). + * + * @param bufferIndex + * The index of the buffer + * @param uri + * The URI of the buffer + * @param bufferLength + * The expected length of the buffer + * @return The buffer data + * @throws AssetLoadException + * If the given URI is null. + * @throws IOException + * If an IO error occurs. + */ protected ByteBuffer getBytes(int bufferIndex, String uri, Integer bufferLength) throws IOException { - ByteBuffer data; - if (uri != null) { - if (uri.startsWith("data:")) { - // base 64 embed data - data = BufferUtils - .createByteBuffer(Base64.getDecoder().decode(uri.substring(uri.indexOf(",") + 1))); - } else { - // external file let's load it - String decoded = decodeUri(uri); - if (!decoded.endsWith(".bin")) { - throw new AssetLoadException( - "Cannot load " + decoded + ", a .bin extension is required."); - } - - BinDataKey key = new BinDataKey(info.getKey().getFolder() + decoded); - try (InputStream input = (InputStream) info.getManager().loadAsset(key)) { - data = BufferUtils.createByteBuffer(bufferLength); - GltfUtils.readToByteBuffer(input, data, bufferLength); - } - - } - } else { + if (uri == null) { // no URI, this should not happen in a gltf file, only in glb files. throw new AssetLoadException("Buffer " + bufferIndex + " has no uri"); } - return data; + if (uri.startsWith("data:")) { + // base 64 embed data + ByteBuffer data = BufferUtils + .createByteBuffer(Base64.getDecoder().decode(uri.substring(uri.indexOf(",") + 1))); + return data; + } + // external file let's load it + String decoded = decodeUri(uri); + BinDataKey key = new BinDataKey(info.getKey().getFolder() + decoded); + try (InputStream input = (InputStream) info.getManager().loadAsset(key)) { + ByteBuffer data = BufferUtils.createByteBuffer(bufferLength); + GltfUtils.readToByteBuffer(input, data, bufferLength); + return data; + } } public Material readMaterial(int materialIndex) throws IOException {