Files
LNXSDK/leenkx/Shaders/chromatic_aberration_pass/chromatic_aberration_pass.frag.glsl
2026-02-24 11:44:01 -08:00

84 lines
1.9 KiB
GLSL

#version 450
#include "compiled.inc"
uniform sampler2D tex;
#ifdef _CPostprocess
uniform vec4 PPComp13;
#endif
in vec2 texCoord;
out vec4 fragColor;
vec2 barrelDistortion(vec2 coord, float amt) {
vec2 cc = coord - 0.5;
float dist = dot(cc, cc);
return coord - cc * dist * amt;
}
float sat(float value)
{
return clamp(value, 0.0, 1.0);
}
float linterp(float t) {
return sat(1.0 - abs(2.0 * t - 1.0) );
}
float remap(float t, float a, float b ) {
return sat((t - a) / (b - a));
}
vec4 spectrum_offset(float t) {
vec4 ret;
float low = step(t,0.5);
float high = 1.0 - low;
float minMap = 1.0;
float maxMap = 6.0;
float w = linterp( remap(t, minMap/maxMap, 5.0/maxMap ) );
ret = vec4(low, 1.0, high, 1.) * vec4(1.0-w, w, 1.0-w, 1.0);
return pow(ret, vec4(1.0/2.2) );
}
void main() {
#ifdef _CPostprocess
float max_distort = PPComp13.x;
int num_iter = int(PPComp13.y);
int CAType = int(PPComp13.z);
int on = int(PPComp13.w);
#else
float max_distort = compoChromaticStrength;
int num_iter = compoChromaticSamples;
int CAType = compoChromaticType;
int on = 1;
#endif
// Spectral
if (CAType == 1) {
float reci_num_iter_f = 1.0 / float(num_iter);
vec4 sumcol = vec4(0.0);
vec4 sumw = vec4(0.0);
for (int i=0; i < num_iter; ++i)
{
float t = float(i) * reci_num_iter_f;
vec4 w = spectrum_offset(t);
sumw += w;
vec2 distortedUV = barrelDistortion(texCoord, 0.6 * max_distort * t);
sumcol += w * texture(tex, distortedUV);
}
if (on == 1) fragColor = sumcol / sumw; else fragColor = texture(tex, texCoord);
}
// inward sampling to avoid edge artifacts
else {
vec3 col = vec3(0.0);
vec2 toCenter = (vec2(0.5) - texCoord) * max_distort / 500.0;
col.x = texture(tex, texCoord + toCenter * 0.0).x;
col.y = texture(tex, texCoord + toCenter * 0.5).y;
col.z = texture(tex, texCoord + toCenter * 1.0).z;
if (on == 1) fragColor = vec4(col.x, col.y, col.z, 1.0);
else fragColor = texture(tex, texCoord);
}
}