Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Soft occlussion shader

Discussion in 'Shaders' started by luzidlab, Oct 26, 2021.

  1. luzidlab

    luzidlab

    Joined:
    Aug 11, 2021
    Posts:
    5
    Hi,
    I'm working on an AR experience and I have this simple occlussion shader. I need it to have soft edges (like introducing fresnel). I searched the internet, but can't find any solution out there. Is this even possible? I found two nifty shaders (a fresnel and and a "rim-light" shader), but I don't know where to start to combine these with the occlussion shader from Vuforia that I'm currently using (if possible at all).

    thanks in advanced.


    Here's the occlusion shader:

    Code (CSharp):
    1.     Shader "DepthMask" {
    2.  
    3.  
    4.  
    5.         SubShader {
    6.             // Render the mask after regular geometry, but before masked geometry and
    7.             // transparent things.
    8.      
    9.             Tags {"Queue" = "Geometry-10" }
    10.      
    11.             // Turn off lighting, because it's expensive and the thing is supposed to be
    12.             // invisible anyway.
    13.      
    14.             Lighting Off
    15.  
    16.             // Draw into the depth buffer in the usual way.  This is probably the default,
    17.             // but it doesn't hurt to be explicit.
    18.  
    19.             ZTest Always
    20.             ZWrite On
    21.  
    22.             // Don't draw anything into the RGBA channels. This is an undocumented
    23.             // argument to ColorMask which lets us avoid writing to anything except
    24.             // the depth buffer.
    25.  
    26.             ColorMask 0
    27.  
    28.             // Do nothing specific in the pass:
    29.  
    30.             Pass {}
    31.         }
    32.     }

    and here's the rim shader:

    Code (CSharp):
    1. Shader "CustomShader/Rim Light" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _RimColor ("Rim Color", Color) = (1, 1, 1, 1)
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _RimWidth ("RimWidth", Range(0,20.0)) = 0.7
    7.     }
    8.  
    9.     SubShader {
    10.  
    11.         Tags { "RenderType" = "Overlay" "IgnoreProjector"="True"  "Queue" = "Transparent"}
    12.         Cull back
    13.         Lighting Off
    14.         ZWrite Off
    15.         Fog { Mode Off }
    16.         SBlend one One
    17.    
    18.         Pass {    
    19.        
    20.             CGPROGRAM
    21.            
    22.                 #pragma vertex vert
    23.                 #pragma fragment frag
    24.                 #include "UnityCG.cginc"
    25.                 struct appdata {
    26.                     float4 vertex : POSITION;
    27.                     float3 normal : NORMAL;
    28.                     float2 texcoord : TEXCOORD0;
    29.                 };
    30.            
    31.                 struct v2f {
    32.                     float4 pos : SV_POSITION;
    33.                     float2 uv : TEXCOORD0;
    34.                     float3 color : COLOR;
    35.                 };
    36.            
    37.                 uniform float4 _MainTex_ST;
    38.                 uniform float4 _RimColor;
    39.                 half _RimWidth;
    40.            
    41.                 v2f vert (appdata_base v) {
    42.                     v2f o;
    43.                     o.pos = UnityObjectToClipPos (v.vertex);
    44.                
    45.                     float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
    46.                     float dotProduct = 1 - dot(v.normal, viewDir);
    47.                     float rimWidth = _RimWidth;
    48.                     o.color = smoothstep(1 - rimWidth, 1.0, dotProduct);
    49.                
    50.                     o.color *= _RimColor;
    51.                
    52.                     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    53.                
    54.                     return o;
    55.                 }
    56.            
    57.                 uniform sampler2D _MainTex;
    58.                 uniform float4 _Color;
    59.            
    60.                 float4 frag(v2f i) : COLOR {
    61.                     float4 texcol = tex2D(_MainTex, i.uv);
    62.                     texcol *= _Color;
    63.                     texcol.rgb += i.color;
    64.                     return texcol;
    65.                 }
    66.            
    67.             ENDCG
    68.         }
    69.     }
    70. }