lre*_*old 3 augmented-reality swift arkit realitykit reality-composer
我使用 Reality Composer 创建了一个非常简单的场景(“SpeechScene”),其中一个语音标注对象(“Speech Bubble”)锚定在一个Face锚点上。
我已通过以下方式将此场景加载到代码中:
let speechAnchor = try! Experience.loadSpeechScene()
arView.scene.anchors.append(speechAnchor)
let bubble = (arView.scene as? Experience.SpeechScene)?.speechBubble
Run Code Online (Sandbox Code Playgroud)
它按预期呈现。但是,我想动态更改此现有实体的文本。
我在这里发现了一个类似的问题,但我不清楚如何引用meshResourcevanillaRealityKit.Entity对象的属性。
这可能吗?谢谢!
首先,您需要找出 Reality Composer 包含
Bubble Speech对象的场景中的层次结构。为此,我使用了简单的 print() 命令:
print(textAnchor.swift!.children[0].components.self) /* Bubble Plate */
print(textAnchor.swift!.children[1].components.self) /* Text Object */
Run Code Online (Sandbox Code Playgroud)
现在我可以提取一个文本实体对象:
let textEntity: Entity = textAnchor.swift!.children[1].children[0].children[0]
Run Code Online (Sandbox Code Playgroud)
和气泡板实体对象:
let bubbleEntity: Entity = textAnchor.swift!.children[0]
Run Code Online (Sandbox Code Playgroud)
这是您可以根据需要进行调整的最终代码版本:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
对于这种情况,您始终可以使用更简单的方法——在 Reality Composer 中创建多个场景,每个场景必须包含不同的speech-object.
考虑一下,这段代码不是为了跟踪,它只是一个使用 动态切换两个对象的测试
Tap Gesture。然后你需要修改这个代码来跟踪人脸。
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
}
}
Run Code Online (Sandbox Code Playgroud)
如果您希望文本部分位于同一位置 - 只需将对象从一个场景复制粘贴到另一个场景即可。
@maxxfrazer 的断言是正确的,即当前动态更改文本的唯一方法是替换假设ModelComponent它Entity当然遵循HasModel Protocol.
我写了一个简单的扩展可以帮助解决这个问题:
//-------------------------
//MARK: - Entity Extensions
//-------------------------
extension Entity{
/// Changes The Text Of An Entity
/// - Parameters:
/// - content: String
func setText(_ content: String){ self.components[ModelComponent] = self.generatedModelComponent(text: content) }
/// Generates A Model Component With The Specified Text
/// - Parameter text: String
func generatedModelComponent(text: String) -> ModelComponent{
let modelComponent: ModelComponent = ModelComponent(
mesh: .generateText(text, extrusionDepth: TextElements().extrusionDepth, font: TextElements().font,
containerFrame: .zero, alignment: .center, lineBreakMode: .byTruncatingTail),
materials: [SimpleMaterial(color: TextElements().colour, isMetallic: true)]
)
return modelComponent
}
}
//--------------------
//MARK:- Text Elements
//--------------------
/// The Base Setup Of The MeshResource
struct TextElements{
let initialText = "Cube"
let extrusionDepth: Float = 0.01
let font: MeshResource.Font = MeshResource.Font.systemFont(ofSize: 0.05, weight: .bold)
let colour: UIColor = .white
}
Run Code Online (Sandbox Code Playgroud)
为了使用它,假设您创建一个Entity名为textEntity:
var textEntity = Entity()
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过简单地调用以下方法随时替换ModelComponent和设置来设置动态更改文本:MeshResource
textEntity.setText("Stack Overflow")
Run Code Online (Sandbox Code Playgroud)
当然,关于居中或对齐文本,您将需要进行一些简单的计算(我在此处省略了)。
希望能帮助到你。