lens_distorted.glsl 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* clang-format off */
  2. [vertex]
  3. #ifdef USE_GLES_OVER_GL
  4. #define lowp
  5. #define mediump
  6. #define highp
  7. #else
  8. precision highp float;
  9. precision highp int;
  10. #endif
  11. attribute highp vec2 vertex; // attrib:0
  12. /* clang-format on */
  13. uniform vec2 offset;
  14. uniform vec2 scale;
  15. varying vec2 uv_interp;
  16. void main() {
  17. uv_interp = vertex.xy * 2.0 - 1.0;
  18. vec2 v = vertex.xy * scale + offset;
  19. gl_Position = vec4(v, 0.0, 1.0);
  20. }
  21. /* clang-format off */
  22. [fragment]
  23. #ifdef USE_GLES_OVER_GL
  24. #define lowp
  25. #define mediump
  26. #define highp
  27. #else
  28. precision mediump float;
  29. precision highp int;
  30. #endif
  31. uniform sampler2D source; //texunit:0
  32. /* clang-format on */
  33. uniform vec2 eye_center;
  34. uniform float k1;
  35. uniform float k2;
  36. uniform float upscale;
  37. uniform float aspect_ratio;
  38. varying vec2 uv_interp;
  39. void main() {
  40. vec2 coords = uv_interp;
  41. vec2 offset = coords - eye_center;
  42. // take aspect ratio into account
  43. offset.y /= aspect_ratio;
  44. // distort
  45. vec2 offset_sq = offset * offset;
  46. float radius_sq = offset_sq.x + offset_sq.y;
  47. float radius_s4 = radius_sq * radius_sq;
  48. float distortion_scale = 1.0 + (k1 * radius_sq) + (k2 * radius_s4);
  49. offset *= distortion_scale;
  50. // reapply aspect ratio
  51. offset.y *= aspect_ratio;
  52. // add our eye center back in
  53. coords = offset + eye_center;
  54. coords /= upscale;
  55. // and check our color
  56. if (coords.x < -1.0 || coords.y < -1.0 || coords.x > 1.0 || coords.y > 1.0) {
  57. gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
  58. } else {
  59. coords = (coords + vec2(1.0)) / vec2(2.0);
  60. gl_FragColor = texture2D(source, coords);
  61. }
  62. }