Sample CodemacOS, visionOSReviewed 2026-07-21View on Apple Developer

Generating interactive geometry with RealityKit

At a glance

Item Summary
Purpose Create an interactive mesh with low-level mesh and low-level texture.
App architecture A C/Objective-C header, Metal, Swift sample with the source-visible chain GeneratingInteractiveGeometrySampleAppHeightMapMeshViewGeneratorRealityKit APIs.
Main patterns Protocol-oriented abstraction
Project style 23 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; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper.
Key frameworks/packages RealityKit, Metal, metal_stdlib, simd, MetalKit; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── GeneratingInteractiveGeometry/
    └── GeneratingInteractiveGeometry/
        ├── GeneratingInteractiveGeometrySampleApp.swift
        ├── Compute/
        │   └── ComputeSystem.swift
        ├── HeightMapMeshView.swift
        ├── HeightMapMesh/
        │   ├── HeightMapGenerator.swift
        │   ├── HeightMapMesh.swift
        │   ├── HeightMap.swift
        │   ├── HeightMapMeshEntity.swift
        │   ├── MeshParams.h
        │   └── PlaneMesh.swift
        ├── Sine Wave/
        │   └── SineWaveHeightMapGenerator.swift
        ├── Terrain/
        │   └── TerrainHeightMapGenerator.swift
        └── Water/
            └── WaterSurfaceHeightMapGenerator.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 20 source declaration(s).

Overall architecture

Reference code

GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/GeneratingInteractiveGeometrySampleApp.swift:10 — architecture anchor

@main
struct GeneratingInteractiveGeometrySampleApp: App {
    // ...
        WindowGroup {
            HeightMapMeshView()
        }
    // ...
}

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

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

struct ComputeUpdateContext {
    // ...
    let deltaTime: TimeInterval
    // ...
}
Owner Object or state Relationship Mutation authority
ComputeUpdateContext TimeInterval (deltaTime) 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 Initialized by the owner; the binding is immutable
ComputeSystemComponent ComputeSystem (computeSystem) 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
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Compute/ComputeSystem.swift:23

@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

GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Compute/ComputeSystem.swift:23 — representative execution boundary

