Advertisement
Guest User

SmoothInkyOutlineHLSL

a guest
Sep 10th, 2021
1,685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Toon/SmoothInky Outline" {
  2.     Properties{
  3.         _OutlineColor("Outline Color", Color) = (0,0,0,1)
  4.         _Outline("Outline width", Range(.002, 0.2)) = .005
  5.         _OutlineZ("Outline Z", Range(-.016, 0)) = -.001// outline z offset
  6.         _Offset("Outline Noise Offset", Range(0.5, 10)) = .005// noise offset
  7.         _NoiseTex("Noise (RGB)", 2D) = "white" { }// noise texture
  8.         _Lerp("Lerp between normals and vertex based", Range(0, 1)) = 0// outline z offset
  9.         [Toggle(NOISE)] _NOISE("Enable Noise?", Float) = 0
  10.     }
  11.    
  12.     HLSLINCLUDE
  13.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  14.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  15.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  16.     #pragma shader_feature NOISE
  17.     struct appdata {
  18.         float4 vertex : POSITION;
  19.         float3 normal : NORMAL;
  20.         float4 texcoord : TEXCOORD0;// texture coordinates
  21.     };
  22.    
  23.     struct v2f {
  24.         float4 pos : SV_POSITION;
  25.         float4 color : COLOR;
  26.     };
  27.    
  28.     uniform float _Outline;
  29.     uniform float _OutlineZ;// outline z offset
  30.     uniform float4 _OutlineColor;
  31.     sampler2D _NoiseTex;// noise texture
  32.     float _Offset,_Lerp; // noise offset
  33.    
  34.     v2f vert(appdata v) {
  35.         v2f o;
  36.  
  37.         // clipspace
  38.         o.pos = TransformObjectToHClip(v.vertex.xyz);
  39.  
  40.         // scale of object
  41.         float3 scale = float3(
  42.         length(unity_ObjectToWorld._m00_m10_m20),
  43.         length(unity_ObjectToWorld._m01_m11_m21),
  44.         length(unity_ObjectToWorld._m02_m12_m22)
  45.         );
  46.        
  47.         // rotate normals to eye space
  48.         float3 norm = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, v.normal)) * scale;
  49.         // attempt to do this to vertex for hard normals
  50.         float3 vert = normalize(mul((float4x4)UNITY_MATRIX_IT_MV, v.vertex)) * scale;
  51.         //
  52.         float2 offset = mul((float2x2)UNITY_MATRIX_P, float2(lerp(norm.x, vert.x, _Lerp), lerp(norm.y, vert.y, _Lerp)));
  53.         // texture for noise
  54.         float4 tex = tex2Dlod(_NoiseTex, float4(v.texcoord.xy, 0, 0) * _Offset);// noise texture based on texture coordinates and offset
  55.        
  56.         #if NOISE // switch for noise
  57.             o.pos.xy += offset * _Outline * tex.r;// add noise
  58.         #else
  59.             o.pos.xy += offset * _Outline;// or not
  60.         #endif
  61.         o.pos.z += _OutlineZ;// push away from camera
  62.        
  63.         o.color = _OutlineColor;
  64.         return o;
  65.     }
  66.     ENDHLSL
  67.    
  68.     SubShader{
  69.        
  70.         Pass{
  71.             Name "OUTLINE"
  72.             Tags { "LightMode"="UniversalForward" }
  73.             Cull Off// we dont want to cull
  74.            
  75.             HLSLPROGRAM
  76.             #pragma vertex vert
  77.             #pragma fragment frag
  78.             float4 frag(v2f i) : SV_Target
  79.             {
  80.                 return i.color;
  81.             }
  82.             ENDHLSL
  83.         }
  84.  
  85.        
  86.     }
  87.    
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement