removed unnecessary extra SCNNodes during loading
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:
-
I updated
loadNodeto get rid of the extra node, changingscnNodefrom aletto avarand then instead of creating a new node withmeshNodeI set the return fromloadMeshdirectly toscnNode -
I remove
primitiveNodeentirely fromloadMeshand instead assign everything that was assigned toprimitiveNodetonode
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
glbhierarchy to the SCN.
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