I have a main app window that presents an Immersive style in Mixed Reality. I am trying to determine the anchor/position of this glass window in the 3D space and place a Sphere entity right next to it. The goal is to ensure that if the user moves the window, the Sphere entity remains attached to it. Does anyone have insights on how to achieve this?
The below code snippet provides the position of the device, and I have positioned it 0.5 meters away from the z-axis. However, my objective is to obtain the position of the glass window and anchor the sphere to it. Any guidance on achieving this would be appreciated.
import RealityKit
import RealityKitContent
import ARKit
struct ImmersiveView: View {
let visionProPose = VisionProPose()
var body: some View {
RealityView { content in
Task { await visionProPose.runArSession() }
// Add the initial RealityKit content
if let scene = try? await Entity(named: "Immersive", in: realityKitContentBundle) {
content.add(scene)
}
} update: { content in
if let scene = content.entities.first {
if let sphere = scene.findEntity(named: "Sphere") as? ModelEntity {
Task {
let transfrom = await visionProPose.getTransform()
sphere.position = [Float((transfrom?.columns.3.x)!),
Float((transfrom?.columns.3.y)!),
Float((transfrom?.columns.3.z)!) - 1 ]
}
}
}
}
}
}
@Observable class VisionProPose {
let session = ARKitSession()
let worldTracking = WorldTrackingProvider()
func runArSession() async {
Task {
try? await session.run([worldTracking])
}
}
func getTransform() async -> simd_float4x4? {
guard let deviceAnchor = worldTracking.queryDeviceAnchor(atTimestamp: 1)
else { return nil }
let transform = deviceAnchor.originFromAnchorTransform
return transform
}
}