Guest User

Highlight Shader.shader

a guest
Mar 13th, 2020
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Hidden/Harryh___h/Highlight"
  2. {
  3.     SubShader
  4.     {
  5.         Cull Off ZWrite Off ZTest Always
  6.  
  7.         Pass
  8.         {
  9.             HLSLPROGRAM
  10.             #pragma vertex VertDefault
  11.             #pragma fragment Frag
  12.  
  13.             #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
  14.  
  15.             TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
  16.             TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
  17.             TEXTURE2D_SAMPLER2D(_CameraGBufferTexture2, sampler_CameraGBufferTexture2);
  18.            
  19.        
  20.             float4 _MainTex_TexelSize;
  21.             float3 _WorldSpaceLightPos0;
  22.             float _Scale;
  23.             float _Shine;
  24.             float _Shadow;
  25.             int _Rotations;
  26.             float _DepthThreshold;
  27.  
  28.             float4 overlay(float4 a, float4 b)
  29.             {
  30.                 // Just got this from wikipedia, no idea if its good or not
  31.                 //  https://en.wikipedia.org/wiki/Blend_modes
  32.  
  33.                 return a < float4(0.5, 0.5, 0.5, 0.5) ? 2 * a * b : 1 - 2 * (1 - a) * (1 - b);
  34.             }
  35.  
  36.             float4 Frag(VaryingsDefault i) : SV_Target
  37.             {  
  38.                 // Getting color, depth, normals, and light direction
  39.                 float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord).rgba;
  40.                 float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoord).r;
  41.                 float3 normal = SAMPLE_TEXTURE2D(_CameraGBufferTexture2, sampler_CameraGBufferTexture2, i.texcoord).rgb * 2 - 1;
  42.                
  43.                 float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
  44.  
  45.                 // Determining lit areas
  46.                 float lit = dot(lightDirection, normal);
  47.                 float steppedLit = lit > 0 ? 1 : 0;
  48.                
  49.                 // Finding edges
  50.                 int number = _Rotations < 0 ? 0 : _Rotations > 300 ? 300 : _Rotations; // just so I don't break my PC
  51.                 float depthSum = 0;
  52.  
  53.                 for (int index = 0; index < number; index++) {
  54.                     float rot = index * 2 * PI / number;
  55.  
  56.                     float depthScale = _Scale * depth;
  57.                     depthScale = max(depthScale, _Scale * 0.0075);
  58.  
  59.                     float2 newUV = i.texcoord + depthScale * float2(_MainTex_TexelSize.x * cos(rot), _MainTex_TexelSize.y * sin(rot));
  60.                
  61.                     float newDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, newUV).r;
  62.  
  63.                     float depthDifference = newDepth - depth;
  64.                     depthSum += depth > newDepth ? dot(depthDifference, depthDifference) : 0;
  65.  
  66.                     // ^ special thing about this is the conditional: depth > newDepth
  67.                     // this makes sure that the outline only goes inwards and doesn't
  68.                     // objects behind it like most outline effects.
  69.  
  70.                     // Like so: https://i.imgur.com/eMXYnaH.gifv
  71.  
  72.                 }
  73.  
  74.                 float edges = sqrt(depthSum) > _DepthThreshold * depth ? 1 : 0;
  75.                
  76.                 // Seperating edges to those in light or in dark
  77.                 float edgeLit = edges * steppedLit;
  78.                 float edgeDark = edges * (1 - steppedLit);
  79.                
  80.                 float edgeLitRemapped = edgeLit * _Shine;
  81.                 float edgeDarkRemapped = edgeDark * -_Shadow;
  82.  
  83.                 float allEdges = edgeLitRemapped + edgeDarkRemapped;
  84.                
  85.                 // Overlaying the edges onto color
  86.                 return overlay(color, allEdges + 0.5);
  87.                
  88.             }
  89.             ENDHLSL
  90.         }
  91.     }
  92. }
Add Comment
Please, Sign In to add comment