Search Unity

Can't seem to set alpha

Discussion in 'Shaders' started by rayD8, May 14, 2020.

  1. rayD8

    rayD8

    Joined:
    Mar 7, 2018
    Posts:
    32
    Hello,
    I'm new to writing shaders.
    I'm having an issue getting alpha to work. Seems alpha is being ignored, though it doesn't cause a compile error. Am I missing a tag or something?
    Code (CSharp):
    1. Shader "Custom/mat test"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _BlendTex("Alpha Texture", 2D) = "white" {}
    7.         _blendValue("Blend Value", Range(0,1)) = 0.5
    8.     }
    9.     SubShader
    10.     {
    11.         Tags {"RenderType"="Transparent" "Queue"="Transparent"}
    12.         ColorMask RGBA
    13.         LOD 100
    14.  
    15.         Pass
    16.         {
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.             #pragma multi_compile_fog
    21.             #include "UnityCG.cginc"
    22.  
    23.             struct appdata
    24.             {
    25.                 float4 vertex : POSITION;
    26.                 float2 uv : TEXCOORD0;
    27.             };
    28.  
    29.             struct v2f
    30.             {
    31.                 float2 uv : TEXCOORD0;
    32.                 UNITY_FOG_COORDS(1)
    33.                 float4 vertex : SV_POSITION;
    34.             };
    35.  
    36.             sampler2D _MainTex;
    37.             float4 _MainTex_ST;
    38.             sampler2D _BlendTex;
    39.             float4 _BlendTex_ST;
    40.             float _blendValue;
    41.  
    42.             v2f vert (appdata v)
    43.             {
    44.                 v2f o;
    45.                 o.vertex = UnityObjectToClipPos(v.vertex);
    46.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    47.                 UNITY_TRANSFER_FOG(o,o.vertex);
    48.                 return o;
    49.             }
    50.  
    51.             fixed4 frag (v2f i) : SV_Target
    52.             {
    53.                 fixed4 col =  lerp(tex2D(_MainTex, i.uv), tex2D(_BlendTex, i.uv), _blendValue);
    54.                
    55.                 fixed4 alpha = tex2D(_BlendTex, i.uv);
    56.                 float a = max(alpha.r, max(alpha.g, alpha.b));
    57.  
    58.                 col.r = 1; // This works (just a test)
    59.                 col.a = 0.5; // THIS DON'T DO ANYTHING...
    60.  
    61.                 // apply fog
    62.                 //UNITY_APPLY_FOG(i.fogCoord, col);
    63.                 return col;
    64.             }
    65.             ENDCG
    66.         }
    67.     }
    68. }
    69.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
  3. rayD8

    rayD8

    Joined:
    Mar 7, 2018
    Posts:
    32
    Thanks, but I've already read that. I don't understand/know what I'm missing. What I'm supposed to add to the code...

    Here's an example without any blending of two textures: (I added Blend SrcAlpha OneMinusSrcAlpha)
    Code (CSharp):
    1. Shader "Custom/AlphaTest"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _alphaValue("Alpha Value", Range(0,1)) = 0.5
    7.     }
    8.     SubShader
    9.     {
    10.         Tags {"RenderType"="Transparent" "Queue"="Transparent"}
    11.         ColorMask RGBA
    12.         LOD 100
    13.  
    14.         Pass
    15.         {
    16.             Blend SrcAlpha OneMinusSrcAlpha
    17.  
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #include "UnityCG.cginc"
    22.  
    23.             struct appdata
    24.             {
    25.                 float4 vertex : POSITION;
    26.                 float2 uv : TEXCOORD0;
    27.             };
    28.  
    29.             struct v2f
    30.             {
    31.                 float2 uv : TEXCOORD0;
    32.                 float4 vertex : SV_POSITION;
    33.             };
    34.  
    35.             sampler2D _MainTex;
    36.             float4 _MainTex_ST;
    37.             float _alphaValue;
    38.  
    39.             v2f vert (appdata v)
    40.             {
    41.                 v2f o;
    42.                 o.vertex = UnityObjectToClipPos(v.vertex);
    43.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    44.                 return o;
    45.             }
    46.  
    47.             fixed4 frag (v2f i) : SV_Target
    48.             {
    49.                 fixed4 col = tex2D(_MainTex, i.uv);
    50.            
    51.                 col.a = _alphaValue; // Here's wher I'm stuck...
    52.  
    53.                 return col;
    54.             }
    55.             ENDCG
    56.         }
    57.     }
    58. }
    59.  
    This "works" but I end up with weird visual glitches.
    Whatever is in the editor scene window ends up being displayed in the game window as well... like TWO cameras (editor scene view and the camera in the scene's hierarchy) rendering to the game window. Further it'll have weird black areas (in this case, on the right) - depending on what the alpha value was set. Their blacked out areas' locations "flicker" in and out as the alpha value changes:
    upload_2020-5-14_18-31-2.png

    My end goal for this was to make a shader that takes a main texture and a 2nd texture in which a *color* channel (not its alpha channel) of the 2nd texture would be used to set alpha values for the main texture. I was going to be using renderTextures (using video files) for the texture inputs.
     
    Last edited: May 15, 2020
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    o_O

    There’s nothing in that shader that would cause that. Apart from missing
    ZWrite Off
    and a possibility your material’s Render Queue is overridden to not be using the shader’s Transparent (3000) queue, but that’s not going to cause it to render in another camera.