Metal:如何在没有颜色/伽马转换的情况下将图像附加到着色器?

dea*_*het 2 shader gamma color-space scenekit metal

是否可以在没有任何颜色/伽马校正的情况下将图像发送到金属着色器(来自场景套件)?

我有一个数据纹理,其中每个通道对应于我希望能够测试的特定值。这是一张世界地图,绿色通道映射到国家索引。如果国家处于“活动状态”,则该国家/地区将呈现红色,否则呈现灰色。

这是我正在使用的代码示例:

sphereNode = SCNNode(geometry: sphereGeometry)
scene.rootNode.addChildNode(sphereNode)

let program = SCNProgram()
program.vertexFunctionName = "sliceVertex"       
program.fragmentFunctionName = "sliceFragment"
sphereNode.geometry?.firstMaterial?.program = program

let imageProperty = SCNMaterialProperty(contents: UIImage(named: "art.scnassets/Textures/earth-data-map-2k.png")!)
mageProperty.mipFilter = SCNFilterMode.none 
sphereNode.geometry?.firstMaterial?.setValue(imageProperty, forKey: "dataTexture")
Run Code Online (Sandbox Code Playgroud)

片段着色器:

fragment float4 sliceFragment(
    Vertex in [[stage_in]],
    texture2d<float, access::sample> dataTexture [[texture(0)]]
){
    constexpr sampler quadSampler(coord::normalized, filter::linear, address::repeat);
    float4 color = dataTexture.sample(quadSampler, in.texCoords);
    int index = int(color.g * 255.0);
    float active = index == 81 ? 1.0 : 0.0;
    float3 col = mix(float3(color.g,color.g,color.g), float3(1.0,0.0,0.0), active);
    return float4(col.r, col.g, col.b, 1);
}
Run Code Online (Sandbox Code Playgroud)

The problem is that the UIImage that I'm using for the texture seems to be converted into linear space but I want to use the image data unchanged in the shader.

Ken*_*ses 5

Metal 始终在着色器中使用线性 RGB 来处理纹理数据。如果纹理是 sRGB 而不是线性,读取和样本从 sRGB 转换为线性,写入从线性转换为 sRGB。

纹理像素格式告诉 Metal 支持纹理的数据是线性的还是 sRGB。大多数像素格式是线性的,但有些名称以_sRGBsRGB结尾。

在幕后(没有双关),SCNMaterialProperty并且UIImage将解释图像数据,并使用所述源颜色特征文件挑像素格式。如果 PNG 的颜色配置文件指定了除线性或 sRGB 之外的其他内容,则它可能会被转换为其中之一。如果它没有颜色配置文件,它可能被假定为 SRGB。

代表非图像数据的 PNG 应该带有一个颜色配置文件,指定它是线性 RGB。如果您无法修改 PNG 并且它被解释为 sRGB,您可能需要通过CGImage并使用copy(colorSpace:)创建具有相同数据的新图像对象并覆盖其颜色配置文件。您将获得所需的色彩空间CGColorSpace(.genericRGBLinear)

此外,您大概应该filter::nearest用于着色器的采样器。合并相邻国家的索引是没有意义的。

不过,比这一切更好的方法可能是在您的应用程序资源中使用数据文件格式,而不是图像文件格式。然后将它传递给缓冲区中的着色器而不是纹理。