Swift 游乐场中的 SceneKit 粒子系统

SceneKit Particle Systems in a Swift Playground

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

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

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

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

我认为您的文件扩展名有误。它是 .scnp 而不是 .sncp.

要么不加任何扩展名试试-

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

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

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

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

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


关于 Swift 游乐场 iPad 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