0
votes

My code below is only displaying 1 arkit object in the touches began function. I would like the user to be able to display several of the same arkit objects in the arkit scene view. Right now the user is able to place a image but as soon as the next one is placed the other one is deleted.

   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    //Handle the shooting
    guard let frame = sceneView.session.currentFrame else { return }
    let camMatrix = SCNMatrix4(frame.camera.transform)
    let direction = SCNVector3Make(-camMatrix.m31 * 5.0, -camMatrix.m32 * 10.0, -camMatrix.m33 * 5.0)
    let position = SCNVector3Make(camMatrix.m41, camMatrix.m42, camMatrix.m43)
    let scene = SCNScene(named: "art.scnassets/dontCare.scn")!

    // Set the scene to the view
 sceneView.scene = scene
}
2

2 Answers

1
votes

Silly Milly, you are changing the whole scene each time, instead, add nodes to the sceneView.scene

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


guard let frame = sceneView.session.currentFrame else { return }
let camMatrix = SCNMatrix4(frame.camera.transform)

let position = SCNVector3Make(camMatrix.m41, camMatrix.m42, camMatrix.m43)
let object = SCNScene(named: "art.scnassets/dontCare.scn")!.rootNode
object.position = position

sceneView.scene.rootNode.addChildNode(object)

}

0
votes

You have to add multiple child node on ARSCNView

if your .scn file structure something like

let careScene = SCNScene.init(named: "art.scnassets/dontCare.scn")
let childNode = (careScene?.rootNode.childNode(withName: "Frame", recursively: false))!
childNode.position = SCNVector3(x,y,z)
scenView.scene.rootNode.addChildNode(childNode)