user4272677
user4272677

Reputation: 91

How to use SpriteKit Particle File in a project developed with SceneKit?

I'm making a simple ARKit App with SceneKit and need to add some particle effect. Usually I could create a SceneKit Particle File, design some particle effect and use it like this

let smoke = SCNParticleSystem(named: "xxx.scnp", inDirectory: nil)!
let hole = SCNNode()
hole.addParticleSystem(smoke)

But since Xcode12, I can only create a SpritKit Particle File whose suffix is .sks. The code above could not work anymore with this file. I am a newbie in ARKit. May anyone tell me how to integrate the particle effect into my SceneKit project? Thanks a lot.

Upvotes: 3

Views: 1195

Answers (2)

Andy Jazz
Andy Jazz

Reputation: 58093

SpriteKit particles implementation

If you want to use 2D particles in 3D scene, there are two approaches to show SpriteKit scenes in SceneKit. The first approach is to assign SKScene as material for SceneKit's geometry:

// 2D scene
let skScene = SKScene()
    
guard let emitter = SKEmitterNode(fileNamed: "sparks.sks")
else { return }
        
emitter.position = CGPoint(x: 10, y: 25)
emitter.targetNode = skScene
skScene.addChild(emitter)
    
// 3D scene
let plane = SCNPlane(width: 5, height: 5)
plane.materials.first?.diffuse.contents = skScene

let planeNode = SCNNode(geometry: plane)
sceneView.scene.rootNode.addChildNode(planeNode)
    

The second approach – assign SKScene to SceneKit's virtual environment.

sceneView.scene.background.contents = skScene


SceneKit particles implementation

The regular way for working with particles in SceneKit is its native particle system:

let particleSystem = SCNParticleSystem()
particleSystem.birthRate = 50

particleSystem.particleSize = 0.5
particleSystem.particleLifeSpan = 10
particleSystem.particleColor = .green

let particlesNode = SCNNode()
particlesNode.addParticleSystem(particleSystem)
sceneView.scene.rootNode.addChildNode(particlesNode)

P. S.

A few words should also be said about deprecated preconfigured SceneKit's SCNP file and its replacement.

If you want to setup such phenomenon as 3D fire, the best way to do it – do it non-programmatically in Scene graph. For better result use 2 or 3 particle system objects with different parameters.

For better understanding how to setup 3D fire based on png samples (sources) open Apple Motion app's scene, apply Particle Emitters and choose Inspector tab. Then, try to implement a fire with the same look in SceneKit's Scene graph.

enter image description here

Upvotes: 5

ZAY
ZAY

Reputation: 5015

For some unknown reason Apple removed the SCNP (SceneKit particle file) template from the recent XCode editions.

You have the following options:

  • Download any sample code project from Apple or GitHub that contains one or more .scnp files. Grab the file from there and implement it into your AR-Project. You can still edit the file within XCode as usual. (Particle Systems Editor)

  • Create your particle system entirely in code.

like so:

let myParticleSystem = SCNParticleSystem()

then you configure everything you want within the instance: myParticleSystem

Upvotes: 0

Related Questions