'ARKit/RealityKit – People Occlusion config not working

For some reason I can't get people occlusion to work, even though I looked at someone's question on Stackoverflow. Here is my code:

//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

What am I missing?



Solution 1:[1]

Your code should look like this:

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)
}


Or there's also a cool solution with type(of:) method:

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")
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1