ARKit/RealityKit - 人员遮挡配置无法工作

3

由于某些原因,我无法让人物遮挡(people occlusion)起作用,尽管我看了Stackoverflow上的某人的问题。以下是我的代码:

//Load ARView
let arView = ARView(frame: .zero)

//Load people occlusion
let session = ARSession()

if let configuration = session.configuration as? ARWorldTrackingConfiguration {
    configuration.frameSemantics.insert(.personSegmentationWithDepth)
    session.run(configuration)
}

//Load custom model(not in use)
let model = try! Entity.loadModel(named: "Mug")

//Load Anchor + Entity
let anchor = AnchorEntity(plane: .horizontal)
let box = MeshResource.generateBox(size: 0.1)
let material = SimpleMaterial(color: .red, isMetallic: true)
let entity = ModelEntity(mesh: box, materials: [material])
arView.scene.anchors.append(anchor)
anchor.addChild(entity)
return arView

我错过了什么?
1个回答

1

你的代码应该长这样:

let arView = ARView(frame: .zero)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    self.switchOcclusion()
}
    
fileprivate func switchOcclusion() {
        
    guard let config = arView.session.configuration as ARWorldTrackingConfiguration
    else { return }
        
    guard ARWorldTrackingConfiguration.supportsFrameSemantics(.personSegmentationWithDepth)
    else { return }
        
    switch config.frameSemantics {            
        case [.personSegmentationWithDepth]: config.frameSemantics.remove(.personSegmentationWithDepth)                    
        default: config.frameSemantics.insert(.personSegmentationWithDepth)
    }           
    arView.session.run(config)
}


还有一种很酷的解决方案,使用 type(of:) 方法:

let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal]
arView.session.run(config)

if type(of: config).supportsFrameSemantics(.sceneDepth) {
    config.frameSemantics = .personSegmentationWithDepth
} else {
    print("This device doesn't support segmentation with depth")
}

由于某些原因,我尝试按照您的回答进行操作,但即使我更改为通用iOS设备,我仍然会收到“类型'ARViewContainer'的值没有成员'arView'” 的错误提示。 - Elementio 21809
1
@AndyFedoroff 第三代11英寸iPad Pro。上个月购买的。 - user3064538
@AndyFedoroff,您介意解释一下为什么需要使用touchesBegan吗?我不想等待用户输入才启用遮挡。我希望尽快启用它。 - user3064538
请注意,仅在启动会话后一段时间进行相机跟踪过程和同步上传模型后才能从深度API开始获取深度数据。深度数据的生成并不始于ARSession的第一帧,因为遮挡来自ARKit的第二阶段——场景理解... - Andy Jazz
1
尝试像这样编写代码 - 在委托方法renderer(...)session(...)中使用 let frame = arView.session.currentFrame,然后使用 guard frame?.estimatedDepthData != nil else { print("depth is being captured") return } - Andy Jazz
显示剩余9条评论

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