cube_to_dp.glsl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 mediump float;
  9. precision mediump int;
  10. #endif
  11. layout(location = 0) in highp vec4 vertex_attrib;
  12. /* clang-format on */
  13. layout(location = 4) in vec2 uv_in;
  14. out vec2 uv_interp;
  15. void main() {
  16. uv_interp = uv_in;
  17. gl_Position = vertex_attrib;
  18. }
  19. /* clang-format off */
  20. [fragment]
  21. #ifdef USE_GLES_OVER_GL
  22. #define lowp
  23. #define mediump
  24. #define highp
  25. #else
  26. #if defined(USE_HIGHP_PRECISION)
  27. precision highp float;
  28. precision highp int;
  29. #else
  30. precision mediump float;
  31. precision mediump int;
  32. #endif
  33. #endif
  34. uniform highp samplerCube source_cube; //texunit:0
  35. /* clang-format on */
  36. in vec2 uv_interp;
  37. uniform bool z_flip;
  38. uniform highp float z_far;
  39. uniform highp float z_near;
  40. uniform highp float bias;
  41. void main() {
  42. highp vec3 normal = vec3(uv_interp * 2.0 - 1.0, 0.0);
  43. /*
  44. if (z_flip) {
  45. normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y));
  46. } else {
  47. normal.z = -0.5 + 0.5 * ((normal.x * normal.x) + (normal.y * normal.y));
  48. }
  49. */
  50. //normal.z = sqrt(1.0 - dot(normal.xy, normal.xy));
  51. //normal.xy *= 1.0 + normal.z;
  52. normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y));
  53. normal = normalize(normal);
  54. /*
  55. normal.z = 0.5;
  56. normal = normalize(normal);
  57. */
  58. if (!z_flip) {
  59. normal.z = -normal.z;
  60. }
  61. //normal = normalize(vec3( uv_interp * 2.0 - 1.0, 1.0 ));
  62. float depth = textureCube(source_cube, normal).r;
  63. // absolute values for direction cosines, bigger value equals closer to basis axis
  64. vec3 unorm = abs(normal);
  65. if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) {
  66. // x code
  67. unorm = normal.x > 0.0 ? vec3(1.0, 0.0, 0.0) : vec3(-1.0, 0.0, 0.0);
  68. } else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) {
  69. // y code
  70. unorm = normal.y > 0.0 ? vec3(0.0, 1.0, 0.0) : vec3(0.0, -1.0, 0.0);
  71. } else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) {
  72. // z code
  73. unorm = normal.z > 0.0 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 0.0, -1.0);
  74. } else {
  75. // oh-no we messed up code
  76. // has to be
  77. unorm = vec3(1.0, 0.0, 0.0);
  78. }
  79. float depth_fix = 1.0 / dot(normal, unorm);
  80. depth = 2.0 * depth - 1.0;
  81. float linear_depth = 2.0 * z_near * z_far / (z_far + z_near + depth * (z_far - z_near));
  82. gl_FragDepth = (z_far - (linear_depth * depth_fix + bias)) / z_far;
  83. }