Jump to content

clone mesh from GLTF


shen
 Share

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? 

Link to comment
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.

Link to comment
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

Link to comment
Share on other sites

The size difference comes from the fact that the original one is animated (with bones hence the scale, rotation and position changes)

Link to comment
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. 

 

Link to comment
Share on other sites

The timing is important here. If you clone the skeleton (not individual bones but the entire skeleton) before launching any animation you should be good

Link to comment
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;
        }

 

Link to comment
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.

Link to comment
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. 

Link to comment
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"?

Link to comment
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.  

Link to comment
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?

Link to comment
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. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...