How do you scale SKSpirteNode without anti aliasing
Asked Answered
S

3

11

I am trying to scale an SKSpriteNode object without smoothing/anti-aliasing (I'm using pixel-art so it looks better pixelated).

Is there a property I need to set to do this? This is the code I am using to render the sprite:

SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"objects"];
SKTexture *f1 = [atlas textureNamed:@"hero_1.png"];

SKSpriteNode *hero = [SKSpriteNode spriteNodeWithTexture:f1];
[self addChild: hero];

hero.scale = 6.0f;

The image is scaled correctly but blurry/smoothed out. This is hero_1.png enter image description here.

Sycamore answered 5/10, 2013 at 1:22 Comment(0)
L
17

Try,

SKTexture *texture = [SKTexture textureWithImageNamed:@"Fak4o"];
texture.filteringMode = SKTextureFilteringNearest;
SKSpriteNode *newHero = [SKSpriteNode spriteNodeWithTexture:texture];
newHero.position = CGPointMake(200, 200);
[newHero setScale:50];
[self addChild:newHero];

SKTextureFilteringNearest

Each pixel is drawn using the nearest point in the texture. This mode is faster, but the results are often pixelated.

Swift:

sprite.texture!.filteringMode = .Nearest
Lifeline answered 5/10, 2013 at 1:26 Comment(9)
its not a property of skspritenode.Sycamore
can you add you sprite to question so I can try a few thingsLifeline
i added to the questionSycamore
that works! thanks - Is there a way to do this without extracting a snapshot using SKView's textureFromNode:?Sycamore
try new way in answer.Lifeline
that works - but it doesn't work with SKTextureAtlasSycamore
This is beautiful. +1Alkalinize
To do this in Swift: sprite.texture!.filteringMode = .NearestEmbolic
@Lifeline Is there a way to set the filteringMode for all SKTextures? So that I don't have to set it for each separately.Askance
P
3

Just a note, if you create a SKSpriteNode instead of SKTexture, you can set the SKTextureFilteringNearest like in the following example:

SKSpriteNode *asteroid = [SKSpriteNode spriteNodeWithImageNamed:@"o1"];
asteroid.size = CGSizeMake(36, 24);
asteroid.position = CGPointMake(startX, startY);
asteroid.name = @"obstacle";
// >>>
asteroid.texture.filteringMode = SKTextureFilteringNearest;
// <<<
[self addChild:asteroid];
Pachston answered 2/6, 2014 at 0:26 Comment(0)
B
0

try this:

hero.xscale = 6.0f;
hero.yscale = 6.0f;
Bayle answered 11/8, 2014 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.