Sample CodevisionOSReviewed 2026-07-21View on Apple Developer

Diorama

At a glance

Item Summary
Purpose Design scenes for your visionOS app using Reality Composer Pro.
App architecture DioramaApp composes WindowGroup + ImmersiveSpace around ContentView; ViewModel holds shared experience state and RealityKit components and systems own per-entity behavior.
Main patterns SwiftUI scene composition, SwiftUI–RealityKit bridge, Explicit immersive-space lifecycle, Observable state owner, Entity-component-system, Provider session boundary
Project style Code-rich sample with 19 scanned Swift file(s) and 1186 Swift line(s); resources and generated assets are excluded from those counts.

Project structure

Source bundle/
├── Diorama/DioramaApp.swift  # DioramaApp, AppDelegate
├── Diorama/ViewModel.swift  # ViewModel
├── Packages/RealityKitContent/Sources/RealityKitContent/PointOfInterestComponent.swift  # Region, PointOfInterestComponent
├── Packages/RealityKitContent/Sources/RealityKitContent/BillboardComponent.swift  # BillboardComponent
├── Diorama/ContentView.swift  # ContentView
├── Diorama/Components.swift  # PointOfInterestRuntimeComponent, ControlledOpacityComponent
├── Diorama/FlockingComponent.swift  # FlockingComponent
├── Diorama/FlockingSystem.swift  # FlockingSystem
└── Packages/RealityKitContent/Package.realitycomposerpro/ProjectData/main.json  # authored RealityKit content

Structure observations

  • The runtime boundary is WindowGroup + ImmersiveSpace; the pruned tree lists only files that explain lifecycle, state, or framework integration.
  • App code is split into role-named views, models, managers, providers, components, or systems.
  • Authored .reality or Reality Composer Pro content is a real implementation boundary; Swift loads or drives it rather than reproducing its entity graph.

Overall architecture

Reference code

Diorama/DioramaApp.swift:14 — the app or executable entry declares the outer scene lifecycle.

struct DioramaApp: App {
    // ...
}

The diagram is a responsibility flow, not a claim that every adjacent node directly calls the next. It keeps scene ownership, shared state, RealityKit content, and framework-provider work at separate levels.

Ownership and state

Ownership evidence

Diorama/DioramaApp.swift:18 — representative stored state or the nearest verified lifecycle anchor.

@main
struct DioramaApp: App {
    // ...
    @State private var viewModel = ViewModel()
    // ...
}
Owner Object or state Relationship Mutation authority
DioramaApp ViewModel as viewModel creates and retains Only the declaring scope writes
ContentView ViewModel as viewModel stores and coordinates The owning type coordinates writes
BillboardSystem ARKitSession as arkitSession creates and retains Only the declaring scope writes
ViewModel Entity as rootEntity stores and coordinates The owning type coordinates writes

Ownership here is deliberately narrow: @Environment and weak references are shared links, initialized @State or stored services are lifecycle ownership, and a RealityView content closure owns additions to its entity graph without making the SwiftUI view a reference-type owner.

Class and protocol design

Type Responsibility Depends on or conforms to
DioramaApp (Diorama/DioramaApp.swift:14) Declares app scenes and top-level dependency lifetime. App
ContentView (Diorama/ContentView.swift:11) Presents UI and forwards gestures or lifecycle events. View
ViewModel (Diorama/ViewModel.swift:14) Owns UI-facing feature state and commands. Concrete framework collaborators
PointOfInterestRuntimeComponent (Diorama/Components.swift:11) Stores RealityKit entity data. Component
ControlledOpacityComponent (Diorama/Components.swift:15) Stores RealityKit entity data. Component
FlockingComponent (Diorama/FlockingComponent.swift:10) Stores RealityKit entity data. Component, Codable
FlockingSystem (Diorama/FlockingSystem.swift:12) Updates matching RealityKit entities. System

The source defines no local substitution protocol in the reviewed boundary. Its protocol use is framework-facing (App, View, RealityKit/ARKit protocols, or platform adapters), so this document does not label the whole app protocol-oriented.

Access control

Symbol Access Verified effect Likely rationale
@State private var viewModel = ViewModel() (Diorama/DioramaApp.swift:18) private Use is restricted to the declaration and same-file extensions permitted by Swift. Inference: Hide implementation details and lifecycle-sensitive state.
public var velocity = SIMD3<Float>(repeating: 0.0) (Diorama/FlockingComponent.swift:11) public The declaration is available to importing modules, subject to its containing type’s visibility. Inference: Satisfy a cross-target or framework-facing surface without implying subclassability.

