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