如何使文本与盒子具有相同的位置和方向?

4

我正在使用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)

上述代码给出的结果是错误的。我想要的结果可以在附加的图片中看到。 enter image description here
2个回答

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

现在的小孩总是跟随着父母。 enter image description here

0

非常感谢 @Andy Fedoroff 的回答。

通过学习您的建议,我得到了结果,以下是我使用的代码。

        let boxDepth: Float = 0.25
        let textBoxMesh = MeshResource.generateBox(width: 0.1,
                                                   height: 0.01,
                                                   depth: 0.25,
                                                   cornerRadius: 0.02)
        let textBoxEntity = ModelEntity.init(mesh: textBoxMesh,
                                             materials: [UnlitMaterial(color: UIColor.white)])
        textBoxEntity.position = .init(boxDepth/2, 0, 0)
        anchor.addChild(textBoxEntity)
        
        let fonts: UIFont = .init(descriptor: .init(name: "Helvetica", size: 0),
                                  size: 0.06)
        let textToDisplay = meterToDisplayText(meters: meters)
        
        let textMesh = MeshResource.generateText(textToDisplay,
                                                 extrusionDepth: 0.001,
                                                 font: fonts)
        let textEntity = ModelEntity(mesh: textMesh, materials: [UnlitMaterial(color: .black)])
      
        textEntity.transform = Transform(pitch: -90 * .pi/180,
                                         yaw: -90 * .pi/180,
                                         roll: 0)
        textBoxEntity.addChild(textEntity)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接