GLTFSceneKit icon indicating copy to clipboard operation
GLTFSceneKit copied to clipboard

removed unnecessary extra SCNNodes during loading

Open digitallysavvy opened this issue 5 years ago • 1 comments

During the loading from glb into Scenekit there are excess nodes being added to the SceneKit hierarchy that don't exist in the glb file. This is a major problem for anyone working with models where you need to target specific children using their names.

Within loadNode and loadMesh there are arbitrary nodes added to the hierarchy which cause there to be duplicate nodes with the same name causing issues with using functions that traverse the scene graph.

For example rootNode.childNode(withName: _) returns the parent to the node you actually are looking for. To work around this distorted hierarchy you have to call the method twice first to get the arbitrary parent and then again on this parent to find the child node you actually want. Or when attempting to set the material on a node that should have geometry you have to use node.childNodes.firstChild.geometry instead of node.geometry.

In my proposed update:

  1. I updated loadNode to get rid of the extra node, changing scnNode from a let to a var and then instead of creating a new node with meshNode I set the return from loadMesh directly to scnNode

  2. I remove primitiveNode entirely from loadMesh and instead assign everything that was assigned to primitiveNode to node

Upon testing it reduces the number of arbitrary parents. For example when querying for a specific node using .childNode(withName: _), the first result is the correct node we want, no longer do we have to search the first result for a child with the same name. Also this solution allows for updating materials without having to get the first child node to find the geometry of the named node.

Note: This is a breaking change for anyone that has written work arounds, but this properly passes the glb hierarchy to the SCN.

digitallysavvy avatar Aug 03 '19 23:08 digitallysavvy

I need to look into it but it seems not working with rigged models like this https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/CesiumMan

magicien avatar Jul 18 '21 11:07 magicien