Search Unity

Alpha cutout on Fakelighting vertex fragment shader

Discussion in 'Shaders' started by ThatsComplicated, Aug 1, 2019.

  1. ThatsComplicated

    ThatsComplicated

    Joined:
    Jun 3, 2019
    Posts:
    8
    Hi,

    for optimization purposes I found this custom fakelighting shader for our mobile project.
    The thing is we need it to be cutout and all the solutions i found was for surface shaders.
    Is it even possible to add cutout to its current form? I've read too that its maybe more efficient to make it transparent instead of cutout.

    Code (CSharp):
    1. Shader "Custom/FakeLighting"
    2. {
    3.   Properties
    4.   {
    5.     _Color ("Color", Color) = (1,1,1,1)
    6.     _Brightness ("Brightness", Range(0,1)) = 0.4
    7.     _MainTex ("Albedo (RGB)", 2D) = "white" {}
    8.   }
    9.  
    10.   SubShader
    11.   {
    12.     Tags { "RenderType" = "Opaque" }
    13.     LOD 200
    14.     Pass
    15.     {
    16.  
    17.       CGPROGRAM
    18.  
    19.       // Define name of vertex shader
    20.       #pragma vertex vert
    21.       // Define name of fragment shader
    22.       #pragma fragment frag
    23.  
    24.       // Include some common helper functions, such as UnityObjectToClipPos
    25.       #include "UnityCG.cginc"
    26.  
    27.       float4 _Color;
    28.       float _Brightness;
    29.  
    30.       // Color Diffuse Map
    31.       sampler2D _MainTex;
    32.       // Tiling/Offset for _MainTex, used by TRANSFORM_TEX in vertex shader
    33.       float4 _MainTex_ST;
    34.  
    35.       // This is the vertex shader input: position, UV0, UV1, normal
    36.       struct appdata
    37.       {
    38.         float4 vertex   : POSITION;
    39.         float2 texcoord : TEXCOORD0;
    40.         float3 normal: NORMAL;
    41.       };
    42.  
    43.       // This is the data passed from the vertex to fragment shader
    44.       struct v2f
    45.       {
    46.         float4 pos  : SV_POSITION;
    47.         float2 txuv : TEXCOORD0;
    48.         float3 normalDir : TEXCOORD2;
    49.       };
    50.  
    51.       // This is the vertex shader
    52.       v2f vert(appdata v)
    53.       {
    54.         v2f o;
    55.         o.pos = UnityObjectToClipPos(v.vertex);
    56.         o.txuv = TRANSFORM_TEX(v.texcoord.xy,_MainTex);
    57.  
    58.         // Calculating normal so it can be used for fake lighting
    59.         // in the fragment shader
    60.         float4x4 modelMatrixInverse = unity_WorldToObject;
    61.         o.normalDir = normalize(mul(float4(v.normal, 0.0), modelMatrixInverse).xyz);
    62.  
    63.         return o;
    64.       }
    65.  
    66.       // This is the fragment shader
    67.       half4 frag(v2f i) : COLOR
    68.       {
    69.         // Reading color from diffuse texture
    70.         half4 col = tex2D(_MainTex, i.txuv.xy);
    71.  
    72.         // Using hard-coded light direction for fake lighting
    73.         half3 th = normalize(half3(0.25, 1, -0.25));
    74.         // Using hard-coded light direction for fake specular
    75.         // This matches the value inside the LightmappedPrefabWithSpec shader
    76.         half3 sth = normalize(half3(0, 1, -0.25));
    77.  
    78.         // Fake lighting
    79.         float lightVal = max(0, dot (i.normalDir, th));
    80.         float lightScale = 0.75;
    81.         lightVal = lightVal * lightScale;
    82.  
    83.         // Fake spec
    84.         float spec = max(0, dot(i.normalDir, sth));
    85.         float specScale = 0.65;
    86.         float specAtt = 0.65;
    87.         spec = specScale * pow (spec, 40.0 * specAtt);
    88.  
    89.         // Add in a general brightness (similar to ambient/gamma) and then
    90.         // calculate the final color of the pixel
    91.         col.rgb = min(half4(1,1,1,1), col.rgb * _Brightness +
    92.                       col.rgb * lightVal * _Color + col.rgb * spec);
    93.         return col;
    94.       }
    95.  
    96.       ENDCG
    97.     }
    98.   }
    99.   FallBack "Diffuse"
    100. }
     
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Insert this before return statement
    clip(col.a-0.3);
    And replace tags part with this
    Tags{ "RenderType" = "TransparentCutout" "Queue" = "AlphaTest"}
     
    Kin0min likes this.
  3. ThatsComplicated

    ThatsComplicated

    Joined:
    Jun 3, 2019
    Posts:
    8
    Thank you very much Mouurusai!
    I'm amazed that was so simple.