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

3

我在使用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 项目中如何同时拥有水平和垂直平面?

1个回答

3

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 结构体符合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)

1
我猜这个问题是针对RealityKit实现而问的? - AnupamChugh
1
嗨@AnupamChugh,问题是:“如何在ARKit或RealityKit项目中同时拥有水平和垂直平面?” - Andy Jazz
1
啊!你说得对!我的错!谢谢你的回答! - AnupamChugh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接