summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/Editor/x64/Bakery/shaderSrc/ftrace.cginc
blob: 15e26290a7ce74d724919f8ff5cb9a256f94242a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifdef LIGHTMAP_RGBM_SCALE

#ifndef FTRACE_INCLUDED
#define FTRACE_INCLUDED

// Bicubic interpolation

float ftBicubic_w0(float a)
{
    return (1.0f/6.0f)*(a*(a*(-a + 3.0f) - 3.0f) + 1.0f);
}

float ftBicubic_w1(float a)
{
    return (1.0f/6.0f)*(a*a*(3.0f*a - 6.0f) + 4.0f);
}

float ftBicubic_w2(float a)
{
    return (1.0f/6.0f)*(a*(a*(-3.0f*a + 3.0f) + 3.0f) + 1.0f);
}

float ftBicubic_w3(float a)
{
    return (1.0f/6.0f)*(a*a*a);
}

float ftBicubic_g0(float a)
{
    return ftBicubic_w0(a) + ftBicubic_w1(a);
}

float ftBicubic_g1(float a)
{
    return ftBicubic_w2(a) + ftBicubic_w3(a);
}

float ftBicubic_h0(float a)
{
    return -1.0f + ftBicubic_w1(a) / (ftBicubic_w0(a) + ftBicubic_w1(a)) + 0.5f;
}

float ftBicubic_h1(float a)
{
    return 1.0f + ftBicubic_w3(a) / (ftBicubic_w2(a) + ftBicubic_w3(a)) + 0.5f;
}

#ifdef SHADER_API_D3D11
#if defined (SHADOWS_SHADOWMASK)

float4 ftBicubicSampleShadow3( Texture2D tex, SamplerState ftShadowSampler, float2 uv )
{
    float width, height;
    tex.GetDimensions(width, height);

    float x = uv.x * width;
    float y = uv.y * height;

    x -= 0.5f;
    y -= 0.5f;

    float px = floor(x);
    float py = floor(y);

    float fx = x - px;
    float fy = y - py;

    float g0x = ftBicubic_g0(fx);
    float g1x = ftBicubic_g1(fx);
    float h0x = ftBicubic_h0(fx);
    float h1x = ftBicubic_h1(fx);
    float h0y = ftBicubic_h0(fy);
    float h1y = ftBicubic_h1(fy);

    float4 r = ftBicubic_g0(fy) * ( g0x * tex.Sample(ftShadowSampler, (float2(px + h0x, py + h0y) * 1.0f/width))   +
                          g1x * tex.Sample(ftShadowSampler, (float2(px + h1x, py + h0y) * 1.0f/width))) +

               ftBicubic_g1(fy) * ( g0x * tex.Sample(ftShadowSampler, (float2(px + h0x, py + h1y) * 1.0f/width))   +
                          g1x * tex.Sample(ftShadowSampler, (float2(px + h1x, py + h1y) * 1.0f/width)));
    return r;
}

float4 ftBicubicSampleShadow( Texture2D tex, float2 uv )
{
    #if defined(LIGHTMAP_ON)
    SamplerState samplerMask = samplerunity_Lightmap;
    #else
    SamplerState samplerMask = samplerunity_ShadowMask;
    #endif

    return ftBicubicSampleShadow3(tex, samplerMask, uv);
}

#define ftBicubicSampleShadow2(t,s,u) ftBicubicSampleShadow3(t, sampler##s, u)

#else
#define ftBicubicSampleShadow UNITY_SAMPLE_TEX2D
#define ftBicubicSampleShadow2 UNITY_SAMPLE_TEX2D_SAMPLER
#endif
#else
#define ftBicubicSampleShadow UNITY_SAMPLE_TEX2D
#define ftBicubicSampleShadow2 UNITY_SAMPLE_TEX2D_SAMPLER
#endif

float3 ftLightmapBicubic( float2 uv )
{
    #ifdef SHADER_API_D3D11
        float width, height;
        unity_Lightmap.GetDimensions(width, height);

        float x = uv.x * width;
        float y = uv.y * height;

        x -= 0.5f;
        y -= 0.5f;

        float px = floor(x);
        float py = floor(y);

        float fx = x - px;
        float fy = y - py;

        // note: we could store these functions in a lookup table texture, but maths is cheap
        float g0x = ftBicubic_g0(fx);
        float g1x = ftBicubic_g1(fx);
        float h0x = ftBicubic_h0(fx);
        float h1x = ftBicubic_h1(fx);
        float h0y = ftBicubic_h0(fy);
        float h1y = ftBicubic_h1(fy);

        float4 r = ftBicubic_g0(fy) * ( g0x * UNITY_SAMPLE_TEX2D(unity_Lightmap, (float2(px + h0x, py + h0y) * 1.0f/width))   +
                              g1x * UNITY_SAMPLE_TEX2D(unity_Lightmap, (float2(px + h1x, py + h0y) * 1.0f/width))) +

                   ftBicubic_g1(fy) * ( g0x * UNITY_SAMPLE_TEX2D(unity_Lightmap, (float2(px + h0x, py + h1y) * 1.0f/width))   +
                              g1x * UNITY_SAMPLE_TEX2D(unity_Lightmap, (float2(px + h1x, py + h1y) * 1.0f/width)));
        return DecodeLightmap(r);
    #else
        return DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, uv));
    #endif
}


// Light falloff

float ftLightFalloff(float4x4 ftUnityLightMatrix, float3 worldPos)
{
    float3 lightCoord = mul(ftUnityLightMatrix, float4(worldPos, 1)).xyz / ftUnityLightMatrix._11;
    float distSq = dot(lightCoord, lightCoord);
    float falloff = saturate(1.0f - pow(sqrt(distSq) * ftUnityLightMatrix._11, 4.0f)) / (distSq + 1.0f);
    return falloff;
}

float ftLightFalloff(float4 lightPosRadius, float3 worldPos)
{
    float3 lightCoord = worldPos - lightPosRadius.xyz;
    float distSq = dot(lightCoord, lightCoord);
    float falloff = saturate(1.0f - pow(sqrt(distSq * lightPosRadius.w), 4.0f)) / (distSq + 1.0f);
    return falloff;
}

#endif

#endif