Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ARKit – Environment Occlusion

In Unity we can implement occlusion with Environment Depth. Which uses ARKit behind the scene. How can I achieve same behaviour in iOS ARkit.

I know we can configure frame semantics with depth, but I doubt it is really same as unity environment depth occlusion?

// Build the set of required frame semantics.
let semantics: ARConfiguration.FrameSemantics = [.sceneDepth]
configuration.frameSemantics = semantics

session.run(configuration)
like image 728
Haris Avatar asked Sep 18 '25 13:09

Haris


1 Answers

In ARKit implement sceneReconstruction option, and in RealityKit turn on .occlusion.

The only drawback is an ugly mask with soft dilated edges around real-world objects...

import RealityKit
import SwiftUI
import ARKit

struct ContentView: View {
    var body: some View {
        return ARContainer().ignoresSafeArea()
    }
}

struct ARContainer: UIViewRepresentable {

    func makeUIView(context: Context) -> ARView {  
        let arView = ARView(frame: .zero)
        arView.cameraMode = .ar
                
        arView.automaticallyConfigureSession = false
        let config = ARWorldTrackingConfiguration()
        config.sceneReconstruction = .mesh
        arView.session.run(config)

        arView.environment.sceneUnderstanding.options = .occlusion

        let box: MeshResource = .generateBox(size: 0.5)
        let material = SimpleMaterial(color: .green, isMetallic: true)
        let entity = ModelEntity(mesh: box, materials: [material])
        let anchor = AnchorEntity(world: [0,0,-1.5])
        anchor.addChild(entity)
        arView.scene.anchors.append(anchor)            
        return arView
    }       
    func updateUIView(_ uiView: ARView, context: Context) { }
}
like image 73
Andy Jazz Avatar answered Sep 20 '25 05:09

Andy Jazz