Added support for base64 embed data and Images

fix-456
Nehon 7 years ago committed by Rémy Bouquet
parent 285d6bb86c
commit e9869e5298
  1. 18
      jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java

@ -14,6 +14,7 @@ import com.jme3.texture.Texture2D;
import com.jme3.util.IntMap; import com.jme3.util.IntMap;
import com.jme3.util.mikktspace.MikktspaceTangentGenerator; import com.jme3.util.mikktspace.MikktspaceTangentGenerator;
import javax.xml.bind.DatatypeConverter;
import java.io.*; import java.io.*;
import java.nio.Buffer; import java.nio.Buffer;
import java.util.*; import java.util.*;
@ -466,10 +467,8 @@ public class GltfLoader implements AssetLoader {
assertNotNull(bufferLength, "No byteLength defined for buffer " + bufferIndex); assertNotNull(bufferLength, "No byteLength defined for buffer " + bufferIndex);
if (uri != null) { if (uri != null) {
if (uri.startsWith("data:")) { if (uri.startsWith("data:")) {
//inlined base64 data //base 64 embed data
//data:<mimeType>;base64,<base64 data> return DatatypeConverter.parseBase64Binary(uri.substring(uri.indexOf(",") + 1));
//TODO handle inlined base64
throw new AssetLoadException("Inlined base64 data is not supported yet");
} else { } else {
//external file let's load it //external file let's load it
if (!uri.endsWith(".bin")) { if (!uri.endsWith(".bin")) {
@ -567,10 +566,15 @@ public class GltfLoader implements AssetLoader {
//TODO support images embed in a buffer //TODO support images embed in a buffer
throw new AssetLoadException("Images embed in a buffer are not supported yet"); throw new AssetLoadException("Images embed in a buffer are not supported yet");
} else if (uri.startsWith("data:")) { } else if (uri.startsWith("data:")) {
//base64 encoded image, not supported yet //base64 encoded image
//TODO support base64 encoded images String[] uriInfo = uri.split(",");
throw new AssetLoadException("Base64 encoded embed images are not supported yet"); byte[] data = DatatypeConverter.parseBase64Binary(uriInfo[1]);
String headerInfo = uriInfo[0].split(";")[0];
String extension = headerInfo.split("/")[1];
TextureKey key = new TextureKey("image" + sourceIndex + "." + extension, false);
return (Texture2D) info.getManager().loadAssetFromStream(key, new ByteArrayInputStream(data));
} else { } else {
//external file image
TextureKey key = new TextureKey(info.getKey().getFolder() + uri, false); TextureKey key = new TextureKey(info.getKey().getFolder() + uri, false);
Texture tex = info.getManager().loadTexture(key); Texture tex = info.getManager().loadTexture(key);
return (Texture2D) tex; return (Texture2D) tex;

Loading…
Cancel
Save