如何改变已经运行的真人秀USDZ动画的速度 我有一个USDZ 3D模型包含一个跑步的人动画,我设法使用此代码很好地运行了该模型: 进口Swiftui 导入RealityKit struct contentview:查看{ @State var AnimationReso ...

问题描述 投票:0回答:1
我想知道如何在动画运行时控制该动画的速度,而无需再生资源并随着每一个速度变化而一遍又一遍地重播动画,知道每当动画重播时,它是从零框架重新启动的,这意味着它在重播之前没有完成其周期,而是从中间开始剪切,而从开始并不是从开始开始的,我不喜欢发生。

如果您想尝试使用USDZ文件链接是

Here
(仍然可以使用任何其他动画USDZ文件进行测试):
Https://www.worldhotelity.com/stack/run.usdz,

我得益于Apple Developers论坛的一些帮助,发现了解决方案,可以从

AnimationDefinition方法转换为enter image description hereAnimationPlaybackController 由于需要将控制器声明为全局变量

swiftui 3d realitykit usdz realityview
1个回答
0
投票

在使用它播放动画之后

        guard let animationResource = manPlayer.availableAnimations.first else { return }
        playbackController = manPlayer.playAnimation(animationResource.repeat(duration: .infinity), transitionDuration: 0.25, startsPaused: false) //infinity looping, smooth transition, animation starts immediately
        playbackController?.speed = speed

然后您可以使用它来控制速度以后

        speed += 0.1
        playbackController?.speed = speed

这里是以下完整代码:

import SwiftUI
import RealityKit

struct ContentView: View {
    @State var manPlayer = Entity()
    @State var speed:Float = 0.5
        
    @State private var playbackController: AnimationPlaybackController?

    var body: some View {
        VStack {
            RealityView { content in
                if let item = try? await Entity(named: "run.usdz") {
                    manPlayer = item
                    
                    content.add(manPlayer)
                }
            }
            HStack {
                Button(action: playThis) {
                    Text("Play")
                }
                
                Button(action: increaseSpeed) {
                    Text("increase Speed (+) ")
                }
                
                Button(action: decreaseSpeed) {
                    Text("decrease Speed (-) ")
                }
            }
        }
    }
    
    func playThis() {
        guard let animationResource = manPlayer.availableAnimations.first else { return }
        playbackController = manPlayer.playAnimation(animationResource.repeat(duration: .infinity), transitionDuration: 0.25, startsPaused: false) //infinity looping, smooth transition, animation starts immediately
        playbackController?.speed = speed
    }
    
    func increaseSpeed() {
        speed += 0.1
        playbackController?.speed = speed
    }
    
    func decreaseSpeed() {
        speed -= 0.1
        playbackController?.speed = speed
    }
}

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.