Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

Problem

like image 829
Morgan Wilde Avatar asked May 03 '15 18:05

Morgan Wilde


2 Answers

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

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

Create Cube

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

Move the Cube

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

Add camera

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 box

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:

Large FOV

Or, if we zoom in:

Zoomed

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.

Hell yeah math!

And now the box completely fills the viewport!

like image 190
Moustach Avatar answered Sep 24 '22 11:09

Moustach


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.

like image 31
LinusGeffarth Avatar answered Sep 25 '22 11:09

LinusGeffarth