Copy SCNParticleSystem doesn't seem to work

I am trying to use SCNParticleSystem as a "template" for others. I basically want the exact same properties, except for the color animation for the particles. Here is what I have so far:

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 ... } 

I copy not only SCNParticleSystem , but also SCNParticlePropertyController and CAKeyframeAnimation just to be safe. I found that I had to manually make these β€œdeep” copies β€œmanually” since .copy() on SCNParticleSystem does not copy the animation, etc.

When I turn on the copied particle system on node, it was added (by setting birthRate to a positive number), nothing happens.

I don’t think the problem is with the node, with which I added it, since I tried to add particleSystemToCopy to this node and enable this, and the original particle system will become visible in this case. This seems to indicate to me that node I added that the copied particle system was in order in terms of its geometry, rendering order, etc.

Something else might be worth mentioning: the scene is loaded from a .scn file and not created programmatically in code. Theoretically, this should not affect anything, but who knows ...

Any ideas on why this copied particle system does nothing when I turn it on?

+5
source share

Source: https://habr.com/ru/post/1267117/


All Articles