vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
467 stars 75 forks source link

Add mesh to last node in hierarchical tree. #68

Closed EugeneNIghtingale closed 3 years ago

EugeneNIghtingale commented 3 years ago

Hello! I am creating a hierarchical tree, the leaves of which should have a mesh. Some like image

I use recursive function and NodeBuilder to create a hierarchy

    public NodeBuilder CreateHierarchy(ModelItem itmParent)
    {
        NodeBuilder retValue = new NodeBuilder(itmParent.DisplayName);
        foreach(ModelItem itmChild in itmParent.Children)
        {
            if (itmChild.HasGeometry)
            {
                //Create Node with Mesh
                retValue.CreateNode(itmChild.DisplayName);   
            }
            else
            {
                retValue.AddNode(CreateHierarchy(itmChild));
            }
        }
        return retValue;
    }

But how can I bind the mesh to a specific Node using NodeBuilder? it doesn't have a .WithMesh method. Thanks =)

vpenades commented 3 years ago

In the SceneBuilder namespace, NodeBuilders don't contain meshes, instead, you bind a mesh and a node at scene level:

Scene.AddRigidMesh(mesh, node);

EugeneNIghtingale commented 3 years ago

How i can with Scene.AddRigidMesh(mesh, node); save my hierarchy of elements, taking into account that the group has several Mesh?

If i use Scene.AddRigidMesh(mesh, node); for each mesh, then I get duplicate hierarchy

vpenades commented 3 years ago

AddRigidMesh detects that you're using the same node graph and reuses it, so the hierarchy is not duplicated.