我将node1放置到场景视图中。当在node1上点击时,我也将node2作为子节点添加到node1。但是我无法将node2正确放置在node1的点击位置。如何将其准确放置在arkit中scn节点的点击位置。
您需要使用SCNHitTestResult
localCoordinates
或worldCoordinates
,这取决于哪个更适合您的问题,然后您可以在该位置添加一个节点
这是示例代码
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
}
}
}
这是我的代码。我不知道如何在评论中提及,所以我这样发布了
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)
}