【问题标题】:Play USDZ animation in RealityKit在 RealityKit 中播放 USDZ 动画
【发布时间】:2021-04-11 22:55:57
【问题描述】:

我花了 2 天时间试图了解如何在我的 RealityKit 项目中正确播放动画。

我遵循了其他 stackoverflow 主题的许多提示,但没有成功。我知道使用 RealityKit v2 我们只能播放 usdz 文件中的第一个动画,好的。我正在尝试直接在 Reality Composer 中播放 Apple 提供的“toy_robot_vintage.usdz”的第一个动画。

这是我的完整代码:

func loadModel(named: String, result: ARRaycastResult) {
        
        var usdzToLoad: String = ""
        
        switch named {
        case "ROBOT":
            usdzToLoad = "toy_robot_vintage.usdz"
        default:
            break;
        }
        
        DispatchQueue.main.async {
            let modelToLoad = try! ModelEntity.loadModel(named: usdzToLoad)
            
            switch named {
            case "ROBOT":
                modelToLoad.name = "ROBOT"
            default:
                break;
            }
            
            let anchor = AnchorEntity(plane: .horizontal, classification: .any, minimumBounds: [0.1, 0.1])
                anchor.position.y = 0.01
                anchor.addChild(modelToLoad)
            
            // Create a "Physics" model of the toy in order to add physics mode
            guard let modelEntity = anchor.children.first as? (Entity & HasPhysics)
            else { return }

            self.arView.installGestures([.rotation], for: modelEntity)
            
            modelEntity.generateCollisionShapes(recursive: true)
            modelEntity.physicsBody = PhysicsBodyComponent(shapes: [.generateBox(size: .one)],
                                                           mass: 1.0,
                                                           material: .default,
                                                           mode: .kinematic)

            self.currentEntity = modelEntity
            self.anchorsEntities.append(anchor)
            
            self.arView.scene.addAnchor(anchor)
            
//            self.currentEntity!.availableAnimations.forEach { self.currentEntity!.playAnimation($0.repeat()) }
            
            let robotAnimationResource = self.currentEntity?.availableAnimations.first
            
            self.currentEntity!.playAnimation(robotAnimationResource!.repeat(duration: .infinity),
                                             transitionDuration: 1.25,
                                                   startsPaused: false)
        }

robotAnimationResource 在我尝试播放动画时总是返回一个致命错误。

有什么想法吗?提前感谢您的帮助和支持。

【问题讨论】:

    标签: swift animation augmented-reality realitykit


    【解决方案1】:

    ModelEntity.loadModel 更改为ModelEntity.load,它现在应该有动画了。
    这很奇怪,我不知道为什么,但这在过去对我有用。

    HasPhysics 也继承了 Entity,所以为了避免自己寻找锚的子代等,您应该能够将 guard let modelEntity... 行替换为:

    guard let modelEntity = modelToLoad as? HasPhysics else { return }
    

    编辑:

    我刚刚在操场上运行了这个,动画运行良好:

    import PlaygroundSupport
    import UIKit
    import RealityKit
    
    let arview = ARView(frame: .zero, cameraMode: .nonAR, automaticallyConfigureSession: true)
    arview.environment.lighting.intensityExponent = 3
    let newAnchor = AnchorEntity(world: .zero)
    let newEnt = try! Entity.load(named: "toy_robot_vintage")
    newAnchor.addChild(newEnt)
    arview.scene.addAnchor(newAnchor)
    for anim in newEnt.availableAnimations {
        newEnt.playAnimation(anim.repeat(duration: .infinity), transitionDuration: 1.25, startsPaused: false)
    }
    
    PlaygroundSupport.PlaygroundPage.current.liveView = arview
    

    问题在于,以这种方式导入的模型不符合 HasPhysics(如果您提到它现在对您失败的地方,这很有用)。

    将 ModelComponent 应用到另一个实体类或 ModelEntity。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-14
    • 2021-01-21
    • 2021-07-14
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    相关资源
    最近更新 更多