Advertisement
Guest User

Sketchy.shader

a guest
Apr 19th, 2019
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Hidden/Sketchy"
  2. {
  3.     Properties
  4.     {
  5.         [HideInInspector]_MainTex("Texture", 2D) = "white" {}
  6.         _Sketch("SketchTexture", 2D) = "white" {}
  7.         _ThresholdLine("Outline Threshold", float) = 0.01
  8.         _EdgeColor("Outline Color", Color) = (0,0,0,1)
  9.         _Smooth("Outline Smoothness", float) = 0.01
  10.            
  11.         _Offset("Blue Cutoff 1", Range(0,1)) = 0.5
  12.         _Offset2("Blue Cutoff 2", Range(0,1)) = 0.5
  13.         _Offset3("Red Cutoff Extra", Range(0,1)) = 0.5
  14.         _Intensity("Red Intensity Extra", Range(1,4)) = 0.5
  15.         [Toggle(COL)] _COL("Original Colors?", Float) = 0
  16.     }
  17.         SubShader
  18.         {
  19.             // No culling or depth
  20.             Cull Off ZWrite Off ZTest Always
  21.  
  22.             // 0
  23.             Pass
  24.             {
  25.                 ZTest Always Cull Off ZWrite Off
  26.                Stencil{
  27.                 Ref 1
  28.                 ReadMask 1
  29.                 Comp Equal
  30.                  Pass Keep
  31.                 }
  32.  
  33.  
  34.                 CGPROGRAM
  35.                 #pragma vertex vert
  36.                 #pragma fragment frag
  37.                 #pragma shader_feature COL
  38.                 #include "UnityCG.cginc"
  39.  
  40.                 struct appdata
  41.                 {
  42.                     float4 vertex : POSITION;
  43.                     float2 uv : TEXCOORD0;
  44.                 };
  45.  
  46.                 struct v2f
  47.                 {
  48.                     float2 uv : TEXCOORD0;
  49.                     float4 vertex : SV_POSITION;
  50.                     float4 screenPos :TEXCOORD1;
  51.                 };
  52.  
  53.                 sampler2D _CameraDepthNormalsTexture;
  54.                 v2f vert(appdata v)
  55.                 {
  56.                     v2f o;
  57.                     o.vertex = UnityObjectToClipPos(v.vertex);
  58.                     o.uv = v.uv;
  59.                     o.screenPos = ComputeScreenPos(o.vertex);
  60.                     return o;
  61.                 }
  62.  
  63.                 sampler2D _MainTex;
  64.                 float4 _MainTex_TexelSize;
  65.                 float _ThresholdLine;
  66.                 fixed4 _EdgeColor;
  67.                 float _Smooth, _Intensity;
  68.  
  69.                 sampler2D _Sketch;
  70.                 float _Offset, _Offset2, _Offset3;
  71.  
  72.                 float4 GetPixelValue(in float2 uv) {
  73.                     half3 normal;
  74.                     float depth;
  75.                     DecodeDepthNormal(tex2D(_CameraDepthNormalsTexture, uv), depth, normal);
  76.                     return fixed4(normal, depth);
  77.                 }
  78.  
  79.                 fixed4 frag(v2f i) : SV_Target
  80.                 {
  81.                     fixed4 col = tex2D(_MainTex, i.uv);
  82.    
  83.                     float2 screenUV = i.screenPos.xy / i.screenPos.w;
  84.                     screenUV *= float2(8, 6);
  85.                     fixed4 sketchFX = tex2D(_Sketch, screenUV * 14);
  86.  
  87.                     fixed4 orValue = GetPixelValue(i.uv);
  88.                     float2 offsets[8] = {
  89.                         float2(-1, -1),
  90.                         float2(-1, 0),
  91.                         float2(-1, 1),
  92.                         float2(0, -1),
  93.                         float2(0, 1),
  94.                         float2(1, -1),
  95.                         float2(1, 0),
  96.                         float2(1, 1)
  97.                     };
  98.                     fixed4 sampledValue = fixed4(0,0,0,0);
  99.                     for (int j = 0; j < 8; j++) {
  100.                         sampledValue += GetPixelValue(i.uv + offsets[j] * _MainTex_TexelSize.xy);
  101.                     }
  102.                     sampledValue /= 8;
  103.  
  104.                     float4 dither = (smoothstep(_Offset, _Offset + 0.05, col.b) + smoothstep(_Offset2, _Offset2+0.05, col.b))* sketchFX.r;
  105.                     dither += (smoothstep(_Offset3, _Offset3 + 0.05, col.b) * _Intensity);
  106. #if COL
  107.                     dither *= col;
  108. #endif         
  109.                     return lerp(dither, _EdgeColor, smoothstep(_ThresholdLine, _ThresholdLine + _Smooth, length(orValue - sampledValue)));
  110.                 }
  111.                 ENDCG
  112.             }
  113.         }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement