SpriteKit particles – how to set filteringMode to nearest

120

Question: SpriteKit particles – how to set filteringMode to nearest

With standard SKSpriteNodes, I am using nearest as the filteringMode:

snowman.texture?.filteringMode = SKTextureFilteringMode.nearest; 

I want to use this for my particle effects too. SKTextureFilteringMode.nearest; doesn't work for particleTextures (have tried), but managed to find particleTexture?.filteringMode, but can't find any info on how to set this to nearest. The following DOES NOT work:

node.particleTexture?.filteringMode = .nearest 

How can I achieve this?

Total Answers: 1

90

Answers 1: of SpriteKit particles – how to set filteringMode to nearest

i'm not sure spritekit can do this natively. however you can "trick" it by laundering your texture through a shader. it's a bit more convoluted but it works. left side is a particle emitter with a "hard edge" shader. the right side is a normal emitter. enter image description here

import SpriteKit  class PixelArtEmitter: SKNode {     var emitter:SKEmitterNode?          private var shader_source:String = """ #define RESOLUTION 50.0 //width and height dimension of pixel art; pass as a uniform if you want to make this dynamic  void main() {     vec2 st = v_tex_coord.xy;      //quantize the xy values for "hard edge" effect     float x = floor(st.x * RESOLUTION) / RESOLUTION;     float y = floor(st.y * RESOLUTION) / RESOLUTION;      gl_FragColor = texture2D(u_texture, vec2(x, y)); } """          override init() {         super.init()                  //create an sks file in xcode using your pixel art image as texture         emitter = SKEmitterNode(fileNamed: "PixelArtEmitter.sks")         emitter?.targetNode = self          //add shader         emitter?.shader = SKShader(source: shader_source)          self.addChild(emitter ?? SKNode())     }      required init?(coder aDecoder: NSCoder) {         fatalError("init(coder:) has not been implemented")     } }