【问题标题】:Copying SCNParticleSystem doesn't seem to work well复制 SCNParticleSystem 似乎效果不佳
【发布时间】: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,还复制了SCNParticlePropertyControllerCAKeyframeAnimation。我发现我必须“手动”手动执行这些“深度”复制,因为 SCNParticleSystem 上的 .copy() 不会复制动画等。

当我在它添加到的节点上打开复制的粒子系统时(通过将birthRate 设置为正数),没有任何反应。

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

还有一点可能值得一提:场景是从 .scn 文件加载的,而不是在代码中以编程方式创建的。理论上应该不会影响任何事情,但谁知道...

关于为什么这个复制的粒子系统在我打开它时不做任何事情的任何想法?

【问题讨论】:

    标签: swift scenekit arkit


    【解决方案1】:

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

    copy() 方法不允许复制粒子的颜色(复制的粒子将默认为白色)。

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

    let particleSystem01 = SCNParticleSystem()
    particleSystem01.birthRate = 2
    particleSystem01.particleSize = 0.5
    particleSystem01.particleColor = .systemIndigo                 // INDIGO
    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()                 // WHITE
    
    let particlesNode02 = SCNNode()
    particlesNode02.addParticleSystem(particleSystem02 as! SCNParticleSystem)
    particlesNode02.position.y = 3
    sceneView.scene.rootNode.addChildNode(particlesNode02)
    


    对节点使用clone() 方法!

    clone() 方法更适用于 3d 对象和粒子系统,它可以帮助您保存粒子的颜色,但当然不允许为每个单独的粒子保存位置。

    let particlesNode02 = particlesNode01.clone()                  // INDIGO
    
    particlesNode02.position.y = 3
    sceneView.scene.rootNode.addChildNode(particlesNode02)
    

    【讨论】:

      猜你喜欢
      • 2015-01-21
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      • 2018-05-18
      • 1970-01-01
      相关资源
      最近更新 更多