Altering RealityKit Rendering with Shader Functions
At a glance
| Item | Summary |
|---|---|
| Purpose | Create rendering effects by writing surface shaders and geometry modifiers. |
| App architecture | PopApp presents a SwiftUI wrapper whose coordinator configures an ARView; custom RealityKit material functions keep surface and geometry effects in Metal while Swift owns UI actions and scene setup. |
| Main patterns | SwiftUI scene composition, UI–RealityKit bridge, Session owner boundary, Custom material shader boundary |
| Project style | Code-rich sample with 12 scanned C/Objective-C header, Metal, Swift file(s) and 970 implementation line(s). |
Project structure
Source bundle/
└── Pop/
├── UI/
│ ├── RealityViewContainer.swift # RealityViewContainer, Coordinator
│ ├── ContentView.swift # ContentView, ContentView_Previews, ResetButton
│ └── RealityView.swift # RealityView
├── Metal Shaders/
│ └── DissolveSurfaceShader.metal
├── PopApp.swift # PopApp
├── ApplicationActions.swift # ApplicationActions
└── Entity+Utilities.swift
Structure observations
- The runtime boundary is WindowGroup; the tree is pruned to lifecycle, state, framework, rendering, and ECS files.
- The source is C/Objective-C header, Metal, Swift; role-named types and feature folders separate the major responsibilities.
Overall architecture
flowchart LR
PopApp_1["PopApp"]
WindowGroup_2["WindowGroup"]
RealityView_3["RealityView"]
Coordinator_4["Coordinator"]
RealityKit_entity_graph_5["RealityKit entity graph"]
RealityKit_custom_materials___Metal_6["RealityKit custom materials + Metal"]
PopApp_1 --> WindowGroup_2
WindowGroup_2 --> RealityView_3
RealityView_3 --> Coordinator_4
Coordinator_4 --> RealityKit_entity_graph_5
RealityKit_entity_graph_5 --> RealityKit_custom_materials___Metal_6
Reference code
Pop/PopApp.swift:12 — executable or app-lifecycle anchor.
struct PopApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}The diagram is a responsibility flow, not a claim that every adjacent node directly calls the next. It separates lifecycle, presentation, stable state, framework/session work, and the RealityKit entity graph.
Ownership and state
classDiagram
RealityView --> AnchorEntity : robotScene
ApplicationActions --> AVAudioPlayer : whooshPlayer
RealityView --> ARCoachingOverlayView : coachingView
RealityView --> MTLDevice : device
Ownership evidence
Pop/UI/RealityView.swift:30 — representative stored state or nearest verified lifecycle anchor.
@available(iOS 15.0, *)
class RealityView: ARView {
// ...
var robotScene: AnchorEntity?
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
RealityView |
AnchorEntity as robotScene |
stores and coordinates | The owning type coordinates writes |
ApplicationActions |
AVAudioPlayer as whooshPlayer |
stores and coordinates | The owning type coordinates writes |
RealityView |
ARCoachingOverlayView as coachingView |
stores and coordinates | The owning type coordinates writes |
RealityView |
MTLDevice as device |
stores and coordinates | The owning type coordinates writes |
Ownership is intentionally narrow: environment, bindings, observed objects, weak links, and delegate callbacks do not prove lifetime ownership. Initialized stored services and wrapper-managed state do; entities added inside a RealityKit content closure belong to that entity graph.
Class and protocol design
| Type | Responsibility | Depends on or conforms to |
|---|---|---|
PopApp (Pop/PopApp.swift:12) |
Declares app lifecycle and top-level dependency lifetime. | App |
RealityView (Pop/UI/RealityView.swift:15) |
Presents UI and forwards gestures or lifecycle events. | ARView |
Coordinator (Pop/UI/RealityViewContainer.swift:34) |
Coordinates long-lived framework or cross-view work. | NSObject |
ContentView (Pop/UI/ContentView.swift:15) |
Presents UI and forwards gestures or lifecycle events. | View |
The reviewed source defines no local substitution protocol. Its App, View, delegate, renderer, ARKit, and RealityKit conformances are framework-facing contracts, so this document does not label the whole app protocol-oriented.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
private func incrementPopProgress(entity: Entity) async { (Pop/UI/RealityView.swift:90) |
private |
Use stays inside the declaration and same-file extensions permitted by Swift. | Inference: Hide lifecycle-sensitive state or an implementation step. |
public func userTapped(at point: CGPoint) { (Pop/UI/RealityView.swift:60) |
public |
The symbol is available to importing modules, subject to its containing type’s visibility. | Inference: Expose an intentional package or cross-target surface. |
No reviewed Swift access example uses fileprivate, private(set), open; declarations without a modifier are internal.
Objective-C/C header and implementation visibility is documented separately from Swift lexical access control.
Reference code
Pop/UI/RealityView.swift:90 — representative visibility boundary.
private func incrementPopProgress(entity: Entity) async {
// ...
}Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Application/command lifecycle | PopApp |
The executable boundary determines app, controller, scene, or command lifetime. |
| Presentation and user intent | RealityView |
The view/controller forwards input and owns only presentation-local work. |
| Framework/session/render coordination | Coordinator |
The role-named collaborator isolates long-lived or per-frame work from presentation code. |
| GPU and low-level resource work | Pop/UI/RealityView.swift:24 |
Buffer, texture, shader, or frame-resource mutation stays in a rendering/compute boundary. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| SwiftUI scene composition | Pop/PopApp.swift:12 |
Keeps windows, volumes, and immersive presentation visible at the app boundary. |
| UI–RealityKit bridge | Pop/UI/RealityViewContainer.swift:20 |
Builds or hosts the RealityKit entity graph from SwiftUI or a platform view/controller lifecycle. |
| Session owner boundary | Pop/UI/RealityViewContainer.swift:34 |
Keeps authorization, session lifetime, and framework callbacks in a stable model, manager, or controller. |
| Custom material shader boundary | Pop/Metal Shaders/DissolveSurfaceShader.metal:12 |
Keeps GPU surface/geometry work in Metal functions while Swift configures material parameters and UI actions. |
Naming conventions
- Role suffixes are evidence, not decoration: App:
PopApp; Coordinator:Coordinator; View:ContentView,RealityView. - Protocols: no app-defined protocol in the reviewed source.
- Commands use verb-led methods:
makeUIView,updateUIView,makeCoordinator,setup,userTapped,resetRobots,incrementPopProgress,initializeMetal. - Files and folders use primary-type or responsibility names such as
Views,Models,Rendering,Components,Systems,Packages, or feature names where those boundaries exist.
Architecture takeaways
- Treat
PopAppas the owner of executable or scene lifecycle, not automatically as the owner of every entity created later. - Keep view/controller input near presentation, but move sessions, reconstruction, capture, rendering, shared game state, or documents into stable owners when their lifetime exceeds one callback or render pass.
Source map
| Source file | Relevant symbols |
|---|---|
Pop/UI/RealityViewContainer.swift:15 |
RealityViewContainer, Coordinator |
Pop/Metal Shaders/DissolveSurfaceShader.metal:1 |
Feature implementation |
Pop/PopApp.swift:12 |
PopApp |
Pop/UI/ContentView.swift:15 |
ContentView, ContentView_Previews, ResetButton |
Pop/UI/RealityView.swift:15 |
RealityView |
Pop/ApplicationActions.swift:13 |
ApplicationActions |
Pop/Entity+Utilities.swift:1 |
Feature implementation |