How can a Model be anchored above the user's Head in visionOS?
Asked Answered
H

2

5

Let's say for example, I want to anchor a cloud above the user's head.

I know that AnchorEntity exists and that you can get a reference to the user's head with AnchorEntity(.head). But how do I actually use it? With this code I am not seeing anything at all.

import SwiftUI
import RealityKit

struct CloudSpace: View {
    
    let headAnchor = AnchorEntity(.head)
    
    var body: some View {
        RealityView { content in
            async let cloud = ModelEntity(named: "Cloud")
            do {
                content.add(headAnchor)
                let cloud = try await cloud
                headAnchor.addChild(cloud)
            } catch {
                print(error)
            }
        }
    }
}
Histogenesis answered 27/6, 2023 at 1:25 Comment(0)
O
9

Anchoring a Model using Head anchor in visionOS

To see AnchoringComponent.Target.head in action, you can use either a real Vision Pro device or Xcode 15 visionOS Simulator. To activate a head's target (also known as .camera target in iOS, or DeviceAnchor object in ARKit for visionOS), try the following code. But consider the fact that head anchor's automatically updated every frame by default – use .once tracking mode to change it.

import SwiftUI
import RealityKit
import RealityKitContent

struct ContentView: View {
    var body: some View {
        VStack {
            RealityView { content in

                if let cloud = try? await Entity(named: "Scene",
                                                in: realityKitContentBundle) {

                    let anchor = AnchorEntity(.head)
                    anchor.anchoring.trackingMode = .once
                    cloud.setParent(anchor)
                    content.add(anchor)
    
                    // Z offset is important
                    cloud.transform.translation.z = -1.0  
                    cloud.transform.translation.y = 0.25
                    anchor.name = "Head Anchor"
                    print(content)
                }
            }
        }
    }
}

enter image description here

Owsley answered 27/6, 2023 at 5:43 Comment(9)
Thanks. It's a shame we can't simulate it. I understand hands/fingers but the head is basically just the camera. Hopefully in Beta 3 🤞Histogenesis
In my testing, if you have it using .continuous it'll work in the simulator at the moment. I'm using Beta 3.Transpicuous
I wonder if I can get the world position for this cloud when it is a child of the head, cause the normal way to do it using position(relative to:nil) or transform.translation gives a fixed place even though the head moves, you have a solution for that?Alviani
@AhmedEl-Bermawy, read this post.Owsley
In that post I wrote: "...the AnchorEntity(.head) transformation matrix is ​​currently hidden in visionOS...", I am now sure that it was hidden on purpose, so you need to use VisionOS ARKit to access it.Owsley
@AndyJazz thanx for the post, I already do this in ARKit before using matrix but the problem here that the line "worldInfo.queryDeviceAnchor(atTimestamp: CACurrentMediaTime()) " gives this error "Trying to get pose for nil service reference" is it because im using the simulator or because i'm working in immersive view?Alviani
Use Full Space immersiveness.Owsley
@AndyJazz, Thanx I figure it out and I can still use it even in immersive mix the only missing code was to await for the world info task " Task { try? await session.run([worldInfo]) }" this have to be in the "RealView {content in ....}",Alviani
Sorry, I can't continue our discussion in chat...Owsley
K
2

If you want to just get the position of the head, to place content appropriately for testing, then you can use the WorldTrackingProvider.queryPose(atTimestamp). It will give you the transform of the device at the TimeInterval specified.

It will mean setting up an ARKitSession with WorldTracking. It's also an "expensive" operation according to the docs, but it will at least give you the current head location:

let pose = worldInfo.queryPose(atTimestamp: CACurrentMediaTime())

https://developer.apple.com/documentation/visionos/tracking-points-in-world-space/#Track-the-device-position-in-the-world

Kyleekylen answered 5/7, 2023 at 2:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.