我想让节点闪烁(例如,从输入颜色循环到当前的firstMaterial)。该节点有一个与许多其他节点共享的材料,但我不希望所有节点都闪烁(只有选定的节点)。
我成功地做了没有 SCNAction / SCNTransaction但代码很脏,很难维护,必须在Update()中调用...我想知道SCNAction / SCNTransaction是否可以帮助我但我不知道如何以正确的方式做到这一点。
答案 0 :(得分:3)
我相信这样的事情会指引你朝着正确的方向发展。
首先,我们将创建一个extension
SCNNode
,以便我们可以在任何SCNNode
上使用我们的函数:
//-----------------------
//MARK: SCNNode Extension
//-----------------------
extension SCNNode{
/// Creates A Pulsing Animation On An Infinite Loop
///
/// - Parameter duration: TimeInterval
func highlightNodeWithDurarion(_ duration: TimeInterval){
//1. Create An SCNAction Which Emmits A Red Colour Over The Passed Duration Parameter
let highlightAction = SCNAction.customAction(duration: duration) { (node, elapsedTime) in
let color = UIColor(red: elapsedTime/CGFloat(duration), green: 0, blue: 0, alpha: 1)
let currentMaterial = self.geometry?.firstMaterial
currentMaterial?.emission.contents = color
}
//2. Create An SCNAction Which Removes The Red Emissio Colour Over The Passed Duration Parameter
let unHighlightAction = SCNAction.customAction(duration: duration) { (node, elapsedTime) in
let color = UIColor(red: CGFloat(1) - elapsedTime/CGFloat(duration), green: 0, blue: 0, alpha: 1)
let currentMaterial = self.geometry?.firstMaterial
currentMaterial?.emission.contents = color
}
//3. Create An SCNAction Sequence Which Runs The Actions
let pulseSequence = SCNAction.sequence([highlightAction, unHighlightAction])
//4. Set The Loop As Infinitie
let infiniteLoop = SCNAction.repeatForever(pulseSequence)
//5. Run The Action
self.runAction(infiniteLoop)
}
}
让我们说,然后我们创建一个SCNNode
SCNSphereGeometry
,如下所示:
let blueNode = SCNNode()
let blueGeometry = SCNSphere(radius: 0.2)
blueGeometry.firstMaterial?.diffuse.contents = UIColor.blue
blueNode.geometry = blueGeometry
blueNode.position = SCNVector3(1.5, 0, -1.5)
blueNode.name = "BlueNode"
augmentedRealityView.scene.rootNode.addChildNode(redNode)
然后我们可以调用此highlight function
(或任何SCNNode
),如下所示:
blueNode.highlightNodeWithDurarion(5)