ios - ARKit中动画播放时如何获取3D对象的当前位置?

标签 ios swift animation scenekit arkit

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

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 中动画进行时获取对象的当前位置?

最佳答案

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

引用文献: SceneKit 动画内容文档:https://developer.apple.com/documentation/scenekit/animation/animating_scenekit_content

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

关于ios - ARKit中动画播放时如何获取3D对象的当前位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59085876/

相关文章:

ios - 尝试将图像保存到 iOS 中具有自定义名称的相册

ios - 将旋转 View Controller 更改为单个公共(public) View Controller objective-c

ios - Swift - 执行 segue 时 Viewcontrollers 不会取消初始化

ios - 在动画中移动时如何允许触摸 UIButton?

javascript - 在网站上查找动画代码

ios - 密集的数据处理使UI交互无法生效

ios - 您可以使用 ios 的 FCM 通知设置线程 ID

xcode - 为什么我的插页式广告不能在我的 GameScene 中工作,但可以在 Swift 的 GameViewController 中工作?

arrays - 选择器不选择值

Android:如何对角扩展和收缩 View ?