TheAbstractDev
TheAbstractDev

Reputation: 159

Swift / ARKit: Place an object node on a detected surface without touching the screen

I'd like to place an object in front of me on a detected surface using hitTest without touching the screen (like IKEA Place or Snapchat)

Anyone have the solution ?

Thanks

Upvotes: 2

Views: 857

Answers (1)

Yevhen
Yevhen

Reputation: 1068

This is approach to place node on a detected surface without touching the screen.

In viewWillAppear add configuration to your ARSCNView. Add boolean variable which will check if the surface was detected.

var surfaceDetected = false

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    sceneView.delegate = self
    let configuration = ARWorldTrackingConfiguration()
    configuration.planeDetection = .horizontal
    sceneView.session.run(configuration)
}

Don't forget to inherit from ARSCNViewDelegate protocol and implement next method.

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

    guard !surfaceDetected else { return }
    surfaceDetected = true

    guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

    let x = CGFloat(planeAnchor.transform.columns.3.x)
    let y = CGFloat(planeAnchor.transform.columns.3.y)
    let z = CGFloat(planeAnchor.transform.columns.3.z)
    let position = SCNVector3(x,y,z)

    sceneView.scene.rootNode.addChildNode(yourNode)
    yourNode.position = position 

}

Hope it helps!

Upvotes: 4

Related Questions