arkit ios swift中SCNNode上的点击位置

问题描述 投票:-2回答:2

我将node1放置到场景视图中。当在node1上点击时,我也将node2作为子节点添加到node1。但是我无法将node2正确放置在node1的点击位置。如何将其准确放置在arkit中scn节点的点击位置。

ios swift arkit scnnode
2个回答
0
投票

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

这是示例代码

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
        }
    }
}

0
投票

这是我的代码。我不知道如何在评论中提及,所以我这样发布了

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)
 }
© www.soinside.com 2019 - 2024. All rights reserved.