动态更改RealityKit实体的文本 [英] Dynamically change text of RealityKit entity
问题描述
我使用Reality Composer创建了一个非常简单的场景("SpeechScene"),将单个语音标注对象("Speech Bubble")锚定到了Face
锚点.
I have created a very simple scene ("SpeechScene") using Reality Composer, with a single speech callout object ("Speech Bubble") anchored to a Face
anchor.
我已通过以下方式将此场景加载到了代码中:
I have loaded this scene into code via the following:
let speechAnchor = try! Experience.loadSpeechScene()
arView.scene.anchors.append(speechAnchor)
let bubble = (arView.scene as? Experience.SpeechScene)?.speechBubble
它呈现预期的效果.但是,我想动态更改此现有实体的文本.
It renders as expected. However, I would like to dynamically change the text of this existing entity.
我在此处,但是我不清楚如何引用香草RealityKit.Entity
对象的meshResource
属性.
I found a similar question here, but it's unclear to me how to refer to the meshResource
property of a vanilla RealityKit.Entity
object.
这可能吗?谢谢!
推荐答案
第一种方法
首先,您需要找出Reality Composer场景中包含
Bubble Speech
对象的层次结构.为此,我使用了简单的print()命令:
At first you need to find out what's an hierarchy in Reality Composer's scene containing
Bubble Speech
object. For that I used simple print() command:
print(textAnchor.swift!.children[0].components.self) /* Bubble Plate */
print(textAnchor.swift!.children[1].components.self) /* Text Object */
现在我可以提取一个文本实体对象:
Now I can extract a text entity object:
let textEntity: Entity = textAnchor.swift!.children[1].children[0].children[0]
和气泡板实体对象:
let bubbleEntity: Entity = textAnchor.swift!.children[0]
这是最终的代码版本,您可以根据需要进行调整:
Here's a final code version that you can adapt for your needs:
import RealityKit
class GameViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let textAnchor = try! SomeText.loadTextScene()
let textEntity: Entity = textAnchor.swift!.children[1].children[0].children[0]
textAnchor.swift!.parent!.scale = [4,4,4] // Scale for both objects
var textModelComp: ModelComponent = (textEntity.components[ModelComponent])!
var material = SimpleMaterial()
material.baseColor = .color(.red)
textModelComp.materials[0] = material
textModelComp.mesh = .generateText("Obj-C",
extrusionDepth: 0.01,
font: .systemFont(ofSize: 0.08),
containerFrame: CGRect(),
alignment: .left,
lineBreakMode: .byCharWrapping)
textEntity.position = [-0.1,-0.05, 0.01]
textAnchor.swift!.children[1].children[0].children[0].components.set(textModelComp)
arView.scene.anchors.append(textAnchor)
}
}
在这种情况下,您总是可以使用一种更简单的方法-在Reality Composer中创建多个场景,每个场景必须包含不同的speech-object
.
And you can always use a simpler approach for this case – to create several scenes in Reality Composer, each one must contain different speech-object
.
考虑一下,此代码不是用于跟踪的,它只是使用
Tap Gesture
动态切换两个对象的测试.然后,您需要修改此代码以跟踪人脸.
Consider, this code isn't for tracking, it's just a test for dynamically switching two objects using
Tap Gesture
. Then you need to adapt this code for tracking faces.
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
var counter = 0
var bonjourObject: FaceExperience.Bonjour? = nil
var holaObject: FaceExperience.Hola? = nil
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Reality Composer Scene named "Bonjour"
// Model name – "french"
bonjourObject = try! FaceExperience.loadBonjour()
bonjourObject?.french?.scale = SIMD3(x: 2, y: 2, z: 2)
bonjourObject?.french?.position.y = 0.25
// Reality Composer Scene named "Hola"
// Model name – "spanish"
holaObject = try! FaceExperience.loadHola()
holaObject?.spanish?.scale = SIMD3(x: 2, y: 2, z: 2)
holaObject?.spanish?.position.z = 0.3
}
@IBAction func tapped(_ sender: UITapGestureRecognizer) {
if (counter % 2) == 0 {
arView.scene.anchors.removeAll()
arView.scene.anchors.append(holaObject!)
} else {
arView.scene.anchors.removeAll()
arView.scene.anchors.append(bonjourObject!)
}
counter += 1
}
}
如果要将文本部分放在同一位置–只需将对象从一个场景复制粘贴到另一个场景即可.
If you want a text portion to be on the same place – just copy-paste object from one scene to another.
这篇关于动态更改RealityKit实体的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!