I have uploaded the project to GitHub for easy replication of the problem.
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:
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.
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With