如何使Text与Box具有相同的位置和方向?

ind*_*jit 4 augmented-reality swift arkit realitykit

我正在使用 RealityKit 并努力设置应该可读并放置在盒子上的文本。无论盒子如何设置,它都应该始终遵循盒子的位置。

let textMesh = MeshResource.generateText("ABC", extrusionDepth: 0.001, font: fonts)
        
let textEntity = ModelEntity(mesh: textMesh, materials: [UnlitMaterial(color: .black)])
let boxMeshForText = MeshResource.generateBox(width: 0.25,
                                             height: 0.1,
                                              depth: 0,
                                       cornerRadius: 0.001)
let boxEntityForText = ModelEntity(mesh: boxMeshForText,
                              materials: [UnlitMaterial(color: UIColor.white)])
textEntity.setPosition([-0.09, -0.03, 0.001], relativeTo: boxEntityForText)
boxEntityForText.addChild(textEntity)
        
let textAnchor = ARAnchorEntity()
textAnchor.addChild(boxEntityForText, preservingWorldTransform: true)
textAnchor.setPosition([0.05, 0.05, 0.02], relativeTo: anchor)
textAnchor.transform.rotation = simd_quatf(angle: -90 * .pi/180, axis: [1,0,0])
anchor.addChild(textAnchor)
Run Code Online (Sandbox Code Playgroud)

上面的代码给了我错误的结果。我想要的结果可以在附图中看到。

在此输入图像描述

ARG*_*Geo 8

这很容易。只需将文本设置为立方体的子元素,然后用锚钉固定该立方体即可。在 ECS 框架中,子级总是从其父级位置、方向和比例复制。

let box = MeshResource.generateBox(size: 1)
let boxEntity = ModelEntity(mesh: box)
    
let anchor = AnchorEntity()
boxEntity.setParent(anchor)
arView.scene.addAnchor(anchor)
    
let text = MeshResource.generateText("AR", 
                      extrusionDepth: 0.01, 
                                font: .systemFont(ofSize: 0.25), 
                      containerFrame: .zero, 
                           alignment: .center, 
                       lineBreakMode: .byWordWrapping)

let shader = UnlitMaterial(color: .yellow)
let textEntity = ModelEntity(mesh: text, materials: [shader])
textEntity.position.z += 0.6
    
// Changing a position, orientation and scale of the parent
boxEntity.addChild(textEntity)        
boxEntity.transform = Transform(pitch: .pi/8, yaw: .pi/8, roll: .pi/8)
boxEntity.position.x = -0.5
boxEntity.scale /= 2
Run Code Online (Sandbox Code Playgroud)

现在孩子总是跟随他的父母。

在此输入图像描述