No reviewed declaration uses fileprivate, private(set), open; unmodified Swift declarations are internal.

Reference code

Diorama/DioramaApp.swift:18 — representative visibility boundary.

@main
struct DioramaApp: App {
    // ...
    @State private var viewModel = ViewModel()
    // ...
}

Logic ownership and placement

Logic Owning type or file Placement rationale
Scene declaration and dependency lifetime DioramaApp The App/entry boundary determines window, volume, and immersive-space lifetime.
Presentation, attachments, and gestures ContentView SwiftUI view code forwards user intent and RealityView lifecycle events.
Shared feature state and commands ViewModel A role-named owner prevents sibling views from duplicating transitions.
Tracking authorization and update streams Packages/RealityKitContent/Sources/RealityKitContent/BillboardSystem.swift:16 Provider lifetime and async updates remain outside render-only view code.
Per-frame entity behavior Diorama/FlockingSystem.swift:12 RealityKit systems query component data instead of centralizing every entity update in SwiftUI.

Design patterns

Pattern Source evidence Purpose or tradeoff
SwiftUI scene composition Diorama/DioramaApp.swift:14 Keeps windows, volumes, and immersive-space lifecycle visible at the app boundary.
SwiftUI–RealityKit bridge Packages/RealityKitContent/Sources/RealityKitContent/BillboardSystem.swift:58 Builds and updates a RealityKit entity graph from SwiftUI lifecycle closures.
Explicit immersive-space lifecycle Diorama/DioramaApp.swift:43 Makes immersive presentation a scene transition rather than hidden global state.
Observable state owner Diorama/ViewModel.swift:14 Shares feature state across multiple views without moving framework resources into view values.
Entity-component-system Diorama/FlockingSystem.swift:12 Stores per-entity data in components and advances behavior in registered RealityKit systems.
Provider session boundary Packages/RealityKitContent/Sources/RealityKitContent/BillboardSystem.swift:16 Owns provider lifetime separately from the SwiftUI view tree.

Naming conventions

  • Role suffixes are evidence, not decoration: App: DioramaApp; ViewModel: ViewModel; Provider: AttachmentsProvider; Delegate: AppDelegate.
  • ECS names pair data and behavior: components PointOfInterestRuntimeComponent, ControlledOpacityComponent, FlockingComponent, BillboardComponent; systems FlockingSystem, BillboardSystem, TrailAnimationSystem.
  • Protocols: no app-defined protocol in the reviewed source.
  • Commands use verb-led methods: applicationShouldTerminateAfterLastWindowClosed, updateScale, updateRegionSpecificOpacity, setupAudio, resetAudio, updateTerrainMaterial, opacity, gain.
  • Files generally match their primary type; Views, Models, Managers, Providers, Components, Systems, and Packages folders describe architectural roles where present.

Architecture takeaways

  • Treat DioramaApp as the owner of scene declarations, not as the owner of every RealityKit entity created later.
  • Keep view-local interaction in SwiftUI, but move provider sessions, playback resources, shared game state, or transport state into a stable owner when their lifetime exceeds one render pass.
  • Run ARKit providers for the scene lifetime and consume their asynchronous updates in cancellable tasks; map anchors to entities at the boundary.
  • Use RealityKit components for per-entity data and systems for repeated simulation instead of a monolithic view model.
  • Model immersive-space open, transition, and close states explicitly so windows and immersive content cannot drift apart.

Source map

Source file Relevant symbols
Diorama/DioramaApp.swift:14 DioramaApp, AppDelegate
Diorama/ViewModel.swift:14 ViewModel
Packages/RealityKitContent/Sources/RealityKitContent/PointOfInterestComponent.swift:10 Region, PointOfInterestComponent
Packages/RealityKitContent/Sources/RealityKitContent/BillboardComponent.swift:9 BillboardComponent
Diorama/ContentView.swift:11 ContentView
Diorama/Components.swift:11 PointOfInterestRuntimeComponent, ControlledOpacityComponent
Diorama/FlockingComponent.swift:10 FlockingComponent
Diorama/FlockingSystem.swift:12 FlockingSystem