在 ARKit / RealityKit 中同时使用水平面和垂直面

Using both horizontal and vertical planes in ARKit / RealityKit

我在 ARKit 中检测水平面和垂直面时遇到问题。到目前为止,我已经能够在某种程度上成功地检测到水平面,但我也想将其与对垂直面的支持结合起来。

我的目标是创建一个 AR 项目,其中的球可以从地板和墙壁上弹起。没有垂直面检测,球弹向远处,直到看不见为止。

下面是我的水平面检测。

public func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
    let width = CGFloat(planeAnchor.extent.x)
    let height = CGFloat(planeAnchor.extent.z)
    let plane = SCNPlane(width: width, height: height)
    plane.materials.first?.diffuse.contents = UIColor.init(white: 1, alpha: 1)
    let planeNode = SCNNode(geometry: plane)
    let x = CGFloat(planeAnchor.center.x)
    let y = CGFloat(planeAnchor.center.y)
    let z = CGFloat(planeAnchor.center.z)
    planeNode.position = SCNVector3(x,y,z)
    planeNode.eulerAngles.x = -.pi / 2
    scene.rootNode.addChildNode(planeNode)
    let box = SCNBox(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z), length: CGFloat(planeAnchor.extent.y), chamferRadius: 0)
    planeNode.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry: box, options: nil))
}

如何在 ARKit 或 RealityKit 项目中同时拥有水平面和垂直面?

ARKit 5.0

您可以照常进行,使用习惯方法:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let configuration = ARWorldTrackingConfiguration()
    configuration.planeDetection = [.horizontal, .vertical]
    arView.session.run(configuration)
}

planeDetection实例属性是可获取和可设置的:

var planeDetection: ARWorldTrackingConfiguration.PlaneDetection { get set }

...和 ​​PlaneDetection struct 符合 OptionSet 协议,它允许您只使用一个单个值或同时存在两个值:

configuration.planeDetection = []
configuration.planeDetection = .vertical
configuration.planeDetection = [.horizontal, .vertical]

然后将 renderer(_:didAdd:for:) 实例方法添加到您的代码中,可能如下所示:

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

    let width = CGFloat(planeAnchor.extent.x)
    let height = CGFloat(planeAnchor.extent.z)
    let myPlane = SCNPlane(width: width, height: height)           
    
    if planeAnchor.alignment == .horizontal {
        myPlane.materials.first?.diffuse.contents = UIColor.semiOpaqueRed
    } else if planeAnchor.alignment == .vertical {
        myPlane.materials.first?.diffuse.contents = UIColor.semiOpaqueCyan
    }
    
    let planeNode = SCNNode(geometry: myPlane)
    let x = CGFloat(planeAnchor.center.x)
    let y = CGFloat(planeAnchor.center.y)
    let z = CGFloat(planeAnchor.center.z)
    
    planeNode.position = SCNVector3(x, y, z)
    planeNode.eulerAngles.x = -.pi / 2
    
    node.addChildNode(planeNode)
}

最后,添加 renderer(_:didUpdate:for:) 实例方法,可能如下所示:

func renderer(_ renderer: SCNSceneRenderer,
          didUpdate node: SCNNode,
              for anchor: ARAnchor) {
    
    guard let planeAnchor = anchor as? ARPlaneAnchor,
          let planeNode = node.childNodes.first,
          let myPlane = planeNode.geometry as? SCNPlane
    else { return }
    
    let width = CGFloat(planeAnchor.extent.x)
    let height = CGFloat(planeAnchor.extent.z)
    myPlane.width = width
    myPlane.height = height
    
    let x = CGFloat(planeAnchor.center.x)
    let y = CGFloat(planeAnchor.center.y)
    let z = CGFloat(planeAnchor.center.z)
    planeNode.position = SCNVector3(x, y, z)
}


RealityKit 2.0

只需要几行代码就可以在RealityKit中实现这个想法。

let arView = ARView(frame: .zero)
let model = try! ModelEntity.load(named: "Model.usdz")

let anchor = AnchorEntity(plane: [.horizontal, .vertical], 
                 classification: [.floor, .wall], 
                  minimumBounds: [0.5, 0.5])

anchor.addChild(model)
arView.scene.anchors.append(anchor)