我正在尝试向场景中的每个节点添加动画。我的一个问题是,在层次结构中-我要将动画添加到哪个节点。我认为这是由您实际看到的表示的节点,该节点将是网格对象...但是我有一个节点同时容纳两盏灯和网格对象...是否也可以动画化一项工作?
这是我的代码-我正在使用委托方法等待场景填充完毕,然后尝试遍历场景的节点并添加动画:
func sceneLocationViewDidSetupSceneNode(sceneLocationView: SceneLocationView, sceneNode: SCNNode) {
let animation = CABasicAnimation(keyPath: "opacity")
animation.fromValue = 100.0
animation.toValue = 0.0
animation.duration = 2.0
animation.autoreverses = true
animation.repeatCount = .infinity
print("\(sceneNode) name name name name name" )
for node in sceneNode.childNodes {
for nodeInner in node.childNodes {
for nodeInnermost in nodeInner.childNodes {
if (nodeInnermost.name?.localizedStandardContains("ship"))! {
print(nodeInnermost.childNodes.first ?? "FIRST DOESNT EXIST")
nodeInnermost.childNodes.first?.addAnimation(animation, forKey: "opac")
}
}
}
}
}
它根本不起作用,不透明度没有变化。
最佳答案
请尝试以下方法:
let plane = SCNPlane(width: 0.2, height: 0.2)
plane.firstMaterial?.diffuse.contents = vc2
let planeNode = SCNNode(geometry: plane)
planeNode.opacity = 0
node.addChildNode(planeNode)
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
SCNTransaction.animationDuration = 4
planeNode.opacity = 1
planeNode.position.z = -0.2
}
return node