Drawing in the air and on surfaces with a spatial stylus
At a glance
| Item | Summary |
|---|---|
| Purpose | Create a spatial stylus drawing experience that balances latency and accuracy for both in-air and on-surface drawing. |
| App architecture | A C/Objective-C header, Swift sample with the source-visible chain SurfaceInkingApp → StylusDrawingSettingsView → AppModel → BrushStyleProvider → RealityKit / SwiftUI APIs. |
| Main patterns | Protocol-oriented abstraction |
| Project style | 29 scanned source file(s) across C/Objective-C header, Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: @MainActor, async declaration or closure, Task, Task closure isolated to MainActor; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: @Observable, NotificationCenter, SwiftUI state property wrapper. |
| Key frameworks/packages | RealityKit, SwiftUI, Foundation, simd, ARKit; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── ARKit-SurfaceInking/
└── ARKit-SurfaceInking/
├── SurfaceInkingApp.swift
├── Drawing/
│ ├── DrawingInputProvider.swift
│ ├── Style/
│ │ └── BrushStyleProvider.swift
│ ├── Mesh/
│ │ └── BrushComponent.swift
│ ├── DrawingDocument.swift
│ └── DrawingSource.swift
├── Models/
│ ├── AppModel.swift
│ ├── StylusModel.swift
│ └── BrushState.swift
└── Views/
├── DeveloperSettingsView.swift
├── StylusDrawingSettingsView.swift
└── BrushTypeView.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, Swift.
- The verified tree contains 4 project/configuration file(s) and 49 source declaration(s).
Overall architecture
flowchart LR
N1["SurfaceInkingApp"]
N2["StylusDrawingSettingsView"]
N3["AppModel"]
N4["BrushStyleProvider"]
N5["RealityKit / SwiftUI APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
Reference code
ARKit-SurfaceInking/ARKit-SurfaceInking/SurfaceInkingApp.swift:11 — architecture anchor
@main
struct SurfaceInkingApp: App {
// ...
private let appModel = AppModel()
// ...
}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 visionOS.
Ownership and state
classDiagram
StylusAnchorInput *-- SIMD3 : position
StylusAnchorInput o-- TimeInterval : timestamp
StylusInputState o-- StylusAnchorInput : initialAnchorInput
StylusInputState *-- Int : initialChangedButtonInputIndex
Ownership evidence
ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/DrawingInputProvider.swift:25 — stored dependency or nearest verified ownership anchor
struct StylusAnchorInput {
let position: SIMD3<Float>
let timestamp: TimeInterval
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
StylusAnchorInput |
SIMD3 (position) |
owns value state | Initialized by the owner; the binding is immutable |
StylusAnchorInput |
TimeInterval (timestamp) |
stores or receives | Initialized by the owner; the binding is immutable |
StylusInputState |
StylusAnchorInput (initialAnchorInput) |
stores or receives | App/module collaborators |
StylusInputState |
Int (initialChangedButtonInputIndex) |
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. | ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/DrawingDocument.swift:13 |
| Suspension boundary | async declaration or closure |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/DrawingDocument.swift:26 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | ARKit-SurfaceInking/ARKit-SurfaceInking/Models/StylusModel.swift:42 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | ARKit-SurfaceInking/ARKit-SurfaceInking/Models/StylusModel.swift:76 |
@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
ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/DrawingDocument.swift:13 — representative execution boundary
@MainActor
class DrawingDocument {
// ...
let rootEntity: Entity
// ...
}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. | ARKit-SurfaceInking/ARKit-SurfaceInking/Models/AppModel.swift:15 |
| State propagation | NotificationCenter |
NotificationCenter distributes named process-local events. | ARKit-SurfaceInking/ARKit-SurfaceInking/Models/StylusModel.swift:74 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | ARKit-SurfaceInking/ARKit-SurfaceInking/Views/BrushTypeView.swift:11 |
| Source import | RealityKit |
The cited file imports this module; runtime use and architectural role are not inferred. | ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/DrawingDocument.swift:9 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/DrawingSource.swift:12 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/DrawingSource.swift:10 |
| Source import | simd |
The cited file imports this module; runtime use and architectural role are not inferred. | ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/CurveProcessing/CurveSample.swift:8 |
| Source import | ARKit |
The cited file imports this module; runtime use and architectural role are not inferred. | ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/DrawingInputProvider.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
ARKit-SurfaceInking/ARKit-SurfaceInking/Utilities/CurveProcessing.swift:12 — representative type boundary
protocol HermiteInterpolant {
static func distance(_: Self, _: Self) -> Float
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
SurfaceInkingApp |
Application entry and top-level composition | App |
DrawingInputProvider |
Supplies a capability or framework resource | Concrete collaborators/imported frameworks |
AppModel |
Feature data or observable state | Concrete collaborators/imported frameworks |
StylusModel |
Feature data or observable state | Concrete collaborators/imported frameworks |
BrushStyleProvider |
Supplies a capability or framework resource | Concrete collaborators/imported frameworks |
DeveloperSettingsView |
User-interface presentation and input forwarding | View |
DeveloperDrawingMediumSettingsView |
User-interface presentation and input forwarding | View |
StylusDrawingSettingsView |
User-interface presentation and input forwarding | View |
StylusSettingsHeaderView |
User-interface presentation and input forwarding | View |
StylusSettingsBodyView |
User-interface presentation and input forwarding | View |
The source explicitly defines local protocol relationships: Float → HermiteInterpolant, SIMD2 → HermiteInterpolant, SIMD3 → HermiteInterpolant, SIMD4 → HermiteInterpolant.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
curve (ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/CurveProcessing/SmoothCurveSampler.swift:31) |
private(set) |
Read access follows the declaration; writes remain in the private scope. | Inference: allow observation while reserving invariant-changing writes for the owner. |
flatness (ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/CurveProcessing/SmoothCurveSampler.swift:36) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
latestStrokeKeyPoints (ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/CurveProcessing/SmoothCurveSampler.swift:42) |
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. |
strokes (ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/CurveProcessing/SmoothCurveSampler.swift:45) |
private(set) |
Read access follows the declaration; writes remain in the private scope. | Inference: allow observation while reserving invariant-changing writes for the owner. |
Reference code
ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/CurveProcessing/SmoothCurveSampler.swift:31 — representative boundary
public struct SmoothCurveSampler {
// ...
private(set) var curve: DrawingMeshGenerator
// ...
}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 | SurfaceInkingApp |
The source’s App suffix makes this role explicit. |
| Stores entity-component data or behavior | BrushComponent |
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 | DrawingMeshGenerator |
The source’s Generator suffix makes this role explicit. |
| Feature data or observable state | AppModel, StylusModel |
The source’s Model suffix makes this role explicit. |
| Supplies a capability or framework resource | BrushStyleProvider, DrawingInputProvider |
The source’s Provider suffix makes this role explicit. |
| Runs entity-component-system update logic | BrushSystem |
The source’s System suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Protocol-oriented abstraction | ARKit-SurfaceInking/ARKit-SurfaceInking/Utilities/CurveProcessing.swift:23 |
A local protocol and concrete conformance create an explicit capability boundary. |
Naming conventions
- Types: App: SurfaceInkingApp; Component: BrushComponent; Document: DrawingDocument; Generator: DrawingMeshGenerator; Model: AppModel, StylusModel; Provider: BrushStyleProvider, DrawingInputProvider; System: BrushSystem; View: BrushStyleView, BrushTypeView, DeveloperDrawingMediumSettingsView, DeveloperSettingsView, ImmersiveDrawingView.
- Protocols:
HermiteInterpolant. - Methods:
isButtonPressed,getButtonPressure,getDrawingMedium,getChangedButtonInputIndex,updateButtonInputs,pruneButtonInputs,getCurrentDrawingMedium,getAnchorInput. - Files:
ARKit-SurfaceInking/ARKit-SurfaceInking/SurfaceInkingApp.swift,ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/DrawingInputProvider.swift,ARKit-SurfaceInking/ARKit-SurfaceInking/Models/AppModel.swift,ARKit-SurfaceInking/ARKit-SurfaceInking/Models/StylusModel.swift,ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/Style/BrushStyleProvider.swift,ARKit-SurfaceInking/ARKit-SurfaceInking/Models/BrushState.swift.
Architecture takeaways
SurfaceInkingAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches RealityKit, SwiftUI, simd, ARKit 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 |
|---|---|
ARKit-SurfaceInking/ARKit-SurfaceInking/SurfaceInkingApp.swift |
Cited implementation, SurfaceInkingApp |
ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/DrawingInputProvider.swift |
Cited implementation, ARKit, StylusAnchorInput, DrawingInputProvider, TrackingMode, CorrectionMode, DrawingMedium, StylusInputState |
ARKit-SurfaceInking/ARKit-SurfaceInking/Utilities/CurveProcessing.swift |
HermiteInterpolant, Cited implementation, HermiteControlPoint, SubdivisionSearchItem |
ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/CurveProcessing/SmoothCurveSampler.swift |
Cited implementation, SmoothCurveSampler, KeyPoint, KeyPointStroke |
ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/DrawingDocument.swift |
@MainActor, async declaration or closure, RealityKit, DrawingDocument |
ARKit-SurfaceInking/ARKit-SurfaceInking/Models/StylusModel.swift |
Task, Task closure isolated to MainActor, NotificationCenter, StylusButtonInput, Source, StylusModel |
ARKit-SurfaceInking/ARKit-SurfaceInking/Models/AppModel.swift |
@Observable, AppModel, ImmersiveSpaceState, DrawingSettings, AnimationSettings |
ARKit-SurfaceInking/ARKit-SurfaceInking/Views/BrushTypeView.swift |
SwiftUI state property wrapper, BrushTypeView, BrushStyleView |
ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/DrawingSource.swift |
SwiftUI, Foundation, DrawingInput, StrokeSampleIndex, DrawingSource, SampleAnimationState |
ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/CurveProcessing/CurveSample.swift |
simd, CurveSample |
ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/Style/BrushStyleProvider.swift |
BrushStyleProvider, ThicknessType, Settings |
ARKit-SurfaceInking/ARKit-SurfaceInking/Models/BrushState.swift |
BrushType, BrushState |
ARKit-SurfaceInking/ARKit-SurfaceInking/Views/DeveloperSettingsView.swift |
DeveloperSettingsView, DeveloperDrawingMediumSettingsView, TrackingMode |
ARKit-SurfaceInking/ARKit-SurfaceInking/Views/StylusDrawingSettingsView.swift |
StylusDrawingSettingsView, StylusSettingsHeaderView, StylusSettingsBodyView |
ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/Mesh/BrushComponent.swift |
BrushComponent, BrushSystem |
ARKit-SurfaceInking/ARKit-SurfaceInking/Drawing/Mesh/DrawingMeshGenerator.swift |
DrawingMeshGenerator |