I would like to create two 3D objects on the AR scene according to the value of the string provided. The default value of the string is "1" and after that, I will jump to another ViewController and change the string value to "2" and then back to the ARViewController.
I would like to ask how can I keep the existing node and world origin of the AR scene remain unchanged when I move to another ViewController and then come back to ARViewController? It seems that every time when I come back to the AR scene, it will do the world tracking again and remove my existing node and world origin.I can only display the latest 3D object by string value "2". The 3D object created by string "1" is being removed.
Here is the code for the AR ViewController:
class ARViewController: UIViewController {
public var object = Connector()
@IBOutlet weak var ARScene: ARSCNView!
public let configuration = ARWorldTrackingConfiguration()
override func viewDidLoad() {
super.viewDidLoad()
self.ARScene.session.run(configuration)
}
override func viewDidAppear(_ animated: Bool) {
if object.stringbeingpassed == "1"{
self.addNode(newposition: SCNVector3(0,0,0))
}else{
self.addNode(newposition: SCNVector3(0.1,0.1,0.1))
}
}
func addNode(newposition:SCNVector3){
let ball = SCNNode()
ball.geometry = SCNSphere(radius: 0.02)
ball.position = newposition
self.ARScene.scene.rootNode.addChildNode(ball)
let action = SCNAction.rotateBy(x: 0, y:CGFloat(360.degreesToRadians), z: 0, duration: 4)
let forever = SCNAction.repeatForever(action)
ball.runAction(forever)
}