How do I get MeshResource.generateText() to anchor to something other than the camera using RealityKit and SwiftUI?

I'm using RealityKit and SwiftUI to place 3DText on iOS but I can't get the text to anchor to a position. I stripped the project down to the bare essentials below and it still doesn't work. The box will anchor and stay in one place as I walk around but the text will follow above my device wherever I move. This is driving me crazy because the box works totally as expected but the text just does not. I have tried so many different iterations of the code below to no avail. I was wondering if anyone else was having this problem or if it was just me, potentially a bug? Is there something else I should try?

import SwiftUI
import RealityKit

struct ContentView : View {
    var body: some View {
        return ARViewContainer().edgesIgnoringSafeArea(.all)    
    }
}

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
    
        let anchor = AnchorEntity()
        anchor.position = simd_make_float3(0, -0.5, -1)
        let textEntity = ModelEntity(mesh: .generateText("Hello there", extrusionDepth: 0.4, font: .boldSystemFont(ofSize: 8), containerFrame: .zero, alignment: .center, lineBreakMode: .byWordWrapping))
        let boxEntity = ModelEntity(mesh: .generateBox(size: 0.2))
        anchor.addChild(textEntity)
        anchor.addChild(boxEntity)
        arView.scene.anchors.append(anchor)
        
        return arView
        
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {}
    
}
Answered by somedonkus in 749231022

Very late reply, but I just had the font size as way too big so I couldn't tell I was walking around something so large. Font size for this function is hundreds of times bigger than a normal 2D font in SwiftUI or UIKit. So the answer is to use a font size like 0.1.

Hi there, this is strange indeed, and sounds like a bug. Are you able to provide us with the sample project you created? Also, what iOS version do you see this behavior on, and have you seen different behavior on other iOS versions?

Try something like this. Adjust the position as needed.

textEntity.setPosition(SIMD3<Float>(0.2, 0.2, 0.2), relativeTo: anchor)
anchor.addChild(textEntity)
Accepted Answer

Very late reply, but I just had the font size as way too big so I couldn't tell I was walking around something so large. Font size for this function is hundreds of times bigger than a normal 2D font in SwiftUI or UIKit. So the answer is to use a font size like 0.1.

How do I get MeshResource.generateText() to anchor to something other than the camera using RealityKit and SwiftUI?
 
 
Q