Generating procedural textures
At a glance
| Item | Summary |
|---|---|
| Purpose | Display a 3D model that generates procedural textures in a reality view. |
| App architecture | Drawable composes WindowGroup around DrawableView; component values store per-entity state and registered RealityKit systems own repeated behavior. |
| Main patterns | SwiftUI scene composition, SwiftUI–RealityKit bridge, Entity-component-system |
| Project style | Code-rich sample with 6 scanned Swift file(s) and 396 Swift line(s); resources and generated assets are excluded from those counts. |
Project structure
Source bundle/
└── RealityKit-Drawable/
├── App/
│ └── Drawable.swift # Drawable
├── Systems/
│ ├── DrawableQueueComponent.swift # DrawableQueueComponent, DrawableComponentError
│ └── DrawableQueueSystem.swift # DrawableQueueSystem
├── Textures/
│ ├── ProceduralTextureGenerator.swift # ProceduralTextureGenerator, ProceduralTextureGeneratorError
│ └── DynamicTextureShaders.metal # Args
└── Views/
├── DrawableView.swift # DrawableView
└── EntityView.swift # EntityView
Structure observations
- The runtime boundary is WindowGroup; the pruned tree lists only files that explain lifecycle, state, or framework integration.
- App code is split into role-named views, models, managers, providers, components, or systems.
Overall architecture
flowchart LR
Drawable_1["Drawable"]
WindowGroup_2["WindowGroup"]
DrawableView_3["DrawableView"]
RealityView_entity_graph_4["RealityView entity graph"]
RealityKit_components_and_systems_5["RealityKit components and systems"]
Drawable_1 --> WindowGroup_2
WindowGroup_2 --> DrawableView_3
DrawableView_3 --> RealityView_entity_graph_4
RealityView_entity_graph_4 --> RealityKit_components_and_systems_5
Reference code
RealityKit-Drawable/App/Drawable.swift:11 — the app or executable entry declares the outer scene lifecycle.
struct Drawable: App {
var body: some Scene {
WindowGroup {
DrawableView()
}
}
}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
classDiagram
DrawableQueueComponent --> MTLComputePipelineState : pipeState
DrawableQueueComponent --> TextureResource : texture
DrawableQueueComponent --> MTLTexture : mtlTextureRead
DrawableQueueComponent --> MTLTexture : mtlTextureWrite
Ownership evidence
RealityKit-Drawable/Systems/DrawableQueueComponent.swift:25 — representative stored state or the nearest verified lifecycle anchor.
struct DrawableQueueComponent: Component {
// ...
let pipeState: MTLComputePipelineState
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
DrawableQueueComponent |
MTLComputePipelineState as pipeState |
stores and coordinates | The owning type coordinates writes |
DrawableQueueComponent |
TextureResource as texture |
stores and coordinates | The owning type coordinates writes |
DrawableQueueComponent |
MTLTexture as mtlTextureRead |
stores and coordinates | The owning type coordinates writes |
DrawableQueueComponent |
MTLTexture as mtlTextureWrite |
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 |
|---|---|---|
Drawable (RealityKit-Drawable/App/Drawable.swift:11) |
Declares app scenes and top-level dependency lifetime. | App |
DrawableView (RealityKit-Drawable/Views/DrawableView.swift:11) |
Presents UI and forwards gestures or lifecycle events. | View |
DrawableQueueComponent (RealityKit-Drawable/Systems/DrawableQueueComponent.swift:13) |
Stores RealityKit entity data. | Component |
DrawableQueueSystem (RealityKit-Drawable/Systems/DrawableQueueSystem.swift:11) |
Updates matching RealityKit entities. | System |
EntityView (RealityKit-Drawable/Views/EntityView.swift:12) |
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 |
|---|
No reviewed declaration uses fileprivate, private(set), public, open; unmodified Swift declarations are internal.
Reference code
RealityKit-Drawable/App/Drawable.swift:11 — representative visibility boundary.
struct Drawable: App {
// ...
}Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Scene declaration and dependency lifetime | Drawable |
The App/entry boundary determines window, volume, and immersive-space lifetime. |
| Presentation, attachments, and gestures | DrawableView |
SwiftUI view code forwards user intent and RealityView lifecycle events. |
| Per-frame entity behavior | RealityKit-Drawable/Systems/DrawableQueueSystem.swift:11 |
RealityKit systems query component data instead of centralizing every entity update in SwiftUI. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| SwiftUI scene composition | RealityKit-Drawable/App/Drawable.swift:11 |
Keeps windows, volumes, and immersive-space lifecycle visible at the app boundary. |
| SwiftUI–RealityKit bridge | RealityKit-Drawable/Views/EntityView.swift:73 |
Builds and updates a RealityKit entity graph from SwiftUI lifecycle closures. |
| Entity-component-system | RealityKit-Drawable/Systems/DrawableQueueSystem.swift:11 |
Stores per-entity data in components and advances behavior in registered RealityKit systems. |
Naming conventions
- Role suffixes are evidence, not decoration: View:
DrawableView,EntityView. - ECS names pair data and behavior: components
DrawableQueueComponent; systemsDrawableQueueSystem. - Protocols: no app-defined protocol in the reviewed source.
- Commands use verb-led methods:
update,generate. - Files generally match their primary type;
Views,Models,Managers,Providers,Components,Systems, andPackagesfolders describe architectural roles where present.
Architecture takeaways
- Treat
Drawableas 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.
- Use RealityKit components for per-entity data and systems for repeated simulation instead of a monolithic view model.
Source map
| Source file | Relevant symbols |
|---|---|
RealityKit-Drawable/App/Drawable.swift:11 |
Drawable |
RealityKit-Drawable/Systems/DrawableQueueComponent.swift:13 |
DrawableQueueComponent, DrawableComponentError |
RealityKit-Drawable/Textures/ProceduralTextureGenerator.swift:13 |
ProceduralTextureGenerator, ProceduralTextureGeneratorError |
RealityKit-Drawable/Systems/DrawableQueueSystem.swift:11 |
DrawableQueueSystem |
RealityKit-Drawable/Views/DrawableView.swift:11 |
DrawableView |
RealityKit-Drawable/Views/EntityView.swift:12 |
EntityView |
RealityKit-Drawable/Textures/DynamicTextureShaders.metal:11 |
Args |