Creating a spatial drawing app with RealityKit
At a glance
| Item | Summary |
|---|---|
| Purpose | Use low-level mesh and texture APIs to achieve fast updates to a person’s brush strokes by integrating RealityKit with ARKit and SwiftUI. |
| App architecture | A C/Objective-C header, Metal, Swift sample with the source-visible chain RealityKitDrawingApp → SplashScreenView → DrawingDocument → AnchorEntityInputProvider → RealityKit / RealityKitContent APIs. |
| Main patterns | Protocol-oriented abstraction |
| Project style | 46 scanned source file(s) across C/Objective-C header, Metal, Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: @MainActor, await suspension point, Task closure isolated to MainActor, Task, Sendable or @Sendable; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: @Observable, SwiftUI state property wrapper. |
| Key frameworks/packages | RealityKit, SwiftUI, Foundation, Collections, RealityKitContent; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── RealityKitDrawingApp/
├── RealityKitDrawingApp.swift
├── Brushes/
│ ├── Solid/
│ │ └── Style/
│ │ └── SolidBrushStyleProvider.swift
│ └── Sparkle/
│ └── SparkleBrushVertex.h
├── Canvas/
│ ├── DrawingCanvasPlacementView.swift
│ └── DrawingCanvasVisualizationView.swift
├── Document/
│ ├── AnchorEntityInputProvider.swift
│ └── DrawingDocument.swift
├── Splash Screen/
│ ├── SplashScreenBackgroundComponent.swift
│ └── SplashScreenForeground.swift
└── UI/
├── Palette/
│ ├── BrushTypeView.swift
│ └── Presets/
│ └── PresetBrushView.swift
└── BrushState.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 4 project/configuration file(s) and 68 source declaration(s).
Overall architecture
flowchart LR
N1["RealityKitDrawingApp"]
N2["SplashScreenView"]
N3["DrawingDocument"]
N4["AnchorEntityInputProvider"]
N5["RealityKit / RealityKitContent APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
Reference code
RealityKitDrawingApp/RealityKitDrawingApp.swift:11 — architecture anchor
@main
struct RealityKitDrawingApp: App {
// ...
private static let configureCanvasWindowId: String = "ConfigureCanvas"
private static let splashScreenWindowId: String = "SplashScreen"
private static let immersiveSpaceWindowId: String = "ImmersiveSpace"
// ...
}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
Settings *-- Float : thickness
Settings o-- ThicknessType : thicknessType
Settings *-- SIMD3 : color
Settings *-- Float : metallic
Ownership evidence
RealityKitDrawingApp/Brushes/Solid/Style/SolidBrushStyleProvider.swift:29 — stored dependency or nearest verified ownership anchor
struct Settings: Equatable, Hashable {
var thickness: Float = 0.005
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
Settings |
Float (thickness) |
owns value state | App/module collaborators |
Settings |
ThicknessType (thicknessType) |
stores or receives | App/module collaborators |
Settings |
SIMD3 (color) |
owns value state | App/module collaborators |
Settings |
Float (metallic) |
owns value state | 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 |
|---|---|---|---|
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | RealityKitDrawingApp/Brushes/Solid/CurveExtruder.swift:36 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | RealityKitDrawingApp/Brushes/Sparkle/SparkleBrushComponent.swift:39 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | RealityKitDrawingApp/Brushes/Sparkle/SparkleDrawingMeshGenerator.swift:197 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | RealityKitDrawingApp/Brushes/Sparkle/SparkleDrawingMeshGenerator.swift:197 |
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | RealityKitDrawingApp/Splash Screen/SplashScreenForeground.swift:157 |
@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
RealityKitDrawingApp/Brushes/Solid/CurveExtruder.swift:36 — representative execution boundary
struct CurveExtruder {
// ...
@MainActor
private var sampleCapacity: Int {
let vertexCapacity = lowLevelMesh?.vertexCapacity ?? 0
let indexCapacity = lowLevelMesh?.indexCapacity ?? 0
// Each sample adds `shape.count` vertices.
let sampleVertexCapacity = vertexCapacity / shape.count
// Each segment between two samples adds `topology.count` indices.
let sampleIndexCapacity = indexCapacity / topology.count + 1
return min(sampleVertexCapacity, sampleIndexCapacity)
}
// ...
}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 | @Observable |
Observation macro publishes source-visible changes. | RealityKitDrawingApp/Canvas/DrawingCanvas.swift:11 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | RealityKitDrawingApp/Canvas/DrawingCanvasConfigurationView.swift:20 |
| Source import | RealityKit |
The cited file imports this module; runtime use and architectural role are not inferred. | RealityKitDrawingApp/Brushes/Solid/CurveExtruder.swift:10 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | RealityKitDrawingApp/Brushes/Solid/Style/SolidBrushStyleProvider.swift:10 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | RealityKitDrawingApp/Brushes/Solid/CurveExtruder.swift:11 |
| Source import | Collections |
The cited file imports this module; runtime use and architectural role are not inferred. | RealityKitDrawingApp/Brushes/Solid/CurveExtruderWithEndcaps.swift:9 |
| Source import | RealityKitContent |
The cited file imports this module; runtime use and architectural role are not inferred. | RealityKitDrawingApp/Canvas/DrawingCanvasPlacementView.swift:10 |
| Source import | simd |
The cited file imports this module; runtime use and architectural role are not inferred. | RealityKitDrawingApp/Brushes/Solid/CurveExtruder.swift:12 |
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
RealityKitDrawingApp/Utilities/CurveProcessing.swift:12 — representative type boundary
protocol HermiteInterpolant {
static func distance(_: Self, _: Self) -> Float
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
RealityKitDrawingApp |
Application entry and top-level composition | App |
SolidBrushStyleProvider |
Supplies a capability or framework resource | Concrete collaborators/imported frameworks |
DrawingCanvasPlacementView |
User-interface presentation and input forwarding | View |
DrawingCanvasPlacementComponent |
Stores entity-component data or behavior | TransientComponent |
DrawingCanvasPlacementSystem |
Runs entity-component-system update logic | System |
DrawingCanvasVisualizationView |
User-interface presentation and input forwarding | View |
DrawingCanvasVisualizationComponent |
Stores entity-component data or behavior | TransientComponent |
DrawingCanvasVisualizationSystem |
Runs entity-component-system update logic | System |
HandComponent |
Stores entity-component data or behavior | Component |
AnchorEntityInputProvider |
Supplies a capability or framework resource | Concrete collaborators/imported frameworks |
The source explicitly defines local protocol relationships: Float → HermiteInterpolant, SIMD2 → HermiteInterpolant, SIMD3 → HermiteInterpolant, SIMD4 → HermiteInterpolant.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
lowLevelMesh (RealityKitDrawingApp/Brushes/Solid/CurveExtruder.swift:16) |
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. |
samples (RealityKitDrawingApp/Brushes/Solid/CurveExtruder.swift:30) |
private(set) |
Read access follows the declaration; writes remain in the private scope. | Inference: allow observation while reserving invariant-changing writes for the owner. |
cachedSampleCount (RealityKitDrawingApp/Brushes/Solid/CurveExtruder.swift:33) |
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. |
sampleCapacity (RealityKitDrawingApp/Brushes/Solid/CurveExtruder.swift:37) |
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. |
Reference code
RealityKitDrawingApp/Brushes/Solid/CurveExtruder.swift:16 — representative boundary
struct CurveExtruder {
private var lowLevelMesh: LowLevelMesh?
// ...
}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 |
|---|---|---|
| Application entry and top-level composition | RealityKitDrawingApp |
The source’s App suffix makes this role explicit. |
| Stores entity-component data or behavior | DrawingCanvasPlacementComponent, DrawingCanvasVisualizationComponent, HandComponent, SolidBrushComponent |
The source’s Component suffix makes this role explicit. |
| Owns document data or lifecycle behavior | DrawingDocument |
The source’s Document suffix makes this role explicit. |
| Generates feature data or resources | SolidDrawingMeshGenerator, SparkleDrawingMeshGenerator |
The source’s Generator suffix makes this role explicit. |
| Supplies a capability or framework resource | AnchorEntityInputProvider, SolidBrushStyleProvider, SparkleBrushStyleProvider |
The source’s Provider suffix makes this role explicit. |
| Runs entity-component-system update logic | DrawingCanvasPlacementSystem, DrawingCanvasVisualizationSystem, DrawingSystem, SolidBrushSystem |
The source’s System suffix makes this role explicit. |
| User-interface presentation and input forwarding | BrushTypeView, DrawingCanvasConfigurationView, DrawingCanvasPlacementView, DrawingCanvasVisualizationView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Protocol-oriented abstraction | RealityKitDrawingApp/Utilities/CurveProcessing.swift:23 |
A local protocol and concrete conformance create an explicit capability boundary. |
Main application flow
sequenceDiagram
participant DrawingDocument
participant ShaderGraphMaterial
participant DrawingSource
DrawingDocument->>ShaderGraphMaterial: ShaderGraphMaterial()
DrawingDocument->>ShaderGraphMaterial: ShaderGraphMaterial()
DrawingDocument->>DrawingSource: DrawingSource()
DrawingDocument->>DrawingSource: DrawingSource()
Reference code
RealityKitDrawingApp/Document/DrawingDocument.swift:85 — init()
@MainActor
init(rootEntity: Entity, brushState: BrushState, canvas: DrawingCanvasSettings) async {
self.rootEntity = rootEntity
self.brushState = brushState
self.startDate = .now
self.canvas = canvas
let leftRootEntity = Entity()
let rightRootEntity = Entity()
rootEntity.addChild(leftRootEntity)
rootEntity.addChild(rightRootEntity)
var solidMaterial: RealityKit.Material = SimpleMaterial()
if let material = try? await ShaderGraphMaterial(named: "/Root/Material",
from: "SolidBrushMaterial",
in: realityKitContentBundle) {
solidMaterial = material
}
var sparkleMaterial: RealityKit.Material = SimpleMaterial()
if var material = try? await ShaderGraphMaterial(named: "/Root/SparkleBrushMaterial",
from: "SparkleBrushMaterial",
in: realityKitContentBundle) {
try? material.setParameter(name: "ParticleUVScale", value: .float(8))
material.writesDepth = false
sparkleMaterial = material
}
leftSource = await DrawingSource(rootEntity: leftRootEntity,
solidMaterial: solidMaterial,
sparkleMaterial: sparkleMaterial)
rightSource = await DrawingSource(rootEntity: rightRootEntity,
solidMaterial: solidMaterial,
sparkleMaterial: sparkleMaterial)
}Naming conventions
- Types: App: RealityKitDrawingApp; Component: DrawingCanvasPlacementComponent, DrawingCanvasVisualizationComponent, HandComponent, SolidBrushComponent, SparkleBrushComponent; Document: DrawingDocument; Generator: SolidDrawingMeshGenerator, SparkleDrawingMeshGenerator; Provider: AnchorEntityInputProvider, SolidBrushStyleProvider, SparkleBrushStyleProvider; System: DrawingCanvasPlacementSystem, DrawingCanvasVisualizationSystem, DrawingSystem, SolidBrushSystem, SparkleBrushSystem; View: BrushTypeView, DrawingCanvasConfigurationView, DrawingCanvasPlacementView, DrawingCanvasVisualizationView, DrawingMeshView.
- Protocols:
HermiteInterpolant. - Methods:
setMode,radius,styleInput,update,generateMesh,receive,generateTexture,setTextureSize. - Files:
RealityKitDrawingApp/RealityKitDrawingApp.swift,RealityKitDrawingApp/Brushes/Solid/Style/SolidBrushStyleProvider.swift,RealityKitDrawingApp/Brushes/Sparkle/SparkleBrushVertex.h,RealityKitDrawingApp/Canvas/DrawingCanvasPlacementView.swift,RealityKitDrawingApp/Canvas/DrawingCanvasVisualizationView.swift,RealityKitDrawingApp/Document/AnchorEntityInputProvider.swift.
Architecture takeaways
RealityKitDrawingAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches RealityKit, SwiftUI, Collections, RealityKitContent 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 |
|---|---|
RealityKitDrawingApp/RealityKitDrawingApp.swift |
Cited implementation, RealityKitDrawingApp, Mode, SetModeKey |
RealityKitDrawingApp/Brushes/Solid/Style/SolidBrushStyleProvider.swift |
Cited implementation, SwiftUI, SolidBrushStyleProvider, ThicknessType, Settings |
RealityKitDrawingApp/Utilities/CurveProcessing.swift |
HermiteInterpolant, Cited implementation, HermiteControlPoint, SubdivisionSearchItem |
RealityKitDrawingApp/Brushes/Solid/CurveExtruder.swift |
Cited implementation, @MainActor, RealityKit, Foundation, simd, CurveExtruder |
RealityKitDrawingApp/Brushes/Sparkle/SparkleBrushComponent.swift |
await suspension point, SparkleBrushComponent, SparkleBrushSystem |
RealityKitDrawingApp/Brushes/Sparkle/SparkleDrawingMeshGenerator.swift |
Task closure isolated to MainActor, Task, SparkleDrawingMeshGenerator, SparkleBrushGenerationError |
RealityKitDrawingApp/Splash Screen/SplashScreenForeground.swift |
Sendable or @Sendable, ForegroundViewError, ModelEntityFillView, SplashScreenForegroundView |
RealityKitDrawingApp/Canvas/DrawingCanvas.swift |
@Observable, DrawingCanvasSettings |
RealityKitDrawingApp/Canvas/DrawingCanvasConfigurationView.swift |
SwiftUI state property wrapper, DrawingCanvasConfigurationView |
RealityKitDrawingApp/Brushes/Solid/CurveExtruderWithEndcaps.swift |
Collections, CurveExtruderWithEndcaps, BrushStroke |
RealityKitDrawingApp/Canvas/DrawingCanvasPlacementView.swift |
RealityKitContent, DrawingCanvasPlacementView, DrawingCanvasPlacementComponent, DrawingCanvasPlacementSystem |
RealityKitDrawingApp/Brushes/Sparkle/SparkleBrushVertex.h |
SparkleBrushAttributes, SparkleBrushParticle, SparkleBrushVertex, SparkleBrushSimulationParams |
RealityKitDrawingApp/Canvas/DrawingCanvasVisualizationView.swift |
DrawingCanvasVisualizationView, DrawingCanvasVisualizationComponent, DrawingCanvasVisualizationSystem |
RealityKitDrawingApp/Document/AnchorEntityInputProvider.swift |
HandComponent, AnchorEntityInputProvider, DrawingSystem |
RealityKitDrawingApp/Document/DrawingDocument.swift |
Chirality, InputData, DrawingDocument |
RealityKitDrawingApp/Splash Screen/SplashScreenBackgroundComponent.swift |
SplashScreenBackgroundComponent, SplashScreenBackgroundGenerationError, SplashScreenBackgroundSystem |