I have a scene with a child node named wc. I want to add dae/model file on that node.
I was able to add the model/dae via following:
- Create parent scene with dae asset from bundle
- Add parent scene to the scnView and typecast it to self.view
- Get the required node where dae is to be added.
- Create a sub-Scene with the required node dae.
- Add the sub-scene root node as child of required node of parent scene.
- Set the position of child node as that of required node of parent scene.
Following is the snippet:
Code Snippet (in viewDidLoad)
//Create Scene
SCNScene *scene = [SCNScene sceneNamed:@"bathScene.scnassets/peace01.dae"];
SCNView *scnView = (SCNView *)self.view;
scnView.scene = scene;
scnView.allowsCameraControl = YES;
scnView.delegate = self;
scnView.autoenablesDefaultLighting = YES;
//Get the required node where dae is to be added
SCNNode * wcNode = [scnView.scene.rootNode childNodeWithName:@"wc" recursively:YES];
SCNVector3 wcNodeScreenPosition = [scnView projectPoint:wcNode.position];
NSLog(@"wcNode position %f, %f, %f",wcNodeScreenPosition.x, wcNodeScreenPosition.y, wcNodeScreenPosition.z);
//Create sub-Scene
SCNScene* wcScene = [SCNScene sceneNamed:@"bathScene.scnassets/closet.dae"];
//Add the sub-scene root node to the required node of parent scene
[scnView.scene.rootNode addChildNode:[wcScene.rootNode childNodeWithName:@"ceramic" recursively: false]];
SCNNode * wcDAENode = [wcScene.rootNode childNodeWithName:@"ceramic" recursively: false];
wcDAENode.position = [wcNode position];
SCNVector3 wcDAENodeScreenPosition = [scnView projectPoint:wcDAENode.position];
NSLog(@"DAE position %f, %f, %f",wcDAENodeScreenPosition.x, wcDAENodeScreenPosition.y, wcDAENodeScreenPosition.z);
Log Output
wcNode position 275.306549, 157.163620, 0.956176
DAE position 284.000000, 160.000000, 0.959596
As you can see in the aforementioned log output, the position of required node and its child Dae Node is not the same which renders the model shifted in the scene.
Why are the positions different ?
How do I compensate for the shift ?