我对 ARKit / RealityKit 相当陌生,我想为加载的 USDZ 模型的旋转设置动画。不幸的是,我无法让它工作。
guard let modelEntity = try? Entity.loadModel(named: "toy_biplane_idle") else {
return
}
anchorEntity.addChild(modelEntity)
var rotationTransform: Transform = Transform()
rotationTransform.rotation = .init(angle: 30 * .pi / 180, axis: .init(x: 1, y: 1, z: 1))
// modelEntity.orientation = rotationTransform.rotation // this works but is not animated
modelEntity.move(to: rotationTransform, relativeTo: modelEntity.parent, duration: 1.0) // no effect at all
我尝试使用带有持续时间的
move(to:
方法,但它根本没有效果,既没有动画也没有任何旋转。我做错了什么?
在 iOS 中,您必须在将双翼飞机模型附加到场景后应用
move(..)
属性方法:
import SwiftUI
import RealityKit
struct ContentView : View {
var body: some View {
ARBiplaneView()
.ignoresSafeArea()
}
}
struct ARBiplaneView : UIViewRepresentable {
let arView = ARView(frame: .zero)
let anchor = AnchorEntity()
func makeUIView(context: Context) -> ARView {
let modelEntity = try! Entity.loadModel(named: "toy_biplane_idle")
modelEntity.scale *= 4
anchor.addChild(modelEntity)
var transform: Transform = Transform()
transform.translation.z = -2.0
transform.rotation = .init(angle: .pi/2, axis: [0,1,0])
// doesn't work here
modelEntity.move(to: transform, relativeTo: modelEntity, duration: 5.0)
arView.scene.anchors.append(anchor)
// DOES WORK HERE
modelEntity.move(to: transform, relativeTo: modelEntity, duration: 5.0)
return arView
}
func updateUIView(_ view: ARView, context: Context) { }
}