Object tracking with Reality Composer Pro experiences
At a glance
| Item | Summary |
|---|---|
| Purpose | Use object tracking in visionOS to attach digital content to real objects to create engaging experiences. |
| App architecture | ObjectTrackingExperiencesSampleApp composes WindowGroup + ImmersiveSpace around ContentView; AppModel coordinates ARKit provider updates and maps anchors into RealityKit content. |
| 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 11 scanned Swift file(s) and 750 Swift line(s); resources and generated assets are excluded from those counts. |
Project structure
Source bundle/
├── ObjectTrackingExperiencesSample/ObjectTrackingExperiencesSampleApp.swift # ObjectTrackingExperiencesSampleApp
├── ObjectTrackingExperiencesSample/Game/KeyboardGame.swift # KeyboardGame, Challenge, Outcome
├── ObjectTrackingExperiencesSample/AppModel.swift # AppModel, ImmersiveSpaceState
├── ObjectTrackingExperiencesSample/Utils/FadeOutComponent.swift # FadeOutComponent, FadeOutSystem
├── ObjectTrackingExperiencesSample/Views/ContentView.swift # ContentView
├── ObjectTrackingExperiencesSample/Views/ImmersiveView.swift # ImmersiveView
├── ObjectTrackingExperiencesSample/Utils/ObjectTrackingGuide.swift # ObjectTrackingGuide
├── Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.swift
└── Packages/RealityKitContent/Package.realitycomposerpro/PluginData/AF09ED6F-1707-48FD-8720-65B998362C09/ShaderGraphEditorPluginID/ShaderGraphEditorPluginID # 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
.realityor Reality Composer Pro content is a real implementation boundary; Swift loads or drives it rather than reproducing its entity graph.
Overall architecture
flowchart LR
ObjectTrackingExperiencesSampleApp_1["ObjectTrackingExperiencesSampleApp"]
WindowGroup___ImmersiveSpace_2["WindowGroup + ImmersiveSpace"]
ContentView_3["ContentView"]
AppModel_4["AppModel"]
RealityView_entity_graph_5["RealityView entity graph"]
ARKit_providers___RealityKit_6["ARKit providers + RealityKit"]
ObjectTrackingExperiencesSampleApp_1 --> WindowGroup___ImmersiveSpace_2
WindowGroup___ImmersiveSpace_2 --> ContentView_3
ContentView_3 --> AppModel_4
AppModel_4 --> RealityView_entity_graph_5
RealityView_entity_graph_5 --> ARKit_providers___RealityKit_6
Reference code
ObjectTrackingExperiencesSample/ObjectTrackingExperiencesSampleApp.swift:10 — the app or executable entry declares the outer scene lifecycle.
struct ObjectTrackingExperiencesSampleApp: 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
classDiagram
ObjectTrackingExperiencesSampleApp *-- AppModel : appModel
ContentView o-- AppModel : appModel
ContentView o-- ScenePhase : scenePhase
ObjectTrackingGuide *-- ARKitSession : arSession
Ownership evidence
ObjectTrackingExperiencesSample/ObjectTrackingExperiencesSampleApp.swift:12 — representative stored state or the nearest verified lifecycle anchor.
@main
struct ObjectTrackingExperiencesSampleApp: App {
// ...
@State private var appModel = AppModel()
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
ObjectTrackingExperiencesSampleApp |
AppModel as appModel |
creates and retains | Only the declaring scope writes |
ContentView |
AppModel as appModel |
receives a shared/non-owning reference | The upstream owner controls lifetime; this scope may invoke its mutable API |
ContentView |
ScenePhase as scenePhase |
receives a shared/non-owning reference | The upstream owner controls lifetime; this scope may invoke its mutable API |
ObjectTrackingGuide |
ARKitSession as arSession |
creates and retains | Only the declaring scope 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 |
|---|---|---|
ObjectTrackingExperiencesSampleApp (ObjectTrackingExperiencesSample/ObjectTrackingExperiencesSampleApp.swift:10) |
Declares app scenes and top-level dependency lifetime. | App |
ContentView (ObjectTrackingExperiencesSample/Views/ContentView.swift:9) |
Presents UI and forwards gestures or lifecycle events. | View |
AppModel (ObjectTrackingExperiencesSample/AppModel.swift:10) |
Owns observable feature state and domain transitions. | Concrete framework collaborators |
FadeOutComponent (ObjectTrackingExperiencesSample/Utils/FadeOutComponent.swift:12) |
Stores RealityKit entity data. | Component |
FadeOutSystem (ObjectTrackingExperiencesSample/Utils/FadeOutComponent.swift:21) |
Updates matching RealityKit entities. | System |
ImmersiveView (ObjectTrackingExperiencesSample/Views/ImmersiveView.swift:14) |
Presents UI and forwards gestures or lifecycle events. | View |
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 appModel = AppModel() (ObjectTrackingExperiencesSample/ObjectTrackingExperiencesSampleApp.swift:12) |
private |
Use is restricted to the declaration and same-file extensions permitted by Swift. | Inference: Hide implementation details and lifecycle-sensitive state. |
public let realityKitContentBundle = Bundle.module (Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.swift:9) |
public |
The declaration is available to importing modules, subject to its containing type’s visibility. | Inference: Export the type across the package-module boundary. |
No reviewed declaration uses fileprivate, private(set), open; unmodified Swift declarations are internal.
Reference code
ObjectTrackingExperiencesSample/ObjectTrackingExperiencesSampleApp.swift:12 — representative visibility boundary.
@main
struct ObjectTrackingExperiencesSampleApp: App {
// ...
@State private var appModel = AppModel()
// ...
}Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Scene declaration and dependency lifetime | ObjectTrackingExperiencesSampleApp |
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 | AppModel |
A role-named owner prevents sibling views from duplicating transitions. |
| Tracking authorization and update streams | ObjectTrackingExperiencesSample/Utils/ObjectTrackingGuide.swift:30 |
Provider lifetime and async updates remain outside render-only view code. |
| Per-frame entity behavior | ObjectTrackingExperiencesSample/Utils/FadeOutComponent.swift:21 |
RealityKit systems query component data instead of centralizing every entity update in SwiftUI. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| SwiftUI scene composition | ObjectTrackingExperiencesSample/ObjectTrackingExperiencesSampleApp.swift:10 |
Keeps windows, volumes, and immersive-space lifecycle visible at the app boundary. |
| SwiftUI–RealityKit bridge | ObjectTrackingExperiencesSample/Views/ImmersiveView.swift:48 |
Builds and updates a RealityKit entity graph from SwiftUI lifecycle closures. |
| Explicit immersive-space lifecycle | ObjectTrackingExperiencesSample/ObjectTrackingExperiencesSampleApp.swift:20 |
Makes immersive presentation a scene transition rather than hidden global state. |
| Observable state owner | ObjectTrackingExperiencesSample/AppModel.swift:10 |
Shares feature state across multiple views without moving framework resources into view values. |
| Entity-component-system | ObjectTrackingExperiencesSample/Utils/FadeOutComponent.swift:21 |
Stores per-entity data in components and advances behavior in registered RealityKit systems. |
| Provider session boundary | ObjectTrackingExperiencesSample/Utils/ObjectTrackingGuide.swift:30 |
Owns provider lifetime separately from the SwiftUI view tree. |
Naming conventions
- Role suffixes are evidence, not decoration: App:
ObjectTrackingExperiencesSampleApp; Model:AppModel; View:ContentView,ImmersiveView. - ECS names pair data and behavior: components
FadeOutComponent; systemsFadeOutSystem. - Protocols: no app-defined protocol in the reviewed source.
- Commands use verb-led methods:
loadChallenges,startGame,respond,encode,update,updateAnimation. - Files generally match their primary type;
Views,Models,Managers,Providers,Components,Systems, andPackagesfolders describe architectural roles where present.
Architecture takeaways
- Treat
ObjectTrackingExperiencesSampleAppas 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 |
|---|---|
ObjectTrackingExperiencesSample/ObjectTrackingExperiencesSampleApp.swift:10 |
ObjectTrackingExperiencesSampleApp |
ObjectTrackingExperiencesSample/Game/KeyboardGame.swift:9 |
KeyboardGame, Challenge, Outcome, PlayerInput, CodingKeys, InputFormat |
ObjectTrackingExperiencesSample/AppModel.swift:10 |
AppModel, ImmersiveSpaceState |
ObjectTrackingExperiencesSample/Utils/FadeOutComponent.swift:12 |
FadeOutComponent, FadeOutSystem |
ObjectTrackingExperiencesSample/Views/ContentView.swift:9 |
ContentView |
ObjectTrackingExperiencesSample/Views/ImmersiveView.swift:14 |
ImmersiveView |
ObjectTrackingExperiencesSample/Utils/ObjectTrackingGuide.swift:16 |
ObjectTrackingGuide |
Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.swift:1 |
Feature implementation |