首页 > 解决方案 > 金属着色器混合片段接收错误的颜色值

问题描述

我在金属中有 2 次通行证:

1 - 节点通行证

2 - mix_pass

用 xCode 调试它:

fragment half4 node_fragment (NodeColorInOut vert [[stage_in]])
{
    float4 color =  float4((vert.center.x + 1.0) * 0.5 , (vert.center.y + 1.0) * -0.5, 0.0, 0.0);
    return half4(color);
}

vert.center是在node_vertex计算的来自节点中心的数据。不管为什么。我有我的理由。

从 XCODE 调试器中,我可以确认“float4 color”返回 [ 0.416 , -0.345,0.0,0.0 ]。这意味着它使用 Y ([1]) 值 -0.345 正确返回了 half4 颜色。

到目前为止,一切都很好!

问题是当它进入 mix_pass 时。

- 混合顶点:

vertex MixColorInOut mix_vertex (VertexInput in [[stage_in]],
                                        constant SCNSceneBuffer & scn_frame [[buffer (0)]])
{
    MixColorInOut out;
    out.position = in.position;
    out.uv = float2 ((in.position.x + 1.0) * 0.5, (in.position.y + 1.0) * -0.5);
    return out;
}

- 混合片段:

fragment half4 mix_fragment (MixColorInOut vert [[stage_in]],
                            texture2d <float, access :: sample> colorScene [[texture (0)]],
                            depth2d <float, access :: sample> depthScene [[texture (1)]],
                            texture2d <float, access :: sample> colorNode [[texture (2)]],
                            depth2d <float, access :: sample> depthNode [[texture (3)]])
{
…
// this part runs only when it's a pixel inside the node
float3 center_map = colorNode.sample(s, vert.uv).rgb;
…
}

在调试器中(选择节点内的像素) float3 center_map为 [0.416, 0.0 ,0.0,0.0]。

0.0对于第二个组件,它应该是-0.345

这是为什么?这很疯狂,因为 node_fragment 的调试器显示 Y ([1]) 的值为-0.345

理论上,它应该是之前通过调试器确认的确切颜色值 [0.416, -0.345 ,0.0,0.0]。

当我执行 center_map.x 时,它返回正确的值 0.416,这意味着它正在从 node_fragment 获取 x 颜色值。

但是当我做center_map.y时,它是 0.0,这不是 node_fragment 的调试器中显示的值。

任何线索这里有什么问题?

标签: xcodeshaderscenekitarkitmetal

解决方案


第一次使用什么像素格式?大多数像素格式都是unsigned,包括MTLPixelFormatBGRA8Unorm_sRGBSceneKit 默认使用的格式。

如果您只使用两个颜色组件MTLPixelFormatRG32Float是一个不错的选择。


推荐阅读