xiaolingxiao
xiaolingxiao

Reputation: 4895

does ARKit exposes an automatic way of displaying ground planes, or should it be added as an object to some coordinates

I am trying to display ground plane using ARKit, and I see that for ARSCNView exposes two options: showWorldOrigin and showFeaturePoints so that when they are "on", the world coordinate and feature points are displayed with no additional code.

Is there such a hook for ground plane as well? I see that if I do:

        let config = ARWorldTrackingConfiguration()
        config.planeDetection = WorldTrackingSessionConfiguration.PlaneDetection.horizontal 

then presumably ground plane is being detected, and I would like two things:

  1. a console printout for the coordinates of the ground plane

  2. an in-camera display of groundplane

Are there pre-exposed options for such tasks, or do they have to be implemented? If so what are some tutorials that goes over such tasks?

Upvotes: 0

Views: 570

Answers (1)

Daij-Djan
Daij-Djan

Reputation: 50089

ARSCNViewDelegate provides callbacks called for every plane detected/updated/removed:

public func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) 
public func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) 
public func renderer(_ renderer: SCNSceneRenderer, didRemove node: SCNNode, for anchor: ARAnchor)

it does NOT draw or print out stuff though but in there, you can add the node and print it. E.G.:

public func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    guard let anchor = anchor as? ARPlaneAnchor else { return }

    print(anchor.extent)

    // Create a SceneKit plane to visualize the node using its position and extent.
    // Create the geometry and its materials
    let plane = SCNPlane(width: CGFloat(anchor.extent.x), height: CGFloat(anchor.extent.z))

    let lavaImage = UIImage(named: "Lava")
    let lavaMaterial = SCNMaterial()
    lavaMaterial.diffuse.contents = lavaImage
    lavaMaterial.isDoubleSided = true

    plane.materials = [lavaMaterial]

    // Create a node with the plane geometry we created
    let planeNode = SCNNode(geometry: plane)
    planeNode.position = SCNVector3Make(anchor.center.x, 0, anchor.center.z)

    // SCNPlanes are vertically oriented in their local coordinate space.
    // Rotate it to match the horizontal orientation of the ARPlaneAnchor.
    planeNode.transform = SCNMatrix4MakeRotation(-Float.pi / 2, 1, 0, 0)

    // ARKit owns the node corresponding to the anchor, so make the plane a child node.
    node.addChildNode(planeNode)
}

of course you need to handle updateNode and removeNode

Upvotes: 3

Related Questions