'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.



Solution 1:[1]

SpriteKit particles implementation

If you wanna use 2D particles in 3D scene, there are two regular ways to show SpriteKit scenes in SceneKit. The first way 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 way – assign SKScene to virtual surrounding environment of SceneKit.

sceneView.scene.lightingEnvironment.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.

About deprecated preconfigured SCNP file and its replacement read HERE.

If you want to setup such phenomenon as 3D fire, the best way to do it – do it 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 5 app scene and click on Inspector button.

enter image description here

Then, try to implement a fire with the same look in SceneKit's Scene graph.

Solution 2:[2]

First of, are you sure this is One-To-Many?

Maybe check out this? model.save(). You can override the save method to increase the counter of the attribute.

Another possibility is to create a property field for number_of_uses. Something like this:

class Attribute(models.Model):
    # fields

    @property
    def number_of_uses(self):
        return Item.objects.filter(attribute = self).count()
                               # or attribute__field = self.field

This can also be serialized and is always accurate.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 DrummerMann