chromaticAberration.fragment.fx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. BABYLON.JS Chromatic Aberration GLSL Shader
  3. Author: Olivier Guyot
  4. Separates very slightly R, G and B colors on the edges of the screen
  5. Inspired by Francois Tarlier & Martins Upitis
  6. */
  7. #ifdef GL_ES
  8. precision highp float;
  9. #endif
  10. // samplers
  11. uniform sampler2D textureSampler; // original color
  12. // uniforms
  13. uniform float chromatic_aberration;
  14. uniform float screen_width;
  15. uniform float screen_height;
  16. // varyings
  17. varying vec2 vUV;
  18. void main(void)
  19. {
  20. vec2 centered_screen_pos = vec2(vUV.x-0.5, vUV.y-0.5);
  21. float radius2 = centered_screen_pos.x*centered_screen_pos.x
  22. + centered_screen_pos.y*centered_screen_pos.y;
  23. float radius = sqrt(radius2);
  24. vec4 original = texture2D(textureSampler, vUV);
  25. if(chromatic_aberration > 0.0) {
  26. //index of refraction of each color channel, causing chromatic dispersion
  27. vec3 ref_indices = vec3(0.6, 0.3, 0.0);
  28. float ref_shiftX = chromatic_aberration * radius * 12.0 / screen_width;
  29. float ref_shiftY = chromatic_aberration * radius * 12.0 / screen_height;
  30. // shifts for red, green & blue
  31. vec2 ref_coords_r = vec2(vUV.x + ref_indices.r*ref_shiftX, vUV.y + ref_indices.r*ref_shiftY*0.5);
  32. vec2 ref_coords_g = vec2(vUV.x + ref_indices.g*ref_shiftX, vUV.y + ref_indices.g*ref_shiftY*0.5);
  33. vec2 ref_coords_b = vec2(vUV.x + ref_indices.b*ref_shiftX, vUV.y + ref_indices.b*ref_shiftY*0.5);
  34. original.r = texture2D(textureSampler, ref_coords_r).r;
  35. original.g = texture2D(textureSampler, ref_coords_g).g;
  36. original.b = texture2D(textureSampler, ref_coords_b).b;
  37. }
  38. gl_FragColor = original;
  39. }