ARkit 场景套件动画

ARkit Scene Kit Animations

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

func loadModels(原子:字符串){

    // 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))
    }

}

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

话虽如此,您可以使用以下 SCNAction 功能:

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

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

因此你可以这样做:

在您的 class 声明下为您的 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

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