Hello @Vision Pro Engineer
If I add a SpatialTrackingSession to your example I can replicate the problem. The world anchor will be added/updated, then immediately removed without calling removeAnchor().
import SwiftUI
import RealityKit
import ARKit
struct TestAnchor: View {
@State var spatialTrackingSession = SpatialTrackingSession()
@State var worldTrackingProvider = WorldTrackingProvider()
@State var arKitSession = ARKitSession()
@State var firstWorldAnchorTransform: simd_float4x4?
var body: some View {
RealityView { content in
}
update: { content in
if let firstWorldAnchorTransform {
let entity = ModelEntity(mesh: .generateSphere(radius: 0.2), materials: [SimpleMaterial(color: .red, isMetallic: false)])
entity.setTransformMatrix(firstWorldAnchorTransform, relativeTo: nil)
content.add(entity)
}
else {
print("Waiting for world anchor.")
}
}
.task {
do {
try await arKitSession.run([worldTrackingProvider])
}
catch {
print("Error starting session", error)
}
let configuration = SpatialTrackingSession.Configuration(tracking: [.plane])
if let unavailableCapabilities = await spatialTrackingSession.run(configuration) {
if unavailableCapabilities.anchor.contains(.plane) {
print("plane tracking is unavailable.")
}
}
Task {
await processWorldAnchorUpdates()
}
// For demo purposes only. Don't do this in prod :)
// Wait 5 seconds for a world anchor. If one isn't added create one.
// Add anchor should only be called the first time you run this.
// Delete the app and reinstall it to clear the anchor.
Task {
try? await Task.sleep(for: .seconds(5))
if firstWorldAnchorTransform == nil {
let anchor = WorldAnchor(originFromAnchorTransform: matrix_identity_float4x4)
do {
try await worldTrackingProvider.addAnchor(anchor)
}
catch {
print("Error adding anchor", error)
}
}
}
}
}
func processWorldAnchorUpdates() async {
print("Tracking the world")
for await update in worldTrackingProvider.anchorUpdates {
print(update)
await processWorldAnchorUpdate(update: update)
}
}
func processWorldAnchorUpdate(update:AnchorUpdate<WorldAnchor>) async {
print("world anchor updated", update.event, update.anchor.id)
if(update.event == .added && firstWorldAnchorTransform == nil) {
firstWorldAnchorTransform = update.anchor.originFromAnchorTransform
}
}
}