简体   繁体   中英

SceneKit camera shows extra space when it should not

Update

I have uploaded the project to GitHub for easy replication of the problem.

Question

I have a test scene, where I want to position an object (a SCNBox in this case) to be as wide as the screen. Here's the entirety of the code.

let scene = SCNScene()
let rootNode = scene.rootNode

// Box
let boxGeometry = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0)
let boxMaterial = SCNMaterial()
boxMaterial.diffuse.contents = UIColor.redColor()
boxGeometry.firstMaterial = boxMaterial
let boxNode = SCNNode(geometry: boxGeometry)
boxNode.position = SCNVector3(
    x: 0,
    y: 0,
    z: -1)
rootNode.addChildNode(boxNode)

// Camera
let camera = SCNCamera()
camera.zNear = 0
let cameraNode = SCNNode()
cameraNode.position = SCNVector3(
    x: 0,
    y: 0,
    z: 0)
cameraNode.camera = camera
rootNode.addChildNode(cameraNode)

But this doesn't produce exactly what I would expect - 100% width coverage. As the screenshot below indicates, there's about 2.5% of white space total on the sides. Why? And how do I need to adjust my camera/box to achieve full coverage of the screens width?

问题

Here's a quick visual representation of what your code does (viewed from the Top):

1) Create a 2x2x2 cube at (0,0,0)

创建多维数据集

2) Move the cube to (0,0,-1)

移动立方体

3) Add Camera at (0,0,0)

添加相机

As you can see, the Camera intersects with the cube. It's an unpredictable behavior, and it can intersect, fight on Z, or simply look inside.

In your question you used a 1x1x1 box. Here's what it looks like:

1x1x1盒子

As you can see, the box should fill the view. But that's because I used a standard camera (36mm, or around 50° xfov). By moving around your scene, I realized you have a really large FOV, meaning your scene looks exactly like this:

大视野

Or, if we zoom in:

放大

Here we go! So how do we fix this? By increasing the FOV. In our case, the angle is 90. But you can find it again with simple trigonometry.

地狱是的数学!

And now the box completely fills the viewport!

Ok so you have two options.

  1. you can kind of cheat, it will be easy but not really convenient
    • therefore increase the size of the box randomly or move it closer to the camera
  2. make it the way it should be which would make it adaptive, will be harder though
    • therefore have a look at .projectPoint() and .unprojectPoint() [as I have trouble figuring out I can not give you any code, you might try your luck]

Hope that helps :)

EDIT
FYI - I do not think that your problem relates to the camera, if you set the size of the box to 0.5 instead of 1 then it actually is smaller which shows that it is no camera-based bug.
Also, if you run the application on a device other than the iPhone 6+, you will see that the box does fill the screen at least on the right side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM