chromaticAberration.fragment.fx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // samplers
  2. uniform sampler2D textureSampler; // original color
  3. // uniforms
  4. uniform float chromatic_aberration;
  5. uniform float radialIntensity;
  6. uniform vec2 direction;
  7. uniform vec2 centerPosition;
  8. uniform float screen_width;
  9. uniform float screen_height;
  10. // varyings
  11. varying vec2 vUV;
  12. void main(void)
  13. {
  14. vec2 centered_screen_pos = vec2(vUV.x - centerPosition.x, vUV.y - centerPosition.y);
  15. vec2 directionOfEffect = direction;
  16. if(directionOfEffect.x == 0. && directionOfEffect.y == 0.){
  17. directionOfEffect = normalize(centered_screen_pos);
  18. }
  19. float radius2 = centered_screen_pos.x*centered_screen_pos.x
  20. + centered_screen_pos.y*centered_screen_pos.y;
  21. float radius = sqrt(radius2);
  22. vec4 original = texture2D(textureSampler, vUV);
  23. //index of refraction of each color channel, causing chromatic dispersion
  24. vec3 ref_indices = vec3(-0.3, 0.0, 0.3);
  25. float ref_shiftX = chromatic_aberration * pow(radius, radialIntensity) * directionOfEffect.x / screen_width;
  26. float ref_shiftY = chromatic_aberration * pow(radius, radialIntensity) * directionOfEffect.y / screen_height;
  27. // shifts for red, green & blue
  28. vec2 ref_coords_r = vec2(vUV.x + ref_indices.r*ref_shiftX, vUV.y + ref_indices.r*ref_shiftY*0.5);
  29. vec2 ref_coords_g = vec2(vUV.x + ref_indices.g*ref_shiftX, vUV.y + ref_indices.g*ref_shiftY*0.5);
  30. vec2 ref_coords_b = vec2(vUV.x + ref_indices.b*ref_shiftX, vUV.y + ref_indices.b*ref_shiftY*0.5);
  31. original.r = texture2D(textureSampler, ref_coords_r).r;
  32. original.g = texture2D(textureSampler, ref_coords_g).g;
  33. original.b = texture2D(textureSampler, ref_coords_b).b;
  34. original.a = clamp(texture2D(textureSampler, ref_coords_r).a + texture2D(textureSampler, ref_coords_g).a + texture2D(textureSampler, ref_coords_b).a, 0., 1.);
  35. gl_FragColor = original;
  36. }