Advertisement
tonynogo

Demo 99 - Pencil (Daniel Taylor version)

Jan 29th, 2018
11,353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/PostRendering/Pencil (Daniel Taylor version)"
  2. {
  3. // Original code : https://www.shadertoy.com/view/ldXfRj
  4.     Properties
  5.     {
  6.         _MainTex ("Texture", 2D) = "white" {}
  7.         _GradThresh ("Gradiant threshold", range(0.000001, 0.01)) = 0.01
  8.         _ColorThreshold ("Color Threshold", range(0.0, 1)) = 0.5
  9.     }
  10.     SubShader
  11.     {
  12.         Tags { "RenderType"="Opaque" }
  13.        
  14.         Pass
  15.         {
  16.             CGPROGRAM
  17.             #pragma vertex vert
  18.             #pragma fragment frag
  19.             #pragma target 4.0
  20.             #include "UnityCG.cginc"
  21.  
  22.             sampler2D _MainTex;
  23.  
  24.             float _GradThresh;
  25.             float _ColorThreshold;
  26.             float _Intensity;
  27.  
  28.             struct v2f {
  29.                 float4 pos : SV_POSITION;
  30.                 float4 screenuv : TEXCOORD0;
  31.             };
  32.  
  33.             v2f vert(appdata_base v)
  34.             {
  35.                 v2f o;
  36.                 o.pos = UnityObjectToClipPos(v.vertex);
  37.                 o.screenuv = ComputeScreenPos(o.pos);
  38.                 return o;
  39.             }
  40.  
  41.             #define PI2 6.28318530717959
  42.             #define STEP 2.0
  43.             #define RANGE 16.0
  44.             #define ANGLENUM 4.0
  45.             #define GRADTHRESH 0.01
  46.             #define SENSITIVITY 10.0
  47.  
  48.             float4 getCol(float2 pos)
  49.             {
  50.                 return tex2D(_MainTex, pos / _ScreenParams.xy);
  51.             }
  52.  
  53.             float getVal(float2 pos)
  54.             {
  55.                 float4 c = getCol(pos);
  56.                 return dot(c.xyz, float3(0.2126, 0.7152, 0.0722));
  57.             }
  58.  
  59.             float2 getGrad(float2 pos, float delta)
  60.             {
  61.                 float2 d = float2(delta, 0.0);
  62.                 return float2(getVal(pos + d.xy) - getVal(pos - d.xy),
  63.                               getVal(pos + d.yx) - getVal(pos - d.yx)) / delta / 2.0;
  64.             }
  65.  
  66.             void pR(inout float2 p, float a) {
  67.                 p =  cos(a) * p + sin(a) * float2(p.y, -p.x);
  68.             }
  69.  
  70.             fixed4 frag(v2f i) : SV_Target
  71.             {
  72.                 float2 screenuv = i.screenuv.xy / i.screenuv.w;
  73.                 float2 screenPos = float2(i.screenuv.x * _ScreenParams.x, i.screenuv.y * _ScreenParams.y);
  74.                 float weight = 1.0;
  75.  
  76.                 for(int j = 0; j < ANGLENUM; j++)
  77.                 {
  78.                     float2 dir = float2(1.0, 0.0) ;
  79.                     pR(dir, j * PI2 / (2.0 * ANGLENUM));
  80.        
  81.                     float2 grad = float2(-dir.y, dir.x);
  82.        
  83.                     for(int i = -RANGE; i <= RANGE; i += STEP)
  84.                     {
  85.                         float2 b = normalize(dir);
  86.                         float2 pos2 = screenPos + float2(b.x, b.y) * i;
  87.            
  88.                         if (pos2.y < 0.0 || pos2.x < 0.0 || pos2.x > _ScreenParams.x || pos2.y > _ScreenParams.y)
  89.                             continue;
  90.            
  91.                         float2 g = getGrad(pos2, 1.0);
  92.  
  93.                         if (sqrt(dot(g,g)) < _GradThresh)
  94.                             continue;
  95.            
  96.                         weight -= pow(abs(dot(normalize(grad), normalize(g))), SENSITIVITY) / floor((2.0 * RANGE + 1.0) / STEP) / ANGLENUM;
  97.                     }
  98.                 }
  99.    
  100.                 float4 col = getCol(screenPos);
  101.                 float4 background = lerp(col, float4(1.0, 1.0, 1.0, 1.0), _ColorThreshold);
  102.  
  103.                 return lerp(float4(0.0, 0.0, 0.0, 0.0), background, weight);
  104.             }
  105.  
  106.             ENDCG
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement