如何将我SKTextureFilteringNearest
的所有 SKTextures 设置为默认过滤模式?如果我没有将过滤设置为最近,我的精灵的所有边缘似乎都是模糊的。
问问题
1185 次
2 回答
2
SpriteKit 为所有使用SKTextureFilteringLinear
导致模糊图像的纹理设置默认过滤模式(尤其是在缩放时)。要解决此问题,您可以创建一个类别SKTexture
并SKTextureAtlas
调用正确的方法。或者你可以使用方法调配textureWithImageNamed:
SKTexture+DefaultSwizzle.h
#import <SpriteKit/SpriteKit.h>
@interface SKTexture (DefaultSwizzle)
@end
SKTexture+DefaultSwizzle.m
#import "SKTexture+DefaultSwizzle.h"
#import <objc/runtime.h> // Include objc runtime for method swizzling methods
@implementation SKTexture (DefaultSwizzle)
+ (SKTexture *)swizzled_textureWithImageNamed:(NSString*)filename
{
// This is the original. At this point the methods have already been switched
// which means that `swizzled_texture*` is the original.
SKTexture *texture = [SKTexture swizzled_textureWithImageNamed:filename];
// Set 'nearest' as default mode
texture.filteringMode = SKTextureFilteringNearest;
return texture;
}
+ (void)load
{
Method original, swizzled;
original = class_getClassMethod(self, @selector(textureWithImageNamed:));
swizzled = class_getClassMethod(self, @selector(swizzled_textureWithImageNamed:));
// Swizzle methods
method_exchangeImplementations(original, swizzled);
}
@end
于 2013-12-10T16:52:13.037 回答
0
我提出了一个不同的解决方案,它可以在没有混乱的情况下工作,并且保持原始方法不变:
@interface SKTexture (FilteringModeCategory)
+(SKTexture*) textureWithImageNamed:(NSString*)file
filteringMode:(SKTextureFilteringMode)mode;
+(SKTexture*) textureNearestFilteredWithImageNamed:(NSString*)file;
@end
实施:
@implementation SKTexture (FilteringModeCategory)
+(SKTexture*) textureWithImageNamed:(NSString*)file
filteringMode:(SKTextureFilteringMode)mode
{
SKTexture* tex = [SKTexture textureWithImageNamed:file];
tex.filteringMode = mode;
return tex;
}
+(SKTexture*) textureNearestFilteredWithImageNamed:(NSString*)file
{
SKTexture* tex = [SKTexture textureWithImageNamed:file];
tex.filteringMode = SKTextureFilteringNearest;
return tex;
}
@end
随意重命名第二种方法。怎么样:crispyTextureWithImageNamed?:)
PS:SKTextureAtlas
可能也需要对应textureNamed:filteringMode:
和textureNearestFilteredNamed:
分类方法。
于 2013-10-28T16:51:19.090 回答