Search Unity

Custom Alphatest issue

Discussion in 'Shaders' started by RockSPb, Jul 10, 2017.

  1. RockSPb

    RockSPb

    Joined:
    Feb 6, 2015
    Posts:
    112
    Hello! We have a custom alphatest shader.
    It take alpha from another texture (not from MainTex.a) and controlled by vertex color.
    But Unity Ignor this alpha for Baked lighting or ray casting.
    Unity use only alpha from main texture and cutoff value.
    Is it possble to tell unity use different alpha?
    Code (CSharp):
    1. Shader "Custom/Unlit Cutout Step (Supports Lightmap)"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.         _AlphaTex("Alpha", 2D) = "grey" {}
    7.         _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags{ "Queue" = "Geometry"}
    13.  
    14.         Pass
    15.         {
    16.             Tags{ "LightMode" = "Always"}
    17.  
    18.  
    19.     //    AlphaTest Greater .5
    20.             CGPROGRAM
    21.  
    22.             #include "UnityCG.cginc"
    23.             #pragma vertex vert
    24.             #pragma fragment frag
    25.             #pragma multi_compile_fog
    26.             #pragma multi_compile _ LIGHTMAP_ON  
    27.             struct appdata
    28.             {
    29.                 float4 vertex : POSITION;
    30.                 float2 uv : TEXCOORD0;
    31.                 float2 uv1 : TEXCOORD1;
    32.                 float4 vertexColor : COLOR;
    33.             };
    34.  
    35.             struct v2f
    36.             {
    37.                 float2 uv : TEXCOORD0;
    38.                 float2 uv1 : TEXCOORD3;
    39.                 UNITY_FOG_COORDS(1)
    40.                 float4 vertex : SV_POSITION;
    41.                 float4 vertexColor : COLOR;
    42.             };
    43.  
    44.                 sampler2D _MainTex;
    45.                 float4 _MainTex_ST;
    46.                 fixed _Cutoff;
    47.                 sampler2D _AlphaTex;
    48.  
    49.             v2f vert(appdata v)
    50.             {
    51.                 v2f o;
    52.                 o.vertex = UnityObjectToClipPos(v.vertex);
    53.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    54.                 UNITY_TRANSFER_FOG(o,o.vertex);
    55.                 o.uv1 = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    56.                 o.vertexColor = v.vertexColor;
    57.                 return o;
    58.                
    59.             }
    60.  
    61.  
    62.             fixed4 frag (v2f i): SV_Target
    63.             {
    64.                 fixed4 color = tex2D(_MainTex, i.uv);
    65.                 fixed alpha = tex2D(_AlphaTex, i.uv);
    66.  
    67.                
    68.        
    69. #if !UNITY_STANDALONE_WIN
    70.                 fixed3 lightmapColor = 2.0f * UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1.xy); //decode ligthmap
    71. #else
    72.                 fixed3 lightmapColor = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1.xy)); //decode ligthmap
    73. #endif
    74. #ifdef LIGHTMAP_ON  
    75.                 color.rgb *= lightmapColor;
    76.                
    77. #endif
    78.                 UNITY_APPLY_FOG(i.fogCoord, color);
    79.                
    80.                 clip(i.vertexColor.r - alpha);
    81.                 return color;
    82.                
    83.             }
    84.             ENDCG
    85.         }
    86.     }
    87.     Fallback "Mobile/Unlit (Suports Lightmap)"
    88. }
    89.  
    qw

    upload_2017-7-10_12-19-16.png upload_2017-7-10_12-20-14.png
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Yes, also adjust the shadow pass of the shader. Or use a surface shader instead.
     
  3. RockSPb

    RockSPb

    Joined:
    Feb 6, 2015
    Posts:
    112
    I try it;
    Work fine in realtime, but seems to be ignored for baking light(
    upload_2017-7-10_16-24-46.png

    upload_2017-7-10_16-25-44.png
    Code (CSharp):
    1.     Properties
    2.     {
    3.         _MainTex("Texture", 2D) = "white" {}
    4.         _AlphaTex("Alpha", 2D) = "grey" {}
    5.         _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Tags{ "Queue" = "Geometry"}
    11.  
    12.         Pass
    13.         {
    14.             Tags{ "LightMode" = "Always"}
    15.  
    16.  
    17.     //    AlphaTest Greater .5
    18.             CGPROGRAM
    19.  
    20.             #include "UnityCG.cginc"
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             #pragma multi_compile_fog
    24.             #pragma multi_compile _ LIGHTMAP_ON  
    25.             struct appdata
    26.             {
    27.                 float4 vertex : POSITION;
    28.                 float2 uv : TEXCOORD0;
    29.                 float2 uv1 : TEXCOORD1;
    30.                 float4 vertexColor : COLOR;
    31.             };
    32.  
    33.             struct v2f
    34.             {
    35.                 float2 uv : TEXCOORD0;
    36.                 float2 uv1 : TEXCOORD3;
    37.                 UNITY_FOG_COORDS(1)
    38.                 float4 vertex : SV_POSITION;
    39.                 float4 vertexColor : COLOR;
    40.             };
    41.  
    42.                 sampler2D _MainTex;
    43.                 float4 _MainTex_ST;
    44.                 fixed _Cutoff;
    45.                 sampler2D _AlphaTex;
    46.  
    47.             v2f vert(appdata v)
    48.             {
    49.                 v2f o;
    50.                 o.vertex = UnityObjectToClipPos(v.vertex);
    51.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    52.                 UNITY_TRANSFER_FOG(o,o.vertex);
    53.                 o.uv1 = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    54.                 o.vertexColor = v.vertexColor;
    55.                 return o;
    56.                
    57.                
    58.             }
    59.  
    60.  
    61.             fixed4 frag (v2f i): SV_Target
    62.             {
    63.                 fixed4 color = tex2D(_MainTex, i.uv);
    64.                 fixed alpha = tex2D(_AlphaTex, i.uv);
    65.  
    66.                 clip(i.vertexColor.r - alpha);
    67.        
    68. #if !UNITY_STANDALONE_WIN
    69.                 fixed3 lightmapColor = 2.0f * UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1.xy); //decode ligthmap
    70. #else
    71.                 fixed3 lightmapColor = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1.xy)); //decode ligthmap
    72. #endif
    73. #ifdef LIGHTMAP_ON  
    74.                 color.rgb *= lightmapColor;
    75.                
    76. #endif
    77.                 UNITY_APPLY_FOG(i.fogCoord, color);
    78.                
    79.            
    80.                 return color;
    81.                
    82.             }
    83.             ENDCG
    84.         }
    85.            
    86.         Pass
    87.         {
    88.             Name "Caster"
    89.             Tags { "LightMode" = "ShadowCaster" }
    90.             Offset 1, 1
    91.              
    92.             Fog {Mode Off}
    93.             ZWrite On ZTest LEqual Cull Off
    94.        
    95.             CGPROGRAM
    96.             #pragma vertex vert
    97.             #pragma fragment frag
    98.             #pragma multi_compile_shadowcaster
    99.             #include "UnityCG.cginc"
    100.            
    101.             struct appdata
    102.             {
    103.                 float4 vertex : POSITION;
    104.                 float2 uv : TEXCOORD0;
    105.                 float4 normal : NORMAL;
    106.                 float4 vertexColor : COLOR;
    107.             };
    108.            
    109.            
    110.             struct v2f {
    111.                 V2F_SHADOW_CASTER;
    112.                 float2 uv : TEXCOORD0;
    113.                 float4 vertexColor : COLOR2;
    114.                 fixed3 normal : TEXCOORD3;
    115.             };
    116.            
    117.             float4 _MainTex_ST;
    118.             sampler2D _AlphaTex;
    119.            
    120.             v2f vert( appdata v )
    121.             {
    122.                 v2f o;
    123.                 TRANSFER_SHADOW_CASTER(o)
    124.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);                
    125.                 o.normal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));
    126.                 o.vertexColor = v.vertexColor;
    127.                 return o;
    128.             }
    129.            
    130.            
    131.            
    132.             float4 frag( v2f i ) : COLOR
    133.             {
    134.                 fixed4 alpha = tex2D( _AlphaTex, i.uv );
    135.                 clip(i.vertexColor.r - alpha.r);
    136.              
    137.                 SHADOW_CASTER_FRAGMENT(i)
    138.             }
    139.         ENDCG
    140.        
    141.         }
    142.          
    143.             // Pass to render object as a shadow collector
    144.         Pass
    145.         {
    146.             Name "ShadowCollector"
    147.             Tags { "LightMode" = "ShadowCollector" }
    148.              
    149.             Fog {Mode Off}
    150.             ZWrite On ZTest LEqual
    151.        
    152.             CGPROGRAM
    153.             #pragma vertex vert
    154.             #pragma fragment frag
    155.             #pragma multi_compile_shadowcollector
    156.            
    157.             #define SHADOW_COLLECTOR_PASS
    158.             #include "UnityCG.cginc"
    159.            
    160.             struct appdata
    161.             {
    162.                 float4 vertex : POSITION;
    163.                 float2 uv : TEXCOORD0;
    164.                 float4 normal : NORMAL;
    165.                 float4 vertexColor : COLOR;
    166.             };
    167.        
    168.             struct v2f
    169.             {
    170.                 V2F_SHADOW_COLLECTOR;
    171.                 float2 uv : TEXCOORD5;
    172.                 float4 vertexColor : COLOR2;
    173.                 fixed3 normal : TEXCOORD6;
    174.             };
    175.        
    176.             float4 _MainTex_ST;
    177.             sampler2D _AlphaTex;        
    178.        
    179.        
    180.             v2f vert (appdata v)
    181.             {
    182.                 v2f o;
    183.                 TRANSFER_SHADOW_COLLECTOR(o)
    184.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);                
    185.                 o.normal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));
    186.                 o.vertexColor = v.vertexColor;
    187.                 return o;
    188.             }
    189.                  
    190.             fixed4 frag (v2f i) : COLOR
    191.             {
    192.                 fixed4 alpha = tex2D( _AlphaTex, i.uv );
    193.                 clip(i.vertexColor.r - alpha.r);
    194.              
    195.                 SHADOW_COLLECTOR_FRAGMENT(i)
    196.             }
    197.             ENDCG        
    198.         }
    199.     }
    200. //    Fallback "Mobile/Unlit (Suports Lightmap)"
    201. }
     
  4. RockSPb

    RockSPb

    Joined:
    Feb 6, 2015
    Posts:
    112
    I thik surfase shader not so good for mobile like pure fragment, am I right?
     
  5. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Should be fine and it's a lot easier compared to customizing all passes. For light baking there is yet another shader pass.
     
  6. RockSPb

    RockSPb

    Joined:
    Feb 6, 2015
    Posts:
    112
    But what is the pass?)
     
  7. RockSPb

    RockSPb

    Joined:
    Feb 6, 2015
    Posts:
    112
    Also we use raytrace for lighting dynamic models on the scene, and unity give incorrect result for surface opacity too.
    (outline when select show "real opacity")
    And on trasparent areas person becomes dark.

    PS Trying to create surface shader. Result is the same.