在同一装备USDZ模型上加载不同的USDZ动画

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

我正在尝试在一个 USDZ 模型上加载多个 Mixamo 3d 动画。所有导出的动画都是 USDZ 动画文件,并且主 USDZ 模型文件具有相同的精确装备。所有动画同时导出,但存储在单独的 USDZ 文件中。如果我播放创建的“modelEntity”附带的动画,则动画可以工作,但是当我尝试从另一个 USDZ 文件更新动画时,我收到此错误:

无法找到任何绑定路径的 BindPoint:“”、“”、“Bboy_Uprock.mixamorig_Hips”、“Bboy_Uprock.mixamorig_Hips.SkeletalPose.SkeletalPoses[0]”

没有想法了。

import SwiftUI
import RealityKit
import ARKit
import Combine
import Foundation

struct ContentView: View {
    @State private var animationTrigger = false
    @StateObject private var arViewModel = ARViewModel()

    var body: some View {
        VStack {
            ARViewContainer(animationTrigger: $animationTrigger, viewModel: arViewModel)
                .edgesIgnoringSafeArea(.all)

            Button("Change Animation") {
                animationTrigger.toggle()
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(10)
        }
    }
}

struct ARViewContainer: UIViewRepresentable {
    @Binding var animationTrigger: Bool
    @ObservedObject var viewModel: ARViewModel 
    @State var mainModelEntity: ModelEntity?
    
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        setupARView(arView)
        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {
           if animationTrigger, let mainModelEntity = viewModel.mainModelEntity {
               print("Applying new animation!")
               applyRandomAnimation(to: mainModelEntity, in: uiView)
           }
       }
    
    
    private func setupARView(_ arView: ARView) {
        let config = ARWorldTrackingConfiguration()
        config.planeDetection = [.horizontal, .vertical]
        arView.session.run(config)
        
        let mainModelName = "BboyUprock.usdz"
       
        if let modelEntity = try? ModelEntity.loadModel(named: mainModelName) {
            modelEntity.scale = SIMD3<Float>(0.01, 0.01, 0.01)

            DispatchQueue.main.async {
                self.viewModel.mainModelEntity = modelEntity
            }
            
            if let animation = modelEntity.availableAnimations.first {
                modelEntity.playAnimation(animation.repeat())
                print("Playing first animation")
            } else {
                print("No animations found in the model")
            }
            
            let anchor = AnchorEntity(plane: .horizontal)
            anchor.addChild(modelEntity)
            arView.scene.addAnchor(anchor)
        } else {
            fatalError("Failed to load main model")
        }
    }
    
    
    private func applyRandomAnimation(to entity: ModelEntity, in arView: ARView) {
        
        let animationModelNames = ["BboyUprock.usdz", "HipHopDancing1.usdz", "HipHopDancing2.usdz", "HipHopDancing3.usdz"]
        
        var animations = [AnimationResource]()
        
        for animModelName in animationModelNames {
            if let animationEntity = try? Entity.load(named: animModelName) {
                if let animation = animationEntity.availableAnimations.first {
                    animations.append(animation)
                } else {
                    print("No animations found in \(animModelName)")
                }
            } else {
                print("Failed to load entity from \(animModelName)")
            }
        }
        
        if let randomAnimation = animations.randomElement() {
            print("Attempting to play animation with bind paths:")
                      
            entity.playAnimation(randomAnimation.repeat(duration: .infinity), transitionDuration: 1.25, startsPaused: false)
            
            print("Playing animation from \(animations.count) loaded animations")
        } else {
            print("No animations available to play")
        }
    }
}

class ARViewModel: ObservableObject {
    @Published var mainModelEntity: ModelEntity?
}
swift xcode animation realitykit usdz
1个回答
0
投票

我相信 RealityKit Composer Pro 会帮助你实现这一目标。

© www.soinside.com 2019 - 2024. All rights reserved.