Sample CodevisionOSReviewed 2026-07-21View on Apple Developer

Simulating particles in your visionOS app

At a glance

Item Summary
Purpose Add a range of visual effects to a RealityKit view by attaching a particle emitter component to an entity.
App architecture A Swift sample with the source-visible chain ParticleEmitterAppMainViewGeneralGoalSystemRealityKit APIs.
Main patterns SwiftUI environment injection, Binding-based state propagation
Project style 12 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: @MainActor, Task closure isolated to MainActor, Task; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, @Observable, ObservableObject.
Key frameworks/packages RealityKit, SwiftUI, PackageDescription, RealityGestures; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── Particle Emitter/
│   ├── ParticleEmitterApp.swift
│   ├── EmitterView.swift
│   ├── MainView.swift
│   └── EmitterSettings.swift
├── Packages/
│   └── RealityGestures/
│       ├── Sources/
│       │   └── RealityGestures/
│       │       ├── RealityDragComponent.swift
│       │       ├── GeneralGoalComponent.swift
│       │       ├── RealityTapComponent.swift
│       │       ├── SwiftUI+RealityGestures.swift
│       │       └── RealityDragComponent+DragEvents.swift
│       └── Package.swift
├── EmitterControls.swift
└── LabelSlider.swift

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 5 project/configuration file(s) and 19 source declaration(s).

Overall architecture

Reference code

Particle Emitter/ParticleEmitterApp.swift:10 — architecture anchor

@main
struct ParticleEmitterApp: App {
    /// Stores settings for the particle emitter.
    @ObservedObject public var emitterSettings = EmitterSettings()

    var body: some Scene {
        WindowGroup {
            MainView()
        }.environment(emitterSettings)
    }
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into RealityKit.

Ownership and state

Ownership evidence

Particle Emitter/ParticleEmitterApp.swift:13 — stored dependency or nearest verified ownership anchor

@main
struct ParticleEmitterApp: App {
    // ...
    @ObservedObject public var emitterSettings = EmitterSettings()
    // ...
}
Owner Object or state Relationship Mutation authority
ParticleEmitterApp EmitterSettings (emitterSettings) observes externally owned state The observed object is authoritative
RealityDragComponent Rotationlocked (rotationLocked) stores or receives App/module collaborators
RealityDragComponent Clamp (clamp) stores or receives App/module collaborators
RealityDragComponent Dragupdate (dragUpdate) stores or receives App/module collaborators

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Concurrency, scheduling, and thread safety

Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.

Concern Source mechanism Verified placement or handoff Evidence
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. Packages/RealityGestures/Sources/RealityGestures/RealityDragComponent.swift:24
Main isolation Task closure isolated to MainActor The cited operation explicitly enters a main-actor-isolated region. Packages/RealityGestures/Sources/RealityGestures/SwiftUI+RealityGestures.swift:64
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. Packages/RealityGestures/Sources/RealityGestures/SwiftUI+RealityGestures.swift:64

@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.

Reference code

Packages/RealityGestures/Sources/RealityGestures/RealityDragComponent.swift:24 — representative execution boundary

