Search Unity

Transparency and Rim lighting (Fresnel)

Discussion in 'Shaders' started by Juice-Tin, Jun 29, 2015.

  1. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    244
    Hello, I'm trying to figure out how to fix the depth transparency to the Rimlit object on the left.
    That is, you should not be able to see the legs/sleeves in the back, like to the object on the right.


    However I'm not a shader person myself so I'm having a hard time applying that to the Rim shader.
    Would anyone be so kind as to show me what change should be made, so the rim shader will not display through itself? Thanks!

    Shader for object on right:
    http://forum.unity3d.com/threads/transparent-depth-shader-good-for-ghosts.149511/

    Shader for object on left:
    Code (CSharp):
    1. Shader "Custom/Ghost Rim" {
    2.     Properties {
    3.         _Color ("Rim Color", Color) = (0.5,0.5,0.5,0.5)
    4.         _FPOW("FPOW Fresnel", Float) = 5.0
    5.         _R0("R0 Fresnel", Float) = 0.05
    6.     }
    7.  
    8.     Category {
    9.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    10.         Blend SrcAlpha One
    11.         ZWrite On
    12.  
    13.         SubShader {
    14.             Pass {
    15.            
    16.                 CGPROGRAM
    17.                 #pragma vertex vert
    18.                 #pragma fragment frag
    19.                 #include "UnityCG.cginc"
    20.  
    21.                 sampler2D _MainTex;
    22.                 fixed4 _Color;
    23.                 float _FPOW;
    24.                 float _R0;
    25.                
    26.                 struct appdata_t {
    27.                     float4 vertex : POSITION;
    28.                     fixed4 color : COLOR;
    29.                     float2 texcoord : TEXCOORD0;
    30.                     float3 normal : NORMAL;
    31.                 };
    32.  
    33.                 struct v2f {
    34.                     float4 vertex : POSITION;
    35.                     fixed4 color : COLOR;
    36.                     float2 texcoord : TEXCOORD0;
    37.                 };
    38.                
    39.                 float4 _MainTex_ST;
    40.  
    41.                 v2f vert (appdata_t v)
    42.                 {
    43.                     v2f o;
    44.                     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    45.                     o.color = v.color;
    46.                     o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
    47.  
    48.                     float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
    49.                     half fresnel = saturate(1.0 - dot(v.normal, viewDir));
    50.                     fresnel = pow(fresnel, _FPOW);
    51.                     fresnel = _R0 + (1.0 - _R0) * fresnel;
    52.                     o.color *= fresnel;
    53.                     return o;
    54.                 }
    55.  
    56.                 fixed4 frag (v2f i) : COLOR
    57.                 {
    58.                     return 2.0f * i.color * _Color * tex2D(_MainTex, i.texcoord);
    59.                 }
    60.                 ENDCG
    61.             }
    62.         }  
    63.     }
    64. }
    65.  
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    As mentioned in that post, you need to write a depth-only pass into the shader before your regular pass.

    So it would look something like this with your shader and the depth only pass;

    Code (csharp):
    1. Shader "Custom/Ghost Rim" {
    2.     Properties {
    3.         _Color ("Rim Color", Color) = (0.5,0.5,0.5,0.5)
    4.         _FPOW("FPOW Fresnel", Float) = 5.0
    5.         _R0("R0 Fresnel", Float) = 0.05
    6.     }
    7.  
    8.     Category {
    9.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    10.         Blend SrcAlpha One
    11.  
    12.         SubShader {
    13.  
    14.             // ZPrime pass.
    15.             Pass {
    16.                 ZWrite On
    17.                 ColorMask 0
    18.            
    19.                 CGPROGRAM
    20.                 #pragma vertex vert
    21.                 #pragma fragment frag
    22.                 #include "UnityCG.cginc"
    23.          
    24.                 struct v2f {
    25.                     float4 pos : SV_POSITION;
    26.                 };
    27.          
    28.                 v2f vert (appdata_base v)
    29.                 {
    30.                     v2f o;
    31.                     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    32.                     return o;
    33.                 }
    34.          
    35.                 half4 frag (v2f i) : COLOR
    36.                 {
    37.                     return half4 (0);
    38.                 }
    39.                 ENDCG  
    40.             }
    41.  
    42.             // Fresnel pass.
    43.             Pass {
    44.                 CGPROGRAM
    45.                 #pragma vertex vert
    46.                 #pragma fragment frag
    47.                 #include "UnityCG.cginc"
    48.  
    49.                 sampler2D _MainTex;
    50.                 fixed4 _Color;
    51.                 float _FPOW;
    52.                 float _R0;
    53.                
    54.                 struct appdata_t {
    55.                     float4 vertex : POSITION;
    56.                     fixed4 color : COLOR;
    57.                     float2 texcoord : TEXCOORD0;
    58.                     float3 normal : NORMAL;
    59.                 };
    60.  
    61.                 struct v2f {
    62.                     float4 vertex : POSITION;
    63.                     fixed4 color : COLOR;
    64.                     float2 texcoord : TEXCOORD0;
    65.                 };
    66.                
    67.                 float4 _MainTex_ST;
    68.  
    69.                 v2f vert (appdata_t v)
    70.                 {
    71.                     v2f o;
    72.                     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    73.                     o.color = v.color;
    74.                     o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
    75.  
    76.                     float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
    77.                     half fresnel = saturate(1.0 - dot(v.normal, viewDir));
    78.                     fresnel = pow(fresnel, _FPOW);
    79.                     fresnel = _R0 + (1.0 - _R0) * fresnel;
    80.                     o.color *= fresnel;
    81.                     return o;
    82.                 }
    83.  
    84.                 fixed4 frag (v2f i) : COLOR
    85.                 {
    86.                     return 2.0f * i.color * _Color * tex2D(_MainTex, i.texcoord);
    87.                 }
    88.                 ENDCG
    89.             }
    90.         }  
    91.     }
    92. }
     
    IgorAherne likes this.
  3. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    244
    Ah thanks! I originally tried putting that under the fresnel pass which didn't do anything, so I thought it needed something custom.

    Putting it above the fresnel pass works perfectly.

    However that guys code has an error, so for anyone else:
    return half4 (0);
    should be
    return half4 (0,0,0,0);
     
  4. nirishere

    nirishere

    Joined:
    Sep 7, 2017
    Posts:
    1
    Great stuff!