Creating a 3D painting space
At a glance
| Item | Summary |
|---|---|
| Purpose | Implement a painting canvas entity, and update its mesh to represent a stroke. |
| App architecture | EntryPoint composes WindowGroup + ImmersiveSpace around MainView; PaintingHandTracking 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 9 scanned Swift file(s) and 572 Swift line(s); resources and generated assets are excluded from those counts. |
Project structure
Source bundle/
└── Painting/
├── App/
│ └── EntryPoint.swift # EntryPoint
├── Components/
│ └── ClosureComponent.swift # ClosureComponent, ClosureSystem
├── Views/
│ ├── MainView.swift # MainView
│ └── PaintingView.swift # PaintingView
├── Trackers/
│ └── PaintingHandTracking.swift # PaintingHandTracking
├── Canvas/
│ ├── PaintingCanvas.swift # PaintingCanvas
│ └── Stroke.swift # Stroke
└── Extensions/
└── Entity.swift
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.
Overall architecture
flowchart LR
EntryPoint_1["EntryPoint"]
WindowGroup___ImmersiveSpace_2["WindowGroup + ImmersiveSpace"]
MainView_3["MainView"]
PaintingHandTracking_4["PaintingHandTracking"]
RealityView_entity_graph_5["RealityView entity graph"]
ARKit_providers___RealityKit_6["ARKit providers + RealityKit"]
EntryPoint_1 --> WindowGroup___ImmersiveSpace_2
WindowGroup___ImmersiveSpace_2 --> MainView_3
MainView_3 --> PaintingHandTracking_4
PaintingHandTracking_4 --> RealityView_entity_graph_5
RealityView_entity_graph_5 --> ARKit_providers___RealityKit_6
Reference code
Painting/App/EntryPoint.swift:11 — the app or executable entry declares the outer scene lifecycle.
struct EntryPoint: 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
PaintingHandTracking *-- ARKitSession : arSession
PaintingHandTracking *-- HandTrackingProvider : handTracking
PaintingCanvas *-- Entity : root
Stroke *-- Entity : entity
Ownership evidence
Painting/Trackers/PaintingHandTracking.swift:15 — representative stored state or the nearest verified lifecycle anchor.
let arSession = ARKitSession()
/// The `HandTrackingProvider` for hand tracking.
let handTracking = HandTrackingProvider()
/// The current left hand anchor the app detects.
@Published var latestLeftHand: HandAnchor?| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
PaintingHandTracking |
ARKitSession as arSession |
creates and retains | The owning type coordinates writes |
PaintingHandTracking |
HandTrackingProvider as handTracking |
creates and retains | The owning type coordinates writes |
PaintingCanvas |
Entity as root |
creates and retains | The owning type coordinates writes |
Stroke |
Entity as entity |
creates and retains | 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 |
|---|---|---|
EntryPoint (Painting/App/EntryPoint.swift:11) |
Declares app scenes and top-level dependency lifetime. | App |
MainView (Painting/Views/MainView.swift:10) |
Presents UI and forwards gestures or lifecycle events. | View |
PaintingHandTracking (Painting/Trackers/PaintingHandTracking.swift:13) |
Provides a sample-specific value or framework adapter. | ObservableObject |
ClosureComponent (Painting/Components/ClosureComponent.swift:12) |
Stores RealityKit entity data. | Component |
ClosureSystem (Painting/Components/ClosureComponent.swift:24) |
Updates matching RealityKit entities. | System |
PaintingView (Painting/Views/PaintingView.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 |
|---|---|---|---|
private func addBox(size: SIMD3<Float>, position: SIMD3<Float>) -> Enti... (Painting/Canvas/PaintingCanvas.swift:36) |
private |
Use is restricted to the declaration and same-file extensions permitted by Swift. | Inference: Hide implementation details and lifecycle-sensitive state. |
No reviewed declaration uses fileprivate, private(set), public, open; unmodified Swift declarations are internal.
Reference code
Painting/Canvas/PaintingCanvas.swift:36 — representative visibility boundary.
private func addBox(size: SIMD3<Float>, position: SIMD3<Float>) -> Entity {
// ...
}Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Scene declaration and dependency lifetime | EntryPoint |
The App/entry boundary determines window, volume, and immersive-space lifetime. |
| Presentation, attachments, and gestures | MainView |
SwiftUI view code forwards user intent and RealityView lifecycle events. |
| Shared feature state and commands | PaintingHandTracking |
A role-named owner prevents sibling views from duplicating transitions. |
| Tracking authorization and update streams | Painting/Trackers/PaintingHandTracking.swift:11 |
Provider lifetime and async updates remain outside render-only view code. |
| Per-frame entity behavior | Painting/Components/ClosureComponent.swift:24 |
RealityKit systems query component data instead of centralizing every entity update in SwiftUI. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| SwiftUI scene composition | Painting/App/EntryPoint.swift:11 |
Keeps windows, volumes, and immersive-space lifecycle visible at the app boundary. |
| SwiftUI–RealityKit bridge | Painting/Views/PaintingView.swift:25 |
Builds and updates a RealityKit entity graph from SwiftUI lifecycle closures. |
| Explicit immersive-space lifecycle | Painting/App/EntryPoint.swift:18 |
Makes immersive presentation a scene transition rather than hidden global state. |
| Observable state owner | Painting/Trackers/PaintingHandTracking.swift:13 |
Shares feature state across multiple views without moving framework resources into view values. |
| Entity-component-system | Painting/Components/ClosureComponent.swift:24 |
Stores per-entity data in components and advances behavior in registered RealityKit systems. |
| Provider session boundary | Painting/Trackers/PaintingHandTracking.swift:15 |
Owns provider lifetime separately from the SwiftUI view tree. |
Naming conventions
- Role suffixes are evidence, not decoration: View:
MainView,PaintingView. - ECS names pair data and behavior: components
ClosureComponent; systemsClosureSystem. - Protocols: no app-defined protocol in the reviewed source.
- Commands use verb-led methods:
update,startTracking. - Files generally match their primary type;
Views,Models,Managers,Providers,Components,Systems, andPackagesfolders describe architectural roles where present.
Architecture takeaways
- Treat
EntryPointas 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 |
|---|---|
Painting/App/EntryPoint.swift:11 |
EntryPoint |
Painting/Components/ClosureComponent.swift:12 |
ClosureComponent, ClosureSystem |
Painting/Views/MainView.swift:10 |
MainView |
Painting/Views/PaintingView.swift:14 |
PaintingView |
Painting/Trackers/PaintingHandTracking.swift:13 |
PaintingHandTracking |
Painting/Canvas/PaintingCanvas.swift:12 |
PaintingCanvas |
Painting/Canvas/Stroke.swift:12 |
Stroke |
Painting/Extensions/Entity.swift:1 |
Feature implementation |