Sample CodeiOS, iPadOS, Mac CatalystReviewed 2026-07-21View on Apple Developer

Using object capture assets in RealityKit

At a glance

Item Summary
Purpose Create a chess game using RealityKit and assets created using Object Capture.
App architecture A Metal, Swift sample with the source-visible chain CaptureChessAppContentViewGameManagerMetalLibLoaderRealityKit APIs.
Main patterns Protocol-oriented abstraction, Delegate or data-source callbacks, Publisher-backed observable state
Project style 24 scanned source file(s) across Metal, Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: RunLoop.main, DispatchQueue.main.asyncAfter; none alone proves a background thread.
State/event model Source-visible mechanisms: AnyCancellable, SwiftUI state property wrapper, ObservableObject, receive(on:).
Key frameworks/packages RealityKit, SwiftUI, Combine, metal_stdlib, Foundation; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── CaptureChess/
    ├── CaptureChessApp.swift
    ├── SplashScreenView.swift
    ├── ContentView.swift
    ├── Game.swift
    ├── GameManager.swift
    ├── AnimationSystem.swift
    ├── MetalLibLoader.swift
    ├── Components.swift
    ├── ChessViewport.swift
    ├── ARViewContainer.swift
    ├── ChessPieceData.swift
    └── Entities/
        └── BoardGame.swift

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Metal, Swift.
  • The verified tree contains 4 project/configuration file(s) and 30 source declaration(s).

Overall architecture

Reference code

CaptureChess/CaptureChessApp.swift:10 — architecture anchor

@main
struct ObjectCaptureSampleApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

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

Ownership evidence

CaptureChess/ContentView.swift:38 — stored dependency or nearest verified ownership anchor

struct OverlayView: View {
    // ...
    @ObservedObject var gameManager: GameManager
    // ...
}
Owner Object or state Relationship Mutation authority
OverlayView GameManager (gameManager) observes externally owned state The observed object is authoritative
Piece Player (player) stores or receives Initialized by the owner; the binding is immutable
Piece PieceType (type) stores or receives Initialized by the owner; the binding is immutable
Move Coordinate (from) stores or receives Initialized by the owner; the binding is immutable

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
Run-loop scheduling RunLoop.main The source refers to the main thread’s run loop, a scheduling/liveness boundary rather than actor isolation. CaptureChess/ChessViewport.swift:70
Queue scheduling DispatchQueue.main.asyncAfter The source addresses the main dispatch queue. CaptureChess/Entities/BoardGame.swift:120

@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

CaptureChess/ChessViewport.swift:70 — representative execution boundary

class ChessViewport: ARView {
    // ...
            .receive(on: RunLoop.main)
    // ...
}

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 AnyCancellable A cancellable value records subscription lifetime management. CaptureChess/ChessViewport.swift:27
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. CaptureChess/ContentView.swift:12
State propagation ObservableObject ObservableObject supplies an observation contract. CaptureChess/GameManager.swift:13
Combine scheduling receive(on:) receive(on:) selects the scheduler for downstream delivery. CaptureChess/ChessViewport.swift:70
Source import RealityKit The cited file imports this module; runtime use and architectural role are not inferred. CaptureChess/ARViewContainer.swift:8
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. CaptureChess/ARViewContainer.swift:9
Source import Combine The cited file imports this module; runtime use and architectural role are not inferred. CaptureChess/ChessViewport.swift:10
Source import metal_stdlib The cited file imports this module; runtime use and architectural role are not inferred. CaptureChess/Captured.metal: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

CaptureChess/Components.swift:10 — representative type boundary

protocol HasChessPiece: Entity { }
Type Responsibility Depends on or conforms to
ObjectCaptureSampleApp Application entry and top-level composition App
SplashScreenView User-interface presentation and input forwarding View
ContentView User-interface presentation and input forwarding View
OverlayView User-interface presentation and input forwarding View
Player Owns media or timeline playback Concrete collaborators/imported frameworks
GameManager Long-lived feature or framework coordination ObservableObject
AnimationSystem Runs entity-component-system update logic System
MetalLibLoader Loads and prepares feature data or resources Concrete collaborators/imported frameworks
CheckerComponent Stores entity-component data or behavior Component
ChessPieceComponent Stores entity-component data or behavior Component

The source explicitly defines local protocol relationships: ChessPieceHasChessPiece.

