problem with transparent custom shader

I’m a little bit desperate since I try to get this “simple” shader running without success since 2 days. I have no real clue about shaders and tutorials and books helped me to display “something” but not what I want. So I hope someone with more experience can give me a helping hand in debugging (and understanding) it.

I create a procedural mesh of quads. Those quads represent hexagonal tiles and overlay each other since I want to display different information with different coloured symbols. So for example in a single hexagon I want to draw a grey outline and a red center (controlled by different uv coordinates). The mesh is built properly and I want to control what is displayed simply by changing the vertexcolors (with transparency) of the mesh. So the outline vertices would recive a grey color and the center vertices a red color and when i want an empty hex i simply set alpha to zero in the color.

When I use shader Legacy/Transparent/Diffuse I get this result (mesh is ok!):
https://dl.dropboxusercontent.com/u/44944089/scr_diffuse.png
When I use my custom shader I get this:
https://dl.dropboxusercontent.com/u/44944089/scr_custom.png
So obviously something is messed up but since debugging shaders is actually not easy I want to ask if someone can see obvious mistakes when looking over my Shadercode:

Shader "Custom/HexShader"
{
   Properties
   {
     _MainTex ("HexTexture", 2D) = "white" {}
     _TransVal ("Transparency Value", Range(0,1)) = 0.5
   }
   SubShader
   {
     Tags
     {
       "Queue" = "Transparent"
     }
     ZWrite Off // don't write to depth buffer in order not to occlude other objects

     Pass
     {
       CGPROGRAM

       #pragma vertex vert
       #pragma fragment frag

       sampler2D _MainTex;
       float _TransVal;

       struct vertInput
       {
         float4 vertPos : POSITION;
         float4 vertColor : COLOR0;
         float2 uv_MainTex : TEXCOORD0;
       };

       struct vertOutput
       {
         float4 scrPos: SV_POSITION;
         float4 pixColor : COLOR0;
         float2 uv_MainTex : TEXCOORD0;
       };

       vertOutput vert(vertInput IN)
       {
         vertOutput OUT;
         OUT.pixColor = IN.vertColor;
         OUT.uv_MainTex = IN.uv_MainTex;
         OUT.scrPos = mul(UNITY_MATRIX_MVP, IN.vertPos);
         return OUT;
       }

       half4 frag(vertOutput IN) : COLOR
       {
         half4 color = tex2D(_MainTex, IN.uv_MainTex);
         color.a = color.a * _TransVal * IN.pixColor.a;
         color.rgb = color.rgb * IN.pixColor.rgb;
         return color.rgba;
       }

       ENDCG
     }
   }
}

Note: the _TransVal property shall actually set the transparency. In the textures the outline and center are (partially) opaque and with this float i want to reduce it to my liking. Its set to 1 here for comparison.

Questions:
This shader should target shader model 2.0 for maximum compatibility (option of targeting mobile later). Is this sufficient?
Can I use a built in shader instead for what I want to achieve? Have tried some but mentioned diffuse seems to ignore my color changes.
What is wrong with my shader? There are overlapping effects and outline is “ignored” completely.
Should I use a surface shader instead of a fragment shader? The mesh shall not be influenced by lighting.

Thanks for your time.

No ideas? How could I improve the question? Is something wrong with it?

Ok, I have solved the issue.
alienheretic offered me some ideas via private communication but it stil didn’t work. So I have taken the built in unlit transparent shader as base since it displayed the mesh properly and added utilization of vertex colors to it.
result:

Shader "Custom/Test1"
{
   Properties
   {
     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
   }

   SubShader
   {
     Tags
     {
       "Queue"="Transparent"
       "IgnoreProjector"="True"
       "RenderType"="Transparent"
     }
     LOD 100
   
     ZWrite Off
     Blend SrcAlpha OneMinusSrcAlpha
   
     Pass
     {  
     CGPROGRAM
       #pragma vertex vert
       #pragma fragment frag
       
       #include "UnityCG.cginc"

       struct appdata_t
       {
         float4 vertex : POSITION;
         fixed4 color : COLOR;
         float2 texcoord : TEXCOORD0;
       };

       struct v2f
       {
         float4 vertex : SV_POSITION;
         fixed4 color : COLOR;
         half2 texcoord : TEXCOORD0;
       };

       sampler2D _MainTex;
       float4 _MainTex_ST;
       
       v2f vert (appdata_t v)
       {
         v2f o;
         o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
         o.color = v.color;
         o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
         return o;
       }
       
       fixed4 frag (v2f i) : SV_Target
       {
         fixed4 col = i.color * tex2D(_MainTex, i.texcoord);
         col.rgb *= col.a;
         return col;
       }
     ENDCG
     }// pass
   }// subshader
}// shader

I’m still unsure what has actually been the issue. And to don’t know/understand why something works or doesnt work usually “scares” me. But I’m glad it works now and I hope i don’t have to deal with shaders soon again ;).

Thanks for reading this anyway.

2 years late but omg the “Blend SrcAlpha OneMinusSrcAlpha” line was what did it for me

(i was trying to edit a shader i downloaded from the internet having no clue about shaders)

1 Like

Haha, me too. Many shaders are hard.