yum-archive/2ner

A toon shader for Unity's BIRP.

git clone https://git.yummers.dev/yum-archive/2ner

yumTouch up shadows478cb4e

master
1.6 KiB49 linesraw
1#ifndef SERVICE_FILTERING_INCLUDED
2#define SERVICE_FILTERING_INCLUDED
3
4#include "SharedSamplingLib.hlsl"
5
6float4 cubic(float v)
7{
8    float4 n = float4(1.0, 2.0, 3.0, 4.0) - v;
9    float4 s = n * n * n;
10    float x = s.x;
11    float y = s.y - 4.0 * s.x;
12    float z = s.z - 4.0 * s.y + 6.0 * s.x;
13    float w = 6.0 - x - y - z;
14    return 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{
23    coord = coord * texSize.xy - 0.5;
24    float fx = frac(coord.x);
25    float fy = frac(coord.y);
26    coord.x -= fx;
27    coord.y -= fy;
28
29    float4 xcubic = cubic(fx);
30    float4 ycubic = cubic(fy);
31
32    float4 c = float4(coord.x - 0.5, coord.x + 1.5, coord.y - 0.5, coord.y + 1.5);
33    float4 s = float4(xcubic.x + xcubic.y, xcubic.z + xcubic.w, ycubic.x + ycubic.y, ycubic.z + ycubic.w);
34    float4 offset = c + float4(xcubic.y, xcubic.w, ycubic.y, ycubic.w) / s;
35
36    float4 sample0 = SAMPLE_TEXTURE2D(tex, smp, float2(offset.x, offset.z) * texSize.zw);
37    float4 sample1 = SAMPLE_TEXTURE2D(tex, smp, float2(offset.y, offset.z) * texSize.zw);
38    float4 sample2 = SAMPLE_TEXTURE2D(tex, smp, float2(offset.x, offset.w) * texSize.zw);
39    float4 sample3 = SAMPLE_TEXTURE2D(tex, smp, float2(offset.y, offset.w) * texSize.zw);
40
41    float sx = s.x / (s.x + s.y);
42    float sy = s.z / (s.z + s.w);
43
44    return lerp(
45        lerp(sample3, sample2, sx),
46        lerp(sample1, sample0, sx), sy);
47}
48
49#endif // SERVICE_FILTERING_INCLUDED