动态更改 RealityKit 实体的文本 [英] Dynamically change text of RealityKit entity
问题描述
我使用 Reality Composer 创建了一个非常简单的场景(SpeechScene"),将单个语音标注对象(Speech Bubble")锚定到 Face
锚点.
我已通过以下方式将此场景加载到代码中:
让 SpeechAnchor = 试试!Experience.loadSpeechScene()arView.scene.anchors.append(speechAnchor)让气泡 = (arView.scene as? Experience.SpeechScene)?.speechBubble
它按预期呈现.但是,我想动态更改此现有实体的文本.
我在
<块引用>首先,您需要找出包含 Bubble Speech
对象的 Reality Composer 场景中的层次结构.为此,我使用了简单的 print() 命令:
print(textAnchor.swift!.children[0].components.self)/* 气泡板 */print(textAnchor.swift!.children[1].components.self)/* 文本对象 */
现在我可以提取文本实体对象了:
let textEntity: Entity = textAnchor.swift!.children[1].children[0].children[0]
和气泡板实体对象:
let bubbleEntity: Entity = textAnchor.swift!.children[0]
<块引用>
这是您可以根据自己的需要进行调整的最终代码版本:
import RealityKit类 GameViewController:UIViewController {@IBOutlet var arView:ARView!覆盖 func viewDidLoad() {super.viewDidLoad()让 textAnchor = 试试!SomeText.loadTextScene()让 textEntity: Entity = textAnchor.swift!.children[1].children[0].children[0]textAnchor.swift!.parent!.scale = [4,4,4]//缩放两个对象var textModelComp: ModelComponent = (textEntity.components[ModelComponent])!var material = SimpleMaterial()material.baseColor = .color(.red)textModelComp.materials[0] = 材料textModelComp.mesh = .generateText(Obj-C",挤压深度:0.01,字体:.systemFont(ofSize: 0.08),容器框架:CGRect(),对齐方式:.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
.
请注意,此代码不是用于跟踪,它只是使用Tap Gesture
动态切换两个对象的测试.然后你需要修改这个代码来跟踪人脸.
import RealityKit类视图控制器:UIViewController {@IBOutlet var arView:ARView!无功计数器 = 0var bonjourObject: FaceExperience.Bonjour?= 零var holaObject:FaceExperience.Hola?= 零覆盖 func viewWillAppear(_ 动画:布尔){super.viewWillAppear(动画)//名为Bonjour"的现实作曲家场景//型号名称——法语"bonjourObject = 试试!FaceExperience.loadBonjour()bonjourObject?.french?.scale = SIMD3(x: 2, y: 2, z: 2)bonjourObject?.french?.position.y = 0.25//名为Hola"的现实作曲家场景//型号名称 - 西班牙文"holaObject = 试试!FaceExperience.loadHola()holaObject?.spanish?.scale = SIMD3(x: 2, y: 2, z: 2)holaObject?.spanish?.position.z = 0.3}@IBAction func tapped(_ sender: UITapGestureRecognizer) {如果(计数器 % 2)== 0 {arView.scene.anchors.removeAll()arView.scene.anchors.append(holaObject!)} 别的 {arView.scene.anchors.removeAll()arView.scene.anchors.append(bonjourObject!)}计数器 += 1}}
如果您希望文本部分位于同一位置 - 只需将对象从一个场景复制粘贴到另一个场景即可.
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.
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.
Is this possible? Thank you!
First Approach
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]
And bubble plate entity object:
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)
}
}
Second Approach
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
.
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屋!