Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 253
user4272677 Avatar asked Nov 22 '25 19:11

user4272677


1 Answers

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

like image 55
Andy Jazz Avatar answered Nov 24 '25 12:11

Andy Jazz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!