protocol ComputeSystem {
    @MainActor
    func update(computeContext: 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. GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMeshView.swift:47
Source import RealityKit The cited file imports this module; runtime use and architectural role are not inferred. GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Compute/ComputeSystem.swift:9
Source import Metal The cited file imports this module; runtime use and architectural role are not inferred. GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Compute/ComputeSystem.swift:8
Source import metal_stdlib The cited file imports this module; runtime use and architectural role are not inferred. GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Compute/Helpers.metal:8
Source import simd The cited file imports this module; runtime use and architectural role are not inferred. GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/MeshParams.h:10
Source import MetalKit The cited file imports this module; runtime use and architectural role are not inferred. GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/HeightMapMeshEntity.swift:10
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/GeneratingInteractiveGeometrySampleApp.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

GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Compute/ComputeSystem.swift:22 — representative type boundary

protocol ComputeSystem {
    @MainActor
    func update(computeContext: ComputeUpdateContext)
}
Type Responsibility Depends on or conforms to
GeneratingInteractiveGeometrySampleApp Application entry and top-level composition App
ComputeSystem Defines a capability or collaboration contract Concrete collaborators/imported frameworks
HeightMapGenerator 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
HeightMapMeshView User-interface presentation and input forwarding View
Generator Generates feature data or resources String, CaseIterable, Identifiable
SineWaveHeightMapGenerator Generates feature data or resources HeightMapGenerator
TerrainHeightMapGenerator Generates feature data or resources HeightMapGenerator
WaterSurfaceHeightMapGenerator Generates feature data or resources HeightMapGenerator

The source explicitly defines local protocol relationships: HeightMapMeshComputeSystem, SineWaveHeightMapGeneratorHeightMapGenerator, TerrainHeightMapGeneratorHeightMapGenerator, WaterSurfaceHeightMapGeneratorHeightMapGenerator.

Access control

Symbol Access Verified effect Likely rationale
deriveNormalsPipeline (GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/HeightMap.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.
setVerticesPipeline (GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/HeightMapMesh.swift:39) 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.
meshParams (GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/HeightMapMesh.swift:54) 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.
threadgroups (GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/HeightMapMesh.swift:56) 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

GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/HeightMap.swift:16 — representative boundary

@MainActor
struct HeightMap {
    // ...
    private let deriveNormalsPipeline: MTLComputePipelineState = makeComputePipeline(named: "deriveNormalsFromHeightMap")!
    // ...
}

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 GeneratingInteractiveGeometrySampleApp The source’s App suffix makes this role explicit.
Stores entity-component data or behavior ComputeSystemComponent The source’s Component suffix makes this role explicit.
Generates feature data or resources Generator, HeightMapGenerator, SineWaveHeightMapGenerator, TerrainHeightMapGenerator The source’s Generator suffix makes this role explicit.
Runs entity-component-system update logic ComputeDispatchSystem, ComputeSystem The source’s System suffix makes this role explicit.
User-interface presentation and input forwarding HeightMapMeshView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Protocol-oriented abstraction GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/HeightMapMesh.swift:30 A local protocol and concrete conformance create an explicit capability boundary.

Naming conventions

  • Types: App: GeneratingInteractiveGeometrySampleApp; Component: ComputeSystemComponent; Generator: Generator, HeightMapGenerator, SineWaveHeightMapGenerator, TerrainHeightMapGenerator, WaterSurfaceHeightMapGenerator; System: ComputeDispatchSystem, ComputeSystem; View: HeightMapMeshView.
  • Protocols: ComputeSystem, HeightMapGenerator.
  • Methods: update, setHeightMapGenerator, createScene, reset, generateHeightMap, resetWaterSurface, disturbWaterSurface, runWaterSimulation.
  • Files: GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/GeneratingInteractiveGeometrySampleApp.swift, GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Compute/ComputeSystem.swift, GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMeshView.swift, GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/HeightMapGenerator.swift, GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Sine Wave/SineWaveHeightMapGenerator.swift, GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Terrain/TerrainHeightMapGenerator.swift.

Architecture takeaways

  • GeneratingInteractiveGeometrySampleApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches RealityKit, Metal, metal_stdlib, simd 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
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/GeneratingInteractiveGeometrySampleApp.swift Cited implementation, SwiftUI, GeneratingInteractiveGeometrySampleApp
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Compute/ComputeSystem.swift Cited implementation, ComputeSystem, @MainActor, RealityKit, Metal, ComputeUpdateContext, ComputeSystemComponent, ComputeDispatchSystem
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/HeightMap.swift Cited implementation, HeightMap
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/HeightMapMesh.swift Cited implementation, HeightMapComputeParams, HeightMapMesh
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMeshView.swift SwiftUI state property wrapper, HeightMapMeshView, Generator
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Compute/Helpers.metal metal_stdlib, Feature implementation
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/MeshParams.h simd, MeshParams
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/HeightMapMeshEntity.swift MetalKit, HeightMapMeshEntity
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/HeightMapGenerator.swift HeightMapGenerator
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Sine Wave/SineWaveHeightMapGenerator.swift SineWaveHeightMapGenerator
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Terrain/TerrainHeightMapGenerator.swift TerrainHeightMapGenerator
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Water/WaterSurfaceHeightMapGenerator.swift WaterSurfaceHeightMapGenerator
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/PlaneMesh.swift PlaneMesh
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/HeightMapMesh/PlaneVertex.h PlaneVertex
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Terrain/TerrainParams.h TerrainParams
GeneratingInteractiveGeometry/GeneratingInteractiveGeometry/Water/WaterParams.h WaterParams