Hi @jnorris441
Thanks so much for recreating the issue and providing a workaround! This sounds like a bug so I want our teams to investigate it further. I'd appreciate it if you open a bug report,
and post the FB number here once you do.
The specific info you include your bug report might help our investigation,
and filing the bug report allows you to get notified when it is resolved.
Bug Reporting: How and Why?
explains how you can open a bug report.
In addition, the code I gave you yesterday is a bit error prone. I've cleaned it up and added comments to highlight the changes and included your changes.
struct ImmersiveView: View {
@State var spatialTrackingSession = SpatialTrackingSession()
@State var worldTrackingProvider = WorldTrackingProvider()
@State var arKitSession = ARKitSession()
@State var firstWorldAnchor: WorldAnchor?
@State var entity: ModelEntity
init() {
// Create the entity once
entity = ModelEntity(mesh: .generateSphere(radius: 0.2), materials: [SimpleMaterial(color: .red, isMetallic: false)])
}
var body: some View {
RealityView { content in
// Add the entity once
content.add(entity)
guard let firstWorldAnchor else { return }
entity.setTransformMatrix(firstWorldAnchor.originFromAnchorTransform, relativeTo: nil)
}
update: { content in
if let firstWorldAnchor {
entity.setTransformMatrix(firstWorldAnchor.originFromAnchorTransform, relativeTo: nil)
}
else {
print("Waiting for world anchor.")
}
}
.task {
do {
try await arKitSession.run([worldTrackingProvider])
}
catch {
print("Error starting session", error)
}
// Uncomment to recreate the issue
// 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 firstWorldAnchor == 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 {
await processWorldAnchorUpdate(update: update)
}
}
func processWorldAnchorUpdate(update:AnchorUpdate<WorldAnchor>) async {
print("world anchor", update.event, update.anchor.id)
// Ignore removed for this demo.
guard update.event != .removed else { return }
// Respond to add and update
if firstWorldAnchor == nil || firstWorldAnchor?.id == update.anchor.id {
firstWorldAnchor = update.anchor
}
}
}