cube_to_dp.glsl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. attribute highp vec4 vertex_attrib; // attrib:0
  12. /* clang-format on */
  13. attribute vec2 uv_in; // attrib:4
  14. varying 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. precision mediump float;
  27. precision mediump int;
  28. #endif
  29. uniform highp samplerCube source_cube; //texunit:0
  30. /* clang-format on */
  31. varying vec2 uv_interp;
  32. uniform bool z_flip;
  33. uniform highp float z_far;
  34. uniform highp float z_near;
  35. uniform highp float bias;
  36. void main() {
  37. highp vec3 normal = vec3(uv_interp * 2.0 - 1.0, 0.0);
  38. /*
  39. if (z_flip) {
  40. normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y));
  41. } else {
  42. normal.z = -0.5 + 0.5 * ((normal.x * normal.x) + (normal.y * normal.y));
  43. }
  44. */
  45. //normal.z = sqrt(1.0 - dot(normal.xy, normal.xy));
  46. //normal.xy *= 1.0 + normal.z;
  47. normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y));
  48. normal = normalize(normal);
  49. /*
  50. normal.z = 0.5;
  51. normal = normalize(normal);
  52. */
  53. if (!z_flip) {
  54. normal.z = -normal.z;
  55. }
  56. //normal = normalize(vec3( uv_interp * 2.0 - 1.0, 1.0 ));
  57. float depth = textureCube(source_cube, normal).r;
  58. // absolute values for direction cosines, bigger value equals closer to basis axis
  59. vec3 unorm = abs(normal);
  60. if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) {
  61. // x code
  62. unorm = normal.x > 0.0 ? vec3(1.0, 0.0, 0.0) : vec3(-1.0, 0.0, 0.0);
  63. } else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) {
  64. // y code
  65. unorm = normal.y > 0.0 ? vec3(0.0, 1.0, 0.0) : vec3(0.0, -1.0, 0.0);
  66. } else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) {
  67. // z code
  68. unorm = normal.z > 0.0 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 0.0, -1.0);
  69. } else {
  70. // oh-no we messed up code
  71. // has to be
  72. unorm = vec3(1.0, 0.0, 0.0);
  73. }
  74. float depth_fix = 1.0 / dot(normal, unorm);
  75. depth = 2.0 * depth - 1.0;
  76. float linear_depth = 2.0 * z_near * z_far / (z_far + z_near - depth * (z_far - z_near));
  77. gl_FragDepth = (linear_depth * depth_fix + bias) / z_far;
  78. }