在 SceneKit 中制作灯光动画

Animating a light in SceneKit

我有一盏灯(Omni 型),我想让它有点动画效果。更确切地说,我想从一个文件中模仿一个光,所以我希望它的 attenuationStartDistance 改变值,color 也是如此。那么,有什么方法可以 运行 一个这样的序列,它是自己的,还是我必须找到一个小技巧来做到这一点?

查看文档:

attenuationStartDistance: The distance from the light at which its intensity begins to diminish. Animatable.

...

You can animate changes to this property’s value. See Animating SceneKit Content.

对于一个连续的动画,你可以这样做:

let flicker = CABasicAnimation(keyPath: "light.attenuationStartDistance")
flicker.fromValue = 100
flicker.toValue = 110
flicker.autoreverses = true
flicker.repeatCount = .infinity
lightNode.addAnimation(flicker, forKey: nil)

一种更现代的方法是使用自定义 SCNAction:

let flickerUp = SCNAction.customAction(duration: 1) { (node, _) in
    node.light?.attenuationStartDistance = 110
}

let flickerDown = SCNAction.customAction(duration: 1) { (node, _) in
    node.light?.attenuationStartDistance = 100
}

let repeat = SCNAction.repeatForever(SCNAction.sequence([flickerUp, flickerDown]))
node.runAction(repeat)