美文网首页
SceneKit - 骨骼动画

SceneKit - 骨骼动画

作者: Heikki_ | 来源:发表于2017-09-30 13:02 被阅读338次

    1.import SceneKit 导入框架

    2.在视图上添加 scnView 用以展示模式

        lazy var scnView: SCNView = {
            let scnView = SCNView(frame: self.view.bounds)
            scnView.scene = SCNScene()
            scnView.backgroundColor = .red
            scnView.allowsCameraControl = true
            return scnView
        }()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            view.addSubview(scnView)
            playSomePart()
        }
    

    3.加载 dae 文件
    .dae 文件中带有动画 将会直接播放动画
    .xcode9中资源文件需要通过 右键 addfile 的方法导入否则资源文件不会被打包到项目中

            if let resourceUrl = Bundle.main.url(forResource: "skinning", withExtension: "dae") {
                if FileManager.default.fileExists(atPath: resourceUrl.path) {
                    let sceneSource = SCNSceneSource(url: resourceUrl, options: nil)
                    scnView.scene = sceneSource?.scene(options: nil)
                }
            }
    

    4.播放部分动画

        func playSomePart() {
            if let resourceUrl = Bundle.main.url(forResource: "skinning", withExtension: "dae") {
                if FileManager.default.fileExists(atPath: resourceUrl.path) {
                    let sceneSource = SCNSceneSource(url: resourceUrl, options: nil)
                    scnView.scene = sceneSource?.scene(options: nil)
                    
                    //获得sceneSource 中所有动画类的 identifier
                    let parts = sceneSource?.identifiersOfEntries(withClass: CAAnimation.self)
                    let partsCount = parts?.count ?? 0
                    var animtionArrM = NSMutableArray(capacity: partsCount) as! [CAAnimation]
                    var maxDuration:CFTimeInterval = 0
                    
                    //通过 identifiers 获得 每个动画animation 添加到数组中备用
                    for index in 0..<partsCount {
                        if let animation = sceneSource?.entryWithIdentifier(parts![index], withClass: CAAnimation.self){
                            maxDuration = max(maxDuration, animation.duration )
                            animtionArrM.append(animation)
                        }
                    }
                    
                    //创建动画组
                    let animationGroup = CAAnimationGroup()
                    animationGroup.animations = animtionArrM
                    animationGroup.duration = maxDuration
                    //截取动画组 20s 之后的动画
                    let animationGroup_copy = animationGroup.copy() as! CAAnimationGroup
                    animationGroup_copy.timeOffset = 20;
                    
                    let playAnimtionGroup = CAAnimationGroup()
                    playAnimtionGroup.animations = [animationGroup_copy]
                    playAnimtionGroup.duration = maxDuration - 20
                    playAnimtionGroup.repeatCount = MAXFLOAT
                    //设定这个属性为 YES 时,在它到达目的地之后,动画的返回到开始的值,代替了直接跳转到开始的值,过渡平滑
                    playAnimtionGroup.autoreverses = true;
                    //可以用来设置动画延迟执行时间,若想延迟1s,就设置为CACurrentMediaTime()+1,CACurrentMediaTime()为图层的当前时间
                    animationGroup.beginTime = 20
                    let node = scnView.scene?.rootNode.childNode(withName: "avatar_attach", recursively: true)
                    node?.addAnimation(playAnimtionGroup, forKey: "animation")
                }
            }
        }
    

    5.显示骨骼

    //显示骨骼
    let boneNode = scnView.scene?.rootNode.childNode(withName: "skeleton", recursively: true)   
    self.visualizeBones(show: true, node: boneNode!, scale: 1)
    
        func visualizeBones(show:Bool,node:SCNNode,scale:CFloat) {
            
            let scaleX = node.scale.x * scale;
            print("scale\(scaleX)")
            if (show) {
                if (node.geometry == nil) {
                    node.geometry = SCNBox(width: CGFloat(6/scaleX), height: CGFloat(6/scaleX), length: CGFloat(6/scaleX), chamferRadius: 0.5)
                }else {
                    let boxNode = node.geometry?.isKind(of: SCNBox.self)
                    if(boxNode == true){
                        node.geometry = nil
                    }
                }
            }
            for child:SCNNode in node.childNodes {
                self.visualizeBones(show: show, node: child, scale: scaleX)
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:SceneKit - 骨骼动画

          本文链接:https://www.haomeiwen.com/subject/rxbgextx.html