What Is The Best Way To Display Pixel Art In Sprite Kit?

烈酒焚心 提交于 2021-02-08 01:51:46

问题


I'm curious to how I would display pixel art for my game. For now i'm just resizing the SKScene to be sceneWithSize:CGSizeMake(256, 192) is this the correct way or is there a bester way to go about doing this sort of task?


回答1:


First, use the default sizes of scenes - you do not need to scale or change their sizes, this is just bad.

Next just scale your sprites either beforehand (in Photoshop for example) using nearest neighbor scale method - this keeps pixels separate and does not introduce antialias.

So for example if you have original 32x32 asset, scale it to 64x64 or 128x128 and use it in game. It will look great and all.

Another way is to have assets at original size, but scale them at runtime.

Here .xScale and .yScale properties of SKSpriteNode come in handy.

But if you scale your sprite it will lose its crispiness - antialiasing artifacts will appear all over.

You have two ways to deal with this - either create textures first and set their filtering method to nearest neighbour like so:

texture.filteringMode = SKTextureFilteringNearest;

But this gets out of hand fast, so I suggest using the category on SKTextureAtlas instead:

SKTextureAtlas+NearestFiltering.h

#import <SpriteKit/SpriteKit.h>

@interface SKTextureAtlas (NearestFiltering)

- (SKTexture *)nearestTextureNamed:(NSString *)name;

@end

SKTextureAtlas+NearestFiltering.m

#import "SKTextureAtlas+NearestFiltering.h"

@implementation SKTextureAtlas (NearestFiltering)

- (SKTexture *)nearestTextureNamed:(NSString *)name
{
    SKTexture *temp = [self textureNamed:name];
    temp.filteringMode = SKTextureFilteringNearest;
    return temp;
}

@end

This way you can create SKSpriteNodes by calling this method:

SKSpriteNode *temp = [[SKSpriteNode alloc] initWithTexture:[self.atlas nearestTextureNamed:@"myTexture"]];


来源:https://stackoverflow.com/questions/24253518/what-is-the-best-way-to-display-pixel-art-in-sprite-kit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!