简体   繁体   中英

How can I remove specific nodes from a scenekit scene?

I use following code, it will check the touch point and it will add the object if the point is empty or delete the object.

@objc func didTap(withGestureRecognizer recognizer: UIGestureRecognizer) {

        let tapLocation = recognizer.location(in: sceneView)
        let hitTestResults = sceneView.hitTest(tapLocation)

        guard let node = hitTestResults.first?.node else {

            let hitTestResultsWithFeaturePoints = sceneView.hitTest(tapLocation, types: .featurePoint)

            if let hitTestResultWithFeaturePoints = hitTestResultsWithFeaturePoints.first {

                let translation = hitTestResultWithFeaturePoints.worldTransform.translation

                guard let carScene = SCNScene(named: "car.dae") else { return }
                let carNode = SCNNode()
                let carSceneChildNodes = carScene.rootNode.childNodes
                for childNode in carSceneChildNodes {
                    carNode.addChildNode(childNode)
                }
                carNode.position = SCNVector3(translation.x, translation.y, translation.z)
                carNode.scale = SCNVector3(0.5, 0.5, 0.5)
                sceneView.scene.rootNode.addChildNode(carNode)
            }
            return

        }
    node.removeFromParentNode()
}

But my object is create by DAE file, it include lot of childNodes. if i use node.removeFromParentNode() it will only remove one node

if i use following code it will remove all of object on the screen.

sceneView.scene.rootNode.enumerateChildNodes { (existingNode, _) in
        existingNode.removeFromParentNode()
    }

How can I remove specific nodes from a scenekit scene?

您应该命名节点,然后可以使用该名称将其过滤掉。

sceneView.scene.rootNode.childNodes.filter({ $0.name == "yourName" }).forEach({ $0.removeFromParentNode() })

You can use:

func childNode(withName name: String, 
   recursively: Bool) -> SCNNode?

Head over to docs, https://developer.apple.com/documentation/scenekit/scnnode/1407951-childnode

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