yum-archive/2ner

A toon shader for Unity's BIRP.

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

yumadd ltcgi, alpha multiplierf478606

master
3.1 KiB93 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, bool lightmap = false)
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    if (lightmap) {
49        sample0 = float4(DecodeLightmap(sample0), 1.0);
50        sample1 = float4(DecodeLightmap(sample1), 1.0);
51        sample2 = float4(DecodeLightmap(sample2), 1.0);
52        sample3 = float4(DecodeLightmap(sample3), 1.0);
53    }
54
55    float sx = s.x / (s.x + s.y);
56    float sy = s.z / (s.z + s.w);
57
58    return lerp(
59        lerp(sample3, sample2, sx),
60        lerp(sample1, sample0, sx), sy);
61}
62
63float4 LTCGI_SampleShadowmap(float2 lmuv)
64{
65    #ifdef LTCGI_ALWAYS_LTC_DIFFUSE
66        return 1;
67    #else
68        lmuv = lmuv * _Udon_LTCGI_LightmapST.xy + _Udon_LTCGI_LightmapST.zw;
69
70        #ifdef LTCGI_BICUBIC_LIGHTMAP
71            float width, height;
72            _Udon_LTCGI_Lightmap.GetDimensions(width, height);
73
74            float4 _Udon_LTCGI_Lightmap_TexelSize = float4(width, height, 1.0/width, 1.0/height);
75
76            return LTCGI_SampleTexture2DBicubicFilter(
77                _Udon_LTCGI_Lightmap, LTCGI_SAMPLER,
78                lmuv, _Udon_LTCGI_Lightmap_TexelSize,
79                true
80            );
81        #else
82            fixed4 sample = _Udon_LTCGI_Lightmap.Sample(LTCGI_SAMPLER, lmuv);
83            return float4(DecodeLightmap(sample), 1.0);
84        #endif
85    #endif
86}
87
88#else
89// surface shader analysis stub
90float4 LTCGI_SampleShadowmap(float2 lmuv) { return 1; }
91#endif
92
93#endif