Sign in to follow this  
shen

clone mesh from GLTF

Recommended Posts

I tried to clone meshes imported from GLTF file and found that meshes were cloned twice. 

https://www.babylonjs-playground.com/#PSR2ZX#38

https://www.babylonjs-playground.com/#PSR2ZX#39

use the debug layer, you can find that the original root mesh was used as the parent of a set of cloned meshes. 

I also wonder whether the scale of the cloned meshes is correct? 

Share this post


Link to post
Share on other sites

Man, that feels like a rick and morty scene!

The issue here is that you clone all of the meshes that are included in the gltf, which also clone the children (there is a doNotCloneChildren flag in the clone function which you could use).

The simplest way would be to only clone the mesh that doesn't have a parent (like this - https://www.babylonjs-playground.com/#PSR2ZX#40) , which also answers the 2nd question you had.

Share this post


Link to post
Share on other sites

@RaananW Thanks! doNotCloneChildren flag seems just do the opposite of your code. 

In your sandbox, the clones meshes are still different from the original ones. Except the size, I found that they got different boundaries by using the debug layers. 

 

Screen Shot 2018-05-10 at 14.27.35.png

Share this post


Link to post
Share on other sites

@Deltakosh Thanks! 

I followed your example and tried to go further. I found that gltf loader seems create meshes from bones, and they are not necessary. In https://www.babylonjs-playground.com/#PSR2ZX#44 line 81-87, I deleted them, and nothing went wrong.

In line 112, I also delete the animation targets without _skeleton, and animations all worked correctly without them. 

Then I cloned skeletons in skeletonClones and meshes in meshClones, and they are all fine.

I cloned "abduction_rings" animationGroup in animationClone, and re-targeted it's animations to skeletonClones. Click "abduction_rings_clone"  and you can see the animation is different from "abduction_rings". In line 127, I output the original targets and bones in skeletonClones. Bones in original targets seem have more information than the cloned ones. 

 

Share this post


Link to post
Share on other sites

For the first question, I just checked babylon.glTFLoader.ts and found that it generated meshes from all "nodes". Shouldn't it just check all the "nodes" contains "meshes" https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#meshes

        private _getMeshes(): Mesh[] {
            const meshes = new Array<Mesh>();

            // Root mesh is always first.
            meshes.push(this._rootBabylonMesh);

            const nodes = this._gltf.nodes;
            if (nodes) {
                for (const node of nodes) {
                    if (node._babylonMesh) {
                        meshes.push(node._babylonMesh);
                    }

                    if (node._primitiveBabylonMeshes) {
                        for (const babylonMesh of node._primitiveBabylonMeshes) {
                            meshes.push(babylonMesh);
                        }
                    }
                }
            }

            return meshes;
        }

 

Share this post


Link to post
Share on other sites
5 hours ago, shen said:

generated meshes from all "nodes"

@shen The root issue is that glTF puts the skeleton in the hierarchy of nodes. There may be usage cases where a node is being used both as a joint and as a node (say as a node for attaching objects to at runtime). There is no way for the loader to tell if the node is going to be used or not and thus the loader just creates a bone and a mesh and animates both of them.

Share this post


Link to post
Share on other sites
50 minutes ago, bghgary said:

@shen The root issue is that glTF puts the skeleton in the hierarchy of nodes. There may be usage cases where a node is being used both as a joint and as a node (say as a node for attaching objects to at runtime). There is no way for the loader to tell if the node is going to be used or not and thus the loader just creates a bone and a mesh and animates both of them.

Can't we sure that "mesh" means mesh? At this moment, light nodes are considered as mesh too. It shouldn't be difficult to exclude obvious no-mesh nodes from meshes. 

In three.js, "mesh", "bone" and other known types are labeled correctly ("Object3D" for unknown), while the hierarchy of nodes is kept. Of course, I can't guarantee for all the gltf file cases, but so far so good. 

Share this post


Link to post
Share on other sites
9 minutes ago, shen said:

Can't we sure that "mesh" means mesh? At this moment, light nodes are considered as mesh too.

A Babylon mesh behaves like a glTF node. You can easily check to see if a mesh has geometry by calling the appropriate functions.

7 minutes ago, shen said:

can't guarantee for all the gltf file cases

This is the problem. There is no way to guarantee it works for all cases.

Do you have a specific problem that is caused by glTF nodes all being converted to Babylon "mesh"?

Share this post


Link to post
Share on other sites
27 minutes ago, bghgary said:

A Babylon mesh behaves like a glTF node.

That's not right. It make all gltf files awful in Babylonjs, that's not better than "working for most cases".

I've described the problems it caused with clone, unnecessary targets in animation and etc. above.  

Share this post


Link to post
Share on other sites
20 minutes ago, shen said:

It make all gltf files awful in Babylonjs, that's not better than "working for most cases".

I'm not sure I follow this. I tried reading through this thread again to see if something would go wrong if these "extra" mesh and animation targets are present and I don't see anything. Maybe I'm missing something?

Share this post


Link to post
Share on other sites

If it will help, I can add a flag to the glTF loader to not add Babylon mesh objects for glTF nodes that don't have any geometry. Then, you can choose which behavior you want.

Share this post


Link to post
Share on other sites

Maybe the loader can provide nodes with meshes, particle, skeletons, and follow "meshes" and other keywords in glTF to fill mesh and bone bins. If certain cases like you mentioned happen, users can check nodes and solve them manually. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.