1
votes

I'm trying to learn how to write shaders in Unity and am having trouble separating the color and alphas values from affecting the other textures(or layers).

In my shader I have 3 textures of letters (The backgrounds are made transparent in the unity texture settings for each).

I want to have a background color then I need 3 texture/layers on top of the background and be able to change the tint and alpha of each layer without affecting the background or any other layer.

also...

When I multiply the Color by the texture in the surf function, The texture tint changes but also does the transparent background. Why is the transparent backgrounds tint changing when it is transparent?

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
You're multiplying the color values, that is definitely not what you want. Particularly by multiplying all the alpha values together at the end! If any one of those is 0, then you'll draw nothing at all! See khronos.org/opengl/wiki/Blending to figure out what math you do want. - Draco18s no longer trusts SE

1 Answers

2
votes

Nice thing to begin the shaders with unity ;)

So here is my solution to your problem, you have 2 exposed textures. One is the background (MainTex) and one is one of your letter (overlay) and you can mix the alpha of the overlay with the variable 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"

}

Hope this will help :D