如何在ARKit中进行动画时获取3D对象的当前位置?

问题描述 投票:0回答:1

关于图像标记检测,我只想使用ARKit在该标记边界内播放正在行走的家伙的动画。为此,我想找出那个3D对象在标记上行走时的位置。动画是使用外部3D创作工具创建的,并作为.scnassets文件保存在.dae中。我添加了节点并使用以下代码开始动画:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {  


     if let imageAnchor = anchor as? ARImageAnchor {  
          DispatchQueue.main.async { 
              //let translation = imageAnchor.transform.columns.3     
                let idleScene = SCNScene(named: "art.scnassets/WalkAround/WalkAround.dae")!  

             // This node will be parent of all the animation models  
                let node1 = SCNNode()  

                // Add all the child nodes to the parent node  
                for child in idleScene.rootNode.childNodes {  
                     node1.addChildNode(child)  
                }  

               node1.scale = SCNVector3(0.2, 0.2, 0.2)  
               let physicalSize = imageAnchor.referenceImage.physicalSize  
               let size = CGSize(width: 500, height: 500)  

               let skScene = SKScene(size: size)  
               skScene.backgroundColor = .white  

               let plane = SCNPlane(width: self.referenceImage!.physicalSize.width, height: self.referenceImage!.physicalSize.height)  

               let material = SCNMaterial()  
               material.lightingModel = SCNMaterial.LightingModel.constant  
               material.isDoubleSided = true  
               material.diffuse.contents = skScene  
               plane.materials = [material]  
               let rectNode = SCNNode(geometry: plane)  
               rectNode.eulerAngles.x = -.pi / 2  

               node.addChildNode(rectNode)  
               node.addChildNode(node1)  

               self.loadAnimation(withKey: "walking", sceneName: "art.scnassets/WalkAround/SambaArmtr", animationIdentifier: "SambaArmtr-1")  
         }  
    }  
}  
func loadAnimation(withKey: String, sceneName:String, animationIdentifier:String) {  
    let sceneURL = Bundle.main.url(forResource: sceneName, withExtension: "dae")  
    let sceneSource = SCNSceneSource(url: sceneURL!, options: nil)  

    if let animationObject = sceneSource?.entryWithIdentifier(animationIdentifier, withClass: CAAnimation.self) {  
        // The animation will only play once  
        animationObject.repeatCount = 1  
    }  
}

我尝试在以下两种方法中使用node.presentation.position来获取对象的当前位置。

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval)          
// Or
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor)

如果一旦动画开始后我不再移动设备,那么这些方法将不会被调用,直到我获得相同的节点位置为止。那就是为什么我没有走错地方。还是在ARKit中进行动画时有什么方法可以获取对象的当前位置?

ios swift animation scenekit arkit
1个回答
0
投票

我不知道如何在嵌入式动画中获取当前帧。话虽如此,嵌入在模型中的动画使用CoreAnimation来运行动画。您可以使用CAAimationDelegate收听动画的开始/结束事件并运行计时器。计时器将为您提供最佳的动画估计时间。

参考:SceneKit动画内容文档:https://developer.apple.com/documentation/scenekit/animation/animating_scenekit_content

CAAnimationDelegate文档:https://developer.apple.com/documentation/quartzcore/caanimationdelegate

© www.soinside.com 2019 - 2024. All rights reserved.