点击 arkit 中 SCNNode 上的位置 ios swift

Tap location on SCNNode in arkit ios swift

我将 node1 放置到 sceneview.I 当点击 node1.But 时还将 node2 作为子节点添加到 node1 我无法将 node2 正确放置在 node1.How 的点击位置我可以放置它吗恰好在arkit中scn节点的tap位置。

已更新

最终代码

let sceneView = sender.view as! ARSCNView
let tapLocation = sender.location(in: sceneView)
let hitTest = sceneView.hitTest(tapLocation, options: [:])
guard let node = hitTest.first?.node.parent
if let hitTest = hitTest.first
{
    guard let scene = SCNScene(named: "furnitures.scnassets/(furnitureName).scn") else{return}
    let childnode = (scene.rootNode.childNode(withName: furnitureName + " " + "parentnode", recursively: false))!
     node.addChildNode(childnode)
     childnode.position = hitTest.localCoordinates
     childnode.scale = SCNVector3(0.5,0.5,0.5)
 }

你需要使用SCNHitTestResultlocalCoordinatesworldCoordinates这取决于哪个更适合你的问题,然后你可以在那个位置添加一个节点

这是示例代码

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let scnView = self.view as! SCNView
    let touch = touches.first
    if let point = touch?.location(in: scnView) {
        let hitResults = scnView.hitTest(point, options: nil)
        if let result: SCNHitTestResult = hitResults.first {
            let position  = result.localCoordinates
            let position2  = result.worldCoordinates
        }
    }
}

这是我的 code.I 不知道如何在评论中提及所以我这样发帖

let sceneView = sender.view as! ARSCNView
let tapLocation = sender.location(in: sceneView)
let hitTest = sceneView.hitTest(tapLocation, options: [:])
guard let node = hitTest.first?.node
if let hitTest = hitTest.first
{
    guard let scene = SCNScene(named: "furnitures.scnassets/(furnitureName).scn") else{return}
    let childnode = (scene.rootNode.childNode(withName: furnitureName + " " + "parentnode", recursively: false))!
     node.addChildNode(childnode)
     childnode.position = hitTest.localCoordinates
     childnode.scale = SCNVector3(0.5,0.5,0.5)
 }

screenshot

你可以看到 node2 放在椅子上和 table.I 在 table 上圈起来的大小差异,所以它是可见的...