Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SCNFloor in ARKIt

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?

like image 501
Lim Thye Chean Avatar asked Aug 29 '17 02:08

Lim Thye Chean


2 Answers

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.

like image 158
Vasilii Muravev Avatar answered Nov 16 '22 11:11

Vasilii Muravev


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))
like image 24
TheCodePig Avatar answered Nov 16 '22 10:11

TheCodePig