Adding text to screen space with reailtykit, swift and swiftui

Viewed 300

How to add text to screen space in RealityKit/SwiftUI?

All the tutorials online are about adding text to screen space with UIKit/Scene Kit, but it never uses SwiftUI/RealityKit. Even Apple's uses UIKit for most of their examples!

Apple's example of screen space

It's not a straightforward conversion from Apple's explain into SwiftUI/RealityKit. Apple would get the 2d screen point, but then update the UIText's view frame with that 2d screen point.

To put the annotation in the right place on the screen, ask the ARView to convert its entity’s world location to a 2D screen point.

guard let projectedPoint = arView.project(note.position) else { return }

All I have right now is a tap handler that would raycast to the tap location. From that I have world coordinates, which I use for my anchor position. I created a mesh of text, but when adding to the anchor, it exists in world space and isn't aligned properly

let raycastResults = self.raycast(from: tapLocation, allowing: .estimatedPlane, alignment: .any)
        
guard let raycastFirstResult: ARRaycastResult = raycastRes.first else { return }

let position = raycastFirstResult.worldTransform
let mesh = MeshResource.generateText("Test")

Has anyone added text to world space using SwiftUI/RealityKit before?

0 Answers
Related