【问题标题】:Tap location on SCNNode in arkit ios swift在 arkit ios swift 中点击 SCNNode 上的位置
【发布时间】:2019-09-25 22:06:36
【问题描述】:

我将 node1 放置到了sceneview。当点击 node1 时,我还将 node2 作为子节点添加到 node1。但是我无法将 node2 正确放置在 node1 上的点击位置。如何将它准确地放置在 scn 节点的点击位置方舟。

更新

最终代码

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

【问题讨论】:

  • 你的代码在哪里?你试过了吗?
  • 我发布了请检查和帮助...

标签: ios swift arkit scnnode


【解决方案1】:

您需要使用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
        }
    }
}

【讨论】:

  • 谢谢 Melian..我也做了同样的事,但 node2 没有放在水龙头位置。它与 node1 的位置或枢轴有关吗?
  • 两个位置都试过了吗? @AnushaCheedella,你能提供你当前的代码吗?
  • @AnushaCheedella 如果您将新节点添加到触摸节点中,则需要 localCoordinates 如果您将 Node2 添加到场景中,则需要 WorldCoordinates
【解决方案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)
 }

【讨论】:

  • 我已将您的答案作为更新添加到您的问题中,现在您可以根据需要删除答案
  • 我的回答是否有助于解决您的问题?
  • 很抱歉,我问了太多问题。我是初学者,所以我有很多问题。你的回答帮助了我...在我的第一个案例中,我想在椅子上添加一个花瓶..在这种情况下一切都很完美。但是当我把它放在桌子上时,花瓶太微不足道了,我看不到它。我可以知道问题出在哪里吗//
  • 你的意思是“非常微不足道”?
  • negligible 表示与放在椅子上时相比,尺寸太小了
【解决方案3】:

screenshot

你可以看到node2放在椅子和桌子上时大小的差异。我在桌子上圈了起来,这样它就可以看到了......

【讨论】:

  • 查看父节点的尺度,父节点的大小与花瓶对象大小有关
最近更新 更多