简体   繁体   中英

function only placing 1 arkit object Node on scenview (Swift4)

My code below is supposed to place a user created arkit object in the scenview however its not working. It Places 1 object and can not place any more other objects. The code I commented out places scn shape balls perfectly and as many as the user request when the function is called. However it only places 1 user created object and I want to place several objects.

    func createBall(hitPosition : SCNVector3) {
 //        let newBall = SCNSphere(radius: 0.01)
 //        let newBallNode = SCNNode(geometry: newBall)
//        newBallNode.position = hitPosition
 //        self.sceneView.scene.rootNode.addChildNode(newBallNode)

    let scene = SCNScene(named: "art.scnassets/dontCare.scn")!

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

}

When you're working with ARkit, you only are working with a single scene, so if the object you want to place is being loaded from an .scn file, you need to load the scene, pull out the Node that you want to drop into place (probably the rootnode of that scene you just loaded), and place that at the .position like you have the example code of the ball doing in the commented out code.

You'll probably find it useful to load the scene ahead of time and grab that rootNode so you can drop it in as many times as you'd like at the positions where it hit-tests onto your field.

Update: I only have my iPad with me, not a laptop, and no immediate playgrounds I can easily share that have a .scn file included within it. Assuming you're starting with an .SCN file that has the object you want to place as it's root node, something akin to:

guard let loadedScene = SCNScene(named: "something.scn")
  else { fatalError("Unable to load scene file.") }
let node = loadedScene.rootnode // type of this attribute is an SCNNode

And then from here, just like you'd place the ball by adding the node into the existing scene, you can take that node we just pulled from the loadedScene object and drop it in where you had the ball.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM