Unity Shader:需要帮助创建不影响其他图层的纹理图层

时间:2017-06-30 18:29:27

标签: unity3d shader

我正在尝试学习如何在Unity中编写着色器,并且无法将颜色和alpha值分离,从而影响其他纹理(或图层)。

在我的着色器中,我有3个字母纹理(背景在每个的统一纹理设置中都是透明的。)

我想要一个背景颜色然后我需要在背景上面有3个纹理/图层,并且能够改变每个图层的色调和alpha而不影响背景或任何其他图层。

...也

当我使用冲浪功能中的纹理乘以颜色时,纹理色调会发生变化但透明背景也会变化。为什么透明背景色调在透明时会改变?

Properties{
  _Background("BackroundColor", Color) = (1,1,1,1)
  _Color("c1", Color) = (1,1,1,1)
  _Color2("c2", Color) = (1,1,1,1)
  _Color3("c3", Color) = (1,1,1,1)
  _MainTex ("layer1", 2D) = "white" {}
  _MainTex2 ("layer2", 2D) = "white" {}
  _MainTex3 ("layer3", 2D) = "white" {}
}

SubShader {
   Tags { "RenderType"="Opaque"}

        CGPROGRAM
        #pragma surface surf Lambert alpha
        #pragma target 3.0

        struct Input {
          float2 uv_MainTex;
          float2 uv2_MainTex2;
          float2 uv3_MainTex3;
        };

        sampler2D _MainTex; 
        sampler2D _MainTex2; 
        sampler2D _MainTex3; 
        float4 _Background;
        float4 _Color;
        float4 _Color2;
        float4 _Color3;

        void surf (Input IN, inout SurfaceOutput o) {
          o.Albedo = _Background * (tex2D (_MainTex, IN.uv_MainTex).rgb*_Color) * (tex2D (_MainTex2, IN.uv2_MainTex2).rgb*_Color2) * (tex2D (_MainTex3, IN.uv3_MainTex3).rgb*_Color3);

          o.Alpha = _Color.a * _Color2.a * _Color3.a;

        }
        ENDCG
}
Fallback "Diffuse"

1 个答案:

答案 0 :(得分:2)

开始统一着色器的好事;)

所以这是我解决问题的方法,你有两个暴露的纹理。一个是背景(MainTex),一个是你的一个字母(叠加),你可以将叠加的alpha与变量Ratio _

混合
Shader "Custom/Overlay" {
Properties {
    _Color ("Color", Color) = (1,1,1,1)
    _MainTex ("Main Texture", 2D) = "white" {}
    _Overlay ("Overlay Texture", 2D) = "white" {} // One texture of letter
    _Ratio("Ratio", Range(0,1)) = 1
}
SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 200
    Blend SrcAlpha OneMinusSrcAlpha
    Lighting On
CGPROGRAM

#pragma surface surf Standard fullforwardshadows

#pragma target 3.0

sampler2D _MainTex;
sampler2D _Overlay;
float _Ratio;
struct Input {
    float2 uv_MainTex;
    float2 uv_Overlay;
};

fixed4 _Color;

void surf (Input IN, inout SurfaceOutputStandard o) {
    // Albedo comes from a texture tinted by color
    fixed4 background = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    fixed4 finalFragment = tex2D (_Overlay, IN.uv_Overlay) * _Color;
    o.Albedo = background.rgb * (1 -finalFragment.a*_Ratio) + finalFragment.rgb * _Ratio;
    o.Alpha = background.a;
}
ENDCG
}FallBack "Diffuse"

}

希望这会有所帮助:D