【问题标题】:ARkit Scene Kit AnimationsARkit 场景套件动画
【发布时间】:2018-07-21 06:52:25
【问题描述】:

我正在使用 ARkit 构建一个应用程序,我希望用户能够使用视图控制器中的按钮来启动和停止场景中的动画。但是,我无法找到此代码的示例。任何指针将不胜感激。作为参考,我的代码如下,用于对 dae 文件进行动画处理。我有一个可以停止动画的版本,但无法让它重新启动。提前致谢。

func loadModels(atom: String) {

    // Create a new scene
        let scene = SCNScene(named: "art.scnassets/" + atom + ".scn")!

    // Set the scene to the view
    sceneViewAr?.scene = scene

    let mainnode = scene.rootNode.childNode(withName: "mainnode", recursively: true)
    mainnode?.runAction(SCNAction.rotateBy(x: 10, y: 0, z: 0, duration: 0))

    let orbit = scene.rootNode.childNode(withName: "orbit", recursively: true)
    orbit?.runAction(SCNAction.rotateBy(x: 0, y: 2 * 50, z: 0, duration: 100))

    if let orbittwo = scene.rootNode.childNode(withName: "orbit2", recursively: true) {
        orbittwo.runAction(SCNAction.rotateBy(x: 0, y: -2 * 50, z: 0, duration: 100))
    }

    if let orbitthree = scene.rootNode.childNode(withName: "orbit3", recursively: true) {
        orbitthree.runAction(SCNAction.rotateBy(x: 0, y: 2 * 50, z: 0, duration: 100))
    }

}

【问题讨论】:

    标签: arkit


    【解决方案1】:

    我认为没有办法以你想要的方式暂停和停止。

    话虽如此,您可以使用以下SCNAction 函数:

     func action(forKey key: String) -> SCNAction?
     func removeAction(forKey key: String)
    

    基本上你将不得不停止然后重新创建动作。

    因此,您可以这样做:

    在您的SCNActions 的类声明下创建一个变量,这样您就不必每次都重写它们,例如:

    let rotateXAction = SCNAction.rotateBy(x: 10, y: 0, z: 0, duration: 10)
    

    然后创建 2 个函数,一个用于添加,另一个用于删除操作,例如:

    /// Adds An SCNAction To A Specified Node With A Key
    ///
    /// - Parameters:
    ///   - action: SCNAction
    ///   - node: SCNNode
    ///   - key: String
    func addAction(_ action: SCNAction, toNode node: SCNNode, forKey key: String){
    
        node.runAction(action, forKey: key)
    }
    
    
    /// Removes An SCNAction For A Specified Node With A Key
    ///
    /// - Parameters:
    ///   - action: SCNAction
    ///   - node: SCNNode,
    ///   - key: String
    func removeActionFromNode(_ node: SCNNode, forKey key: String){
    
        node.removeAction(forKey: key)
    }
    

    然后你可以像这样测试:

    /// Tests Whether The `SCNActions` Can Be Stated & Stopped
    func testActions(){
    
        //1. Create An Array Of Colours
        let colours: [UIColor] = [.red, .green, .blue, .orange, .purple, .black]
    
        //2. Create An Array Of Face Materials
        var faceArray = [SCNMaterial]()
    
        //3. Create An SCNNode With An SCNBox Geometry
        let firstNode = SCNNode(geometry: SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0))
    
        for faceIndex in 0..<colours.count{
            let material = SCNMaterial()
            material.diffuse.contents = colours[faceIndex]
            faceArray.append(material)
        }
        firstNode.geometry?.materials = faceArray
    
        //4. Add It To The Scene
        self.augmentedRealityView.scene.rootNode.addChildNode(firstNode)
        firstNode.position = SCNVector3(0 , 0, -1)
    
        //5. Run The Action
        addAction(rotateXAction, toNode: firstNode, forKey: "rotateX")
    
    
        //6. Stop It After 4 Seconds
        DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
            self.removeActionFromNode(firstNode, forKey: "rotateX")
    
        }
    
        //7. Start It Again After 8
        DispatchQueue.main.asyncAfter(deadline: .now() + 8) {
            self.addAction(self.rotateXAction, toNode: firstNode, forKey: "rotateX")
    
        }
    }
    

    在您的上下文中,您可以轻松调整这些函数以申请特定的IBAction 等。

    这只是一个开始,绝不是重构或改进的示例,但它应该为您指明正确的方向......

    【讨论】:

      猜你喜欢
      • 2012-08-14
      • 2018-12-22
      • 2018-07-19
      • 2018-08-26
      • 2017-12-01
      • 2017-11-23
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      相关资源
      最近更新 更多