Files
Cielonos/Assets/Shaders/ScriptablePostProcessor/Shaders/Sharpen.shader
2025-12-22 18:36:29 -05:00

77 lines
3.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Shader "SLS/Postprocessing/Sharpen"
{
SubShader
{
Tags
{
"RenderType" = "Opaque"
"RenderPipeline" = "UniversalPipeline"
}
ZWrite Off
Cull Off
HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
// 变量定义
float4 _SharpnessParams;
// x: Strength (锐化强度)
// y: Threshold (对比度阈值,用于降噪)
// z: Clamp (最大亮度变化限制,防光晕)
// w: Unused
half4 SharpenPassFragment(Varyings input) : SV_Target
{
half2 uv = input.texcoord;
// _BlitTexture_TexelSize 包含了纹理像素的大小 (1/width, 1/height)
float4 texelSize = _BlitTexture_TexelSize;
// 1. 5-Tap 采样 (中心 + 上下左右)
half4 center = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uv);
half3 up = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uv + float2(0, texelSize.y)).rgb;
half3 down = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uv - float2(0, texelSize.y)).rgb;
half3 left = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uv - float2(texelSize.x, 0)).rgb;
half3 right = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uv + float2(texelSize.x, 0)).rgb;
// 2. 计算周围像素的平均值
half3 neighborsAvg = (up + down + left + right) * 0.25;
// 3. 计算差异 (边缘信号)
// 如果 center 比周围亮diff 为正;反之为负。
half3 diff = center.rgb - neighborsAvg;
// 参数解包
float strength = _SharpnessParams.x;
float threshold = _SharpnessParams.y;
float clampVal = _SharpnessParams.z;
// 4. 【优化一】对比度阈值 (Threshold) - 降噪用
// 计算差异的亮度值
float lumaDiff = dot(abs(diff), float3(0.2126, 0.7152, 0.0722));
// 如果差异小于阈值,则不进行锐化 (factor = 0)
// 使用 smoothstep 让过渡更平滑
float factor = smoothstep(threshold, threshold + 0.001, lumaDiff);
// 5. 应用锐化
half3 sharpened = center.rgb + diff * strength * factor;
// 6. 【优化二】钳制 (Clamp) - 防光晕用
// 限制锐化后的值与原始值的偏差不能超过 clampVal
sharpened = max(sharpened, center.rgb - clampVal); // 不能太暗
sharpened = min(sharpened, center.rgb + clampVal); // 不能太亮
return half4(sharpened, center.a);
}
ENDHLSL
Pass
{
Name "Sharpen Pass"
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment SharpenPassFragment
ENDHLSL
}
}
}