Search Unity

How to modify this Two Sided Soft Blend (Hair) shader to work with transparent background?

Discussion in 'Shaders' started by Airmouse, Oct 6, 2020.

  1. Airmouse

    Airmouse

    Joined:
    Jan 12, 2019
    Posts:
    107
    I'm trying to use a double sided "hair" shader for my game characters. it works very well except for when i set my scene camera Clear Flags to a 100% transparent solid color - then only some of the hair is rendered:


    However when I set the scene camera to a non-transparent solid color then the hair looks perfect:

    I am unsure why the hair is occluded when using transparent background, and after spending a few hours trying to find a solution I am completely unsuccessful.

    Is there any option or way I can modify this shader so it will render the hair properly over the transparent areas?

    Here's the code for the shader I am using:
    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Hair/Standard Two Sided Soft Blend" {
    4.     Properties {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         [NoScaleOffset] _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         [Toggle] _UseMetallicMap ("Use Metallic Map", Float) = 0.0
    8.         [NoScaleOffset] _MetallicGlossMap("Metallic", 2D) = "black" {}
    9.         [Gamma] _Metallic ("Metallic", Range(0,1)) = 0.0
    10.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    11.         _BumpScale("Scale", Float) = 1.0
    12.         [NoScaleOffset] _BumpMap("Normal Map", 2D) = "bump" {}
    13.         _Cutoff("Alpha Cutoff", Range(0.01,1)) = 0.5
    14.     }
    15.     SubShader {
    16.         Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
    17.         Blend SrcAlpha OneMinusSrcAlpha
    18.         LOD 200
    19.         ZWrite Off
    20.         Cull Off
    21.         Pass {
    22.             ColorMask 0
    23.             ZWrite On
    24.             CGPROGRAM
    25.             #pragma vertex vert
    26.             #pragma fragment frag
    27.    
    28.             #include "UnityCG.cginc"
    29.             struct v2f {
    30.                 float4 vertex : SV_POSITION;
    31.                 float2 texcoord : TEXCOORD0;
    32.             };
    33.             sampler2D _MainTex;
    34.             fixed _Cutoff;
    35.             v2f vert (appdata_img v)
    36.             {
    37.                 v2f o;
    38.                 o.vertex = UnityObjectToClipPos(v.vertex);
    39.                 o.texcoord = v.texcoord;
    40.                 return o;
    41.             }
    42.             fixed4 frag (v2f i) : SV_Target
    43.             {
    44.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    45.                 clip(col.a - _Cutoff);
    46.                 return 0;
    47.             }
    48.             ENDCG
    49.         }
    50.         Pass
    51.         {
    52.             Tags {"LightMode"="ShadowCaster"}
    53.             ZWrite On
    54.             Cull Off
    55.             CGPROGRAM
    56.             #pragma vertex vert
    57.             #pragma fragment frag
    58.             #pragma multi_compile_shadowcaster
    59.             #include "UnityCG.cginc"
    60.             struct v2f {
    61.                 V2F_SHADOW_CASTER;
    62.                 float2 texcoord : TEXCOORD1;
    63.             };
    64.             v2f vert(appdata_base v)
    65.             {
    66.                 v2f o;
    67.                 TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    68.                 o.texcoord = v.texcoord;
    69.                 return o;
    70.             }
    71.    
    72.             sampler2D _MainTex;
    73.             fixed _Cutoff;
    74.             float4 frag(v2f i) : SV_Target
    75.             {
    76.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    77.                 clip(col.a - _Cutoff);
    78.                 SHADOW_CASTER_FRAGMENT(i)
    79.             }
    80.             ENDCG
    81.         }
    82.  
    83.         CGPROGRAM
    84.         #pragma surface surf Standard fullforwardshadows alpha:fade nolightmap
    85.         #pragma shader_feature _USEMETALLICMAP_ON
    86.         #pragma target 3.0
    87.         sampler2D _MainTex;
    88.         sampler2D _MetallicGlossMap;
    89.         sampler2D _BumpMap;
    90.         struct Input {
    91.             float2 uv_MainTex;
    92.             fixed facing : VFACE;
    93.         };
    94.         half _Glossiness;
    95.         half _Metallic;
    96.         fixed4 _Color;
    97.         half _BumpScale;
    98.         fixed _Cutoff;
    99.         void surf (Input IN, inout SurfaceOutputStandard o) {
    100.             // Albedo comes from a texture tinted by color
    101.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    102.             o.Albedo = c.rgb;
    103.             #ifdef _USEMETALLICMAP_ON
    104.             fixed4 mg = tex2D(_MetallicGlossMap, IN.uv_MainTex);
    105.             o.Metallic = mg.r;
    106.             o.Smoothness = mg.a;
    107.             #else
    108.             o.Metallic = _Metallic;
    109.             o.Smoothness = _Glossiness;
    110.             #endif
    111.             // Rescales the alpha on the blended pass
    112.             o.Alpha = saturate(c.a / _Cutoff);
    113.             o.Normal = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_MainTex), _BumpScale);
    114.             if (IN.facing < 0.5)
    115.                 o.Normal *= -1.0;
    116.         }
    117.         ENDCG
    118.     }
    119.     FallBack "Diffuse"
    120. }
    Thanks so much for any help!!
     
  2. sammiie

    sammiie

    Joined:
    Sep 18, 2021
    Posts:
    2
    Hello! I am having the same problem but I can’t find the solution. Did you solve the problem?