swiftuirealitykitvisionos

Rendering order of Entities in RealityKit and Vision OS


In SceneKit you can override the rendering order of nodes, so regardless of their position in space, some things will always be drawn on top of others. But I cannot find any way to do this in realityKit. Is there a way of doing this with post rendering effects, or do you have to convert the whole thing to Metal and build your own engine?


Solution

  • Models' rendering order in RealityKit 4.0

    In order to control the rendering order of your 3D models in visionOS, iOS and macOS apps, you need to create a ModelSortGroup object and pass it to ModelSortGroupComponent specifying the rendering order in Int32 type. The .prePass and .postPass values ​​in ModelSortGroup instructs the renderer to draw the depth of a model only before or after it draws the colors in the group.

    import SwiftUI
    import RealityKit
    
    struct ContentView : View {
        let ball = ModelEntity(mesh: .generateSphere(radius: 0.125))
        let cube = ModelEntity(mesh: .generateBox(size: 0.15))
        let group = ModelSortGroup(depthPass: .postPass)
    
        var body: some View {
            RealityView { rvc in
                // BALL
                ball.model?.materials = [UnlitMaterial(color: .black)]
                ball.position = [0.0, 2.0,-1.0]
                let ballComponent = ModelSortGroupComponent(group: group, order: 0)
                ball.components.set(ballComponent)
                rvc.add(ball)
                
                // CUBE
                cube.model?.materials = [UnlitMaterial(color: .cyan)]
                cube.position = [0.0, 2.0,-1.3]
                let cubeComponent = ModelSortGroupComponent(group: group, order: 1)
                cube.components.set(cubeComponent)
                rvc.add(cube)
            }
        }
    }
    

    P. S.

    Note that RealityKit's default purple striped material doesn't work with ModelSortGroup.