How can I get rid of blurry textures in Metal?

蹲街弑〆低调 提交于 2020-03-21 10:55:28

问题


I just created a Metal template and changed up the code a little. I switched out the default colormap with the Minecraft 16x16 lapis ore texture, but for some reason they are blurred out when they are low resolution. I am trying to achieve that pixelated, Minecraft look and so would like to know how to disable this blurring/filtering.

Is there a way to load/present assets without this blurring? Here is my load asset function:

class func loadTexture(device: MTLDevice, textureName: String) throws -> MTLTexture {
    /// Load texture data with optimal parameters for sampling
    return try MTKTextureLoader(device: device).newTexture(name: textureName, scaleFactor: 1.0, bundle: nil, options: [
        MTKTextureLoader.Option.textureUsage: NSNumber(value: MTLTextureUsage.shaderRead.rawValue),
        MTKTextureLoader.Option.textureStorageMode: NSNumber(value: MTLStorageMode.`private`.rawValue)
    ])
}

Here is a screenshot of the blurry cube I'm getting:


回答1:


In your texture sample call (in the shader), you need to set your magnification filter to 'nearest', instead of 'linear', like so (assuming your sampler is declared inline inside your shader):

constexpr sampler textureSampler (mag_filter::nearest, // <-- Set this to 'nearest' if you don't want any filtering on magnification
                                  min_filter::nearest);

// Sample the texture to obtain a color
const half4 colorSample = colorTexture.sample(textureSampler, in.textureCoordinate);


来源:https://stackoverflow.com/questions/60573810/how-can-i-get-rid-of-blurry-textures-in-metal

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