我有一个 ModelEntity 动画,它从 A 点移动到 B 点,需要一段时间才能完成。当用户点击 ModelEntity 时,我也想为 ModelEntity 添加一个缩小的动画。
我尝试将比例动画直接添加到 ModelEntity 视图 .move 但问题是我当前的模型转换是模型预期结束的地方。导致 ModelEnity 跳转到动画的末尾。
var transform = modelEntity.transform
transform.scale *= factor
modelEntity.move(to: transform, relativeTo: modelEntity.parent, duration: duration) // will not work because the translation of the transform is already at the end of the animation
有没有办法向已经处于不同动画中间的 ModelEntity 添加缩放动画并使它们一起工作?
这是我的问题的解决方案:
var transform = modelEntity.transform
transform.scale *= factor
modelEntity.move(to: modelEntity.transform, relativeTo: modelEntity.parent)
modelEntity.move(to: transform, relativeTo: modelEntity.parent, duration: duration)
看起来问题是我需要让实体到达它在动画中间的位置。出于某种原因,当您调用
.move
方法时,无论持续时间如何,它都希望从应该结束的地方开始。
使用 Xcode 14.2 进行测试。 :
import RealityKit
import CoreGraphics
extension Entity {
func scaleAnimated(with value: SIMD3<Float>, duration: CGFloat) {
var scaleTransform: Transform = Transform()
scaleTransform.scale = value
self.move(to: self.transform, relativeTo: self.parent)
self.move(to: scaleTransform, relativeTo: self.parent, duration: duration)
}
}
用法:
var scaleTransform: Transform = Transform()
if exampleEntity.scale.x == 1.0 {
exampleEntity.scaleAnimated(with: [0.012, 0.012, 0.012], duration: 1.0)
} else {
exampleEntity.scaleAnimated(with: [1.0, 1.0, 1.0], duration: 1.0)
}