GLTFSceneKit
GLTFSceneKit copied to clipboard
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
loadNode
to get rid of the extra node, changingscnNode
from alet
to avar
and then instead of creating a new node withmeshNode
I set the return fromloadMesh
directly toscnNode
-
I remove
primitiveNode
entirely fromloadMesh
and instead assign everything that was assigned toprimitiveNode
tonode
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.
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