对于我的AR应用程序,我要从.dae文件添加一个Node。然后,我使用tapRecognizer尝试选择节点并向其中添加动画。
选择节点后,我将显示一个我想控制动画(擦洗)的女巫UISlider
我能够放置节点,选择它并通过动画对其进行擦洗。但是由于您无法将参数传递给UISlider,因此我在全局SCNNode上执行操作。我无法使其在会话中与多个节点一起运行。
当我尝试将animationPlayer添加到Node时,我的程序失败,hitTest返回。它失败,因为它为零。
这是用于添加节点
func addItem() {
let dancingNode = SCNNode()
let results = sceneView?.hitTest(screenCenter, types: .existingPlaneUsingExtent).first
if results == nil{
print("find surface")
}else{
print("place")
let transform = results!.worldTransform
let thirdColumn = transform.columns.3
let sceneURL = Bundle.main.url(forResource: "art.scnassets/twist_danceFixed", withExtension: "dae")
let sceneSource = SCNSceneSource(url: sceneURL!, options: nil)
let animationObject = sceneSource?.entryWithIdentifier("twist_danceFixed-1", withClass: CAAnimation.self)
animationObject?.speed = 0.0
let idleScene = SCNScene(named: "art.scnassets/idleFixed.dae")!
for child in idleScene.rootNode.childNodes {
child.categoryBitMask = BodyType.ObjectModel.rawValue
dancingNode.addChildNode(child)
}
dancingNode.addAnimation(animationObject!, forKey: "dancing" )
dancingNode.position = SCNVector3(thirdColumn.x,thirdColumn.y,thirdColumn.z)
dancingNode.scale = SCNVector3(0.01, 0.01, 0.01)
dancingNode.categoryBitMask = BodyType.ObjectModel.rawValue
self.sceneView.scene.rootNode.addChildNode(dancingNode)
}
}
这是手势识别器:
@objc func tap(recognizer :UITapGestureRecognizer) {
let node = SCNNode()
let sceneView = recognizer.view as! ARSCNView
let taplocation = recognizer.location(in: sceneView)
let hitTest = sceneView.hitTest(taplocation, options: [SCNHitTestOption.categoryBitMask: BodyType.ObjectModel.rawValue])
for result in hitTest{
node.addChildNode(result.node)
}
animationPlayer = node.animationPlayer(forKey: "dancing")!
if !slider.isDescendant(of: self.view) {
if !hitTest.isEmpty {
self.view.addSubview(slider)
}
}else{
slider.removeFromSuperview()
}
}
这是滑块的回调方法:
@objc func changeVlaue(_ sender: UISlider) {
let moment = sender.value
animationPlayer.animation.timeOffset = TimeInterval(moment)
animationPlayer.play()
animationPlayer.speed = 0.0
}