如何设置相机指向物体

How to setup camera to point at object

在我的应用程序中,我从不同的文件(格式相同)加载模型,它们具有不同的几何形状:大、小、宽等。我对对象和相机位置进行了硬编码,在某些情况下我没有看到任何东西,因为相机没有指向物体。

也许有一种方法可以在将模型添加到场景之前对其进行规范化。

更新。 有了 Moustach 的回答,我想到了以下解决方案:

// 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];

很适合我。

您可以calculate the bounding box of your mesh,并根据您得到的数字缩放它,使其与其他对象的大小相同。