Hyluss

outline shader postprocess

Jul 23rd, 2019
529
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.50 KB | None | 0 0
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2.  
  3. Shader "Custom/Post Outline"
  4. {
  5.     Properties
  6.     {
  7.          _Color("Main Color", Color) = (0.5,0.5,0.5,1)
  8.         _MainTex("Main Texture",2D) = "black"{}
  9.         _SceneTex("Scene Texture",2D) = "black"{}
  10.     }
  11.    
  12.     SubShader
  13.     {
  14.         Pass
  15.         {
  16.             ZWrite off
  17.  
  18.             CGPROGRAM
  19.  
  20.             sampler2D _MainTex;
  21.  
  22.             //<SamplerName>_TexelSize is a float2 that says how much screen space a texel occupies.
  23.             float2 _MainTex_TexelSize;
  24.  
  25.             #pragma vertex vert
  26.             #pragma fragment frag
  27.             #include "UnityCG.cginc"
  28.  
  29.             struct v2f
  30.             {
  31.                 float4 pos : SV_POSITION;
  32.                 float2 uvs : TEXCOORD0;
  33.             };
  34.  
  35.             v2f vert(appdata_base v)
  36.             {
  37.                 v2f o;
  38.  
  39.                 //Despite the fact that we are only drawing a quad to the screen, Unity requires us to multiply vertices by our MVP matrix, presumably to keep things working when inexperienced people try copying code from other shaders.
  40.                 o.pos = UnityObjectToClipPos(v.vertex);
  41.  
  42.                 //Also, we need to fix the UVs to match our screen space coordinates. There is a Unity define for this that should normally be used.
  43.                 o.uvs = o.pos.xy / 2 + 0.5;
  44.  
  45.                 return o;
  46.             }
  47.  
  48.  
  49.             half frag(v2f i) : COLOR
  50.             {
  51.                 //arbitrary number of iterations for now
  52.                 int NumberOfIterations = 4;
  53.  
  54.                 //split texel size into smaller words
  55.                 float TX_x = _MainTex_TexelSize.x;
  56.  
  57.                 //and a final intensity that increments based on surrounding intensities.
  58.                 float ColorIntensityInRadius;
  59.  
  60.                 //for every iteration we need to do horizontally
  61.                 for (int k = 0; k < NumberOfIterations; k += 1)
  62.                 {
  63.                     //increase our output color by the pixels in the area
  64.                     ColorIntensityInRadius += tex2D(
  65.                                                     _MainTex,
  66.                                                     i.uvs.xy + float2
  67.                                                                 (
  68.                                                                     (k - NumberOfIterations / 2)*TX_x,
  69.                                                                     0
  70.                                                                 )
  71.                                                     ).r / NumberOfIterations;
  72.                 }
  73.                 //output some intensity of teal
  74.                 return ColorIntensityInRadius;
  75.             }
  76.         ENDCG
  77.  
  78.         }
  79.         //end pass    
  80.  
  81.         GrabPass{}
  82.  
  83.         Pass
  84.         {
  85.             CGPROGRAM
  86.  
  87.             sampler2D _MainTex;
  88.             sampler2D _SceneTex;
  89.  
  90.             //we need to declare a sampler2D by the name of "_GrabTexture" that Unity can write to during GrabPass{}
  91.             sampler2D _GrabTexture;
  92.  
  93.             //<SamplerName>_TexelSize is a float2 that says how much screen space a texel occupies.
  94.             float2 _GrabTexture_TexelSize;
  95.  
  96.             #pragma vertex vert
  97.             #pragma fragment frag
  98.             #include "UnityCG.cginc"
  99.  
  100.             struct v2f
  101.             {
  102.                 float4 pos : SV_POSITION;
  103.                 float2 uvs : TEXCOORD0;
  104.             };
  105.  
  106.             v2f vert(appdata_base v)
  107.             {
  108.                 v2f o;
  109.  
  110.                 //Despite the fact that we are only drawing a quad to the screen, Unity requires us to multiply vertices by our MVP matrix, presumably to keep things working when inexperienced people try copying code from other shaders.
  111.                 o.pos = UnityObjectToClipPos(v.vertex);
  112.  
  113.                 //Also, we need to fix the UVs to match our screen space coordinates. There is a Unity define for this that should normally be used.
  114.                 o.uvs = o.pos.xy / 2 + 0.5;
  115.  
  116.                 return o;
  117.             }
  118.  
  119.  
  120.             half4 frag(v2f i) : COLOR
  121.             {
  122.                 //arbitrary number of iterations for now
  123.                 int NumberOfIterations = 20;
  124.  
  125.                 //split texel size into smaller words
  126.                 float TX_y = _GrabTexture_TexelSize.y;
  127.  
  128.                 //and a final intensity that increments based on surrounding intensities.
  129.                 half ColorIntensityInRadius = 0;
  130.  
  131.                 //if something already exists underneath the fragment (in the original texture), discard the fragment.
  132.                 if (tex2D(_MainTex,i.uvs.xy).r > 0)
  133.                 {
  134.                     return tex2D(_SceneTex,float2(i.uvs.x,1 - i.uvs.y));
  135.                 }
  136.  
  137.                 //for every iteration we need to do vertically
  138.                 for (int j = 0; j < NumberOfIterations; j += 1)
  139.                 {
  140.                     //increase our output color by the pixels in the area
  141.                     ColorIntensityInRadius += tex2D(
  142.                                                     _GrabTexture,
  143.                                                     float2(i.uvs.x,1 - i.uvs.y) + float2
  144.                                                                                     (
  145.                                                                                         0,
  146.                                                                                         (j - NumberOfIterations / 2)*TX_y
  147.                                                                                     )
  148.                                                     ).r / NumberOfIterations;
  149.                 }
  150.  
  151.  
  152.                 //this is alpha blending, but we can't use HW blending unless we make a third pass, so this is probably cheaper.
  153.                 //half4 outcolor = ColorIntensityInRadius * half4(0,1,1,1) * 2 + (1 - ColorIntensityInRadius)*tex2D(_SceneTex,float2(i.uvs.x,1 - i.uvs.y));
  154.                 half4 outcolor = half4(0, 0, 0, 0) * 2 + (1 - ColorIntensityInRadius) *tex2D(_SceneTex, float2(i.uvs.x, 1 - i.uvs.y));
  155.  
  156.                 return outcolor;
  157.             }
  158.         ENDCG
  159.         }
  160.         //end pass    
  161.     }
  162.     //end subshader
  163. }
  164. //end shader
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment