点击arkit ios swift中SCNNode上的位置
我将node1放置在sceneview中。当点击node1时,我还将node2作为子节点添加到node1中。但我无法将node2正确放置在node1的点击位置。如何将其准确放置在arkit中scn节点的点击位置 更新 最终代码点击arkit ios swift中SCNNode上的位置,ios,swift,arkit,scnnode,Ios,Swift,Arkit,Scnnode,我将node1放置在sceneview中。当点击node1时,我还将node2作为子节点添加到node1中。但我无法将node2正确放置在node1的点击位置。如何将其准确放置在arkit中scn节点的点击位置 更新 最终代码 let sceneView = sender.view as! ARSCNView let tapLocation = sender.location(in: sceneView) let hitTest = sceneView.hitTest(tapLocation,
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)
}
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
}
}
}
您需要使用
SCNHITTSRESULT
localCoordinates
或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)
}
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
}
}
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
让scnView=self.view为!scnView
让我们先接触
如果让点=触摸?位置(在:scnView中){
让hitResults=scnView.hitTest(点,选项:nil)
如果let result:SCNHitTestResult=hitsresults.first{
让位置=result.localCoordinates
让位置2=结果。世界坐标
}
}
}
这是我的代码。我不知道如何在评论中提及,所以我就这样发布了
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)
}
当你把节点2放在椅子和桌子上时,你可以看到它的大小不同。我在桌子上画了一个圈,这样它就可以看得见了…你的代码在哪里?你试过了吗?我发布了plz检查和帮助…谢谢你Melian..我也这么做了,但是节点2没有放置在抽头位置。它与节点1位置或枢轴相关吗?你试过这两个位置了吗@AnushaCheedella,你能提供你当前的代码吗?@AnushaCheedella如果你正在将新节点添加到触摸的节点中,你需要本地坐标如果你正在将节点2添加到场景中,那么你需要世界坐标我在你的问题中添加了你的答案作为更新,现在,如果您想让我的答案帮助您解决问题,您可以删除答案?很抱歉,bcz我问的问题太多了。我是初学者,所以我有很多问题。您的答案帮助了我…在我的第一个案例中,我想在椅子上添加一个花瓶。在这种情况下,一切都很完美。但是,当我将花瓶添加到桌子上时,花瓶太容易疏忽,我不能我可以知道问题出在哪里吗//你对“is soo negligeble”的意思是什么?可忽略意味着与放置在椅子上时相比,大小太小检查父节点的比例,以及与花瓶对象大小相关的父节点的大小