Sample CodevisionOSReviewed 2026-07-21View on Apple Developer

Composing interactive 3D content with RealityKit and Reality Composer Pro

At a glance

Item Summary
Purpose Build an interactive scene using an animation timeline.
App architecture RealityKitAnimationApp owns AppModel; ImmersiveView loads authored content and registered components/systems turn Reality Composer Pro entities into interactive runtime behavior.
Main patterns SwiftUI scene composition, UI–RealityKit bridge, Observable state owner, Entity-component-system, Authored-content boundary, Authored component-system bridge
Project style Code-rich sample with 12 scanned Swift file(s) and 1733 implementation line(s).

Project structure

Source bundle/
├── RealityKitAnimation/RealityKitAnimation/AppModel.swift  # ImmersiveSpaceState, AppModel
├── RealityKitAnimation/RealityKitAnimation/ImmersiveView.swift  # ImmersiveView
├── RealityKitAnimation/RealityKitAnimation/RealityKitAnimationApp.swift  # RealityKitAnimationApp
├── RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/HeroRobotComponent.swift  # HeroRobotState, RobotTravelHomeAction, RobotMoveToHomeComplete
├── RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/EntityMoverComponent.swift  # EntityMoverComponent, EntityMoverRuntimeComponent, EntityMoverSystem
├── RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/HeroPlantComponent.swift  # PlantState, HeroPlantComponent, HeroPlantRuntimeComponent
├── RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/StationaryRobotComponent.swift  # StationaryRobotComponent, StationaryRobotRuntimeComponent, StationaryRobotSystem
└── RealityKitAnimation/Packages/RealityKitContent/Package.realitycomposerpro/ProjectData/main.json  # authored RealityKit content

Structure observations

  • The runtime boundary is WindowGroup + volumetric window + ImmersiveSpace; the tree is pruned to lifecycle, state, framework, rendering, and ECS files.
  • The source is Swift; role-named types and feature folders separate the major responsibilities.
  • Authored .reality, .rkassets, or Reality Composer Pro content is an implementation boundary; Swift loads or drives it rather than reproducing its entity graph.

Overall architecture

Reference code

RealityKitAnimation/RealityKitAnimation/RealityKitAnimationApp.swift:11 — executable or app-lifecycle anchor.

struct RealityKitAnimationApp: App {
    // ...
}

The diagram is a responsibility flow, not a claim that every adjacent node directly calls the next. It separates lifecycle, presentation, stable state, framework/session work, and the RealityKit entity graph.

Ownership and state

Ownership evidence

RealityKitAnimation/RealityKitAnimation/RealityKitAnimationApp.swift:13 — representative stored state or nearest verified lifecycle anchor.

@main
struct RealityKitAnimationApp: App {
    // ...
    @State private var appModel = AppModel()
    // ...
}
Owner Object or state Relationship Mutation authority
RealityKitAnimationApp AppModel as appModel creates and retains The declaring type controls writes
ImmersiveView AppModel as appModel receives a shared or non-owning reference The upstream owner controls lifetime; this scope may invoke the exposed mutable API
AppModel ImmersiveSpaceState as immersiveSpaceState owns value state The owning type coordinates writes
HeroPlantRuntimeComponent AnimationPlaybackController as wiltingPlaybackController stores and coordinates The declaring type controls writes

Ownership is intentionally narrow: environment, bindings, observed objects, weak links, and delegate callbacks do not prove lifetime ownership. Initialized stored services and wrapper-managed state do; entities added inside a RealityKit content closure belong to that entity graph.

Class and protocol design

Type Responsibility Depends on or conforms to
RealityKitAnimationApp (RealityKitAnimation/RealityKitAnimation/RealityKitAnimationApp.swift:11) Declares app lifecycle and top-level dependency lifetime. App
ImmersiveView (RealityKitAnimation/RealityKitAnimation/ImmersiveView.swift:17) Presents UI and forwards gestures or lifecycle events. View
AppModel (RealityKitAnimation/RealityKitAnimation/AppModel.swift:18) Owns feature state and domain transitions. Concrete framework collaborators
EntityMoverSystem (RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/EntityMoverComponent.swift:94) Updates entities matching a RealityKit component query. System
EntityMoverComponent (RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/EntityMoverComponent.swift:14) Stores RealityKit entity data. Component, Codable
EntityMoverRuntimeComponent (RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/EntityMoverComponent.swift:29) Stores RealityKit entity data. Component
HeroPlantComponent (RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/HeroPlantComponent.swift:29) Stores RealityKit entity data. Component, Codable
HeroPlantRuntimeComponent (RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/HeroPlantComponent.swift:48) Stores RealityKit entity data. Component

The reviewed source defines no local substitution protocol. Its App, View, delegate, renderer, ARKit, and RealityKit conformances are framework-facing contracts, so this document does not label the whole app protocol-oriented.

Access control

