fix: highlight halo

This commit is contained in:
zhoubinjia committed 2026-06-26 16:46:54 +08:00
1 parent d471e052f9
commit 471a453f85
1 file changed
+120 -68
@@ -26,9 +26,21 @@ object ShadowsHighlightsShader {
const float SH_LAB_EPSILON = 216.0 / 24389.0;
const float SH_LAB_KAPPA = 24389.0 / 27.0;
const float SH_LOW_APPROX = 0.000001;
const float SH_COMPRESS = 0.5;
const float SH_HIGHLIGHTS_COLOR_ADJUSTMENT = 0.6;
const float SH_SHADOWS_COLOR_ADJUSTMENT = 1.0;
const float SH_RANGE_SIGMA = 0.115;
const float SH_RANGE_SIGMA2 = SH_RANGE_SIGMA * SH_RANGE_SIGMA;
// Negative highlights recover high-key structure: move the highlight base out of
// the near-white range, expand L - base detail, and keep chroma from washing out.
const float SH_HIGHLIGHT_START = 0.38;
const float SH_HIGHLIGHT_FULL = 0.78;
const float SH_HIGHLIGHT_RECOVERY_SHIFT = 0.12;
const float SH_HIGHLIGHT_BRIGHTEN_SHIFT = 0.12;
const float SH_HIGHLIGHT_DETAIL_GAIN = 1.15;
const float SH_HIGHLIGHT_CHROMA_GAIN = 0.55;
const float SH_SHADOW_FULL = 0.22;
const float SH_SHADOW_END = 0.62;
const float SH_SHADOW_MAX_SHIFT = 0.18;
const float SH_DETAIL_LIMIT = 0.22;
const float SH_DETAIL_GAIN_LIMIT = 2.2;
float shSanitizeFloat(float value) {
if (value != value) return 0.0;
@@ -43,19 +55,6 @@ object ShadowsHighlightsShader {
);
}
float shSign(float value) {
return value < 0.0 ? -1.0 : 1.0;
}
float shSignedInv(float value, float signSource) {
float inv = abs(value) > SH_LOW_APPROX ? 1.0 / abs(value) : 1.0 / SH_LOW_APPROX;
return signSource < 0.0 ? -inv : inv;
}
float shColorCorrection(float adjustment, float signSource) {
return (clamp(adjustment, 0.0, 1.0) - 0.5) * shSign(signSource) + 0.5;
}
vec3 shLabF(vec3 value) {
vec3 linearPart = (SH_LAB_KAPPA * value + vec3(16.0)) / 116.0;
vec3 cubePart = pow(max(value, vec3(0.0)), vec3(1.0 / 3.0));
@@ -89,37 +88,41 @@ object ShadowsHighlightsShader {
return shXyzToRgb(shLabToXyz(lab));
}
float shTonalRangeWeight(float sampleL, float centerL) {
float delta = sampleL - centerL;
float bilateral = exp(-(delta * delta) / max(2.0 * SH_RANGE_SIGMA2, SH_LOW_APPROX));
float edgeStop = 1.0 - smoothstep(0.16, 0.32, abs(delta));
return bilateral * edgeStop;
}
void shAddBaseSample(vec2 uv, float centerL, float spatialWeight, inout float sum, inout float weightSum) {
float sampleL = shRgbToLabScaled(sampleToneSource(clamp(uv, vec2(0.0), vec2(1.0)))).x;
float delta = sampleL - centerL;
float rangeWeight = exp(-(delta * delta) / 0.25);
float weight = spatialWeight * rangeWeight;
float weight = spatialWeight * shTonalRangeWeight(sampleL, centerL);
sum += sampleL * weight;
weightSum += weight;
}
float shSampleBaseL(vec2 uv, vec3 centerLab) {
float centerL = centerLab.x;
float shSampleBaseL(vec2 uv, float centerL) {
vec2 radiusSmall = uTexelSize * 8.0;
vec2 radiusMid = uTexelSize * 32.0;
vec2 radiusLarge = uTexelSize * 112.0;
vec2 radiusMid = uTexelSize * 40.0;
vec2 radiusLarge = uTexelSize * 144.0;
float sum = centerL * 0.22;
float weightSum = 0.22;
float sum = centerL * 0.55;
float weightSum = 0.55;
shAddBaseSample(uv + vec2(radiusSmall.x, 0.0), centerL, 0.13, sum, weightSum);
shAddBaseSample(uv - vec2(radiusSmall.x, 0.0), centerL, 0.13, sum, weightSum);
shAddBaseSample(uv + vec2(0.0, radiusSmall.y), centerL, 0.13, sum, weightSum);
shAddBaseSample(uv - vec2(0.0, radiusSmall.y), centerL, 0.13, sum, weightSum);
shAddBaseSample(uv + radiusSmall, centerL, 0.08, sum, weightSum);
shAddBaseSample(uv - radiusSmall, centerL, 0.08, sum, weightSum);
shAddBaseSample(uv + vec2(radiusSmall.x, -radiusSmall.y), centerL, 0.08, sum, weightSum);
shAddBaseSample(uv + vec2(-radiusSmall.x, radiusSmall.y), centerL, 0.08, sum, weightSum);
shAddBaseSample(uv + vec2(radiusSmall.x, 0.0), centerL, 0.14, sum, weightSum);
shAddBaseSample(uv - vec2(radiusSmall.x, 0.0), centerL, 0.14, sum, weightSum);
shAddBaseSample(uv + vec2(0.0, radiusSmall.y), centerL, 0.14, sum, weightSum);
shAddBaseSample(uv - vec2(0.0, radiusSmall.y), centerL, 0.14, sum, weightSum);
shAddBaseSample(uv + radiusSmall, centerL, 0.09, sum, weightSum);
shAddBaseSample(uv - radiusSmall, centerL, 0.09, sum, weightSum);
shAddBaseSample(uv + vec2(radiusSmall.x, -radiusSmall.y), centerL, 0.09, sum, weightSum);
shAddBaseSample(uv + vec2(-radiusSmall.x, radiusSmall.y), centerL, 0.09, sum, weightSum);
shAddBaseSample(uv + vec2(radiusMid.x, 0.0), centerL, 0.07, sum, weightSum);
shAddBaseSample(uv - vec2(radiusMid.x, 0.0), centerL, 0.07, sum, weightSum);
shAddBaseSample(uv + vec2(0.0, radiusMid.y), centerL, 0.07, sum, weightSum);
shAddBaseSample(uv - vec2(0.0, radiusMid.y), centerL, 0.07, sum, weightSum);
shAddBaseSample(uv + vec2(radiusMid.x, 0.0), centerL, 0.075, sum, weightSum);
shAddBaseSample(uv - vec2(radiusMid.x, 0.0), centerL, 0.075, sum, weightSum);
shAddBaseSample(uv + vec2(0.0, radiusMid.y), centerL, 0.075, sum, weightSum);
shAddBaseSample(uv - vec2(0.0, radiusMid.y), centerL, 0.075, sum, weightSum);
shAddBaseSample(uv + vec2(radiusLarge.x, 0.0), centerL, 0.035, sum, weightSum);
shAddBaseSample(uv - vec2(radiusLarge.x, 0.0), centerL, 0.035, sum, weightSum);
@@ -129,52 +132,101 @@ object ShadowsHighlightsShader {
return sum / max(weightSum, 0.0001);
}
vec3 shOverlay(vec3 a, vec3 b, float opacity, float transform, float ccorrect) {
float opacity2 = opacity * opacity;
for (int i = 0; i < 4; i++) {
if (opacity2 <= 0.0) break;
float shSmoothUnit(float value) {
float t = clamp(value, 0.0, 1.0);
return t * t * (3.0 - 2.0 * t);
}
float la = a.x;
float lb = (b.x - 0.5) * shSign(opacity) * shSign(1.0 - la) + 0.5;
lb = clamp(lb, 0.0, 1.0);
float lref = shSignedInv(la, la);
float href = shSignedInv(1.0 - la, 1.0 - la);
float shHighlightWeight(float value) {
return shSmoothUnit(
(value - SH_HIGHLIGHT_START) / max(SH_HIGHLIGHT_FULL - SH_HIGHLIGHT_START, SH_LOW_APPROX)
);
}
float chunk = opacity2 > 1.0 ? 1.0 : opacity2;
float optrans = chunk * transform;
opacity2 -= 1.0;
float shShadowWeight(float value) {
return shSmoothUnit(
(SH_SHADOW_END - value) / max(SH_SHADOW_END - SH_SHADOW_FULL, SH_LOW_APPROX)
);
}
float overL = la > 0.5
? 1.0 - (1.0 - 2.0 * (la - 0.5)) * (1.0 - lb)
: 2.0 * la * lb;
a.x = la * (1.0 - optrans) + overL * optrans;
float shEdgeConfidence(float baseL, float centerL) {
return 1.0 - smoothstep(SH_RANGE_SIGMA * 1.35, SH_RANGE_SIGMA * 2.7, abs(centerL - baseL));
}
float chromaFactor = a.x * lref * ccorrect + (1.0 - a.x) * href * (1.0 - ccorrect);
a.y = a.y * (1.0 - optrans) + (a.y + b.y) * chromaFactor * optrans;
a.z = a.z * (1.0 - optrans) + (a.z + b.z) * chromaFactor * optrans;
float shHighlightRegionMask(float baseL, float centerL) {
return shHighlightWeight(baseL) * shEdgeConfidence(baseL, centerL);
}
float shApplyHighlightBase(float baseL, float centerL, float highlights) {
float mask = shHighlightRegionMask(baseL, centerL);
if (highlights < 0.0) {
return baseL - (-highlights) * SH_HIGHLIGHT_RECOVERY_SHIFT * mask;
}
return a;
return baseL + highlights * SH_HIGHLIGHT_BRIGHTEN_SHIFT * mask;
}
float shApplyShadowBase(float baseL, float centerL, float shadows) {
float mask = min(shShadowWeight(baseL), shShadowWeight(centerL));
if (shadows > 0.0) {
return baseL + shadows * SH_SHADOW_MAX_SHIFT * mask;
}
return baseL - (-shadows) * SH_SHADOW_MAX_SHIFT * mask;
}
float shAdjustTone(float value, float highlights, float shadows) {
float adjusted = shApplyHighlightBase(value, value, highlights);
adjusted = shApplyShadowBase(adjusted, value, shadows);
return clamp(adjusted, 0.0, 1.0);
}
float shHighlightDetailGain(float baseL, float highlights) {
if (highlights >= 0.0) {
return 1.0;
}
float highMask = shHighlightWeight(baseL);
float targetGain = 1.0 + (-highlights) * SH_HIGHLIGHT_DETAIL_GAIN;
return mix(1.0, min(targetGain, SH_DETAIL_GAIN_LIMIT), highMask);
}
float shBoundedDetailScale(float adjustedBaseL, float detail, float targetGain) {
if (abs(detail) < SH_LOW_APPROX) {
return 1.0;
}
float boundScale = detail > 0.0
? (1.0 - adjustedBaseL) / max(detail, SH_LOW_APPROX)
: adjustedBaseL / max(-detail, SH_LOW_APPROX);
return clamp(min(targetGain, boundScale), 0.0, targetGain);
}
vec3 applyShadowsHighlights(vec3 inputColor, vec2 uv) {
float highlights = 1.6 * clamp(uHighlights, -1.0, 1.0);
float shadows = 1.6 * clamp(uShadows, -1.0, 1.0);
float highlights = clamp(uHighlights, -1.0, 1.0);
float shadows = clamp(uShadows, -1.0, 1.0);
if (abs(highlights) < 0.001 && abs(shadows) < 0.001) {
return inputColor;
}
vec3 lab = shRgbToLabScaled(inputColor);
float baseL = shSampleBaseL(uv, lab);
vec3 maskLab = vec3(1.0 - baseL, 0.0, 0.0);
float compressDenom = max(1.0 - SH_COMPRESS, 0.0001);
float centerL = clamp(lab.x, 0.0, 1.0);
float baseL = clamp(shSampleBaseL(uv, centerL), 0.0, 1.0);
float detail = clamp(centerL - baseL, -SH_DETAIL_LIMIT, SH_DETAIL_LIMIT);
float highlightsXform = clamp(1.0 - maskLab.x / compressDenom, 0.0, 1.0);
float highlightsCcorrect = shColorCorrection(SH_HIGHLIGHTS_COLOR_ADJUSTMENT, -highlights);
lab = shOverlay(lab, maskLab, -highlights, highlightsXform, 1.0 - highlightsCcorrect);
float highlightBaseL = shApplyHighlightBase(baseL, centerL, highlights);
float adjustedBaseL = shApplyShadowBase(highlightBaseL, centerL, shadows);
adjustedBaseL = clamp(adjustedBaseL, 0.0, 1.0);
float shadowsXform = clamp(maskLab.x / compressDenom - SH_COMPRESS / compressDenom, 0.0, 1.0);
float shadowsCcorrect = shColorCorrection(SH_SHADOWS_COLOR_ADJUSTMENT, shadows);
lab = shOverlay(lab, maskLab, shadows, shadowsXform, shadowsCcorrect);
float detailGain = shHighlightDetailGain(baseL, highlights);
float detailScale = shBoundedDetailScale(adjustedBaseL, detail, detailGain);
float localL = clamp(adjustedBaseL + detail * detailScale, 0.0, 1.0);
float directL = shAdjustTone(centerL, highlights, shadows);
float edgeFallback = smoothstep(SH_RANGE_SIGMA * 1.5, SH_RANGE_SIGMA * 3.0, abs(centerL - baseL));
lab.x = mix(localL, directL, edgeFallback);
float chromaMask = max(-highlights, 0.0) * shHighlightRegionMask(baseL, centerL);
lab.yz *= 1.0 + SH_HIGHLIGHT_CHROMA_GAIN * chromaMask;
return shSanitizeColor(shLabScaledToRgb(lab));
}