Reality Kit - Occlusion

Hi, I have a question, is it possible to create an occlusion material that hide only specific entities. For example I would. like to create a mask that hides the an cube entity which is in front of another sphere entity and would like to be able to see the sphere but not the cube trough occlusion.

Answered by Vision Pro Engineer in 799128022

You could combine OcclusionMaterial and ModelSortGroupComponent to choose which entities it occludes. For example starting with this setup of three cubes:

You can replace the middle cube's material with OcclusionMaterial, and add the following ModelSortGroup component to both it and the red cube to the left:

let modelSortGroup = ModelSortGroup(depthPass: .prePass)
redEntity.components.set(ModelSortGroupComponent(group: modelSortGroup, order: 1))
hiderEntity.components.set(ModelSortGroupComponent(group: modelSortGroup, order: 0))

You get the following result:

The docs for ModelSortGroupComponent have a lot of detail there to help explain how you can take full advantage of it: https://developer.apple.com/documentation/realitykit/modelsortgroupcomponent


Full RealityView code:

RealityView { content in
    let redEntity = Entity()
    redEntity.position.x = -0.06
    redEntity.components.set(
        ModelComponent(mesh: .generateBox(size: 0.1), materials: [UnlitMaterial(color: .red)])
    )
    let blueEntity = Entity()
    blueEntity.position.x = 0.06
    blueEntity.components.set(
        ModelComponent(mesh: .generateBox(size: 0.1), materials: [UnlitMaterial(color: .blue)])
    )
    let hiderEntity = Entity()
    hiderEntity.components.set(
        ModelComponent(mesh: .generateBox(size: 0.125), materials: [OcclusionMaterial()])
    )
    content.add(redEntity)
    content.add(blueEntity)
    content.add(hiderEntity)

    let modelSortGroup = ModelSortGroup(depthPass: .prePass)
    redEntity.components.set(ModelSortGroupComponent(group: modelSortGroup, order: 1))
    hiderEntity.components.set(ModelSortGroupComponent(group: modelSortGroup, order: 0))
}
Accepted Answer

You could combine OcclusionMaterial and ModelSortGroupComponent to choose which entities it occludes. For example starting with this setup of three cubes:

You can replace the middle cube's material with OcclusionMaterial, and add the following ModelSortGroup component to both it and the red cube to the left:

let modelSortGroup = ModelSortGroup(depthPass: .prePass)
redEntity.components.set(ModelSortGroupComponent(group: modelSortGroup, order: 1))
hiderEntity.components.set(ModelSortGroupComponent(group: modelSortGroup, order: 0))

You get the following result:

The docs for ModelSortGroupComponent have a lot of detail there to help explain how you can take full advantage of it: https://developer.apple.com/documentation/realitykit/modelsortgroupcomponent


Full RealityView code:

RealityView { content in
    let redEntity = Entity()
    redEntity.position.x = -0.06
    redEntity.components.set(
        ModelComponent(mesh: .generateBox(size: 0.1), materials: [UnlitMaterial(color: .red)])
    )
    let blueEntity = Entity()
    blueEntity.position.x = 0.06
    blueEntity.components.set(
        ModelComponent(mesh: .generateBox(size: 0.1), materials: [UnlitMaterial(color: .blue)])
    )
    let hiderEntity = Entity()
    hiderEntity.components.set(
        ModelComponent(mesh: .generateBox(size: 0.125), materials: [OcclusionMaterial()])
    )
    content.add(redEntity)
    content.add(blueEntity)
    content.add(hiderEntity)

    let modelSortGroup = ModelSortGroup(depthPass: .prePass)
    redEntity.components.set(ModelSortGroupComponent(group: modelSortGroup, order: 1))
    hiderEntity.components.set(ModelSortGroupComponent(group: modelSortGroup, order: 0))
}
Reality Kit - Occlusion
 
 
Q