Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "Hidden/Harryh___h/Highlight"
- {
- SubShader
- {
- Cull Off ZWrite Off ZTest Always
- Pass
- {
- HLSLPROGRAM
- #pragma vertex VertDefault
- #pragma fragment Frag
- #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
- TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
- TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
- TEXTURE2D_SAMPLER2D(_CameraGBufferTexture2, sampler_CameraGBufferTexture2);
- float4 _MainTex_TexelSize;
- float3 _WorldSpaceLightPos0;
- float _Scale;
- float _Shine;
- float _Shadow;
- int _Rotations;
- float _DepthThreshold;
- float4 overlay(float4 a, float4 b)
- {
- // Just got this from wikipedia, no idea if its good or not
- // https://en.wikipedia.org/wiki/Blend_modes
- return a < float4(0.5, 0.5, 0.5, 0.5) ? 2 * a * b : 1 - 2 * (1 - a) * (1 - b);
- }
- float4 Frag(VaryingsDefault i) : SV_Target
- {
- // Getting color, depth, normals, and light direction
- float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord).rgba;
- float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoord).r;
- float3 normal = SAMPLE_TEXTURE2D(_CameraGBufferTexture2, sampler_CameraGBufferTexture2, i.texcoord).rgb * 2 - 1;
- float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
- // Determining lit areas
- float lit = dot(lightDirection, normal);
- float steppedLit = lit > 0 ? 1 : 0;
- // Finding edges
- int number = _Rotations < 0 ? 0 : _Rotations > 300 ? 300 : _Rotations; // just so I don't break my PC
- float depthSum = 0;
- for (int index = 0; index < number; index++) {
- float rot = index * 2 * PI / number;
- float depthScale = _Scale * depth;
- depthScale = max(depthScale, _Scale * 0.0075);
- float2 newUV = i.texcoord + depthScale * float2(_MainTex_TexelSize.x * cos(rot), _MainTex_TexelSize.y * sin(rot));
- float newDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, newUV).r;
- float depthDifference = newDepth - depth;
- depthSum += depth > newDepth ? dot(depthDifference, depthDifference) : 0;
- // ^ special thing about this is the conditional: depth > newDepth
- // this makes sure that the outline only goes inwards and doesn't
- // objects behind it like most outline effects.
- // Like so: https://i.imgur.com/eMXYnaH.gifv
- }
- float edges = sqrt(depthSum) > _DepthThreshold * depth ? 1 : 0;
- // Seperating edges to those in light or in dark
- float edgeLit = edges * steppedLit;
- float edgeDark = edges * (1 - steppedLit);
- float edgeLitRemapped = edgeLit * _Shine;
- float edgeDarkRemapped = edgeDark * -_Shadow;
- float allEdges = edgeLitRemapped + edgeDarkRemapped;
- // Overlaying the edges onto color
- return overlay(color, allEdges + 0.5);
- }
- ENDHLSL
- }
- }
- }
Add Comment
Please, Sign In to add comment