Hello World
At a glance
| Item | Summary |
|---|---|
| Purpose | Use windows, volumes, and immersive spaces to teach people about the Earth. |
| App architecture | WorldApp composes Window + WindowGroup + volumetric window + ImmersiveSpace around Modules; 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 |
| Project style | Code-rich sample with 43 scanned Swift file(s) and 3184 Swift line(s); resources and generated assets are excluded from those counts. |
Project structure
Source bundle/
├── World/WorldApp.swift # WorldApp
├── World/Model/ViewModel.swift # ViewModel, WindowState
├── Packages/WorldAssets/Sources/WorldAssets/SunPositionSystem.swift # SunPositionComponent, SunPositionSystem
├── World/Systems/TraceSystem.swift # TraceComponent, TraceSystem, TraceMesh
├── World/Modules/Modules.swift # Modules
├── World/Systems/RotationSystem.swift # RotationComponent, RotationSystem
├── World/RealityViews/OpenWindow.swift # OpenWindow, OpenWindowButton
├── World/Globe/GlobeControls.swift # GlobeControls, GlobeTiltPicker, TiltButtonAlignment
└── Packages/WorldAssets/Package.realitycomposerpro/ProjectData/main.json # authored RealityKit content
Structure observations
- The runtime boundary is Window + WindowGroup + volumetric window + 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
WorldApp_1["WorldApp"]
Window___WindowGroup___volumetric_window___ImmersiveSpace_2["Window + WindowGroup + volumetric window + ImmersiveSpace"]
Modules_3["Modules"]
ViewModel_4["ViewModel"]
RealityView_entity_graph_5["RealityView entity graph"]
RealityKit_components_and_systems_6["RealityKit components and systems"]
WorldApp_1 --> Window___WindowGroup___volumetric_window___ImmersiveSpace_2
Window___WindowGroup___volumetric_window___ImmersiveSpace_2 --> Modules_3
Modules_3 --> ViewModel_4
ViewModel_4 --> RealityView_entity_graph_5
RealityView_entity_graph_5 --> RealityKit_components_and_systems_6
Reference code
World/WorldApp.swift:13 — the app or executable entry declares the outer scene lifecycle.
struct WorldApp: 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
WorldApp *-- ViewModel : model
WorldApp *-- ImmersionStyle : orbitImmersionStyle
WorldApp *-- ImmersionStyle : solarImmersionStyle
WindowState --> WindowState : navigationWindowState
Ownership evidence
World/WorldApp.swift:15 — representative stored state or the nearest verified lifecycle anchor.
@State private var model = ViewModel()
// The immersion styles for different modules.
@State private var orbitImmersionStyle: ImmersionStyle = .mixed
@State private var solarImmersionStyle: ImmersionStyle = .full
static let modulesWindowID = "ModulesWindowID"
static let solarSystemControlsWindowID = "SolarSystemControlsWindowID"| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
WorldApp |
ViewModel as model |
creates and retains | Only the declaring scope writes |
WorldApp |
ImmersionStyle as orbitImmersionStyle |
owns SwiftUI value state | Only the declaring scope writes |
WorldApp |
ImmersionStyle as solarImmersionStyle |
owns SwiftUI value state | Only the declaring scope writes |
WindowState |
WindowState as navigationWindowState |
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 |
|---|---|---|
WorldApp (World/WorldApp.swift:13) |
Declares app scenes and top-level dependency lifetime. | App |
Modules (World/Modules/Modules.swift:11) |
Presents UI and forwards gestures or lifecycle events. | View |
ViewModel (World/Model/ViewModel.swift:12) |
Owns UI-facing feature state and commands. | Concrete framework collaborators |
SunPositionComponent (Packages/WorldAssets/Sources/WorldAssets/SunPositionSystem.swift:12) |
Stores RealityKit entity data. | Component, Codable |
SunPositionSystem (Packages/WorldAssets/Sources/WorldAssets/SunPositionSystem.swift:20) |
Updates matching RealityKit entities. | System |
RotationComponent (World/Systems/RotationSystem.swift:12) |
Stores RealityKit entity data. | Component |
RotationSystem (World/Systems/RotationSystem.swift:23) |
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 |
|---|---|---|---|
private var earth: Entity = Entity() (World/Entities/EarthEntity.swift:18) |
private |
Use is restricted to the declaration and same-file extensions permitted by Swift. | Inference: Hide implementation details and lifecycle-sensitive state. |
fileprivate var earthEntity: EarthEntity? { (World/RealityViews/Earth.swift:123) |
fileprivate |
Use is restricted to declarations in this source file. | Inference: Share a helper across same-file extensions without making it module-wide. |
public let rootNodeName = "Earth" (Packages/WorldAssets/Sources/WorldAssets/WorldAssets.swift:14) |
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 private(set), open; unmodified Swift declarations are internal.
Reference code
World/Entities/EarthEntity.swift:18 — representative visibility boundary.
class EarthEntity: Entity {
// ...
private var earth: Entity = Entity()
// ...
}Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Scene declaration and dependency lifetime | WorldApp |
The App/entry boundary determines window, volume, and immersive-space lifetime. |
| Presentation, attachments, and gestures | Modules |
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. |
| Per-frame entity behavior | Packages/WorldAssets/Sources/WorldAssets/SunPositionSystem.swift:20 |
RealityKit systems query component data instead of centralizing every entity update in SwiftUI. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| SwiftUI scene composition | World/WorldApp.swift:13 |
Keeps windows, volumes, and immersive-space lifecycle visible at the app boundary. |
| SwiftUI–RealityKit bridge | World/RealityViews/OpenWindow.swift:13 |
Builds and updates a RealityKit entity graph from SwiftUI lifecycle closures. |
| Explicit immersive-space lifecycle | World/WorldApp.swift:57 |
Makes immersive presentation a scene transition rather than hidden global state. |
| Observable state owner | World/Model/ViewModel.swift:12 |
Shares feature state across multiple views without moving framework resources into view values. |
| Entity-component-system | Packages/WorldAssets/Sources/WorldAssets/SunPositionSystem.swift:20 |
Stores per-entity data in components and advances behavior in registered RealityKit systems. |
Naming conventions
- Role suffixes are evidence, not decoration: App:
WorldApp; ViewModel:ViewModel; View:ItemView. - ECS names pair data and behavior: components
SunPositionComponent,RotationComponent,TraceComponent; systemsSunPositionSystem,RotationSystem,TraceSystem. - Protocols: no app-defined protocol in the reviewed source.
- Commands use verb-led methods:
update,getModelDescendents,setSunPosition,addPosition,generateIndices,makeTraceModel. - Files generally match their primary type;
Views,Models,Managers,Providers,Components,Systems, andPackagesfolders describe architectural roles where present.
Architecture takeaways
- Treat
WorldAppas 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.
- 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 |
|---|---|
World/WorldApp.swift:13 |
WorldApp |
World/Model/ViewModel.swift:12 |
ViewModel, WindowState |
Packages/WorldAssets/Sources/WorldAssets/SunPositionSystem.swift:12 |
SunPositionComponent, SunPositionSystem |
World/Systems/TraceSystem.swift:12 |
TraceComponent, TraceSystem, TraceMesh |
World/Modules/Modules.swift:11 |
Modules |
World/Systems/RotationSystem.swift:12 |
RotationComponent, RotationSystem |
World/RealityViews/OpenWindow.swift:11 |
OpenWindow, OpenWindowButton |
World/Globe/GlobeControls.swift:11 |
GlobeControls, GlobeTiltPicker, TiltButtonAlignment, GlobeTilt |