代码之家  ›  专栏  ›  技术社区  ›  kennyc

可以更改金属材质球中采样器的边界吗?

  •  0
  • kennyc  · 技术社区  · 6 年前

    在金属材质球文件的片段函数中,是否有方法根据样本将其视为标准化坐标的内容重新定义纹理的“边界”?

    默认情况下,采样值0,0是纹理的左上角“像素”,1,1是纹理的右下角“像素”。但是,我正在使用纹理进行绘制,在任何给定的渲染过程中,只有一部分纹理包含相关数据。

    我意识到我可以将采样器更改为使用像素寻址,但这需要一些限制,如金属材质球规范中所述。

    1 回复  |  直到 6 年前
        1
  •  2
  •   warrenm    6 年前

    不知道,但如果知道要从中采样的区域,则很容易在着色器中进行一些数学运算以修复采样坐标。这通常与 texture atlases .

    float4 ,将边界存储为(左、上、宽、高) xyzw

    fragment float4 fragment_main(FragmentParams in [[stage_in]],
                                  texture2d<float, access::sample> tex2d [[texture(0)]],
                                  sampler sampler2d [[sampler(0)]],
                                  // ...
                                  constant float4 &spriteBounds [[buffer(0)]])
    {
        // original coordinates, normalized with respect to subimage
        float2 texCoords = in.texCoords;
    
        // texture dimensions
        float2 texSize = float2(tex2d.get_width(), tex2d.get_height());
    
        // adjusted texture coordinates, normalized with respect to full texture
        texCoords = (texCoords * spriteBounds.zw + spriteBounds.xy) / texSize;
    
        // sample color at modified coordinates
        float4 color = tex2d.sample(sampler2d, texCoords);
        // ...
    }