我不知道如何重复下面的move函数,以便在简单的meshResource上创建恒定的旋转。有什么建议吗?
var transform = sphereEntity.transform
transform.rotation = simd_quatf(angle: .pi, axis: [0,1,0])
let anim = sphereEntity.move(to: transform, relativeTo: sphereEntity.parent, duration: 5.0)
答案 0 :(得分:1)
任何move()
实例方法都可以在将实体添加到场景的瞬间之后使用。
let boxEntity = ModelEntity(mesh: .generateBox(size: 1.0),
materials: [SimpleMaterial()])
var transform = boxEntity.transform
transform.rotation = simd_quatf(angle: .pi,
axis: [0, 1, 0])
let boxAnchor = AnchorEntity()
boxAnchor.children.append(boxEntity)
arView.scene.anchors.append(boxAnchor)
boxEntity.move(to: transform, relativeTo: nil, duration: 5.0)
目前( 2020年3月4日),所有RealityKit的
move()
实例方法无法循环。
答案 1 :(得分:0)
通过订阅 AnimationEvents.PlaybackCompleted
事件可以循环播放动画。
您必须将动画逻辑移动到单独调用一次的函数中。在此函数中,您订阅 PlaybackCompleted
事件。触发此事件时,您将调用相同的动画函数。
您可以使用 AnimationEvents
方法订阅 Scene.subscribe()
:
func subscribe<E>(to event: E.Type,
on sourceObject: EventSource? = nil,
_ handler: @escaping (E) -> Void) -> Cancellable where E : Event
请注意,您必须导入Combine 框架才能使用它:
import Combine
动画函数如下所示:
// This needs to be an instance variable, otherwise it'll
// get deallocated immediately and the animation won't start.
var animUpdateSubscription: Cancellable?
func animate(entity: HasTransform,
angle: Float,
axis: SIMD3<Float>,
duration: TimeInterval,
loop: Bool)
{
var transform = entity.transform
// We'll add the rotation to the current rotation
transform.rotation *= simd_quatf(angle: angle, axis: axis)
entity.move(to: transform, relativeTo: entity.parent, duration: duration)
// Checks if we should loop and if we have already subscribed
// to the AnimationEvent. We only need to do this once.
guard loop,
animUpdateSubscription == nil
else { return }
animUpdateSubscription = scene.subscribe(to: AnimationEvents.PlaybackCompleted.self,
on: entity,
{ _ in
self.animate(entity: entity,
angle: angle,
axis: axis,
duration: duration,
loop: loop)
})
}
现在你可以调用一次这个函数来触发一个循环动画:
animate(entity: entity,
angle: angle,
axis: axis,
duration: duration,
loop: loop)