swift - RealityKit – 平面检测
问题描述
在与 ARKit 类似的 RealityKit 中,物体在相机检测到某种平面之前不会显示。一旦相机检测到该表面,对象将显示并固定在该表面上。
我如何(通过代码)知道相机是否检测到平面?实际上,我想突出显示可选区域,但我不确定 RealityKit 是否真的允许你这样做,我知道 SceneKit 可以。
解决方案
plane
RealityKit 中有一个用于此目的的初始化程序(和枚举案例):
convenience init(plane alignment: AnchoringComponent.Target.Alignment,
classification: AnchoringComponent.Target.Classification,
minimumBounds: SIMD2<Float>)
/* Where `minimumBounds` is the minimum size of the target plane */
它是 ARKit 的ARPlaneAnchor
withextent
属性的对应物(即检测到的平面的估计宽度和长度)。但在 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)
推荐阅读
- python-3.x - 连续 vs n = 256 个离散彩条 python
- rust - 如何在不使用 OptionFuture 的情况下在 `Option::and_then` 或 `Option::map` 的闭包中使用 async/await?
- express - 无法在浏览器中设置 cookie
- mysql - 如何在 SQL 数据库中组织嵌套的 JSON 对象数据?
- python - 更改 DataFrame 中的值,其中相同的值满足一个条件,而前一个值满足另一个条件
- macos - 如何在 MacOS Monterey 上修复 VS COde“致命错误:运行时:bsdthread_register 错误”
- macros - imageJ 上的宏,循环不会移动到下一个 ROI
- c++ - 我应该如何解决我在 OnlineGBD 上收到的这个错误?
- javascript - 我正在将表中的数据导出为 CSV 和 JavaScript 变量。. 但是得到错误 "fputcsv() 期望参数 2 是数组" 。怎么做?
- apache-flink - CEP 是否可以将事件与复杂模式关联起来?