Advertisement
IRCSS

ShadowMapping With Outline

Jan 5th, 2019
4,860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Unlit/recieveShadowMapWithOutline"
  2.  
  3. {
  4.     Properties
  5.     {
  6.         _MainTex ("Texture", 2D) = "white" {}
  7.     }
  8.     SubShader
  9.     {
  10.         Tags { "LightMode" = "ForwardBase"
  11.                 "RenderType"="Opaque"
  12.     }
  13.         LOD 100
  14.  
  15.         Pass
  16.         {
  17.             CGPROGRAM
  18.             #pragma vertex vert
  19.             #pragma fragment frag
  20.             // make fog work
  21.             #pragma multi_compile_fog
  22. #define SHADOWS_SCREEN
  23.             #include "AutoLight.cginc"
  24.             #include "UnityCG.cginc"
  25.  
  26.             struct appdata
  27.             {
  28.                 float4 vertex : POSITION;
  29.                 float2 uv : TEXCOORD0;
  30.             };
  31.  
  32.             struct v2f
  33.             {
  34.                 float2 uv : TEXCOORD0;
  35.                 UNITY_FOG_COORDS(1)
  36.                 SHADOW_COORDS(5)
  37.                 float4 pos : SV_POSITION;
  38.             };
  39.  
  40.             sampler2D _MainTex;
  41.             float4 _MainTex_ST;
  42.            
  43.             v2f vert (appdata v)
  44.             {
  45.                 v2f o;
  46.                 o.pos = UnityObjectToClipPos(v.vertex);
  47.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  48.                 TRANSFER_SHADOW(o);
  49.                 UNITY_TRANSFER_FOG(o,o.pos);
  50.                 return o;
  51.             }
  52.            
  53.             float sampleEdge(sampler2D tex, float2 uv, float Offset) {
  54.                
  55.                 float mi = 1000.;
  56.                 float ma = -100.;
  57.                 float x, y;
  58.                 for (y = -1.; y <= 1.; y += 1.0)
  59.                 {
  60.                     for (x = -1.; x <= 1.; x += 1.0)
  61.                     {
  62.                         float offsets = Offset / _ScreenParams.xy;
  63.  
  64.                         float v = tex2D(tex, uv + float2(x, y)*offsets );
  65.                         mi = min(v, mi);
  66.                         ma = max(v, ma);
  67.                     }
  68.                 }
  69.  
  70.                 return abs(ma - mi);
  71.             }
  72.  
  73.             fixed4 frag (v2f i) : SV_Target
  74.             {
  75.                 // sample the texture
  76.                 fixed4 col = tex2D(_MainTex, i.uv);
  77.             float3 shadowCoord = i._ShadowCoord.xyz / i._ShadowCoord.w;
  78.             float shadowmap = tex2D(_ShadowMapTexture, shadowCoord.xy);
  79.             float thickness = 20.;
  80.             float e = sampleEdge(_ShadowMapTexture, shadowCoord.xy, thickness / i._ShadowCoord.w);
  81.             col.xyz = lerp(pow(col.xyz, 3.6)*0.45, col.xyz, shadowmap);
  82.             col.xyz = lerp(col.xyz, float3(.1,0.2,0.3), e);
  83.             UNITY_APPLY_FOG(i.fogCoord, col);
  84.                 return col;
  85.             }
  86.             ENDCG
  87.         }
  88.     }   FallBack "VertexLit"
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement