How to setup camera to point at object

285 Views Asked by At

In my app I load models from different files (format is the same) and they have different geometry: big, small, wide, etc. I have object and camera position hard coded and for some cases I don't see anything because camera not point to object.

Maybe there is a way to normalise model before adding it to scene.

Update. With Moustach answer I came up to following solution:

// import object from file
SCNNode *object = [importer load:path];
object.position = SCNVector3Make(0, 0, 0);        
[scene.rootNode addChildNode:object];

// create and add a camera to the scene
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
// to avoid view clipping
cameraNode.camera.automaticallyAdjustsZRange = YES;
// set camera position to front of object
SCNVector3 sphereCenter;
CGFloat sphereRadius;
[object getBoundingSphereCenter:&sphereCenter radius:&sphereRadius];
cameraNode.position = SCNVector3Make(sphereCenter.x, sphereCenter.y, sphereCenter.z + 2 * sphereRadius);
[scene.rootNode addChildNode:cameraNode];

Works well for me.

1

There are 1 best solutions below

0
On BEST ANSWER

You can calculate the bounding box of your mesh, and scale it based on the number you get to make it the same size as other objects.