How to custom gltf extentions?


#1

like KHR_draco_mesh_compression extention
how to use GltfModelKey to add a custom extention?


#2

This extension is not supported by JME gltf loader.


#3

so I want to custom this extention. But How do I get the GltfModelKey class instance to add this extention from SimpleApplication?


#4

For adding your own extension first you should implement this interface:

For example see PBRSpecGlossExtensionLoader :

Then you should use this method to register your extension:

Note this field is static in GltfModelKey :

So I suppose you can just create a new GltfModelKey and register your extension there then it will be used by loader.

like this:

new GltfModelKey().registerExtensionLoader(...)

It will be cool if we have this extension in JME :slightly_smiling_face:


#5

Thanks.


#6

When I use new GltfModelKey().registerExtensionLoader(...) , the key property of CustomContentManager is null so it cannot load my extension ,any idea to solve this?


#7

Show us the code that you use to register the extension, load the model, etc.


#8

I use static block to register the ext.

static {
        new GltfModelKey().registerExtensionLoader("KHR_draco_mesh_compression",
                new DracoMeshCompressionExtensionLoader());
}

and load model by

Spatial green = assetManager.loadModel("Models/Box/Box.gltf");
green.setLocalTranslation(1,1,1);
Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
material.setColor("Color", ColorRGBA.Green);
green.setMaterial(material);

#9

problem is key property is null in this method of CustomContentManager:

private <T> T readExtension(String name, JsonElement el, T input) throws AssetLoadException, IOException {
        JsonElement extensions = el.getAsJsonObject().getAsJsonObject("extensions");
        if (extensions == null) {
            return input;
        }

        for (Map.Entry<String, JsonElement> ext : extensions.getAsJsonObject().entrySet()) {
            ExtensionLoader loader = null;
            if (key != null) {
                loader = key.getExtensionLoader(ext.getKey());
            }

            if (loader == null) {
                loader = defaultExtensionLoaders.get(ext.getKey());
            }

            if (loader == null) {
                logger.log(Level.WARNING, "Could not find loader for extension " + ext.getKey());
                continue;
            }

            try {
                return (T) loader.handleExtension(gltfLoader, name, el, ext.getValue(), input);
            } catch (ClassCastException e) {
                throw new AssetLoadException("Extension loader " + loader.getClass().getName() + " for extension " + ext.getKey() + " is incompatible with type " + input.getClass(), e);
            }
        }

        return input;
    }

#10

It looks like you can only use extensions if you load with a GltfModelKey and not a raw string name.


#11

can you try this instead :

GltfModelKey k = new GltfModelKey("Models/Box/Box.gltf");

Spatial green = assetManager.loadModel(k);

#12

He should be able to register the loader like before on an empty key.

…but the code indicates that you definitely need to use a GltfModelKey to load the model if you want extension support. (No idea why.)


#13

Yep, edited my post.


#14

it works. Thanks.


#15

I am curious to know what is the size before and after compression for a high poly model. :slightly_smiling_face:

Also, does it compress animation data (morph / bone animations) as well ?


#16

you can read this

KHR_draco_mesh_compression

I think draco has the ablity to do what you said. but it’s mesh only for gltf.