116 lines
3.8 KiB
HLSL
116 lines
3.8 KiB
HLSL
#ifndef AA_FROM_GREEN_CHANNEL_INCLUDED
|
|
#define AA_FROM_GREEN_CHANNEL_INCLUDED
|
|
|
|
// Shader Graph Custom Function
|
|
// UnityTexture2D와 UnitySamplerState를 받아서 G 채널에 안티에일리어싱 적용
|
|
|
|
void AAFromGreenChannel_float(
|
|
float2 UV,
|
|
UnityTexture2D Tex,
|
|
UnitySamplerState Samp,
|
|
float Strength,
|
|
float2 Resolution,
|
|
out float New)
|
|
{
|
|
// Texel Size 계산
|
|
float2 texelSize = 1.0 / Resolution;
|
|
|
|
// 중앙값
|
|
float center = SAMPLE_TEXTURE2D(Tex, Samp, UV).g;
|
|
|
|
if (Strength > 0.0)
|
|
{
|
|
// 주변 8방향 샘플링
|
|
float left = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(-texelSize.x, 0)).g;
|
|
float right = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(texelSize.x, 0)).g;
|
|
float top = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(0, texelSize.y)).g;
|
|
float bottom = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(0, -texelSize.y)).g;
|
|
|
|
float topLeft = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(-texelSize.x, texelSize.y)).g;
|
|
float topRight = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(texelSize.x, texelSize.y)).g;
|
|
float bottomLeft = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(-texelSize.x, -texelSize.y)).g;
|
|
float bottomRight = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(texelSize.x, -texelSize.y)).g;
|
|
|
|
// 9탭 박스 필터 평균
|
|
float average = (center + left + right + top + bottom +
|
|
topLeft + topRight + bottomLeft + bottomRight) / 9.0;
|
|
|
|
// Strength에 따라 블렌딩
|
|
New = lerp(center, average, saturate(Strength));
|
|
}
|
|
else
|
|
{
|
|
New = center;
|
|
}
|
|
}
|
|
|
|
// Simple 버전 - 5탭 크로스 필터
|
|
void AAFromGreenChannelSimple_float(
|
|
float2 UV,
|
|
UnityTexture2D Tex,
|
|
UnitySamplerState Samp,
|
|
float Strength,
|
|
float2 Resolution,
|
|
out float New)
|
|
{
|
|
float2 texelSize = 1.0 / Resolution;
|
|
|
|
float center = SAMPLE_TEXTURE2D(Tex, Samp, UV).g;
|
|
|
|
if (Strength > 0.0)
|
|
{
|
|
float left = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(-texelSize.x, 0)).g;
|
|
float right = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(texelSize.x, 0)).g;
|
|
float top = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(0, texelSize.y)).g;
|
|
float bottom = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(0, -texelSize.y)).g;
|
|
|
|
float average = (center + left + right + top + bottom) / 5.0;
|
|
|
|
New = lerp(center, average, saturate(Strength));
|
|
}
|
|
else
|
|
{
|
|
New = center;
|
|
}
|
|
}
|
|
|
|
// Weighted 버전 - 가우시안 가중치
|
|
void AAFromGreenChannelWeighted_float(
|
|
float2 UV,
|
|
UnityTexture2D Tex,
|
|
UnitySamplerState Samp,
|
|
float Strength,
|
|
float2 Resolution,
|
|
out float New)
|
|
{
|
|
float2 texelSize = 1.0 / Resolution;
|
|
|
|
float center = SAMPLE_TEXTURE2D(Tex, Samp, UV).g;
|
|
|
|
if (Strength > 0.0)
|
|
{
|
|
float left = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(-texelSize.x, 0)).g;
|
|
float right = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(texelSize.x, 0)).g;
|
|
float top = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(0, texelSize.y)).g;
|
|
float bottom = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(0, -texelSize.y)).g;
|
|
|
|
float topLeft = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(-texelSize.x, texelSize.y)).g;
|
|
float topRight = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(texelSize.x, texelSize.y)).g;
|
|
float bottomLeft = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(-texelSize.x, -texelSize.y)).g;
|
|
float bottomRight = SAMPLE_TEXTURE2D(Tex, Samp, UV + float2(texelSize.x, -texelSize.y)).g;
|
|
|
|
// 가우시안 가중치
|
|
float weighted = center * 0.25;
|
|
weighted += (left + right + top + bottom) * 0.125;
|
|
weighted += (topLeft + topRight + bottomLeft + bottomRight) * 0.0625;
|
|
|
|
New = lerp(center, weighted, saturate(Strength));
|
|
}
|
|
else
|
|
{
|
|
New = center;
|
|
}
|
|
}
|
|
|
|
#endif // AA_FROM_GREEN_CHANNEL_INCLUDED
|