在RealityKit中,类似于ARKit,物体只有在相机检测到某种平面后才会显示出来。一旦相机检测到该表面,物体就会显示并固定在它上面。
我如何知道(通过代码)摄像机是否检测到平面?实际上,我想突出显示可选区域,但我不确定RealityKit是否真的允许这样做,我知道SceneKit是这样做的。
发布于 2020-04-19 18:08:35
在RealityKit中有一个用于此目的的plane初始化器(和枚举用例):
convenience init(plane alignment: AnchoringComponent.Target.Alignment,
classification: AnchoringComponent.Target.Classification,
minimumBounds: SIMD2<Float>)
/* Where `minimumBounds` is the minimum size of the target plane */它是具有extent属性(这是检测到的平面的估计宽度和长度)的ARKit的ARPlaneAnchor的对应物。但在RealityKit中,它的工作原理略有不同。
在真实的代码中,你可以这样使用它:
let anchor = AnchorEntity(.plane([.horizontal, .vertical],
classification: [.wall, .table, .floor],
minimumBounds: [0.375, 0.375]))
/* Here we create an anchor for detected planes with a minimum area of 37.5 cm2 */
anchor.addChild(semiTranparentPlaneEntity) // visualising a detected plane
arView.scene.anchors.append(anchor)请注意,alignment和classification参数符合OptionSet协议。
您可以随时查看平面锚点是否已创建:
let arView = ARView(frame: .zero)
let anchor = AnchorEntity(.plane(.any, classification: .any,
minimumBounds: [0.5, 0.5]))
anchor.name = "PlaneAnchor"
let containsOrNot = arView.scene.anchors.contains(where: {
$0.name == "PlaneAnchor"
})
print(containsOrNot)
print(arView.scene.anchors.count)
print(arView.scene.anchors.first?.anchor?.id)https://stackoverflow.com/questions/61298228
复制相似问题