复制SCNParticleSystem似乎无法正常工作

问题描述 投票:6回答:1

我正在尝试使用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,还复制SCNParticlePropertyControllerCAKeyframeAnimation。我发现我不得不“手动”手动制作这些“深度”副本,因为.copy()上的SCNParticleSystem不能复制动画,等等。

当我将复制的粒子系统添加到节点上时(通过将birthRate设置为正数),什么都没有发生。

我不认为问题出在添加节点上,因为我尝试将particleSystemToCopy添加到该节点并打开它,并且在这种情况下原始粒子系统变得可见。这似乎向我表明,已添加复制粒子系统的节点在其几何形状,渲染顺序等方面都可以。

也许还有其他值得一提的内容:场景是从.scn文件加载的,而不是通过代码以编程方式创建的。从理论上讲不应该影响任何东西,但是谁知道...

关于打开此复制的粒子系统时为何不执行任何操作的任何想法?

swift scenekit arkit
1个回答
0
投票

不要对粒子系统使用copy()方法!

用于SceneKit的粒子系统,因为它不会复制粒子的颜色(复制的粒子将默认为白色)。

您可以使用以下代码对其进行测试:

let particleSystem01 = SCNParticleSystem()
particleSystem01.birthRate = 2
particleSystem01.particleLifeSpan = 2
particleSystem01.particleSize = 0.5
particleSystem01.particleColor = .systemIndigo
particleSystem01.speedFactor = 1
particleSystem01.emittingDirection = SCNVector3(1,1,1)
particleSystem01.emitterShape = .some(SCNSphere(radius: 2.0))

let particlesNode01 = SCNNode()
particlesNode01.addParticleSystem(particleSystem01)
particlesNode01.position.y = -3
sceneView.scene.rootNode.addChildNode(particlesNode01)

let particleSystem02 = particleSystem01.copy()

let particlesNode02 = SCNNode()
particlesNode02.addParticleSystem(particleSystem02 as! SCNParticleSystem)
particlesNode02.position.y = 3
sceneView.scene.rootNode.addChildNode(particlesNode02)

对节点使用clone()方法!

clone()方法无法保存颜色位,不允许您为每个粒子保存位置。

let particlesNode02 = particlesNode01.clone()

particlesNode02.position.y = 3
sceneView.scene.rootNode.addChildNode(particlesNode02)
© www.soinside.com 2019 - 2024. All rights reserved.