Reset ARKit

I have a simple question. If I want to start the game and put the board right in front of me:

gameBoard!.position = SCNVector3(0, 0, -0.6) 

This works until I leave the game and come back again. Can I show the playing field in exactly the same position in front of the camera or 0.6m in front of me? I could physically move to another position.

+5
source share
4 answers

If you want to reset ARSession, you need to pause, delete all nodes and restart the session, resetting tracking and deleting bindings.

I made a reset button that does this whenever I want to reset:

 @IBAction func reset(_ sender: Any) { sceneView.session.pause() sceneView.scene.rootNode.enumerateChildNodes { (node, stop) in node.removeFromParentNode() } sceneView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors]) } 

Or put it in your session the function was interrupted!

+7
source

This can be done using the resetTracking option when you call run again on ARSession .

Example:

 if let configuration = sceneView.session.configuration { sceneView.session.run(configuration, options: .resetTracking) } 
+1
source

"It works until I leave the game and return."

You cannot track the position of the camera in the background. Whenever your application goes to the background and the camera turns off, you lose your position and sessionWasInterrupted (_ :) will be called.

A session is interrupted when it is not possible to obtain a camera or motion sensitive data. Session interruptions occur when camera capture is not available, for example, when your application is in the background or it is several applications in the foreground or when the device is too busy to process motion sensor data.

0
source

Dropping ARSession on the ARKit framework is quite simple:

 class ViewController: UIViewController, ARSCNViewDelegate, ARSessionDelegate { @IBOutlet var arView: ARSCNView! @IBOutlet weak var sessionInfoLabel: UILabel! override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) let configuration = ARWorldTrackingConfiguration() configuration.planeDetection = [.horizontal, .vertical] arView.session.run(configuration) arView.session.delegate = self } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) arView.session.pause() } func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { guard let planeAnchor = anchor as? ARPlaneAnchor else { return } let plane = Plane(anchor: planeAnchor, in: arView) node.addChildNode(plane) } func sessionInterruptionEnded(_ session: ARSession) { resetSessionTracking() sessionInfoLabel.text = "ARSession interruption has ended" } private func resetSessionTracking() { let config = ARWorldTrackingConfiguration() config.planeDetection = [.vertical, .horizontal] arView.scene.rootNode.enumerateChildNodes { (childNode, _) in childNode.removeFromParentNode() } arView.session.run(config, options: [.removeExistingAnchors, .resetTracking, ]) } } 

Hope this helps.

0
source

Source: https://habr.com/ru/post/1271068/


All Articles