I'm constructing a RealityView where I'd like to display content in front of user's face.
When testing, I found that the deviceAnchor I initially get was wrong, so I implement following code to wait until the deviceAnchor I get from worldTrackingProvider
has the correct value:
private let arkitSession = ARKitSession()
private let worldTrackingProvider = WorldTrackingProvider()
var body: some View {
RealityView { content, attachments in
Task {
do {
// init worldTrackingProvider
try await arkitSession.run([worldTrackingProvider])
// wait until deviceAnchor returns correct info
var deviceAnchor : DeviceAnchor?
// continuously get deviceAnchor and check until it's valid
while (deviceAnchor == nil || !checkDeviceAnchorValid(Transform(matrix: deviceAnchor!.originFromAnchorTransform).translation)) {
deviceAnchor = worldTrackingProvider.queryDeviceAnchor(atTimestamp: CACurrentMediaTime())
}
let cameraTransform = Transform(matrix: deviceAnchor!.originFromAnchorTransform)
// ...codes that update my entity's translation
} catch {
print("Error: \(error)")
}
}
}
}
private func checkDeviceAnchorValid(_ translation: SIMD3<Float>) -> Bool {
// codes that check if the `deviceAnchor` has a valid translation.
}
However, I found that sometimes I can't get out from the while loop defined above. Not because my rules inside checkDeviceAnchorValid
func are too strict, but because the translation I get from deviceAnchor
is always invalid(it is [0,0,0] and never changed)
Why is this happening? Is this a known issue? I wonder if I can get recalled when the worldTrackingProvider returns the correct deviceAnchor,