我在 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允许您仅使用一个值或同时使用两个值的协议(protocol):
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)
关于swift - 在 ARKit/RealityKit 中同时使用水平面和垂直面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58944384/