SKTexture 最近的过滤模式不起作用(制作像素艺术)

SKTexture nearest filtering mode doesn't work (Making Pixel Art)

我使用了相对较小的图像(44 像素高)并将其放大以获得像素艺术效果。我试图将过滤模式更改为最近,这样抗锯齿就会消失,但事实并非如此。它看起来边缘模糊。我尝试了相同的背景,得到了相同的结果。

let myNode = SKSpriteNode()

class GameScene: SKScene {


    func makeNode() {

        myNode.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
        myNode.size = CGSize(width: 200, height: 200)
        myNode.texture = SKTexture(imageNamed: "image")
        myNode.texture!.filteringMode = .Nearest

        return self.addChild(myNode)
    }
}
override func didMoveToView(view: SKView) {

    makeNode()

}

这是 Xcode7 beta / iOS9 中的错误。如果你使用 Xcode 6.

更新: SKTexture 最近的过滤模式现在可以在 Xcode 7 GM 中使用。无需按照我在下面的建议手动缩放图像。


我在 Xcode 7 beta 5 和 beta iOS 9 中遇到了同样的问题; Nearest 过滤模式似乎被忽略了。在我的项目中,使用最近邻缩放进行缩放之前在 Xcode 6 on iOS 8 中工作。

如果在 iOS 9 的最终版本之前未解决该错误,我将在将图像放入各自的地图集之前对其进行预缩放。

为此,我编写了一个简单的 python 脚本来递归查找 png 文件并使用 imagmagick 缩放它们。

如果您没有安装 imagemagick,您可以使用 macports 安装它,如下所示:

sudo port install ImageMagick

如果你有 homebrew 它看起来像这样:

brew install imagemagick

我只是将这个脚本(如下)放在我的图集文件上方目录中名为 imgscaler.py 的文件中(在我的情况下我想将其缩放 400%),然后从终端启动它:

python imgscaler.py

脚本如下所示:

import subprocess
import os
import sys
import fnmatch


def main():
    execution_folder_name = os.getcwd()
    file_extension_name = "png"

    #
    # Collect names of files that will be manipulated
    matches = []
    for root, dirNames, fileNames in os.walk(execution_folder_name):
        for filename in fnmatch.filter(fileNames, '*.' + file_extension_name):
            full_name = os.path.join(root, filename)
            matches.append(full_name)

    scale_percentage = "400"

    if not __query_yes_no("This script will scale images by " + scale_percentage + "% recursively from directory " + execution_folder_name + ".  Proceed?"):
        return

    #
    # Scale the images
    for match in matches:
        execution_str = "convert " + match + " -interpolate Nearest -filter point -resize " + scale_percentage + "% " + match + "\n"
        sys.stdout.write(execution_str)
        __run_command(execution_str)


def __run_command(input_cmd):
    """Runs a command on the terminal, and returns the output in a string array.
    """
    process = subprocess.Popen(input_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
    output = []
    while True:
        line = process.stdout.readline()
        if line != '':
            output.append(line)
        else:
            break

    return output


def __query_yes_no(question, default="yes"):
    """Asks a yes or no question, returns a true is answered "yes".
    """
    valid = {"yes": True, "y": True, "ye": True,
             "no": False, "n": False}
    if default is None:
        prompt = " [y/n] "
    elif default == "yes":
        prompt = " [Y/n] "
    elif default == "no":
        prompt = " [y/N] "
    else:
        raise ValueError("invalid default answer: '%s'" % default)

    while True:
        sys.stdout.write(question + prompt)
        sys.stdout.flush()
        choice = raw_input().lower()
        if default is not None and choice == '':
            sys.stdout.write(default)
            sys.stdout.write('\n')
            sys.stdout.flush()
            return valid[default]
        elif choice in valid:
            sys.stdout.write(choice)
            sys.stdout.write('\n')
            sys.stdout.flush()
            return valid[choice]
        else:
            sys.stdout.write("Please respond with 'yes' or 'no' (or 'y' or 'n').\n")
            sys.stdout.flush()


main()

scale_percentage 更改为您想要缩放的任何百分比。

顺便说一句,我猜这个缩放问题最终会得到解决。考虑到这个假设,我目前正在更新我的代码。 这只是一个创可贴,如果 SpriteKit 中最近邻缩放的修复晚于 iOS 9.0。