【问题标题】:SceneKit Particle Systems in a Swift PlaygroundSwift Playground 中的 SceneKit 粒子系统
【发布时间】:2019-05-28 10:32:36
【问题描述】:

通常我使用这样的文件初始化器来实例化 SCNParticleSystems:

var stars = SCNParticleSystem(named: "Stars.sncp", inDirectory: nil)

然而,这个项目需要一个 Swift Playground,当我尝试将该 init 函数与存储在 Playground 的 Resources 文件夹中的系统一起使用时,它返回 nil(即使我将指定的目录更改为“Resources”或“/Resources”等) . ).

Playground 资源路径的处理方式是否与普通应用不同,还是我犯了一个非常愚蠢的文件命名错误?

【问题讨论】:

    标签: swift scenekit swift-playground


    【解决方案1】:

    在 Xcode 11 及更高版本中,没有预配置的 .scnp 粒子系统文件。相反,您可以使用直接来自 Xcode 库的 Particle System 对象。

    或者,像往常一样,您可以在 Xcode Playground 中以编程方式创建粒子系统。


    关于 iPad 版 Swift Playgrounds read here


    这是一个代码:

    import PlaygroundSupport
    import SceneKit
    
    let rectangle = CGRect(x: 0, y: 0, width: 1000, height: 200)
    var sceneView = SCNView(frame: rectangle)
    
    var scene = SCNScene()
    sceneView.scene = scene
    sceneView.backgroundColor = .black
    
    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    cameraNode.position.z = 70
    sceneView.scene!.rootNode.addChildNode(cameraNode)
    
    let particleSystem = SCNParticleSystem()
    particleSystem.birthRate = 500
    particleSystem.particleLifeSpan = 0.5
    particleSystem.particleColor = .systemIndigo
    particleSystem.speedFactor = 7
    particleSystem.emittingDirection = SCNVector3(1,1,1)
    particleSystem.emitterShape = .some(SCNSphere(radius: 15))
    
    let particlesNode = SCNNode()
    particlesNode.scale = SCNVector3(2,2,2)
    particlesNode.addParticleSystem(particleSystem)
    sceneView.scene!.rootNode.addChildNode(particlesNode)
    
    PlaygroundPage.current.liveView = sceneView
    

    【讨论】:

      【解决方案2】:

      我认为您在文件扩展名方面犯了一个错误。它是 .scnp 而不是 .sncp

      尝试不带任何扩展名 -

      var stars =  SCNParticleSystem(named: "Stars", inDirectory: nil)
      

      或尝试使用正确的扩展名 -

      var stars =  SCNParticleSystem(named: "Stars.scnp", inDirectory: nil)
      

      【讨论】:

      • 好吧,那真是太愚蠢了。非常感谢您花时间浪费在这个问题上!
      猜你喜欢
      • 2023-03-30
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 2012-06-28
      相关资源
      最近更新 更多