如何在RealityKit中访问Reality Composer的模型组件? [英] How do I access the model component of Reality Composer in RealityKit?
问题描述
我试图在我的代码中更改在Reality Composer中创建的文本实体的模型组件,但这是!将gui创建的实体强制转换为对具有模型组件的实体的引用.
I'm trying to change the model component of a text entity created in Reality Composer in my code, but this as! casting the gui-created entity to a reference to an entity with a model component failed.
self.entityReference = scene.realityComposerEntity as! HasModel
textEntity.model!.mesh = MeshResource.generateText("New Text")
RealityKit中的文本实体应该具有模型属性,因为它在ARView中具有视觉外观,但是我不知道如何访问它.有谁知道如何?
The text entity in RealityKit should have a model property as it has a visual appearance in the ARView, but I don't know how to access it. Does anyone have any idea how?
还有其他简便的方法可以使用RealityKit
/Reality Composer
在同一位置动态显示不同的文本吗?
Are there any other easy ways to dynamically display different text in the same spot using RealityKit
/Reality Composer
?
推荐答案
要在RealityKit中访问Reality Composer的
ModelComponent
,请尝试以下方法:
To access Reality Composer's
ModelComponent
in RealityKit try the following approach:
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
arView.environment.background = .color(.darkGray)
let textAnchor = try! SomeText.loadTextScene()
let textEntity: Entity = textAnchor.realityComp!.children[0]
textEntity.scale = [5,5,5]
var textModelComp: ModelComponent = textEntity.children[0].components[ModelComponent]!
var material = SimpleMaterial()
material.baseColor = .color(.systemTeal)
textModelComp.materials[0] = material
textAnchor.realityComp!.children[0].children[0].components.set(textModelComp)
arView.scene.anchors.append(textAnchor)
}
}
这篇关于如何在RealityKit中访问Reality Composer的模型组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!