Files
LNXSDK/leenkx/Shaders/chromatic_aberration_pass/chromatic_aberration_pass.frag.glsl

84 lines
1.9 KiB
Plaintext
Raw Normal View History

2025-01-22 16:18:30 +01:00
#version 450
#include "compiled.inc"
uniform sampler2D tex;
#ifdef _CPostprocess
2025-06-02 17:00:57 +00:00
uniform vec4 PPComp13;
2025-01-22 16:18:30 +01:00
#endif
in vec2 texCoord;
out vec4 fragColor;
vec2 barrelDistortion(vec2 coord, float amt) {
vec2 cc = coord - 0.5;
float dist = dot(cc, cc);
2026-02-24 11:44:01 -08:00
return coord - cc * dist * amt;
2025-01-22 16:18:30 +01:00
}
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);
2025-06-02 17:00:57 +00:00
int CAType = int(PPComp13.z);
int on = int(PPComp13.w);
2025-01-22 16:18:30 +01:00
#else
float max_distort = compoChromaticStrength;
int num_iter = compoChromaticSamples;
2025-06-02 17:00:57 +00:00
int CAType = compoChromaticType;
int on = 1;
2025-01-22 16:18:30 +01:00
#endif
// Spectral
2025-06-02 17:00:57 +00:00
if (CAType == 1) {
2025-01-22 16:18:30 +01:00
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;
2026-02-24 11:44:01 -08:00
vec2 distortedUV = barrelDistortion(texCoord, 0.6 * max_distort * t);
sumcol += w * texture(tex, distortedUV);
2025-01-22 16:18:30 +01:00
}
2025-06-02 17:00:57 +00:00
if (on == 1) fragColor = sumcol / sumw; else fragColor = texture(tex, texCoord);
2025-01-22 16:18:30 +01:00
}
2026-02-24 11:44:01 -08:00
// inward sampling to avoid edge artifacts
2025-01-22 16:18:30 +01:00
else {
vec3 col = vec3(0.0);
2026-02-24 11:44:01 -08:00
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);
2025-06-02 17:00:57 +00:00
else fragColor = texture(tex, texCoord);
2025-01-22 16:18:30 +01:00
}
}