如何从 SpriteAtlas 获取原始纹理以在着色器中用作 alpha 蒙版?

How to get original texture from SpriteAtlas to use in a shader as alpha mask?

我有问题,需要帮助来解决。

我正在尝试通过自定义着色器和附加纹理作为蒙版动态地为精灵的某些部分着色。这不是问题,我很容易解决。

为了更好地理解屏幕:

这是真实的实现,其图像的颜色混合是通过着色器、精灵和蒙版实现的。

但我被迫使用图集来节省 RAM,因为必须同时加载许多精灵(每个不同单元有 3 个状态和 5 个方向)。这引起了问题。当我尝试在编辑模式下(通过使用 ExecuteInEditMode)执行此操作时,一切正常。但是当我按下 "play" 按钮时,一切都崩溃了。

它的样子:

据我了解,问题是当我按下 "play" 时,图集已构建。所以当我从它那里得到精灵时,我得到了它。但是当我尝试从 Sprite 获取纹理以将其设置为 Shader 时,我得到了很大的纹理(完整图集)。着色器不知道这个 sheet 上的实际位置,因为像素需要着色。

所以我的问题是:如何从 sprite 获取 "small" 纹理并将其设置为着色器?

我如何将 "mask" 设置为着色器:

public void UpdateMask(Texture tex)
{
    //Debug.LogFormat("UpdateMask {0}", tex);
    m_SRenderer.GetPropertyBlock(m_SpriteMbp);
    m_SpriteMbp.SetTexture("_Mask", tex);
    m_SRenderer.SetPropertyBlock(m_SpriteMbp);
}

一些着色器的片段:

Properties 
{
    [PerRendererData] _MainTex("Sprite Texture (RGB)", 2D) = "white" {}
    [PerRendererData] _Mask("Alpha (A)", 2D)  = "white" {}

    _FriendlyColor("FriendlyColor", Color) = (1,1,1,1)
    _EnemyColor("EnemyColor", Color) = (1,1,1,1)
    _NeutralColor("NeutralColor", Color) = (1,1,1,1)

    _Intencity("BlendingIntencity", Range(0, 1)) = 0.5
    [PerRendererData] _IsFriendly("IsFriendly", Float) = 0

    [PerRendererData] _IsHighlight("Outline", Range(0, 1)) = 0
    [PerRendererData] _HighlightColor("Outline Color", Color) = (1,1,1,1)
}


fixed4 frag(v2f IN) : COLOR
{
    fixed4 mainTex = tex2D(_MainTex, IN.texcoord) * IN.color;
    fixed4 alphaMask = tex2D(_Mask, IN.texcoord) * IN.color;
    fixed4 output;
    fixed4 blendColor;

    if (alphaMask.a > 1.0e-6)
    {
        if (_IsFriendly == 4)
        {
            blendColor = fixed4(_NeutralColor.r, _NeutralColor.g, _NeutralColor.b, alphaMask.r);
        }
        else
        {
            if (_IsFriendly == 1)
            {
                blendColor = fixed4(_FriendlyColor.r, _FriendlyColor.g, _FriendlyColor.b, alphaMask.r);                     
            }
            else
            {
                blendColor = fixed4(_EnemyColor.r, _EnemyColor.g, _EnemyColor.b, alphaMask.r);
            }
        }                   
        output = BlendOverelay(mainTex, blendColor * _Intencity);
    }   
    else
    {
        output = mainTex;
        output.rgb *= output.a;
    }

    if (_IsHighlight != 0)
    {
        fixed4 blendedColor = BlendAdditive(output, _HighlightColor);
        blendedColor.a = output.a;
        blendedColor.rgb *= output.a;
        output = blendedColor;
    }

    return output;
}

你需要告诉sprite渲染器它在图集中的位置在哪里,图集有多大,这样它才能将图集中的IN.texcoord UV转换成space中对应的UV精灵 space。然后,您可以使用 sprite space UV

从 alpha 贴图中采样

在 C# 中,将图集偏移和比例信息设置为例如 _AtlasPosition:

public void UpdateMask(Texture tex)
{
    //Debug.LogFormat("UpdateMask {0}", tex);
    m_SRenderer.GetPropertyBlock(m_SpriteMbp);
    m_SpriteMbp.SetTexture("_Mask", tex);
    Vector4 result = new Vector4(sprite.textureRect.position.x, sprite.textureRect.position.y, sprite.textureRect.size.x, sprite.textureRect.size.y)

    m_SpriteMbp.SetVector("_AtlasPosition", result)
    m_SRenderer.SetPropertyBlock(m_SpriteMbp);
}

在着色器中,计算子画面中的当前 UV space,并使用它从 _Mask:

进行采样
fixed4 frag(v2f IN) : COLOR
{
    fixed4 mainTex = tex2D(_MainTex, IN.texcoord) * IN.color;
    // multiply both the position offset and size by the texel size to bring them into UV space
    float4 atlasOffsetScale = _AtlasPosition * _MainTex_TexelSize.xyxy;
    // apply UV position offset and scale, sample from alpha mask
    fixed4 alphaMask = tex2D(_Mask, (IN.texcoord - atlasOffsetScale.xy) / atlasOffsetScale.zw) * IN.color; 
    fixed4 output;
    fixed4 blendColor;
    // ...

如果您还没有声明,则必须在着色器中声明 _MainTex_TexelSize

如果您使用紧密包装,这将不起作用。对于 Sprite Packer,您需要在 sprite packer 中指定 DefaultPackerPolicy 或在 packing 标签中指定 [RECT]。如果您使用 SpriteAtlas,则需要禁用 Tight Packing

代码来自 this thread on the unity forums