问题
I am importing a simple dae file. I want some of the nodes to be a subclass of SCNNode - MySCNNode.
MySCNNode *node = [scnView.scene.rootNode childNodeWithName:@"Box1" recursively:YES];
//additional initialization goes here
Tried casting to (MySCNNode *) too.
But this is not working. "node" is still an SCNNode. Why?
I need to add a few properties and methods to SCNNode. So I subclassed SCNNode. I want the nodes from the scene(imported from a dae) to have the properties and behaviour. The nodes from the scene is always SCNNode. I want it to be of class MySCNNode.
回答1:
I understand needing a subclass. And I understand why it's atypical. In my case I'm making a RTS and am creating it's "Mission editor" so I can take 1 scene filled with the various objects created in blender and build custom scenes in the editor. So I need to know when tiles are buildable, passable (and on which level), etc. This may not be perfect but it should work:
+(instancetype)mySCNNodeWithNode:(SCNNode*)node{
SCNVector3 min,max;
[node getBoundingBoxMin:&min max:&max];
MySCNNode *newNode = [MySCNNode node];
newNode.position = node.position;
newNode.rotation = node.rotation;
newNode.transform = node.transform;
[node setBoundingBoxMin:&min max:&max];
newNode.geometry = [node.geometry copy];
SCNMaterial * material = [node.geometry.firstMaterial copy];
newNode.geometry.firstMaterial = material;
return newNode;
}
来源:https://stackoverflow.com/questions/29616913/subclassing-scnnode