3

在 iOS 的 Metal 应用程序中,我需要渲染附加到简单四边形的半透明纹理。我无法弄清楚正确的 colorAttachment rgb 和 alpha 混合因子。

我的设置:

1) 在 Photoshop 中创建的红色图像,不透明度为 50%。保存为具有透明度的 PNG。该图像存储在我的项目资产文件夹中。

在此处输入图像描述

2)我首先创建一个金属纹理,UIImage然后使用.cgImage-CoreImage - 字段提取图像数据。该图像现在采用预乘格式,因此可以应用经典的 Porter-Duff 公式。稍后再谈。

// load hero texture
do {

    let textureLoader = MTKTextureLoader(device: device)

    guard let image = UIImage(named:"red_translucent") else {
        fatalError("Error: Can not create UIImage")
    }

    if (image.cgImage?.alphaInfo == .premultipliedLast) {
        print("texture uses premultiplied alpha. Rock.")
    }

    heroTexture = try textureLoader.newTexture(with: image.cgImage!, options: nil)
} catch {
    fatalError("Error: Can not load texture")
}

3)这是我相当无聊的纹理片段着色器

fragment float4 textureFragmentShader(_Vertex_ vert [[ stage_in ]], texture2d<float> texas [[ texture(0) ]]) {

    constexpr sampler defaultSampler;

    float4 rgba = texas.sample(defaultSampler, vert.st).rgba;
    return rgba;
}

4)这是我的 colorAttachment 混合因子设置,它是 Porter-Duff “over” 公式:

descriptor.colorAttachments[ 0 ].isBlendingEnabled = true

descriptor.colorAttachments[ 0 ].rgbBlendOperation = .add
descriptor.colorAttachments[ 0 ].alphaBlendOperation = .add

descriptor.colorAttachments[ 0 ].sourceRGBBlendFactor = .one
descriptor.colorAttachments[ 0 ].sourceAlphaBlendFactor = .one

descriptor.colorAttachments[ 0 ].destinationRGBBlendFactor = .oneMinusSourceAlpha
descriptor.colorAttachments[ 0 ].destinationAlphaBlendFactor = .oneMinusSourceAlpha

5)当我在白色背景上用这种红色半透明纹理渲染四边形时,图像太暗了。不正确的渲染(右图)是 rgb = (182, 127, 127)。来自 Photoshop 的正确图像是 rgb (255, 127, 127):

在此处输入图像描述

什么是正确的混合功能?

更新

如果人们想看看它在 Github 上的代码: https ://github.com/turner/HelloMetal/tree/2_pass_render

4

1 回答 1

1

根据 Warren 的建议,解决方案是将MTKTextureLoaderOptionSRGB选项设置为false.

于 2016-10-20T20:24:22.183 回答