简体   繁体   中英

Updating geometry in SpriteKit (or SceneKit)

We are porting a game to SpriteKit and I've run in to a bit of a problem. Some objects in our game have triangle-strip trails attached to them. The vertex buffers of the trails are continuously updated as the objects move in the world to create a seamless and flowing effect (there are constraints on how many vertices are in the buffer and how often we emit new vertices).

In the previous implementation we simply updated the affected vertices in the corresponding buffer whenever we emitted new vertices. In SceneKit it seems I am unable to update geometry sources, unless I use geometrySourceWithBuffer:vertexFormat:semantic:vertexCount:dataOffset:dataStride: . To do this however, it seems I need a Metal device, command queue and encoder, to be able to submit commands to update my buffer before it renders.

Is there any way I can do this with a normal SCNView , or do I have to do everything manually with a CAMetalLayer , creating my own metal device etc?

In essence, everything we need, bar this trail geometry is available in SpriteKit, so I was hoping there was some way I could get a hold of the metal device and command queue used by the SKView and simply use that to upload my geometry. This would make things a lot simpler.

I've used a particle system to get a similar effect. The particle emitter is attached to the moving object, with particles set to fade out and eventually die. You might need to set the emitter's targetNode to your scene, depending on the effect you want.

    emitter = SKEmitterNode()
    emitter.name = marbleNodeNames.trail.rawValue
    emitter.particleTexture = SKTexture(imageNamed: "spark")
    emitter.particleAlphaSpeed = -1.0
    emitter.particleLifetime = 2
    emitter.particleScale = 0.2
    marbleSprite.addChild(emitter)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM