2023-02-25 19:03:39 +00:00
|
|
|
#ifndef LIB_MATERIAL_ALPHA
|
|
|
|
#define LIB_MATERIAL_ALPHA
|
2020-12-16 23:44:15 +00:00
|
|
|
|
2020-12-18 01:36:20 +00:00
|
|
|
#define FUNC_NEVER 512 // 0x0200
|
|
|
|
#define FUNC_LESS 513 // 0x0201
|
|
|
|
#define FUNC_EQUAL 514 // 0x0202
|
|
|
|
#define FUNC_LEQUAL 515 // 0x0203
|
|
|
|
#define FUNC_GREATER 516 // 0x0204
|
|
|
|
#define FUNC_NOTEQUAL 517 // 0x0205
|
|
|
|
#define FUNC_GEQUAL 518 // 0x0206
|
|
|
|
#define FUNC_ALWAYS 519 // 0x0207
|
2020-12-16 23:44:15 +00:00
|
|
|
|
2021-01-05 22:21:54 +00:00
|
|
|
float mipmapLevel(vec2 scaleduv)
|
|
|
|
{
|
|
|
|
vec2 dUVdx = dFdx(scaleduv);
|
|
|
|
vec2 dUVdy = dFdy(scaleduv);
|
|
|
|
float maxDUVSquared = max(dot(dUVdx, dUVdx), dot(dUVdy, dUVdy));
|
|
|
|
return max(0.0, 0.5 * log2(maxDUVSquared));
|
|
|
|
}
|
|
|
|
|
|
|
|
float coveragePreservingAlphaScale(sampler2D diffuseMap, vec2 uv)
|
|
|
|
{
|
2021-07-04 22:29:22 +00:00
|
|
|
#if @adjustCoverage
|
2021-01-05 22:21:54 +00:00
|
|
|
vec2 textureSize;
|
2021-01-07 18:13:51 +00:00
|
|
|
#if @useGPUShader4
|
2021-01-05 22:21:54 +00:00
|
|
|
textureSize = textureSize2D(diffuseMap, 0);
|
|
|
|
#else
|
2021-01-07 18:13:51 +00:00
|
|
|
textureSize = vec2(256.0);
|
2021-01-05 22:21:54 +00:00
|
|
|
#endif
|
|
|
|
return 1.0 + mipmapLevel(uv * textureSize) * 0.25;
|
|
|
|
#else
|
|
|
|
return 1.0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-02-26 22:31:41 +00:00
|
|
|
float alphaTest(float alpha, float ref)
|
2020-12-16 23:44:15 +00:00
|
|
|
{
|
2020-12-26 22:45:53 +00:00
|
|
|
#if @alphaToCoverage
|
2023-02-26 22:31:41 +00:00
|
|
|
float coverageAlpha = (alpha - clamp(ref, 0.0001, 0.9999)) / max(fwidth(alpha), 0.0001) + 0.5;
|
2020-12-26 22:45:53 +00:00
|
|
|
|
|
|
|
// Some functions don't make sense with A2C or are a pain to think about and no meshes use them anyway
|
|
|
|
// Use regular alpha testing in such cases until someone complains.
|
|
|
|
#if @alphaFunc == FUNC_NEVER
|
2020-12-16 23:44:15 +00:00
|
|
|
discard;
|
2020-12-26 22:45:53 +00:00
|
|
|
#elif @alphaFunc == FUNC_LESS
|
2023-02-25 19:03:39 +00:00
|
|
|
return 1.0 - coverageAlpha;
|
2020-12-26 22:45:53 +00:00
|
|
|
#elif @alphaFunc == FUNC_EQUAL
|
2023-02-26 22:31:41 +00:00
|
|
|
if (alpha != ref)
|
2020-12-26 22:45:53 +00:00
|
|
|
discard;
|
|
|
|
#elif @alphaFunc == FUNC_LEQUAL
|
2023-02-25 19:03:39 +00:00
|
|
|
return 1.0 - coverageAlpha;
|
2020-12-26 22:45:53 +00:00
|
|
|
#elif @alphaFunc == FUNC_GREATER
|
2023-02-25 19:03:39 +00:00
|
|
|
return coverageAlpha;
|
2020-12-26 22:45:53 +00:00
|
|
|
#elif @alphaFunc == FUNC_NOTEQUAL
|
2023-02-26 22:31:41 +00:00
|
|
|
if (alpha == ref)
|
2020-12-26 22:45:53 +00:00
|
|
|
discard;
|
|
|
|
#elif @alphaFunc == FUNC_GEQUAL
|
2023-02-25 19:03:39 +00:00
|
|
|
return coverageAlpha;
|
2020-12-26 22:45:53 +00:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#if @alphaFunc == FUNC_NEVER
|
2020-12-16 23:44:15 +00:00
|
|
|
discard;
|
2020-12-26 22:45:53 +00:00
|
|
|
#elif @alphaFunc == FUNC_LESS
|
2023-02-26 22:31:41 +00:00
|
|
|
if (alpha >= ref)
|
2020-12-26 22:45:53 +00:00
|
|
|
discard;
|
|
|
|
#elif @alphaFunc == FUNC_EQUAL
|
2023-02-26 22:31:41 +00:00
|
|
|
if (alpha != ref)
|
2020-12-26 22:45:53 +00:00
|
|
|
discard;
|
|
|
|
#elif @alphaFunc == FUNC_LEQUAL
|
2023-02-26 22:31:41 +00:00
|
|
|
if (alpha > ref)
|
2020-12-26 22:45:53 +00:00
|
|
|
discard;
|
|
|
|
#elif @alphaFunc == FUNC_GREATER
|
2023-02-26 22:31:41 +00:00
|
|
|
if (alpha <= ref)
|
2020-12-26 22:45:53 +00:00
|
|
|
discard;
|
|
|
|
#elif @alphaFunc == FUNC_NOTEQUAL
|
2023-02-26 22:31:41 +00:00
|
|
|
if (alpha == ref)
|
2020-12-26 22:45:53 +00:00
|
|
|
discard;
|
|
|
|
#elif @alphaFunc == FUNC_GEQUAL
|
2023-02-26 22:31:41 +00:00
|
|
|
if (alpha < ref)
|
2020-12-26 22:45:53 +00:00
|
|
|
discard;
|
|
|
|
#endif
|
2020-12-16 23:44:15 +00:00
|
|
|
#endif
|
2023-02-25 19:03:39 +00:00
|
|
|
|
|
|
|
return alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|