stereo.glslf 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #version GLSL_VERSION
  2. /*==============================================================================
  3. VARS
  4. ==============================================================================*/
  5. #var ANAGLYPH 0
  6. /*============================================================================*/
  7. #include <precision_statement.glslf>
  8. #include <std.glsl>
  9. #include <color_util.glslf>
  10. uniform sampler2D u_sampler_left;
  11. uniform sampler2D u_sampler_right;
  12. /*==============================================================================
  13. SHADER INTERFACE
  14. ==============================================================================*/
  15. GLSL_IN vec2 v_texcoord;
  16. //------------------------------------------------------------------------------
  17. GLSL_OUT vec4 GLSL_OUT_FRAG_COLOR;
  18. /*============================================================================*/
  19. #if !ANAGLYPH
  20. uniform int u_enable_hmd_stereo;
  21. // u_distortion_params = [distortion_coef_1, distortion_coef_2, base_line_factor, inter_lens_factor]
  22. uniform vec4 u_distortion_params;
  23. uniform vec4 u_chromatic_aberration_coefs;
  24. void hmd_distorsion(vec2 texcoord, vec2 center, float size, sampler2D sampler) {
  25. vec2 theta = (texcoord - center) * 2.0;
  26. theta /= size;
  27. float rsquare = theta.x * theta.x + theta.y * theta.y;
  28. vec2 rvector = theta * (1.0 + u_distortion_params[0] * rsquare
  29. + u_distortion_params[1] * rsquare * rsquare);
  30. vec2 tc = rvector * 0.5 / (1.0 + u_distortion_params[0] + u_distortion_params[1]);
  31. if (length(u_chromatic_aberration_coefs) > 0.0) {
  32. vec2 tc_r = tc * (1.0 + u_chromatic_aberration_coefs[0] +
  33. rsquare * u_chromatic_aberration_coefs[1]) + center;
  34. vec2 tc_g = tc + center;
  35. vec2 tc_b = tc * (1.0 + u_chromatic_aberration_coefs[2] +
  36. rsquare * u_chromatic_aberration_coefs[3]) + center;
  37. if (clamp(tc_b, 0.0, 1.0) != tc_b) {
  38. GLSL_OUT_FRAG_COLOR = vec4(0.0);
  39. } else {
  40. vec4 orig_rgba = GLSL_TEXTURE(sampler, tc_g);
  41. GLSL_OUT_FRAG_COLOR[0] = GLSL_TEXTURE(sampler, tc_r).x;
  42. GLSL_OUT_FRAG_COLOR[1] = orig_rgba.y;
  43. GLSL_OUT_FRAG_COLOR[2] = GLSL_TEXTURE(sampler, tc_b).z;
  44. GLSL_OUT_FRAG_COLOR[3] = orig_rgba.w;
  45. }
  46. } else {
  47. tc = tc * size + center;
  48. if (clamp(tc, 0.0, 1.0) != tc) {
  49. GLSL_OUT_FRAG_COLOR = vec4(0.0);
  50. } else {
  51. GLSL_OUT_FRAG_COLOR = GLSL_TEXTURE(sampler, tc);
  52. }
  53. }
  54. }
  55. #endif // !ANAGLYPH
  56. /*==============================================================================
  57. MAIN
  58. ==============================================================================*/
  59. void main(void) {
  60. #if ANAGLYPH
  61. vec4 lc = GLSL_TEXTURE(u_sampler_left, v_texcoord);
  62. vec4 rc = GLSL_TEXTURE(u_sampler_right, v_texcoord);
  63. // Photoshop algorithm
  64. //GLSL_OUT_FRAG_COLOR = vec4(lc[0], rc[1], rc[2], lc[3] + rc[3]);
  65. // Dubois algorithm
  66. vec3 lc3 = vec3(lc[0], lc[1], lc[2]);
  67. srgb_to_lin(lc3);
  68. vec3 rc3 = vec3(rc[0], rc[1], rc[2]);
  69. srgb_to_lin(rc3);
  70. mat3 left = mat3(0.437, -0.062, -0.048,
  71. 0.449, -0.062, -0.050,
  72. 0.164, -0.024, -0.017);
  73. mat3 right = mat3(-0.011, 0.377, -0.026,
  74. -0.032, 0.761, -0.093 ,
  75. -0.007, 0.009, 1.234);
  76. vec3 color = clamp(left * lc3, 0.0, 1.0) + clamp(right * rc3, 0.0, 1.0);
  77. lin_to_srgb(color);
  78. GLSL_OUT_FRAG_COLOR = vec4(color, lc[3] + rc[3]);
  79. #else // ANAGLYPH
  80. if (u_enable_hmd_stereo != 0) {
  81. if (v_texcoord[0] < 0.5) {
  82. // left eye
  83. vec2 texcoord = vec2(2.0 * v_texcoord[0], v_texcoord[1]);
  84. vec2 center = vec2(0.5 - (u_distortion_params[3] - 0.5) / 2.0, u_distortion_params[2]);
  85. float size = 2.0 * u_distortion_params[3];
  86. hmd_distorsion(texcoord, center, size, u_sampler_left);
  87. } else {
  88. // right eye
  89. vec2 texcoord = vec2(2.0 * (v_texcoord[0] - 0.5), v_texcoord[1]);
  90. vec2 center = vec2(0.5 + (u_distortion_params[3] - 0.5) / 2.0, u_distortion_params[2]);
  91. float size = 2.0 * u_distortion_params[3];
  92. hmd_distorsion(texcoord, center, size, u_sampler_right);
  93. }
  94. } else {
  95. GLSL_OUT_FRAG_COLOR = GLSL_TEXTURE(u_sampler_left, v_texcoord);
  96. }
  97. #endif // ANAGLYPH
  98. }