Building an immersive experience with RealityKit
At a glance
| Item | Summary |
|---|---|
| Purpose | Use systems and postprocessing effects to create a realistic underwater scene. |
| App architecture | A C/Objective-C header, Metal, Swift sample with the source-visible chain AppDelegate → DebugSettingsViewController → GameManager → MetalRenderer → RealityKit APIs. |
| Main patterns | View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks |
| Project style | 59 scanned source file(s) across C/Objective-C header, Metal, Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: DispatchQueue.main.async, DispatchQueue.global.async, DispatchSemaphore, DispatchQueue(label:); none alone proves a background thread. |
| State/event model | Source-visible mechanisms: AnyCancellable. |
| Key frameworks/packages | RealityKit, Foundation, Combine, ARKit, os; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── BugBreakAR/
├── Components/
│ ├── EntitySwitcherComponent.swift
│ ├── InteractionComponent.swift
│ ├── PathfindingComponent.swift
│ ├── AudioComponent.swift
│ ├── VoxelTrailComponent.swift
│ └── CustomCameraComponent.swift
├── AppDelegate.swift
├── DebugSettings/
│ ├── DebugSettings.swift
│ ├── DebugSettingsCell.swift
│ └── DebugSettingsViewController.swift
├── GameManager.swift
└── GameViewController.swift
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: C/Objective-C header, Metal, Swift.
- The verified tree contains 8 project/configuration file(s) and 83 source declaration(s).
Overall architecture
flowchart LR
N1["AppDelegate"]
N2["DebugSettingsViewController"]
N3["GameManager"]
N4["MetalRenderer"]
N5["RealityKit APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
Reference code
BugBreakAR/AppDelegate.swift:12 — architecture anchor
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
// ...
}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
classDiagram
EntitySwitcherComponent *-- OSLog : log
ChildEntityInfo *-- String : name
ChildEntityInfo o-- EntitySwitcherAnimationType : type
ChildEntityInfo o-- Scene : scene
Ownership evidence
BugBreakAR/Components/EntitySwitcherComponent.swift:15 — stored dependency or nearest verified ownership anchor
private let log = OSLog(subsystem: appSubsystem, category: "EntitySwitchComponent")| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
EntitySwitcherComponent |
OSLog (log) |
creates and retains | Initialized by the owner; the binding is immutable |
ChildEntityInfo |
String (name) |
owns value state | Initialized by the owner; the binding is immutable |
ChildEntityInfo |
EntitySwitcherAnimationType (type) |
stores or receives | Initialized by the owner; the binding is immutable |
ChildEntityInfo |
Scene (scene) |
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 |
|---|---|---|---|
| Queue scheduling | DispatchQueue.main.async |
The source addresses the main dispatch queue. | BugBreakAR/CreatureEntity.swift:390 |
| Queue scheduling | DispatchQueue.global.async |
The source addresses a global dispatch queue; no stable thread identity is implied. | BugBreakAR/GameViewController+ARSessionDelegate.swift:34 |
| Synchronization | DispatchSemaphore |
The source references a synchronization primitive; the protected state requires surrounding review. | BugBreakAR/Rendering/MetalRenderer.swift:17 |
| Queue scheduling | DispatchQueue(label:) |
The source constructs a dispatch queue; its label alone does not prove a thread. | BugBreakAR/Spawn.swift:21 |
@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
BugBreakAR/CreatureEntity.swift:390 — representative execution boundary
resetPhysics()
physicsBody?.mode = .kinematic
DispatchQueue.main.async {
guard let shatterModel = self.gameManager?.assets?.shatterVoxels else { return }
// ...
}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 | AnyCancellable |
A cancellable value records subscription lifetime management. | BugBreakAR/Components/EntitySwitcherComponent.swift:47 |
| Source import | RealityKit |
The cited file imports this module; runtime use and architectural role are not inferred. | BugBreakAR/AppDelegate.swift:10 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | BugBreakAR/AudioFiles.swift:8 |
| Source import | Combine |
The cited file imports this module; runtime use and architectural role are not inferred. | BugBreakAR/AudioResources.swift:9 |
| Source import | ARKit |
The cited file imports this module; runtime use and architectural role are not inferred. | BugBreakAR/AppDelegate.swift:9 |
| Source import | os |
The cited file imports this module; runtime use and architectural role are not inferred. | BugBreakAR/Components/EntitySwitcherComponent.swift:11 |
| Source import | UIKit |
The cited file imports this module; runtime use and architectural role are not inferred. | BugBreakAR/AppDelegate.swift:8 |
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
BugBreakAR/Components/EntitySwitcherComponent.swift:68 — representative type boundary
public protocol EntitySwitcherComponentDelegate: AnyObject {
func animationCompleted()
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
AppDelegate |
Receives callback-driven events | UIResponder, UIApplicationDelegate |
EntitySwitcherComponentDelegate |
Defines a capability or collaboration contract | AnyObject |
InteractionComponentDelegate |
Defines a capability or collaboration contract | AnyObject |
PathfindingComponentDelegate |
Defines a capability or collaboration contract | AnyObject |
HasAudioComponent |
Defines a capability or collaboration contract | Concrete collaborators/imported frameworks |
GameViewControllerDelegate |
Defines a capability or collaboration contract | AnyObject |
InputSystemDelegate |
Defines a capability or collaboration contract | AnyObject |
OptionsSettingsDelegate |
Defines a capability or collaboration contract | AnyObject |
EntitySwitcherComponent |
Stores entity-component data or behavior | Component |
InteractionComponent |
Stores entity-component data or behavior | Component |
The source explicitly defines local protocol relationships: DebugSettingBool → DebugSettingProtocol, DebugSettingInt → DebugSettingProtocol, DebugSettingFloat → DebugSettingProtocol, DebugSettingEnum → DebugSettingProtocol, CreatureEntity → HasEntitySwitcher, CreatureEntity → EntitySwitcherComponentDelegate.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
isUpdating (BugBreakAR/Classifications.swift:11) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
classifications (BugBreakAR/Classifications.swift:12) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
classification (BugBreakAR/Classifications.swift:14) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
newClassification (BugBreakAR/Classifications.swift:18) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
Reference code
BugBreakAR/Classifications.swift:11 — representative boundary
class Classifications {
public static var isUpdating: Bool = false
// ...
}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 |
|---|---|---|
| Stores entity-component data or behavior | AudioComponent, CustomCameraComponent, EntitySwitcherComponent, HasAudioComponent |
The source’s Component suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | DebugSettingsViewController, GameViewController, MainMenuViewController |
The source’s Controller suffix makes this role explicit. |
| Receives callback-driven events | AppDelegate, EntitySwitcherComponentDelegate, GameViewControllerDelegate, InputSystemDelegate |
The source’s Delegate suffix makes this role explicit. |
| Long-lived feature or framework coordination | GameManager |
The source’s Manager suffix makes this role explicit. |
| Feature data or observable state | MetalModel |
The source’s Model suffix makes this role explicit. |
| Owns media or timeline playback | PathfindingBehaviorTurnAwayFromPlayer |
The source’s Player suffix makes this role explicit. |
| Owns drawing, GPU, or presentation processing | MetalRenderer |
The source’s Renderer suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | BugBreakAR/DebugSettings/DebugSettingsViewController.swift:11 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Protocol-oriented abstraction | BugBreakAR/DebugSettings/DebugSettings.swift:27 |
A local protocol and concrete conformance create an explicit capability boundary. |
| Delegate or data-source callbacks | BugBreakAR/AppDelegate.swift:13 |
Callback protocols invert event delivery back into the sample’s owner. |
Naming conventions
- Types: Component: AudioComponent, CustomCameraComponent, EntitySwitcherComponent, HasAudioComponent, InteractionComponent; Controller: DebugSettingsViewController, GameViewController, MainMenuViewController; Delegate: AppDelegate, EntitySwitcherComponentDelegate, GameViewControllerDelegate, InputSystemDelegate, InteractionComponentDelegate; Manager: GameManager; Model: MetalModel; Player: PathfindingBehaviorTurnAwayFromPlayer; Renderer: MetalRenderer; System: InputSystem.
- Protocols:
EntitySwitcherComponentDelegate,InteractionComponentDelegate,PathfindingComponentDelegate,HasAudioComponent,GameViewControllerDelegate,InputSystemDelegate,OptionsSettingsDelegate,HasEntitySwitcher. - Methods:
animationCompleted,addChildEntity,fixChild,didClone,forEach,registerCompletion,activateEntity,deactivateEntity. - Files:
BugBreakAR/Components/EntitySwitcherComponent.swift,BugBreakAR/AppDelegate.swift,BugBreakAR/Components/InteractionComponent.swift,BugBreakAR/Components/PathfindingComponent.swift,BugBreakAR/DebugSettings/DebugSettings.swift,BugBreakAR/Components/AudioComponent.swift.
Architecture takeaways
AppDelegateis the main source-visible entry or composition anchor for this sample.- Framework work reaches RealityKit, ARKit, UIKit, MetalKit 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.
- Local protocol relationships provide an explicit substitution boundary.
Source map
| Source file | Relevant symbols |
|---|---|
BugBreakAR/AppDelegate.swift |
Cited implementation, RealityKit, ARKit, UIKit, AppDelegate |
BugBreakAR/Components/EntitySwitcherComponent.swift |
Cited implementation, EntitySwitcherComponentDelegate, AnyCancellable, os, EntitySwitcherAnimationType, EntitySwitcherComponent, ChildEntityInfo, HasEntitySwitcher |
BugBreakAR/Classifications.swift |
Cited implementation, Classifications |
BugBreakAR/DebugSettings/DebugSettingsViewController.swift |
DebugSettingsViewController |
BugBreakAR/DebugSettings/DebugSettings.swift |
Cited implementation, DebugSettingProtocol, DebugSettingBool, DebugSettingInt, DebugSettingFloat, DebugSettingEnum, DebugSettings |
BugBreakAR/CreatureEntity.swift |
DispatchQueue.main.async, CreatureEntity, State |
BugBreakAR/GameViewController+ARSessionDelegate.swift |
DispatchQueue.global.async, Feature implementation |
BugBreakAR/Rendering/MetalRenderer.swift |
DispatchSemaphore, MetalRenderer |
BugBreakAR/Spawn.swift |
DispatchQueue(label:), Spawn |
BugBreakAR/AudioFiles.swift |
Foundation, AudioFile, AudioFiles |
BugBreakAR/AudioResources.swift |
Combine, AudioResources |
BugBreakAR/Components/InteractionComponent.swift |
InteractionComponent, InteractionComponentDelegate, HasInteraction |
BugBreakAR/Components/PathfindingComponent.swift |
PathfindingComponent, PathfindingComponentDelegate, HasPathfinding |
BugBreakAR/Components/AudioComponent.swift |
AudioComponent, HasAudioComponent |
BugBreakAR/Components/VoxelTrailComponent.swift |
VoxelTrailComponent, HasVoxelTrail |
BugBreakAR/DebugSettings/DebugSettingsCell.swift |
DebugSettingsCell, DebugSettingsSwitchCell, DebugSettingsSliderCell, DebugSettingsButtonCell, DebugSettingsSectionCell |