我有一个从 A 点移动到 B 点的 ModelEntity 动画,需要一段时间才能完成。当用户点击 ModelEntity 时,我也想向 ModelEntity 添加收缩动画。
我尝试将缩放动画直接添加到 ModelEntity View .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 并使它们一起工作?
最佳答案
使用 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)
}
关于animation - RealityKit:更改 ModelEntity 的比例而不改变其位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71144866/