Access control

Symbol Access Verified effect Likely rationale
prepareTexture (CaptureChess/ChessViewport+Bloom.swift:46) fileprivate Use is restricted to this source file. Inference: share with same-file helpers or extensions without exposing the symbol module-wide.
compatibleTargetTexture (CaptureChess/ChessViewport+Bloom.swift:62) fileprivate Use is restricted to this source file. Inference: share with same-file helpers or extensions without exposing the symbol module-wide.
animationDuration (CaptureChess/ChessViewport.swift:13) 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.
gameManager (CaptureChess/ChessViewport.swift:17) 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

CaptureChess/ChessViewport+Bloom.swift:46 — representative boundary

    fileprivate func prepareTexture(_ texture: inout MTLTexture?, format pixelFormat: MTLPixelFormat = .rgba8Unorm) {
        if texture?.width != self.sourceColorTexture.width
        // ...
    }

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 ObjectCaptureSampleApp The source’s App suffix makes this role explicit.
Stores entity-component data or behavior CheckerComponent, ChessPieceComponent The source’s Component suffix makes this role explicit.
Loads and prepares feature data or resources MetalLibLoader The source’s Loader suffix makes this role explicit.
Long-lived feature or framework coordination GameManager The source’s Manager suffix makes this role explicit.
Owns media or timeline playback Player The source’s Player suffix makes this role explicit.
Runs entity-component-system update logic AnimationSystem The source’s System suffix makes this role explicit.
User-interface presentation and input forwarding ContentView, GestureView, OverlayView, SplashScreenView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Protocol-oriented abstraction CaptureChess/Entities/ChessPiece.swift:22 A local protocol and concrete conformance create an explicit capability boundary.
Delegate or data-source callbacks CaptureChess/ChessViewport.swift:258 Callback protocols invert event delivery back into the sample’s owner.
Publisher-backed observable state CaptureChess/GameManager.swift:15 Published properties notify observers while mutation remains with the state object.

Naming conventions

  • Types: App: ObjectCaptureSampleApp; Component: CheckerComponent, ChessPieceComponent; Loader: MetalLibLoader; Manager: GameManager; Player: Player; System: AnimationSystem; View: ContentView, GestureView, OverlayView, SplashScreenView.
  • Protocols: HasChessPiece.
  • Methods: validateMove, toggle, piece, setPiece, removePiece, makeMove, kingCoordinate, coordinates.
  • Files: CaptureChess/SplashScreenView.swift, CaptureChess/ContentView.swift, CaptureChess/GameManager.swift, CaptureChess/AnimationSystem.swift, CaptureChess/MetalLibLoader.swift, CaptureChess/ChessViewport.swift.

Architecture takeaways

  • CaptureChessApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches RealityKit, SwiftUI, metal_stdlib, UIKit 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
CaptureChess/CaptureChessApp.swift Cited implementation, ObjectCaptureSampleApp
CaptureChess/ContentView.swift Cited implementation, SwiftUI state property wrapper, ContentView, OverlayView, ContentView_Previews
CaptureChess/Components.swift HasChessPiece, CheckerComponent, ChessPieceComponent
CaptureChess/ChessViewport+Bloom.swift Cited implementation, Feature implementation
CaptureChess/ChessViewport.swift Cited implementation, RunLoop.main, AnyCancellable, receive(on:), Combine, ChessViewport, GestureView
CaptureChess/Entities/ChessPiece.swift Cited implementation, ChessPiece
CaptureChess/GameManager.swift Cited implementation, ObservableObject, GameManager, State
CaptureChess/Entities/BoardGame.swift DispatchQueue.main.asyncAfter, BoardGame
CaptureChess/ARViewContainer.swift RealityKit, SwiftUI, ARViewContainer
CaptureChess/Captured.metal metal_stdlib, Feature implementation
CaptureChess/SplashScreenView.swift SplashScreenView, RectangleGrid, RectangleRow, EmptyCircle, SplashScreenView_Preview
CaptureChess/Game.swift ChessGame, Coordinate, Delta, Piece, PieceType, Move, Player
CaptureChess/AnimationSystem.swift AnimationSystem
CaptureChess/MetalLibLoader.swift MetalLibLoader
CaptureChess/ChessPieceData.swift ChessPieceData
CaptureChess/Entities/Chessboard.swift Chessboard