《Unity着色器圣经》4.0.3 | 为Cg/HLSL着色器加上透明度

目录索引

译文

在本节中,我们将添加“Blend”,以便着色器具有明确的Alpha通道。在USB_simple_color的先前配置中,我们添加了颜色来更改纹理的色调。现在,值得一提的是,颜色属性有四个通道(RGBA),但是,如果我们从Inspector修改颜色的Alpha通道,我们会注意到这不会产生任何变化,为什么会这样?主要是因为它不具有混合特性。为了使着色器受到透明度的影响,我们必须:在Tags中添加RenderType=Transparent,Queue=Transparent,并添加要使用的Blend类型(如下图所示的“Blend SrcAlpha OneMinusSrcAlpha” )。

Shader "USB/USB_simple_color"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color ("Texture Color", Color) = (1, 1, 1, 1)
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent" }
        Blend SrcAlpha OneMinusSrcAlpha
        LOD 100

    Pass { ... }
    }
}

如果我们保存着色器并返回Unity,我们现在可以通过更改Alpha通道颜色从而影响材质纹理的透明度。


原文对照

In this section, we will add Blending so that our shader has a defined Alpha channel. In the previous configuration of USB_simple_color, we added color to change the tint of the texture. Now, it is worth mentioning that the color property has four channels (RGBA), however, if we modify the Alpha channel of the color from the Inspector, we will notice that this does not generate any change, why is this? Mainly because it does not have Blending properties. For our shader to be affected by transparency, we must: configure the RenderType as transparent, add the Queue with transparency and add the Blending type that we want to use.

Shader "USB/USB_simple_color"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color ("Texture Color", Color) = (1, 1, 1, 1)
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent" }
        Blend SrcAlpha OneMinusSrcAlpha
        LOD 100

    Pass { ... }
    }
}

If we save our shader and go back to Unity, we can now change the Alpha channel color, consequently, this will influence the transparency of the material’s texture.

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容