Using SCNFloor in ARKIt

1.3k views Asked by At

In almost all tutorials of ARKit, it seems that we always use ARPlane for floor.

let planeGeometry = SCNPlane(width:CGFloat(planeAnchor.extent.x), height:CGFloat(planeAnchor.extent.z))
let planeNode = SCNNode(geometry:planeGeometry)
planeNode.position = SCNVector3(x:planeAnchor.center.x, y:0, z:planeAnchor.center.y)
planeNode.transform = SCNMatrix4MakeRotation(-Float.pi / 2, 1.0, 0, 0)

What if you wanted an infinity plane (for casting shadows)? I tried SCNFloor and the result is weird - the floor hung in mid air:

let planeGeometry = SCNFloor()
let planeNode = SCNNode(geometry:planeGeometry)
planeNode.position = SCNVector3(planeAnchor.center.x, 0, planeAnchor.center.z)

I have done a Google search and the only result I have come out with is this (which also does not work): https://github.com/arirawr/ARKit-FloorIsLava/blob/master/FloorIsLava/ViewController.swift

Does SCNFloor() works in ARKit? If not, what can I do to create an big plane?

2

There are 2 answers

2
Vasilii Muravev On

If you add your floor node into rootNode, your Y position should be changed:

planeNode.position = SCNVector3(planeAnchor.center.x, -1.0, planeAnchor.center.z)

In this case I used minus one meter ^

You can use surface detection to find your floor surface anchor, and then you'll add your floor into related node for this anchor. World coordinates of the anchor already will be correct (with minus Y), so you could use SCNVector3Zero as position for your floor.

0
TheCodePig On

You can just change this line to make your plane as big as you like:

let planeGeometry = SCNPlane(width:CGFloat(planeAnchor.extent.x),height:CGFloat(planeAnchor.extent.z)

The way it is above, it matches the size of the detected plane, which ARKit updates along the way.

You can make it a little larger like this:

let overHang:CGFloat = 0.2
...
let planeGeometry = SCNNode(geometry: SCNPlane(width: CGFloat(planeAnchor.extent.x)+overHang, height: CGFloat(planeAnchor.extent.z)+overHang))

Or, just use a CGFloat to specify size in meters:

let theSize:CGFloat = 4.0
...
let planeGeometry = SCNNode(geometry: SCNPlane(width: theSize, height: theSize))