【发布时间】:2017-09-22 01:23:56
【问题描述】:
我正在尝试将 SCNParticleSystem 用作其他人的“模板”。除了粒子的颜色动画外,我基本上想要完全相同的属性。到目前为止,这是我所得到的:
if let node = self.findNodeWithName(nodeName),
let copiedParticleSystem: SCNParticleSystem = particleSystemToCopy.copy() as? SCNParticleSystem,
let colorController = copiedParticleSystem.propertyControllers?[SCNParticleSystem.ParticleProperty.color],
let animation: CAKeyframeAnimation = colorController.animation as? CAKeyframeAnimation {
guard animation.values?.count == animationColors.count else {
return nil
}
// Need to copy both the animations and the controllers
let copiedAnimation: CAKeyframeAnimation = animation.copy() as! CAKeyframeAnimation
copiedAnimation.values = animationColors
let copiedController: SCNParticlePropertyController = colorController.copy() as! SCNParticlePropertyController
copiedController.animation = copiedAnimation
// Finally set the new copied controller
copiedParticleSystem.propertyControllers?[SCNParticleSystem.ParticleProperty.color] = copiedController
// Add the particle system to the desired node
node.addParticleSystem(copiedParticleSystem)
// Some other work ...
}
为了安全起见,我不仅复制了SCNParticleSystem,还复制了SCNParticlePropertyController 和CAKeyframeAnimation。我发现我必须“手动”手动执行这些“深度”复制,因为 SCNParticleSystem 上的 .copy() 不会复制动画等。
当我在它添加到的节点上打开复制的粒子系统时(通过将birthRate 设置为正数),没有任何反应。
我认为问题不在于我添加它的节点,因为我尝试将particleSystemToCopy 添加到该节点并打开它,原始粒子系统在这种情况下变得可见.这似乎向我表明,我添加了复制的粒子系统的节点在几何、渲染顺序等方面是可以的。
还有一点可能值得一提:场景是从 .scn 文件加载的,而不是在代码中以编程方式创建的。理论上应该不会影响任何事情,但谁知道...
关于为什么这个复制的粒子系统在我打开它时不做任何事情的任何想法?
【问题讨论】: