Skip to content

Instantly share code, notes, and snippets.

@kkebo
Last active May 7, 2025 04:30
Show Gist options
  • Save kkebo/caf4a9e7454ea1dcfe468d635e2ba175 to your computer and use it in GitHub Desktop.
Save kkebo/caf4a9e7454ea1dcfe468d635e2ba175 to your computer and use it in GitHub Desktop.
Implement scene understanding and visualize meshes with `RealityView`
import RealityKit
import SwiftUI
@MainActor
final class MySystem: System {
// After turning on scene-understanding options, RealityKit automatically generates entities representing real-world geometry with a SceneUnderstandingComponent.
// https://developer.apple.com/documentation/realitykit/realitykit-scene-understanding#Use-scene-reconstruction-in-iOS-and-macOS
private static let query = EntityQuery(where: .has(SceneUnderstandingComponent.self) && .has(ModelComponent.self))
private static let debugMaterial: UnlitMaterial = {
var material = UnlitMaterial(color: .green)
material.triangleFillMode = .lines
return material
}()
init(scene: RealityKit.Scene) {}
func update(context: SceneUpdateContext) {
for entity in context.entities(matching: Self.query, updatingSystemWhen: .rendering) {
entity.components[ModelComponent.self]?.materials = [Self.debugMaterial]
}
}
}
struct ContentView: View {
var body: some View {
RealityView { content in
content.camera = .spatialTracking
MySystem.registerSystem()
let session = SpatialTrackingSession()
let config = SpatialTrackingSession.Configuration(
tracking: [],
sceneUnderstanding: [
.occlusion,
//.physics,
//.collision,
.shadow,
])
await session.run(config)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment