Sample CodevisionOSReviewed 2026-07-21View on Apple Developer

Obscuring virtual items in a scene behind real-world items

At a glance

Item Summary
Purpose Increase the realism of an immersive experience by adding entities with invisible materials real-world objects.
App architecture EntryPoint composes WindowGroup + ImmersiveSpace around MainView; MeshAnchorGenerator coordinates ARKit provider updates and maps anchors into RealityKit content.
Main patterns SwiftUI scene composition, SwiftUI–RealityKit bridge, Explicit immersive-space lifecycle, Provider session boundary, Async update consumer
Project style Code-light sample with 5 scanned Swift file(s) and 235 Swift line(s); resources and generated assets are excluded from those counts.

Project structure

Source bundle/
└── Occlusion/
    ├── App/
    │   └── EntryPoint.swift  # EntryPoint
    ├── Generators/
    │   └── MeshAnchorGenerator.swift  # MeshAnchorGenerator
    ├── Views/
    │   ├── MainView.swift  # MainView
    │   └── WorldOcclusionView.swift  # WorldOcclusionView
    └── Extensions/
        └── SIMD3.swift

Structure observations

  • The runtime boundary is WindowGroup + ImmersiveSpace; the pruned tree lists only files that explain lifecycle, state, or framework integration.
  • App code is compact; any explicit model, provider, or entity owner is still shown below rather than collapsed into view-local state.

Overall architecture

Reference code

Occlusion/App/EntryPoint.swift:11 — the app or executable entry declares the outer scene lifecycle.

struct EntryPoint: App {
    // ...
}

The diagram is a responsibility flow, not a claim that every adjacent node directly calls the next. It keeps scene ownership, shared state, RealityKit content, and framework-provider work at separate levels.

Ownership and state

Ownership evidence

Occlusion/Generators/MeshAnchorGenerator.swift:16 — representative stored state or the nearest verified lifecycle anchor.

    var root: Entity?

    /// The collection that comprises the `MeshAnchor.id` and the entity associated with the anchors.
    private var anchors: [UUID: Entity] = [:]

    init(root: Entity) {
        self.root = root
    }
Owner Object or state Relationship Mutation authority
MeshAnchorGenerator Entity as root stores and coordinates The owning type coordinates writes

Ownership here is deliberately narrow: @Environment and weak references are shared links, initialized @State or stored services are lifecycle ownership, and a RealityView content closure owns additions to its entity graph without making the SwiftUI view a reference-type owner.

Class and protocol design

Type Responsibility Depends on or conforms to
EntryPoint (Occlusion/App/EntryPoint.swift:11) Declares app scenes and top-level dependency lifetime. App
MainView (Occlusion/Views/MainView.swift:10) Presents UI and forwards gestures or lifecycle events. View
MeshAnchorGenerator (Occlusion/Generators/MeshAnchorGenerator.swift:14) Provides a sample-specific value or framework adapter. Concrete framework collaborators
WorldOcclusionView (Occlusion/Views/WorldOcclusionView.swift:14) Presents UI and forwards gestures or lifecycle events. View

The source defines no local substitution protocol in the reviewed boundary. Its protocol use is framework-facing (App, View, RealityKit/ARKit protocols, or platform adapters), so this document does not label the whole app protocol-oriented.

Access control

Symbol Access Verified effect Likely rationale
private var anchors: [UUID: Entity] = [:] (Occlusion/Generators/MeshAnchorGenerator.swift:19) private Use is restricted to the declaration and same-file extensions permitted by Swift. Inference: Hide implementation details and lifecycle-sensitive state.

No reviewed declaration uses fileprivate, private(set), public, open; unmodified Swift declarations are internal.

Reference code

Occlusion/Generators/MeshAnchorGenerator.swift:19 — representative visibility boundary.

    private var anchors: [UUID: Entity] = [:]

    init(root: Entity) {
        self.root = root
    }

Logic ownership and placement

Logic Owning type or file Placement rationale
Scene declaration and dependency lifetime EntryPoint The App/entry boundary determines window, volume, and immersive-space lifetime.
Presentation, attachments, and gestures MainView SwiftUI view code forwards user intent and RealityView lifecycle events.
Shared feature state and commands MeshAnchorGenerator A role-named owner prevents sibling views from duplicating transitions.
Tracking authorization and update streams Occlusion/Views/WorldOcclusionView.swift:47 Provider lifetime and async updates remain outside render-only view code.

Design patterns

Pattern Source evidence Purpose or tradeoff
SwiftUI scene composition Occlusion/App/EntryPoint.swift:11 Keeps windows, volumes, and immersive-space lifecycle visible at the app boundary.
SwiftUI–RealityKit bridge Occlusion/Views/WorldOcclusionView.swift:79 Builds and updates a RealityKit entity graph from SwiftUI lifecycle closures.
Explicit immersive-space lifecycle Occlusion/App/EntryPoint.swift:17 Makes immersive presentation a scene transition rather than hidden global state.
Provider session boundary Occlusion/Views/WorldOcclusionView.swift:47 Owns provider lifetime separately from the SwiftUI view tree.
Async update consumer Occlusion/Generators/MeshAnchorGenerator.swift:29 Consumes provider or event streams in cancellable structured tasks.

Naming conventions

  • Role suffixes are evidence, not decoration: View: MainView, WorldOcclusionView.
  • Protocols: no app-defined protocol in the reviewed source.
  • Commands use verb-led methods: run, runSession.
  • Files generally match their primary type; Views, Models, Managers, Providers, Components, Systems, and Packages folders describe architectural roles where present.

Architecture takeaways

  • Treat EntryPoint as the owner of scene declarations, not as the owner of every RealityKit entity created later.
  • Keep view-local interaction in SwiftUI, but move provider sessions, playback resources, shared game state, or transport state into a stable owner when their lifetime exceeds one render pass.
  • Run ARKit providers for the scene lifetime and consume their asynchronous updates in cancellable tasks; map anchors to entities at the boundary.
  • Model immersive-space open, transition, and close states explicitly so windows and immersive content cannot drift apart.
  • The compact source does not justify adding repository, coordinator, or protocol layers beyond the model, provider, or view boundary the sample actually declares.

Source map

Source file Relevant symbols
Occlusion/App/EntryPoint.swift:11 EntryPoint
Occlusion/Generators/MeshAnchorGenerator.swift:14 MeshAnchorGenerator
Occlusion/Views/MainView.swift:10 MainView
Occlusion/Views/WorldOcclusionView.swift:14 WorldOcclusionView
Occlusion/Extensions/SIMD3.swift:1 Feature implementation