Sample CodevisionOSReviewed 2026-07-21View on Apple Developer

Tracking a handheld accessory as a virtual sculpting tool

At a glance

Item Summary
Purpose Use a tracked accessory with Apple Vision Pro to create a virtual sculpture.
App architecture A C/Objective-C header, Metal, Swift sample with the source-visible chain SpatialSculptingAppContentViewHapticsModelComputeDispatchSystemARKit APIs.
Main patterns Protocol-oriented abstraction
Project style 25 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: SwiftUI state property wrapper, @Observable, NotificationCenter.
Key frameworks/packages RealityKit, Metal, SwiftUI, ARKit, GameController; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── SpatialSculpting/
    └── SpatialSculpting/
        ├── SpatialSculptingApp.swift
        ├── Compute/
        │   └── ComputeSystem.swift
        ├── ECS/
        │   └── SculptingToolComponent.swift
        ├── Document/
        │   └── VolumeDocument.swift
        ├── ContentView.swift
        ├── ViewModel/
        │   ├── HapticsModel.swift
        │   ├── SculptingToolModel.swift
        │   └── SculptingToolModel+GameController.swift
        ├── Mesh/
        │   ├── MarchingCubesMesh.swift
        │   ├── MarchingCubesParams.h
        │   └── MeshVertex.h
        └── Volume/
            └── VoxelVolume.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 24 source declaration(s).

Overall architecture

Reference code

SpatialSculpting/SpatialSculpting/SpatialSculptingApp.swift:10 — architecture anchor

@main
struct SpatialSculptingApp: App {

    init() {
        ComputeDispatchSystem.registerSystem()
    }

    var body: some Scene {
        WindowGroup {
            ContentView().frame(width: 1500, height: 1500).frame(depth: 1500)
        }
        .windowStyle(.volumetric)
    }
}

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 ARKit.

Ownership and state

Ownership evidence

SpatialSculpting/SpatialSculpting/Compute/ComputeSystem.swift:14 — stored dependency or nearest verified ownership anchor

struct ComputeUpdateContext {
    // ...
    let sceneUpdateContext: SceneUpdateContext
    // ...
}
Owner Object or state Relationship Mutation authority
ComputeUpdateContext SceneUpdateContext (sceneUpdateContext) stores or receives Initialized by the owner; the binding is immutable
ComputeUpdateContext MTLCommandBuffer (commandBuffer) stores or receives Initialized by the owner; the binding is immutable
ComputeUpdateContext MTLComputeCommandEncoder (_computeEncoder) stores or receives Owning lexical scope
ComputeUpdateContext MTLBlitCommandEncoder (_blitEncoder) stores or receives Owning lexical scope

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. SpatialSculpting/SpatialSculpting/Compute/ComputeSystem.swift:56
Suspension boundary await suspension point The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. SpatialSculpting/SpatialSculpting/ContentView.swift:96
Main isolation Task closure isolated to MainActor The cited operation explicitly enters a main-actor-isolated region. SpatialSculpting/SpatialSculpting/ContentView.swift:124
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. SpatialSculpting/SpatialSculpting/ContentView.swift:124
Transfer contract Sendable or @Sendable The source declares a sendability boundary; this alone does not synchronize mutable state. SpatialSculpting/SpatialSculpting/ECS/SculptingToolComponent.swift:33

@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

SpatialSculpting/SpatialSculpting/Compute/ComputeSystem.swift:56 — representative execution boundary

protocol ComputeSystem {
    @MainActor
    func update(computeContext: inout ComputeUpdateContext)
}

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 SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. SpatialSculpting/SpatialSculpting/ContentView.swift:17
State propagation @Observable Observation macro publishes source-visible changes. SpatialSculpting/SpatialSculpting/ViewModel/HapticsModel.swift:14
State propagation NotificationCenter NotificationCenter distributes named process-local events. SpatialSculpting/SpatialSculpting/ViewModel/SculptingToolModel+GameController.swift:79
Source import RealityKit The cited file imports this module; runtime use and architectural role are not inferred. SpatialSculpting/SpatialSculpting/Compute/ComputeSystem.swift:9
Source import Metal The cited file imports this module; runtime use and architectural role are not inferred. SpatialSculpting/SpatialSculpting/Compute/ComputeSystem.swift:8
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. SpatialSculpting/SpatialSculpting/ContentView.swift:8
Source import ARKit The cited file imports this module; runtime use and architectural role are not inferred. SpatialSculpting/SpatialSculpting/ContentView.swift:9
Source import GameController The cited file imports this module; runtime use and architectural role are not inferred. SpatialSculpting/SpatialSculpting/ContentView.swift:11

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

SpatialSculpting/SpatialSculpting/Compute/ComputeSystem.swift:55 — representative type boundary

protocol ComputeSystem {
    @MainActor
    func update(computeContext: inout ComputeUpdateContext)
}
Type Responsibility Depends on or conforms to
SpatialSculptingApp Application entry and top-level composition App
ComputeSystem Defines a capability or collaboration contract Concrete collaborators/imported frameworks
ComputeSystemComponent Stores entity-component data or behavior Component
ComputeDispatchSystem Runs entity-component-system update logic System
SculptingToolComponent Stores entity-component data or behavior Component
SculptingToolSystem Runs entity-component-system update logic ComputeSystem
VolumeDocument Owns document data or lifecycle behavior FileDocument
ContentView User-interface presentation and input forwarding View
HapticsModel Feature data or observable state Concrete collaborators/imported frameworks
SculptingToolModel Feature data or observable state Concrete collaborators/imported frameworks

