How to respond to changes in immersion level

When using progressive immersion, our apps can respond to changes when the user turns the Digital Crown.

In this example, we create a particle emitter that uses the snow preset. Then we listen for changes to immersion level with the onImmersionChange modifier. We can use the amount provided by this modifier to as a multiplier for the speed and birth rate of the particle emitter.

struct ImmersiveView: View {

    @Environment(AppModel.self) private var appModel
    @State var immersionAmount: Float = 0

    var speed: Float = 1
    var birthRate: Float = 3000

    var body: some View {
        RealityView { content in

            // Create some snow!
            var snowParticles = ParticleEmitterComponent.Presets.snow
            snowParticles.speed = 0.5
            snowParticles.mainEmitter.birthRate = 2000
            snowParticles.emitterShape = .sphere
            snowParticles.emitterShapeSize = [5,5,5]
            snowParticles.mainEmitter.size = 0.01

            let snowEntity = Entity()
            snowEntity.name = "Snow"
            snowEntity.components.set(snowParticles)
            content.add(snowEntity)


        } update: { content in
            if let snow = content.entities.first(where: { $0.name == "Snow" }){
                // Get the particle component
                if var snowParticles = snow.components[ParticleEmitterComponent.self] {

                    // Adjust the speed and birthrate using the immersion amount
                    snowParticles.speed = speed * immersionAmount
                    snowParticles.mainEmitter.birthRate = birthRate * immersionAmount

                    // Reassign the component to the entity
                    snow.components.set(snowParticles)
                }
            }
        }
        .onImmersionChange { inintial, new in
            immersionAmount = Float(new.amount ?? 0)
        }
    }
}

Whenever the immersion level changes (the user turns the Digital Crown) onImmersionChange will update the state var immersionAmount. The RealityView update closure will respond to this change and will modify the particle component.

Note: I added a floor mesh here because without it I felt like I was falling through the universe…

Video demo

Video demo showing a fully immersed progressive space with dense snow particles. The user dials the immersion down and the particle system emits less snow.

Sample code is available in Garden17 in the Step Into Example Projects repo.

Support our work so we can continue to bring you new examples and articles.

Download the Xcode project with this and many more examples from Step Into Vision.
Some examples are provided as standalone Xcode projects. You can find those here.

Questions or feedback?