Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Unlit Cutout with Shadows

Discussion in 'Shaders' started by djweinbaum, Mar 30, 2014.

  1. djweinbaum

    djweinbaum

    Joined:
    Nov 3, 2013
    Posts:
    525
    This seems like a theoretically simple shader but I can't find it anywhere! Like a cutout diffuse accept with totally flat shading. Where can I find it/how can I make it?
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,478
    I think this is what you're looking for:
    Code (csharp):
    1.  
    2. Shader "Unlit/Transparent Cutout (Shadow)"
    3. {
    4.     Properties
    5.     {
    6.         _Color   ("Main Color", Color) = (1,1,1,1)
    7.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    8.         _Cutoff  ("Alpha cutoff", Range(0,1)) = 0.5
    9.     }
    10.     SubShader
    11.     {
    12.         Tags
    13.         {
    14.             "Queue" = "AlphaTest"
    15.             "IgnoreProjector" = "True"
    16.             "RenderType" = "TransparentCutout"
    17.         }
    18.         LOD      100
    19.         Lighting Off
    20.  
    21.         CGPROGRAM
    22.         #pragma surface surf Lambert alphatest:_Cutoff
    23.  
    24.         sampler2D _MainTex;
    25.         fixed4 _Color;
    26.  
    27.         struct Input
    28.         {
    29.             float2 uv_MainTex;
    30.         };
    31.  
    32.         void surf (Input IN, inout SurfaceOutput o)
    33.         {
    34.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    35.             o.Emission = c.rgb;
    36.             o.Alpha = c.a;
    37.         }
    38.         ENDCG
    39.     }
    40.  
    41.     Fallback "Transparent/Cutout/VertexLit"
    42. }
    43.  
     
  3. djweinbaum

    djweinbaum

    Joined:
    Nov 3, 2013
    Posts:
    525
    That's exactly what I'm looking for! I can't tell you how much time a spent searching for this. Thank you so much Peter77, you've really helped me out. You're assets in the store look very useful, and in a modest attempt to repay you I've purchased your Texture Overview plugin. You've helped me in two big ways today because that texture overview is friggin useful! That's so awesome how you can see all the textures in your scene and where your memory is going. I'll certainly drop a good review.
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,478
    I'm glad I could help and that you like the plugin. Also thanks for the review! :)
     
    CTPEJIOK22 likes this.
  5. djweinbaum

    djweinbaum

    Joined:
    Nov 3, 2013
    Posts:
    525
    I did have one more question for you Peter or anyone who may know: How would I go about making this shader receive shadows as well as cast them?
     
  6. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,478
    My first thought was it's not going to happen to cast shadow on an unlit object, but then I found this thread over at polycount where an unlit cast/receive shadow shader can be found. I only added the alpha cut-out part and changed the tags. I'm not really certain what it does in detail to be honest, but it seems to work.
    Code (csharp):
    1.  
    2. Shader "Unlit/Transparent Cutout (Shadow)" {
    3.     Properties {
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _Cutoff  ("Alpha cutoff", Range(0,1)) = 0.5
    7.     }
    8.     SubShader {
    9.         Tags {"Queue" = "AlphaTest" "RenderType" = "TransparentCutout"}
    10.  
    11.         Pass {
    12.             Tags {"LightMode" = "ForwardBase"}
    13.             CGPROGRAM
    14.                 #pragma vertex vert
    15.                 #pragma fragment frag
    16.                 #pragma multi_compile_fwdbase
    17.                 #pragma fragmentoption ARB_fog_exp2
    18.                 #pragma fragmentoption ARB_precision_hint_fastest
    19.                 #pragma alphatest:_Cutoff
    20.                
    21.                 #include "UnityCG.cginc"
    22.                 #include "AutoLight.cginc"
    23.                
    24.                 struct v2f
    25.                 {
    26.                     float4  pos         : SV_POSITION;
    27.                     float2  uv          : TEXCOORD0;
    28.                     LIGHTING_COORDS(1,2)
    29.                 };
    30.  
    31.                 v2f vert (appdata_tan v)
    32.                 {
    33.                     v2f o;
    34.                    
    35.                     o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
    36.                     o.uv = v.texcoord.xy;
    37.                     TRANSFER_VERTEX_TO_FRAGMENT(o);
    38.                     return o;
    39.                 }
    40.  
    41.                 sampler2D _MainTex;
    42.                 float _Cutoff;
    43.                 fixed4 _Color;
    44.                
    45.                 fixed4 frag(v2f i) : COLOR
    46.                 {
    47.                     fixed4 color = tex2D(_MainTex, i.uv);
    48.                     clip(color.a - _Cutoff);
    49.                    
    50.                     fixed atten = LIGHT_ATTENUATION(i); // Light attenuation + shadows.
    51.                     //fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.
    52.                     return color * atten * _Color;
    53.                 }
    54.             ENDCG
    55.         }
    56.  
    57.         Pass {
    58.             Tags {"LightMode" = "ForwardAdd"}
    59.             Blend One One
    60.             CGPROGRAM
    61.                 #pragma vertex vert
    62.                 #pragma fragment frag
    63.                 #pragma multi_compile_fwdadd_fullshadows
    64.                 #pragma fragmentoption ARB_fog_exp2
    65.                 #pragma fragmentoption ARB_precision_hint_fastest
    66.                
    67.                 #include "UnityCG.cginc"
    68.                 #include "AutoLight.cginc"
    69.                
    70.                 struct v2f
    71.                 {
    72.                     float4  pos         : SV_POSITION;
    73.                     float2  uv          : TEXCOORD0;
    74.                     LIGHTING_COORDS(1,2)
    75.                 };
    76.  
    77.                 v2f vert (appdata_tan v)
    78.                 {
    79.                     v2f o;
    80.                    
    81.                     o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
    82.                     o.uv = v.texcoord.xy;
    83.                     TRANSFER_VERTEX_TO_FRAGMENT(o);
    84.                     return o;
    85.                 }
    86.  
    87.                 sampler2D _MainTex;
    88.                 float _Cutoff;
    89.                 fixed4 _Color;
    90.                
    91.                 fixed4 frag(v2f i) : COLOR
    92.                 {
    93.                     fixed4 color = tex2D(_MainTex, i.uv);
    94.                     clip(color.a - _Cutoff);   
    95.                                    
    96.                     fixed atten = LIGHT_ATTENUATION(i); // Light attenuation + shadows.
    97.                     //fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.        
    98.                     return tex2D(_MainTex, i.uv) * atten * _Color;
    99.                 }
    100.             ENDCG
    101.         }
    102.     }
    103.     Fallback "Transparent/Cutout/VertexLit"
    104. }
    105.  
     
    ninjakohaku likes this.
  7. djweinbaum

    djweinbaum

    Joined:
    Nov 3, 2013
    Posts:
    525
    Man I was scouring polycount too but didn't find this. Ah, well I can see how this is going to end up being more complicated than I'd initially thought, since receiving shadows gives away the flat nature of a far lod. I think I'm going to have to go with a two material solution, where the trunks get full shading and shadows and the leaves only cast shadows. I really appreciate your help.
     
  8. Yonderboy

    Yonderboy

    Joined:
    Nov 19, 2014
    Posts:
    94
    Hi, this shader is so close to what I need, except the cutout edge isn't antialiased :( Would you know how I could achieve the antialiased edge that the legacy shader 'transparent/cutout/soft edge unlit' has? thanks
     
  9. TheGabelle

    TheGabelle

    Joined:
    Aug 23, 2013
    Posts:
    211
    This shader is very close to what I need. I am using this shader on a quad with a tiled cloud texture. The alpha cutoff and shadows are great! Problem is, the resulting clouds are solid white with very hard edges. How could I edit this shader to keep the texture instead of the solid white, and with a soft edge on the alpha cutoff? I've attached an image of the ideal shader I wish to achieve.

    Thanks!
     

    Attached Files:

    Last edited: Aug 17, 2015
unityunity