Guest User

DirectionalOutlineRendererFeature.shader

a guest
Sep 23rd, 2021
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Toon/Directional Rim Outline Renderer" {
  2.     Properties{    
  3.         _Outline("Outline width", Range(.002, 0.5)) = .005
  4.         _OutlineZ("Outline Z", Range(-.016, 0)) = -.001// outline z offset
  5.         _Brightness("Outline Brightness", Range(0.5, 10)) = .005// noise offset
  6.         _Lerp("Lerp between normals and vertex based", Range(0, 1)) = 0// outline z offset     
  7.     }
  8.    
  9.     HLSLINCLUDE
  10.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  11.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  12.     struct appdata {
  13.         float4 vertex : POSITION;
  14.         float4 texcoord : TEXCOORD0;// texture coordinates
  15.         float4 normal : NORMAL;
  16.     };
  17.    
  18.     struct v2f {
  19.         float4 pos : SV_POSITION;
  20.         float3 lightDir : TEXCOORD1;
  21.         float4 screenPos : TEXCOORD2;
  22.     };
  23.    
  24.     float _Lerp; // noise offset
  25.     float _Outline;
  26.     float _OutlineZ;
  27.    
  28.     v2f vert(appdata v) {
  29.         v2f o;
  30.  
  31.         // clipspace
  32.         o.pos = TransformObjectToHClip(v.vertex.xyz);
  33.  
  34.         // scale of object
  35.         float3 scale = float3(
  36.         length(unity_ObjectToWorld._m00_m10_m20),
  37.         length(unity_ObjectToWorld._m01_m11_m21),
  38.         length(unity_ObjectToWorld._m02_m12_m22)
  39.         );
  40.  
  41.         // Get the light direction
  42.         Light light = GetMainLight();
  43.         o.lightDir = normalize(light.direction);//
  44.        
  45.         // rotate normals to eye space
  46.         float3 norm = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, v.normal)) * scale;
  47.         // attempt to do this to vertex for hard normals
  48.         float3 vert = normalize(mul((float4x4)UNITY_MATRIX_IT_MV, v.vertex)) * scale;
  49.         // create an offset and add in the light direction
  50.         float2 offset = mul((float2x2)UNITY_MATRIX_P, float2(lerp(norm.x, vert.x, _Lerp), lerp(norm.y, vert.y, _Lerp)))+ (float2(o.lightDir.x, -o.lightDir.y) * 2);
  51.         // screenpos for grabpass
  52.         o.screenPos = ComputeScreenPos(o.pos); 
  53.         // move vertices with offset           
  54.         o.pos.xy += offset * _Outline; 
  55.         // push away from camera
  56.         o.pos.z += _OutlineZ;
  57.         return o;
  58.     }
  59.     ENDHLSL
  60.    
  61.     SubShader{
  62.        
  63.  
  64.  
  65.         Pass{
  66.             Name "OUTLINE"
  67.             Tags { "LightMode"="UniversalForward" }
  68.             Cull Off// we dont want to cull
  69.            
  70.             HLSLPROGRAM
  71.             #pragma vertex vert
  72.             #pragma fragment frag
  73.  
  74.             sampler2D _CameraOpaqueTexture;
  75.             float _Brightness;
  76.             float4 frag(v2f i) : SV_Target
  77.             {
  78.                 // grab the camera view to colorize the outline
  79.                 half4 col = tex2D(_CameraOpaqueTexture , i.screenPos.xy / i.screenPos.w) * _Brightness;
  80.                 return saturate(col);
  81.             }
  82.             ENDHLSL
  83.         }
  84.  
  85.        
  86.     }
  87.    
  88. }
Advertisement
Add Comment
Please, Sign In to add comment