Chromatic aberration is a screen space technique that simulates lens distortion. Use it to give your scene a cinematic, lo-fi analog feel or to emphasize a chaotic event.
The input texture needed is the scene's colors captured into a framebuffer texture.
The adjustable parameters for this technique are the red, green, and blue offsets. Feel free to play around with these to get the particular color fringe you're looking for. These particular offsets produce a yellowish orange and blue fringe.
// ...
uniform vec2 mouseFocusPoint;
// ...
vec2 texSize = textureSize(colorTexture, 0).xy;
vec2 texCoord = gl_FragCoord.xy / texSize;
vec2 direction = texCoord - mouseFocusPoint;
// ...
The offsets can occur horizontally, vertically, or radially. One approach is to radiate out from the depth of field focal point. As the scene gets more out of focus, the chromatic aberration increases.
// ...
out vec4 fragColor;
// ...
fragColor.r = texture(colorTexture, texCoord + (direction * vec2(redOffset ))).r;
fragColor.g = texture(colorTexture, texCoord + (direction * vec2(greenOffset))).g;
fragColor.ba = texture(colorTexture, texCoord + (direction * vec2(blueOffset ))).ba;
}
With the direction and offsets, make three samples of the scene's colors—one for the red, green, and blue channels. These will be the final fragment color.
(C) 2021 David Lettier
lettier.com