swift - 如何在 ARKit 中从特定时间间隔播放 DAE 动画?

标签 swift scenekit augmented-reality arkit realitykit

我的动画模型的总时间间隔为 45 秒。我点击模型,应该能够从一开始就播放它,而不是从第 15 秒开始播放。

如果您以任何方式认为这是可能的,谁能帮助我吗?

编辑:

我一加载我的动画模型,SceneKit 就会播放动画。现在有了 key ,我可以借助遇到的自定义方法裁剪动画。

通过点击模型,我枚举了所有父/子节点以停止或从场景中移除动画。到目前为止,一切都很好。

当我尝试将裁剪后的动画添加回场景时出现问题。什么都没有发生,因为场景保持空闲状态,没有任何 Action 。

我是不是做错了什么?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    let touchLocation = touches.first!.location(in: sceneView)

    // Let's test if a 3D Object was touch
    var hitTestOptions = [SCNHitTestOption: Any]()
    hitTestOptions[SCNHitTestOption.boundingBoxOnly] = true

    let hitResults: [SCNHitTestResult] = sceneView.hitTest(touchLocation, options: hitTestOptions)

    let animation = animScene?.entryWithIdentifier("myKey", withClass: CAAnimation.self)
    print(" duration is...", animation!.duration)

    let animationNew = subAnimation(of:(animation)!, startFrame: 10, endFrame: 360)
    print("New duration is...", animationNew.duration)

    sceneView.scene.rootNode.enumerateChildNodes { (node, stop) in
        node.removeAllAnimations()
    }
    sceneView.scene.rootNode.enumerateChildNodes { (node, _) in
        node.addAnimation(animationNew, forKey: "myKey")
    }        
}

最佳答案

假设,这是播放 Collada 文件中包含的动画的最可靠方法:

import SceneKit

func myAnimation(path: String) -> SCNAnimation? {

    let scene = SCNScene(named: path)
    var animation: SCNAnimationPlayer?

    scene?.rootNode.enumerateChildNodes( { (child, stop) in
        if let animationKey = child.animationKeys.first {

            animation = child.animationPlayer(forKey: animationKey)

            // variable pointee: ObjCBool { get nonmutating set }
            stop.pointee = true
        }
    })
    return animation?.animation
}

let node = SCNNode()
let animation = myAnimation(path: "animation.dae")
node.addAnimation(animation!, forKey: "FirstAnimationSet")

关于swift - 如何在 ARKit 中从特定时间间隔播放 DAE 动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56912593/

相关文章:

swift - 如何使用@MainActor 初始化全局变量?

ios - 如何从自定义 TableViewCell 切换到另一个 ViewController (Swift)

swift - SceneKit - 在 SCNNode 上应用 CIFilter 隐藏 SCNTorus

ios - SceneKit - 音频在我第一次播放声音时导致 cxa_throw 延迟

ios - 在 Apple 的增强现实示例代码 "pARk"中使用四元数

swift - 是否可以将节点保持在适当的位置

ios - AWS S3 公共(public)对象与私有(private)对象?

ios - 保持缩放以 UIView 中心为中心(而不是从左上角缩放)

ios - SCNLight 向各个方向转换阴影

android - 无法在 AR 增强脸部中设置 3D 模型的正确方向