How do I access the model component of Reality Composer in RealityKit?

孤人 提交于 2020-07-09 07:58:08

问题


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")

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?

Are there any other easy ways to dynamically display different text in the same spot using RealityKit/Reality Composer?


回答1:


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)
    }
}



来源:https://stackoverflow.com/questions/59796488/how-do-i-access-the-model-component-of-reality-composer-in-realitykit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!