cube_to_dp.glsl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* clang-format off */
  2. [vertex]
  3. layout(location = 0) in highp vec4 vertex_attrib;
  4. /* clang-format on */
  5. layout(location = 4) in vec2 uv_in;
  6. out vec2 uv_interp;
  7. void main() {
  8. uv_interp = uv_in;
  9. gl_Position = vertex_attrib;
  10. }
  11. /* clang-format off */
  12. [fragment]
  13. uniform highp samplerCube source_cube; //texunit:0
  14. /* clang-format on */
  15. in vec2 uv_interp;
  16. uniform bool z_flip;
  17. uniform highp float z_far;
  18. uniform highp float z_near;
  19. uniform highp float bias;
  20. void main() {
  21. highp vec3 normal = vec3(uv_interp * 2.0 - 1.0, 0.0);
  22. /*
  23. if (z_flip) {
  24. normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y));
  25. } else {
  26. normal.z = -0.5 + 0.5 * ((normal.x * normal.x) + (normal.y * normal.y));
  27. }
  28. */
  29. //normal.z = sqrt(1.0 - dot(normal.xy, normal.xy));
  30. //normal.xy *= 1.0 + normal.z;
  31. normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y));
  32. normal = normalize(normal);
  33. /*
  34. normal.z = 0.5;
  35. normal = normalize(normal);
  36. */
  37. if (!z_flip) {
  38. normal.z = -normal.z;
  39. }
  40. //normal = normalize(vec3(uv_interp * 2.0 - 1.0, 1.0));
  41. float depth = texture(source_cube, normal).r;
  42. // absolute values for direction cosines, bigger value equals closer to basis axis
  43. vec3 unorm = abs(normal);
  44. if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) {
  45. // x code
  46. unorm = normal.x > 0.0 ? vec3(1.0, 0.0, 0.0) : vec3(-1.0, 0.0, 0.0);
  47. } else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) {
  48. // y code
  49. unorm = normal.y > 0.0 ? vec3(0.0, 1.0, 0.0) : vec3(0.0, -1.0, 0.0);
  50. } else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) {
  51. // z code
  52. unorm = normal.z > 0.0 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 0.0, -1.0);
  53. } else {
  54. // oh-no we messed up code
  55. // has to be
  56. unorm = vec3(1.0, 0.0, 0.0);
  57. }
  58. float depth_fix = 1.0 / dot(normal, unorm);
  59. depth = 2.0 * depth - 1.0;
  60. float linear_depth = 2.0 * z_near * z_far / (z_far + z_near - depth * (z_far - z_near));
  61. gl_FragDepth = (linear_depth * depth_fix + bias) / z_far;
  62. }