Symbol Access Verified effect Likely rationale
private var settingsSource: EntityMoverComponent? = nil (RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/EntityMoverComponent.swift:31) private Use stays inside the declaration and same-file extensions permitted by Swift. Inference: Hide lifecycle-sensitive state or an implementation step.
public mutating func initialize(entity: Entity, (RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/EntityMoverComponent.swift:53) public The symbol is available to importing modules, subject to its containing type’s visibility. Inference: Expose an intentional package or cross-target surface.
internal var settingsSource: HeroRobotComponent? = nil (RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/HeroRobotComponent.swift:49) internal With no narrower modifier, Swift keeps the declaration internal to the module. Inference: Allow app-target collaboration without exporting a library API.

No reviewed Swift access example uses fileprivate, private(set), open; declarations without a modifier are internal.

Reference code

RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/EntityMoverComponent.swift:31 — representative visibility boundary.

    private var settingsSource: EntityMoverComponent? = nil

    // The x, y, z extent of movement for the current frame.
    private var currentExtentX: Float = 0.0
    private var currentExtentY: Float = 0.0
    private var currentExtentZ: Float = 0.0

Logic ownership and placement

Logic Owning type or file Placement rationale
Application/command lifecycle RealityKitAnimationApp The executable boundary determines app, controller, scene, or command lifetime.
Presentation and user intent ImmersiveView The view/controller forwards input and owns only presentation-local work.
Shared feature state and commands AppModel A stable owner prevents view recomputation or callbacks from duplicating transitions.
Framework/session/render coordination EntityMoverSystem The role-named collaborator isolates long-lived or per-frame work from presentation code.
Per-entity data and repeated behavior RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/EntityMoverComponent.swift:94 Components carry data; the system queries and updates matching entities.
Authored scene composition RealityKitAnimation/Packages/RealityKitContent/Package.realitycomposerpro/ProjectData/main.json Reality Composer Pro owns hierarchy, materials, animation, or behavior that Swift loads and coordinates.

Design patterns

Pattern Source evidence Purpose or tradeoff
SwiftUI scene composition RealityKitAnimation/RealityKitAnimation/RealityKitAnimationApp.swift:11 Keeps windows, volumes, and immersive presentation visible at the app boundary.
UI–RealityKit bridge RealityKitAnimation/RealityKitAnimation/ImmersiveView.swift:24 Builds or hosts the RealityKit entity graph from SwiftUI or a platform view/controller lifecycle.
Observable state owner RealityKitAnimation/RealityKitAnimation/AppModel.swift:18 Keeps shared feature state in a stable owner rather than in transient view values.
Entity-component-system RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/EntityMoverComponent.swift:94 Stores per-entity data in components and advances repeated behavior in registered systems.
Authored-content boundary RealityKitAnimation/RealityKitAnimation/ImmersiveView.swift:11 Treats Reality Composer Pro assets as implementation and keeps Swift responsible for loading and runtime coordination.
Authored component-system bridge RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/EntityMoverComponent.swift:14 Attaches authored component data to entities and runs the corresponding behavior in registered systems.

Naming conventions

  • Role suffixes are evidence, not decoration: App: RealityKitAnimationApp; Model: AppModel; System: EntityMoverSystem, HeroPlantSystem, HeroRobotSystem, StationaryRobotSystem; Component: EntityMoverComponent, EntityMoverRuntimeComponent, HeroPlantComponent, HeroPlantRuntimeComponent, HeroRobotComponent; View: ContentView, ImmersiveView.
  • ECS names pair data and behavior: components EntityMoverComponent, EntityMoverRuntimeComponent, HeroPlantComponent, HeroPlantRuntimeComponent, HeroRobotComponent; systems EntityMoverSystem, HeroPlantSystem, HeroRobotSystem, StationaryRobotSystem.
  • Protocols: no app-defined protocol in the reviewed source.
  • Commands use verb-led methods: getHeroRobotIfAvailable, getHeroRobot, isPlantUnhealthy, isAvailable, setState, enableIK, calculateReachPosition, initialize.
  • Files and folders use primary-type or responsibility names such as Views, Models, Rendering, Components, Systems, Packages, or feature names where those boundaries exist.

Architecture takeaways

  • Treat RealityKitAnimationApp as the owner of executable or scene lifecycle, not automatically as the owner of every entity created later.
  • Keep view/controller input near presentation, but move sessions, reconstruction, capture, rendering, shared game state, or documents into stable owners when their lifetime exceeds one callback or render pass.
  • Use components for per-entity data and systems for repeated simulation instead of centralizing every update in a view model or controller.
  • Count authored Reality Composer Pro assets as implementation; the Swift shell is not the complete architecture.

Source map

Source file Relevant symbols
RealityKitAnimation/RealityKitAnimation/AppModel.swift:10 ImmersiveSpaceState, AppModel
RealityKitAnimation/RealityKitAnimation/ImmersiveView.swift:17 ImmersiveView
RealityKitAnimation/RealityKitAnimation/RealityKitAnimationApp.swift:11 RealityKitAnimationApp
RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/HeroRobotComponent.swift:14 HeroRobotState, RobotTravelHomeAction, RobotMoveToHomeComplete, HeroRobotComponent, HeroRobotRuntimeComponent, HeroRobotSystem
RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/EntityMoverComponent.swift:14 EntityMoverComponent, EntityMoverRuntimeComponent, EntityMoverSystem
RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/HeroPlantComponent.swift:13 PlantState, HeroPlantComponent, HeroPlantRuntimeComponent, HeroPlantSystem
RealityKitAnimation/Packages/RealityKitContent/Sources/RealityKitContent/StationaryRobotComponent.swift:17 StationaryRobotComponent, StationaryRobotRuntimeComponent, StationaryRobotSystem