ARKit中,ARSKViewDelegate中的平面检测委托方法有哪些?

In ARKit, what are the plane detection delegate methods in ARSKViewDelegate?

正在阅读 the documentation for planeDetection,它指出

If you enable horizontal plane detection, the session adds ARPlaneAnchor objects and notifies your ARSessionDelegate, ARSCNViewDelegate, or ARSKViewDelegate object whenever its analysis of captured video images detects an area that appears to be a flat surface.

但是,我在 ARSKViewDelegate 中找不到可以接收平面检测事件的方法。我看到很多 ARSCNViewDelegate 的例子。它会在方法 view(_:didAdd:for:) 中吗?如果是,我如何检测它是否是平面检测锚点?

检测到的平面是添加到 ARSession 的锚点,因此您使用委托方法来响应新添加的锚点。

Apple's "Providing 2D Virtual Content with SpriteKit" doc 中,他们展示了一些创建 SpriteKit 节点以响应新锚点的基本代码:

func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {
    return SKLabelNode(text: "")
}

如果您想在每个检测到的飞机的中心放置一个广告牌表情符号,这就是您需要的全部代码。否则,您可以执行以下一项或多项操作...

  • 提供一个不同的 SpriteKit 节点——在那个方法中初始化它,然后 return 它在那里。 (请参阅 SpriteKit 文档、教程、SO 问题等,了解如何使用 SpriteKit。)

  • 也可以手动将锚点添加到场景中,在这种情况下,您可能需要从其余锚点中挑选出基于平面检测的锚点。平面锚点是 ARPlaneAnchor 个实例,因此您可以在该方法中测试类型:

    func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {
        if let plane = anchor as? ARPlaneAnchor {
            // this anchor came from plane detection
            return SKLabelNode(text: "✈️") // or whatever other SK content
        } else {
            // this anchor came from manually calling addAnchor on the ARSession
            return SKLabelNode(text: "⚓️") // or whatever other SK content
        }
    }
    
  • 使用 ARPlaneAnchor 的一些属性来选择要提供的 SK 内容或设置方式。在这种情况下,请像上面那样使用条件转换 (as? ARPlaneAnchor),以便您可以访问这些属性。

  • 通过 ARKit 更改您的 SK 内容相对于 provided/managed 的 position/orientation,或者为每个锚点添加多个 SK 节点。在这种情况下,改为实施 view(_:didAdd:for:),为您的 SK 内容创建新节点并设置它们的位置(等),然后再将它们添加为该方法提供的 node 的子节点。