我可以通过编码从 Reality Composer 获取动画吗?

问题描述 投票:0回答:1

我通过reality converter得到了一个带有动画的usdz模型,然后将它导入到我的realitykit项目中。在reality composer中,我可以设置一个触发器,比如点击模型播放动画。但我仍然找不到在 Swift 代码中控制动画的方法。

当我打印 entity.availableAnimations[0].definition 时,它输出一个空对象。那么我怎样才能得到动画呢?非常感谢。

print(cat!.availableAnimations[0].definition) outputs:

ios swift arkit realitykit reality-composer
1个回答
0
投票

检查并验证动画是否正确导出到USDZ文件中:您可以使用Quick Look或Xcode的Preview来检查动画是否正确包含在USDZ文件中。在 Finder 中打开 USDZ 文件,然后按空格键使用“快速查看”预览文件。或者,您可以将 USDZ 文件拖到一个空的 Xcode 项目中,并使用内置的预览功能来测试动画。

并使用代码在 RealityKit 中控制 USDZ 模型的动画。

import UIKit
import RealityKit
import Combine

    class ViewController: UIViewController {
        
        @IBOutlet var arView: ARView!
        var modelEntity: ModelEntity?
        var animationController: AnimationPlaybackController?
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let modelAnchor = try! Experience.loadModel()
            arView.scene.anchors.append(modelAnchor)
            
            if let modelEntity = modelAnchor.findEntity(named: "YourModelName") as? ModelEntity {
                self.modelEntity = modelEntity
                if let animationResource = modelEntity.availableAnimations.first {
                    animationController = modelEntity.playAnimation(animationResource,
                                                                    transitionDuration: 0.5,
                                                                    blendLayerOffset: 0,
                                                                    repeatMode: .repeat(1),
                                                                    delay: 0)
                }
            }
        }
        
        // A function to play the animation when you want to trigger it.
        func playAnimation() {
            guard let animationController = animationController else { return }
            animationController.resume()
        }
        
        // A function to pause the animation.
        func pauseAnimation() {
            guard let animationController = animationController else { return }
            animationController.pause()
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.