yum-archive/2ner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum-archive/2ner
478cb4e
master
1#ifndef SERVICE_FILTERING_INCLUDED 2#define SERVICE_FILTERING_INCLUDED 3 4#include "SharedSamplingLib.hlsl" 5 6float4 cubic ( float v ) 7{ 8float4 n = float4 ( 1.0 , 2.0 , 3.0 , 4.0 ) - v ; 9float4 s = n * n * n ; 10float x = s . x ; 11float y = s . y - 4.0 * s . x ; 12float z = s . z - 4.0 * s . y + 6.0 * s . x ; 13float w = 6.0 - x - y - z ; 14return float4 ( x , y , z , w ); 15} 16 17 18// Unity's SampleTexture2DBicubic doesn't exist in 2018, which is our target here. 19// So this is a similar function with tweaks to have similar semantics. 20 21float4 SampleTexture2DBicubicFilter ( TEXTURE2D_PARAM ( tex , smp ), float2 coord , float4 texSize ) 22{ 23coord = coord * texSize . xy - 0.5 ; 24float fx = frac ( coord . x ); 25float fy = frac ( coord . y ); 26coord . x -= fx ; 27coord . y -= fy ; 28 29float4 xcubic = cubic ( fx ); 30float4 ycubic = cubic ( fy ); 31 32float4 c = float4 ( coord . x - 0.5 , coord . x + 1.5 , coord . y - 0.5 , coord . y + 1.5 ); 33float4 s = float4 ( xcubic . x + xcubic . y , xcubic . z + xcubic . w , ycubic . x + ycubic . y , ycubic . z + ycubic . w ); 34float4 offset = c + float4 ( xcubic . y , xcubic . w , ycubic . y , ycubic . w ) / s ; 35 36float4 sample0 = SAMPLE_TEXTURE2D ( tex , smp , float2 ( offset . x , offset . z ) * texSize . zw ); 37float4 sample1 = SAMPLE_TEXTURE2D ( tex , smp , float2 ( offset . y , offset . z ) * texSize . zw ); 38float4 sample2 = SAMPLE_TEXTURE2D ( tex , smp , float2 ( offset . x , offset . w ) * texSize . zw ); 39float4 sample3 = SAMPLE_TEXTURE2D ( tex , smp , float2 ( offset . y , offset . w ) * texSize . zw ); 40 41float sx = s . x / ( s . x + s . y ); 42float sy = s . z / ( s . z + s . w ); 43 44return lerp ( 45lerp ( sample3 , sample2 , sx ), 46lerp ( sample1 , sample0 , sx ), sy ); 47} 48 49#endif // SERVICE_FILTERING_INCLUDED