Files
2025-06-04 09:09:39 +08:00

305 lines
8.2 KiB
Plaintext

half4 StochasticSampleDiffuse(float3 uv, out half4 cw, MIPFROMATRAW mipLevel)
{
float3 uv1, uv2, uv3;
half3 w;
PrepareStochasticUVs(_StochasticScale, uv, uv1, uv2, uv3, w);
#if _PERTEXSTOCHASTIC
half4 data = SAMPLE_TEXTURE2D_LOD(_PerTexProps, shared_point_clamp_sampler, float2(uv.z * _PerTexProps_TexelSize.x, 9.5/32), 0);
MSBRANCHCLUSTER(data.b-0.5)
{
COUNTSAMPLE
cw = half4(1,0,0,1);
return MICROSPLAT_SAMPLE(_Diffuse, uv, mipLevel);
}
#endif
half4 G1 = half4(0,0,0,0);
half4 G2 = half4(0,0,0,0);
half4 G3 = half4(0,0,0,0);
float contrast = _StochasticContrast;
#if _PERTEXCLUSTERCONTRAST
contrast = SAMPLE_TEXTURE2D_LOD(_PerTexProps, shared_point_clamp_sampler, float2(uv.z * _PerTexProps_TexelSize.x, 10.5/32), 0).r;
#endif
// apply contrast early to help sample culling in albedo pass
// this changes our contrast curve to have a minimum, but I don't think
// blurry blends are desired with stochastic..
half3 mw = min(0.0, contrast - 0.45);
w = saturate(lerp(mw, 1, w));
MSBRANCHCLUSTER(w.x)
{
G1 = MICROSPLAT_SAMPLE(_Diffuse, uv1, mipLevel);
COUNTSAMPLE
}
MSBRANCHCLUSTER(w.y)
{
G2 = MICROSPLAT_SAMPLE(_Diffuse, uv2, mipLevel);
COUNTSAMPLE
}
MSBRANCHCLUSTER(w.z)
{
G3 = MICROSPLAT_SAMPLE(_Diffuse, uv3, mipLevel);
COUNTSAMPLE
}
cw.xyz = BaryWeightBlend(w, G1.a, G2.a, G3.a, contrast);
cw.w = 1;
return G1 * cw.x + G2 * cw.y + G3 * cw.z;
}
half4 StochasticSampleDiffuseLOD(float3 uv, out half4 cw, float mipLevel)
{
float3 uv1, uv2, uv3;
half3 w;
PrepareStochasticUVs(_StochasticScale, uv, uv1, uv2, uv3, w);
#if _PERTEXSTOCHASTIC
half4 data = SAMPLE_TEXTURE2D_LOD(_PerTexProps, shared_point_clamp_sampler, float2(uv.z * _PerTexProps_TexelSize.x, 9.5/32), 0);
MSBRANCHCLUSTER(data.b-0.5)
{
return UNITY_SAMPLE_TEX2DARRAY_LOD(_Diffuse, uv, mipLevel);
}
#endif
float contrast = _StochasticContrast;
#if _PERTEXCLUSTERCONTRAST
contrast = SAMPLE_TEXTURE2D_LOD(_PerTexProps, shared_point_clamp_sampler, float2(uv.z * _PerTexProps_TexelSize.x, 10.5/32), 0).r;
#endif
// pre contrast for culling
half3 mw = min(0.0, contrast - 0.45);
w = saturate(lerp(mw, 1, w));
half4 G1 = half4(0,0,0,0);
half4 G2 = half4(0,0,0,0);
half4 G3 = half4(0,0,0,0);
MSBRANCHCLUSTER(w.x)
{
G1 = UNITY_SAMPLE_TEX2DARRAY_LOD(_Diffuse, uv1, mipLevel);
}
MSBRANCHCLUSTER(w.y)
{
G2 = UNITY_SAMPLE_TEX2DARRAY_LOD(_Diffuse, uv2, mipLevel);
}
MSBRANCHCLUSTER(w.z)
{
G3 = UNITY_SAMPLE_TEX2DARRAY_LOD(_Diffuse, uv3, mipLevel);
}
cw.xyz = BaryWeightBlend(w, G1.a, G2.a, G3.a, contrast);
cw.w = 1;
return G1 * cw.x + G2 * cw.y + G3 * cw.z;
}
half4 StochasticSampleNormal(float3 uv, half4 cw, MIPFROMATRAW mipLevel)
{
float3 uv1, uv2, uv3;
half3 w;
PrepareStochasticUVs(_StochasticScale, uv, uv1, uv2, uv3, w);
#if _PERTEXSTOCHASTIC
half4 data = SAMPLE_TEXTURE2D_LOD(_PerTexProps, shared_point_clamp_sampler, float2(uv.z * _PerTexProps_TexelSize.x, 9.5/32), 0);
MSBRANCHCLUSTER(data.b-0.5)
{
COUNTSAMPLE
return MICROSPLAT_SAMPLE(_NormalSAO, uv, mipLevel);
}
#endif
half4 G1 = half4(0,0.5,1,0.5);
half4 G2 = half4(0,0.5,1,0.5);
half4 G3 = half4(0,0.5,1,0.5);
// So, when triplanar is on, the cw data gets stomped somehow, and we can't do stochastic on the normals. So
// we have to disabled that here and recompute the blend, which decorilates the texture from the albedo.
// So it doesn't look or perform as good, which is sad..
#if _TRIPLANAR
float contrast = _StochasticContrast;
#if _PERTEXCLUSTERCONTRAST
contrast = SAMPLE_TEXTURE2D_LOD(_PerTexProps, shared_point_clamp_sampler, float2(uv.z * _PerTexProps_TexelSize.x, 10.5/32), 0).r;
#endif
// pre contrast for culling
half3 mw = min(0.0, contrast - 0.45);
w = saturate(lerp(mw, 1, w));
//MSBRANCHCLUSTER(cw.x)
{
G1 = MICROSPLAT_SAMPLE(_NormalSAO, uv1, mipLevel);
COUNTSAMPLE
#if _PACKINGHQ
G1.rb = MICROSPLAT_SAMPLE(_SmoothAO, uv1, mipLevel).ga;
COUNTSAMPLE
#endif
}
//MSBRANCHCLUSTER(cw.y)
{
G2 = MICROSPLAT_SAMPLE(_NormalSAO, uv2, mipLevel);
COUNTSAMPLE
#if _PACKINGHQ
G2.rb = MICROSPLAT_SAMPLE(_SmoothAO, uv2, mipLevel).ga;
COUNTSAMPLE
#endif
}
//MSBRANCHCLUSTER(cw.z)
{
G3 = MICROSPLAT_SAMPLE(_NormalSAO, uv3, mipLevel);
COUNTSAMPLE
#if _PACKINGHQ
G3.rb = MICROSPLAT_SAMPLE(_SmoothAO, uv3, mipLevel).ga;
COUNTSAMPLE
#endif
}
cw.xyz = BaryWeightBlend(w, G1.a, G2.a, G3.a, contrast);
cw.w = 1;
#else
MSBRANCHCLUSTER(cw.x)
{
G1 = MICROSPLAT_SAMPLE(_NormalSAO, uv1, mipLevel);
COUNTSAMPLE
#if _PACKINGHQ
G1.rb = MICROSPLAT_SAMPLE(_SmoothAO, uv1, mipLevel).ga;
COUNTSAMPLE
#endif
}
MSBRANCHCLUSTER(cw.y)
{
G2 = MICROSPLAT_SAMPLE(_NormalSAO, uv2, mipLevel);
COUNTSAMPLE
#if _PACKINGHQ
G2.rb = MICROSPLAT_SAMPLE(_SmoothAO, uv2, mipLevel).ga;
COUNTSAMPLE
#endif
}
MSBRANCHCLUSTER(cw.z)
{
G3 = MICROSPLAT_SAMPLE(_NormalSAO, uv3, mipLevel);
COUNTSAMPLE
#if _PACKINGHQ
G3.rb = MICROSPLAT_SAMPLE(_SmoothAO, uv3, mipLevel).ga;
COUNTSAMPLE
#endif
}
#endif
return G1 * cw.x + G2 * cw.y + G3 * cw.z;
}
half4 StochasticSampleEmis(float3 uv, half4 cw, MIPFROMATRAW mipLevel)
{
#if _USEEMISSIVEMETAL
float3 uv1, uv2, uv3;
half3 w;
PrepareStochasticUVs(_StochasticScale, uv, uv1, uv2, uv3, w);
#if _PERTEXSTOCHASTIC
half4 data = SAMPLE_TEXTURE2D_LOD(_PerTexProps, shared_point_clamp_sampler, float2(uv.z * _PerTexProps_TexelSize.x, 9.5/32), 0);
MSBRANCHCLUSTER(data.b-0.5)
{
COUNTSAMPLE
return MICROSPLAT_SAMPLE(_EmissiveMetal, uv, mipLevel);
}
#endif
half4 G1 = half4(0,0,0,0);
half4 G2 = half4(0,0,0,0);
half4 G3 = half4(0,0,0,0);
MSBRANCHCLUSTER(cw.x)
{
G1 = MICROSPLAT_SAMPLE(_EmissiveMetal, uv1, mipLevel);
COUNTSAMPLE
}
MSBRANCHCLUSTER(cw.y)
{
G2 = MICROSPLAT_SAMPLE(_EmissiveMetal, uv2, mipLevel);
COUNTSAMPLE
}
MSBRANCHCLUSTER(cw.z)
{
G3 = MICROSPLAT_SAMPLE(_EmissiveMetal, uv3, mipLevel);
COUNTSAMPLE
}
return G1 * cw.x + G2 * cw.y + G3 * cw.z;
#endif
return 0;
}
half4 StochasticSampleSpecular(float3 uv, half4 cw, MIPFROMATRAW mipLevel)
{
#if _USESPECULARWORKFLOW
float3 uv1, uv2, uv3;
half3 w;
PrepareStochasticUVs(_StochasticScale, uv, uv1, uv2, uv3, w);
#if _PERTEXSTOCHASTIC
half4 data = SAMPLE_TEXTURE2D_LOD(_PerTexProps, shared_point_clamp_sampler, float2(uv.z * _PerTexProps_TexelSize.x, 9.5/32), 0);
MSBRANCHCLUSTER(data.b-0.5)
{
COUNTSAMPLE
return MICROSPLAT_SAMPLE(_Specular, uv, mipLevel);
}
#endif
half4 G1 = half4(0,0,0,0);
half4 G2 = half4(0,0,0,0);
half4 G3 = half4(0,0,0,0);
MSBRANCHCLUSTER(cw.x)
{
G1 = MICROSPLAT_SAMPLE(_Specular, uv1, mipLevel);
COUNTSAMPLE
}
MSBRANCHCLUSTER(cw.y)
{
G2 = MICROSPLAT_SAMPLE(_Specular, uv2, mipLevel);
COUNTSAMPLE
}
MSBRANCHCLUSTER(cw.z)
{
G3 = MICROSPLAT_SAMPLE(_Specular, uv3, mipLevel);
COUNTSAMPLE
}
return G1 * cw.x + G2 * cw.y + G3 * cw.z;
#endif
return 0;
}
// ----------------------------------------------------------------------------
#undef MICROSPLAT_SAMPLE_DIFFUSE
#undef MICROSPLAT_SAMPLE_NORMAL
#undef MICROSPLAT_SAMPLE_DIFFUSE_LOD
#undef MICROSPLAT_SAMPLE_EMIS
#undef MICROSPLAT_SAMPLE_SPECULAR
#define MICROSPLAT_SAMPLE_DIFFUSE(u, cl, l) StochasticSampleDiffuse(u, cl, l)
#define MICROSPLAT_SAMPLE_NORMAL(u, cl, l) StochasticSampleNormal(u, cl, l)
#define MICROSPLAT_SAMPLE_DIFFUSE_LOD(u, cl, l) StochasticSampleDiffuseLOD(u, cl, l)
#define MICROSPLAT_SAMPLE_EMIS(u, cl, l) StochasticSampleEmis(u, cl, l)
#define MICROSPLAT_SAMPLE_SPECULAR(u, cl, l) StochasticSampleSpecular(u, cl, l)