UnityGLTF icon indicating copy to clipboard operation
UnityGLTF copied to clipboard

How are GLB loaded at runtime?

Open iambumblehead opened this issue 8 years ago • 2 comments

The best possible examples I found are here and here. I'd like to test the import functionality with a local file but am not sure how to do it.

Please excuse my novice code. This function is called from a UnityEditor Window in an attempt to load a glb file using UnityGLTF. No errors are emitted, even when the path the glb file is wrong. The Wolf does not appear in the scene.

    void LoadGLB ()
    {
        string Url = "file://Users/bumblehead/software/wolf/Wolf.glb";

        // https://github.com/KhronosGroup/UnityGLTF/blob \
        //    /03dd3739d9c08cb899f319032da53188a3c64da7 \
        //    /UnityGLTF/Assets/UnityGLTF/Scripts/Tests/Integration/GLTFTestComponent.cs
        ILoader loader = new WebRequestLoader(URIHelper.GetDirectoryName(Url));
        GLTFSceneImporter sceneImporter = new GLTFSceneImporter(
            URIHelper.GetFileFromUri(new Uri(Url)), loader);

        sceneImporter.LoadScene();
        Debug.Log("loaded");
    }

If I add the following code, it prints "GLTFNodes: 73". But I don't know how to add gltfRoot to the current scene.

        string GLB_PATH = "/Users/bumblehead/software/wolf/Wolf.glb";
        if (File.Exists(GLB_PATH))
        {
            FileStream gltfStream = File.OpenRead(GLB_PATH);
            GLTFRoot gltfRoot = GLTFParser.ParseJson(gltfStream);

            Debug.Log("GLTFNodes: " + gltfRoot.Nodes.Count);
        }

iambumblehead avatar Sep 06 '18 18:09 iambumblehead

I was able to get a GLB loaded by getting the streams ready and just used them instead of using the URL loading way.

In my case, I downloaded the GLB off the web, loaded the bytes I received in a MemoryStream and used that. FileStream should work fine too.

//'stream' being your filestream/memorystream
GLTF.Schema.GLTFRoot gLTFRoot = GLTF.GLTFParser.ParseJson(stream);
ILoader loader = null;
UnityGLTF.GLTFSceneImporter sceneImporter = new UnityGLTF.GLTFSceneImporter(
               gLTFRoot,
               loader,
               stream
               );
yield return sceneImporter.LoadScene(-1, false);

NDrexlAxonom avatar Sep 06 '18 19:09 NDrexlAxonom

@NDrexlAxonom thank you :) your sample works well for me, but leads me to a separate problem that I'll make a new issue for.

iambumblehead avatar Sep 06 '18 20:09 iambumblehead