    public init(
        // ...
    ) {
        // ...
    }

State propagation, frameworks, and dependencies

Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.

Category Mechanism or module Verified role Evidence
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. EmitterControls.swift:20
State propagation @Observable Observation macro publishes source-visible changes. Particle Emitter/EmitterSettings.swift:15
State propagation ObservableObject ObservableObject supplies an observation contract. Particle Emitter/EmitterSettings.swift:16
Source import RealityKit The cited file imports this module; runtime use and architectural role are not inferred. EmitterControls.swift:9
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. EmitterControls.swift:8
Source import PackageDescription The cited file imports this module; runtime use and architectural role are not inferred. Packages/RealityGestures/Package.swift:10
Source import RealityGestures The cited file imports this module; runtime use and architectural role are not inferred. Particle Emitter/EmitterView.swift:10

receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.

Class and protocol design

Particle Emitter/ParticleEmitterApp.swift:11 — representative type boundary

@main
struct ParticleEmitterApp: App {
    // ...
    @ObservedObject public var emitterSettings = EmitterSettings()
    // ...
}
Type Responsibility Depends on or conforms to
ParticleEmitterApp Application entry and top-level composition App
RealityDragComponent Stores entity-component data or behavior Component
GeneralGoalComponent Stores entity-component data or behavior Component, Codable
GeneralGoalSystem Runs entity-component-system update logic System
RealityTapComponent Stores entity-component data or behavior Component
EmitterView User-interface presentation and input forwarding View
MainView User-interface presentation and input forwarding View
MoveConstraint Defines a closed set of feature states or choices Concrete collaborators/imported frameworks
DragComponentType Defines a closed set of feature states or choices Concrete collaborators/imported frameworks
DragStatus Defines a closed set of feature states or choices Concrete collaborators/imported frameworks

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
body (EmitterControls.swift:23) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
body (LabelSlider.swift:29) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
linear (Packages/RealityGestures/Sources/RealityGestures/GeneralGoalComponent.swift:14) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
angular (Packages/RealityGestures/Sources/RealityGestures/GeneralGoalComponent.swift:15) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.

Reference code

EmitterControls.swift:23 — representative boundary

    public var body: some View {
        // ...
                presetSection
        // ...
    }

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Application entry and top-level composition ParticleEmitterApp The source’s App suffix makes this role explicit.
Stores entity-component data or behavior GeneralGoalComponent, RealityDragComponent, RealityTapComponent The source’s Component suffix makes this role explicit.
Runs entity-component-system update logic GeneralGoalSystem The source’s System suffix makes this role explicit.
User-interface presentation and input forwarding EmitterView, MainView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
SwiftUI environment injection EmitterControls.swift:20 The environment supplies state or a capability without threading it through every initializer.
Binding-based state propagation LabelSlider.swift:20 A binding exposes controlled read/write access while the upstream owner remains authoritative.

Naming conventions

  • Types: App: ParticleEmitterApp; Component: GeneralGoalComponent, RealityDragComponent, RealityTapComponent; System: GeneralGoalSystem; View: EmitterView, MainView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: getCollisionPoints, getClampedPosition, getClampedRotation, handleMoveState, handleRotationState, closestPoint, clamp, get.
  • Files: Particle Emitter/ParticleEmitterApp.swift, Packages/RealityGestures/Sources/RealityGestures/RealityDragComponent.swift, Packages/RealityGestures/Sources/RealityGestures/GeneralGoalComponent.swift, Packages/RealityGestures/Sources/RealityGestures/RealityTapComponent.swift, Particle Emitter/EmitterView.swift, Particle Emitter/MainView.swift.

Architecture takeaways

  • ParticleEmitterApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches RealityKit, SwiftUI, PackageDescription, RealityGestures through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
Particle Emitter/ParticleEmitterApp.swift Cited implementation, ParticleEmitterApp
EmitterControls.swift Cited implementation, SwiftUI state property wrapper, RealityKit, SwiftUI, EmitterControls
LabelSlider.swift Cited implementation, LabelSlider
Packages/RealityGestures/Sources/RealityGestures/GeneralGoalComponent.swift Cited implementation, GeneralGoalComponent, GeneralGoal, GeneralGoalSystem
Packages/RealityGestures/Sources/RealityGestures/RealityDragComponent.swift @MainActor, RealityDragComponent, MoveConstraint, DragComponentType, DragStatus, DragData
Packages/RealityGestures/Sources/RealityGestures/SwiftUI+RealityGestures.swift Task closure isolated to MainActor, Task, RealityTapGesture, RealityDragGesture
Particle Emitter/EmitterSettings.swift @Observable, ObservableObject, EmitterSettings, EmitterPresets, ColorSetting
Packages/RealityGestures/Package.swift PackageDescription, Feature implementation
Particle Emitter/EmitterView.swift RealityGestures, EmitterView
Packages/RealityGestures/Sources/RealityGestures/RealityTapComponent.swift RealityTapComponent
Particle Emitter/MainView.swift MainView
Packages/RealityGestures/Sources/RealityGestures/RealityDragComponent+DragEvents.swift Feature implementation