如何将ARPlaneAnchor放在与ARImageAnchor相同的位置

时间:2018-02-26 00:06:06

标签: ios swift arkit

我正在尝试将ARImageAnchor放置在与ARPlaneAnchor相同的位置,但它无法正常工作。这是renderer函数的开始,我认为问题在于:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        guard let imageAnchor = anchor as? ARImageAnchor else { return }
        guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
        ...
}

当我评论与planeAnchor有关的所有内容时,它工作正常,但是一旦我尝试将它们放在同一个位置,它仍会编译,但它不会检测参考图像。以前有人看到这样的问题吗?谢谢!

更新:使用sceneView作为我的ARSCNView,我使用此代码:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    print("got here")
    guard let touchLocation = touches.first?.location(in: sceneView),
        let hitTest = sceneView?.hitTest(touchLocation, options: nil),
        //let detectedPlane = planes[planeAnchor],
        let nodeTapped = hitTest.first?.node,
        let nodeName = nodeTapped.name else { return }
    print("got here 2")

    print(nodeName)

}

但是“来到这里2”从未打印过,当我点击飞机时没有任何反应。我该怎么办?

1 个答案:

答案 0 :(得分:0)

进一步观察我不这是可能的。

如果您将ARWorldTrackingConfiguration设置为检测平面,例如:

  configuration.planeDetection = .horizontal

然后在委托方法中添加此调试日志:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

    print(anchor.classForCoder)

}

您将看到,当您检测到目标图像时,它将记录ARImageAnchor,而如果它检测到水平面,则会记录:ARPlaneAnchor

这就是为什么你的警卫声明不能解释的原因:

 guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

仅仅因为同时不能有两个锚点......

ARPlaneAnchorARImageAnchor

基于您上次回复的更新,这将允许您获取这些属性:

   func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

    //1. If Out Target Image Has Been Detected Than Get The Corresponding Anchor
    guard let currentImageAnchor = anchor as? ARImageAnchor else { return }

    //2. Get The Targets Name
    let name = currentImageAnchor.referenceImage.name!

    //3. Get The Targets Width & Height
    let width = currentImageAnchor.referenceImage.physicalSize.width
    let height = currentImageAnchor.referenceImage.physicalSize.height

    //4. Log The Reference Images Information
    print("""
    Image Name = \(name)
    Image Width = \(width)
    Image Height = \(height)
    """) 
}

我仍然不明白为什么你需要一个PlaneAnchor,你可以用它来显示模型等:))

如果你想在那个Anchor上添加一些内容,那么试试这个:

  /// Generates An SCNPlane On The Detected Image At The Size Of The Target
///
/// - Parameters:
///   - node: SCNNode
///   - width: CGFloat
///   - height: CGFloat
func generateModel(_ node: SCNNode, width: CGFloat, height: CGFloat){

    let nodeToAdd = SCNNode()
    let planeGeometry = SCNPlane(width: width, height: height)
    planeGeometry.firstMaterial?.diffuse.contents = UIColor.red
    nodeToAdd.geometry = planeGeometry
    nodeToAdd.transform = SCNMatrix4MakeRotation(-Float.pi / 2, 1, 0, 0)
    node.addChildNode(nodeToAdd)
}

并在委托方法中调用它:

 generateModel(node, width: width, height: height)

我认为这是你想要实现的目标。

更新:要在添加的节点上获取hitTest,只需使用touchesBegan,例如。

   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    guard let touch = touches.first?.location(in: self.augmentedRealityView),
    let hitTest = self.augmentedRealityView?.hitTest(touch, options: nil),
    let nodeTapped = hitTest.first?.node,
    let nodeName = nodeTapped.name else { return }

    print(nodeName)

}

<强>更新 如果放置在ImageAnchor上的对象看起来不稳定,则可能是您在ARAssets Bundle中错误地设置了referenceImage大小:

enter image description here