yum-archive/Tooner

A toon shader for Unity's BIRP.

git clone https://git.yummers.dev/yum-archive/Tooner

yumAdd LTCGI6eed98f

v0.0.0
2.7 KiB84 linesraw
1#ifndef LTCGI_SHADOWMAP_INCLUDED
2#define LTCGI_SHADOWMAP_INCLUDED
3
4// Adapted from: https://gitlab.com/s-ilent/filamented
5// Licensed under the terms of the Apache License 2.0
6// Full text: https://gitlab.com/s-ilent/filamented/-/blob/master/LICENSE
7//
8// Conforming to the terms of the above license, this file is redistributed
9// under the terms of the MIT license as part of the LTCGI shader package,
10// provided this notice is kept.
11
12#ifndef SHADER_TARGET_SURFACE_ANALYSIS_MOJOSHADER
13
14float4 LTCGI_cubic(float v)
15{
16    float4 n = float4(1.0, 2.0, 3.0, 4.0) - v;
17    float4 s = n * n * n;
18    float x = s.x;
19    float y = s.y - 4.0 * s.x;
20    float z = s.z - 4.0 * s.y + 6.0 * s.x;
21    float w = 6.0 - x - y - z;
22    return float4(x, y, z, w);
23}
24
25// Unity's SampleTexture2DBicubic doesn't exist in 2018, which is our target here.
26// So this is a similar function with tweaks to have similar semantics. 
27
28float4 LTCGI_SampleTexture2DBicubicFilter(Texture2D tex, SamplerState smp, float2 coord, float4 texSize)
29{
30    coord = coord * texSize.xy - 0.5;
31    float fx = frac(coord.x);
32    float fy = frac(coord.y);
33    coord.x -= fx;
34    coord.y -= fy;
35
36    float4 xcubic = LTCGI_cubic(fx);
37    float4 ycubic = LTCGI_cubic(fy);
38
39    float4 c = float4(coord.x - 0.5, coord.x + 1.5, coord.y - 0.5, coord.y + 1.5);
40    float4 s = float4(xcubic.x + xcubic.y, xcubic.z + xcubic.w, ycubic.x + ycubic.y, ycubic.z + ycubic.w);
41    float4 offset = c + float4(xcubic.y, xcubic.w, ycubic.y, ycubic.w) / s;
42
43    float4 sample0 = tex.Sample(smp, float2(offset.x, offset.z) * texSize.zw);
44    float4 sample1 = tex.Sample(smp, float2(offset.y, offset.z) * texSize.zw);
45    float4 sample2 = tex.Sample(smp, float2(offset.x, offset.w) * texSize.zw);
46    float4 sample3 = tex.Sample(smp, float2(offset.y, offset.w) * texSize.zw);
47
48    float sx = s.x / (s.x + s.y);
49    float sy = s.z / (s.z + s.w);
50
51    return lerp(
52        lerp(sample3, sample2, sx),
53        lerp(sample1, sample0, sx), sy);
54}
55
56float4 LTCGI_SampleShadowmap(float2 lmuv)
57{
58    #ifdef LTCGI_ALWAYS_LTC_DIFFUSE
59        return 1;
60    #else
61        lmuv = lmuv * _Udon_LTCGI_LightmapST.xy + _Udon_LTCGI_LightmapST.zw;
62
63        #ifdef LTCGI_BICUBIC_LIGHTMAP
64            float width, height;
65            _Udon_LTCGI_Lightmap.GetDimensions(width, height);
66
67            float4 _Udon_LTCGI_Lightmap_TexelSize = float4(width, height, 1.0/width, 1.0/height);
68
69            return LTCGI_SampleTexture2DBicubicFilter(
70                _Udon_LTCGI_Lightmap, LTCGI_SAMPLER,
71                lmuv, _Udon_LTCGI_Lightmap_TexelSize
72            );
73        #else
74            return _Udon_LTCGI_Lightmap.Sample(LTCGI_SAMPLER, lmuv);
75        #endif
76    #endif
77}
78
79#else
80// surface shader analysis stub
81float4 LTCGI_SampleShadowmap(float2 lmuv) { return 1; }
82#endif
83
84#endif