Particle system in RealityKit
Asked Answered
S

1

1

Is there a particle system in RealityKit? if so, can someone point me to the correct documentation/articles?

So far I did not find any particle systems in the RealityKit module.

Smithery answered 14/6, 2023 at 14:23 Comment(0)
S
8

Particle System in RealityKit

@available(visionOS 1.0, macOS 15.0, iOS 18.0, *)
public struct ParticleEmitterComponent : Component, Codable

At WWDC'23, it was announced that RealityKit and Reality Composer Pro will get their own high-level Particle System with an accompanying set of parameters. For those who do not know what particles are, I could say that it's a 3D tool for creating and rendering such phenomena as rain, snow, flying dust and debris, fire, sparkles, smoke, fallen leaves, etc.

enter image description here

Here's my code for a Volume initial scene type. Read this post if you need to change a Volume's size.

import SwiftUI
import RealityKit

struct ContentView: View {
    var body: some View {
        
        RealityView { content in
            let model = ModelEntity()
            model.components.set(particleSystem())
            content.add(model)
        }
    }        
    func particleSystem() -> ParticleEmitterComponent {
        var particles = ParticleEmitterComponent()
        particles.emitterShape = .sphere
        particles.emitterShapeSize = [1,1,1] * 0.05

        particles.mainEmitter.birthRate = 2000
        particles.mainEmitter.size = 0.05
        particles.mainEmitter.lifeSpan = 0.5
        particles.mainEmitter.color = .evolving(start: .single(.white),
                                                  end: .single(.cyan))
        return particles
    }
}
#Preview {
    ContentView()
}

Retrieving particles from Reality Composer Pro scene

You can easily change/customize parameters of Particle System imported from Reality Composer Pro scene. Here's the code where I used a content of RealityView's update closure:

enter image description here

import SwiftUI
import RealityKit
import RealityKitContent

struct ContentView: View {
    var body: some View {
        VStack {
            RealityView { content in
                if let scene = try? await Entity(named: "Scene",
                                                    in: realityKitContentBundle) {
                    content.add(scene)
                    print(scene)
                }
            } update: { content in
                if let scene = content.entities.first {
                    let particles = scene.findEntity(named: "ParticleEmitter")
                    var particleComp = particles?.components[ParticleEmitterComponent.self]
                    particleComp?.speed = 0.5
                    particleComp?.mainEmitter.blendMode = .additive
                    particleComp?.mainEmitter.color = .constant(.single(.red))
                    particleComp?.mainEmitter.birthRate = 2000
                    particleComp?.mainEmitter.isLightingEnabled = true
                    particleComp?.mainEmitter.size = 0.2
                    particles?.components.set(particleComp!)
                }
            }
        }
    }
}
#Preview {
    ContentView()
}

enter image description here


Loading USDZ file with particles using Model3D view

Additionally, you are capable of using SwiftUI's Model3D view to asynchronously load a .usdz scene containing not only geometry but also our particles.

import SwiftUI
import RealityKit

struct ContentView: View {
    var body: some View {
        Model3D(named: "pEmitter.usdz") {
            if let model = $0.model {
                model
            } else {
                Color.clear
            }
        }
    }
}
#Preview {
    ContentView()
}
Skit answered 14/6, 2023 at 14:26 Comment(3)
I'm told in one of the slack sessions that this new particle system in RealityKit is only available on visionOSCerveny
What is the best way to do this in a non visionOS app?Trifocals
There are no particles in iOS RealityKit app.Skit

© 2022 - 2025 — McMap. All rights reserved.