The source explicitly defines local protocol relationships: SculptingToolSystemComputeSystem.

Access control

Symbol Access Verified effect Likely rationale
_computeEncoder (SpatialSculpting/SpatialSculpting/Compute/ComputeSystem.swift:49) 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.
_blitEncoder (SpatialSculpting/SpatialSculpting/Compute/ComputeSystem.swift:51) 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.
maxVertexCapacityPerMeshChunk (SpatialSculpting/SpatialSculpting/Mesh/MarchingCubesMesh.swift:21) 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.
maxVerticesPerVoxel (SpatialSculpting/SpatialSculpting/Mesh/MarchingCubesMesh.swift:22) 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

SpatialSculpting/SpatialSculpting/Compute/ComputeSystem.swift:49 — representative boundary

struct ComputeUpdateContext {
    // ...
    private var _computeEncoder: MTLComputeCommandEncoder? = nil
    // ...
}

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 SpatialSculptingApp The source’s App suffix makes this role explicit.
Stores entity-component data or behavior ComputeSystemComponent, SculptingToolComponent The source’s Component suffix makes this role explicit.
Owns document data or lifecycle behavior VolumeDocument The source’s Document suffix makes this role explicit.
Feature data or observable state HapticsModel, SculptingToolModel The source’s Model suffix makes this role explicit.
Runs entity-component-system update logic ComputeDispatchSystem, ComputeSystem, SculptingToolSystem The source’s System suffix makes this role explicit.
User-interface presentation and input forwarding ContentView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Protocol-oriented abstraction SpatialSculpting/SpatialSculpting/ECS/SculptingToolComponent.swift:38 A local protocol and concrete conformance create an explicit capability boundary.

Naming conventions

  • Types: App: SpatialSculptingApp; Component: ComputeSystemComponent, SculptingToolComponent; Document: VolumeDocument; Model: HapticsModel, SculptingToolModel; System: ComputeDispatchSystem, ComputeSystem, SculptingToolSystem; View: ContentView.
  • Protocols: ComputeSystem.
  • Methods: computeEncoder, blitEncoder, endEncoding, update, loadFromURL, fileWrapper, createMeshChunkEntity, sculptingVolume.
  • Files: SpatialSculpting/SpatialSculpting/SpatialSculptingApp.swift, SpatialSculpting/SpatialSculpting/Compute/ComputeSystem.swift, SpatialSculpting/SpatialSculpting/ECS/SculptingToolComponent.swift, SpatialSculpting/SpatialSculpting/Document/VolumeDocument.swift, SpatialSculpting/SpatialSculpting/ContentView.swift, SpatialSculpting/SpatialSculpting/ViewModel/HapticsModel.swift.

Architecture takeaways

  • SpatialSculptingApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches RealityKit, Metal, SwiftUI, 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
SpatialSculpting/SpatialSculpting/SpatialSculptingApp.swift Cited implementation, SpatialSculptingApp
SpatialSculpting/SpatialSculpting/Compute/ComputeSystem.swift Cited implementation, ComputeSystem, @MainActor, RealityKit, Metal, ComputeUpdateContext, ComputeSystemComponent, ComputeDispatchSystem
SpatialSculpting/SpatialSculpting/Mesh/MarchingCubesMesh.swift Cited implementation, MarchingCubesMeshChunk, MarchingCubesMesh, func
SpatialSculpting/SpatialSculpting/ECS/SculptingToolComponent.swift Cited implementation, Sendable or @Sendable, SculptingMode, SculptingToolComponent, SculptingToolSystem
SpatialSculpting/SpatialSculpting/ContentView.swift await suspension point, Task closure isolated to MainActor, Task, SwiftUI state property wrapper, SwiftUI, ARKit, GameController, ContentView
SpatialSculpting/SpatialSculpting/ViewModel/HapticsModel.swift @Observable, HapticsModel
SpatialSculpting/SpatialSculpting/ViewModel/SculptingToolModel+GameController.swift NotificationCenter, Feature implementation
SpatialSculpting/SpatialSculpting/Document/VolumeDocument.swift VolumeDocumentError, VolumeDocument
SpatialSculpting/SpatialSculpting/ViewModel/SculptingToolModel.swift SculptingToolModel
SpatialSculpting/SpatialSculpting/Volume/VoxelVolume.swift VoxelVolumeError, VoxelVolume
SpatialSculpting/SpatialSculpting/Mesh/MarchingCubesParams.h MarchingCubesParams
SpatialSculpting/SpatialSculpting/Mesh/MeshVertex.h MeshVertex
SpatialSculpting/SpatialSculpting/Mesh/TriangleTable.swift MarchingCubesData
SpatialSculpting/SpatialSculpting/Sculpting/MarchingCubesMeshSculptor.swift MarchingCubesMeshSculptor
SpatialSculpting/SpatialSculpting/Sculpting/SculptParams.h SculptParams
SpatialSculpting/SpatialSculpting/UI/ToolbarElement.swift ToolbarElement