Sslr.glsl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. // SSLR functions and data
  6. #include "shaders/Common.glsl"
  7. #include "shaders/Functions.glsl"
  8. #include "shaders/Pack.glsl"
  9. const float ONE = 0.9;
  10. // Returns the Z of the position in view space
  11. float readZ(in vec2 uv)
  12. {
  13. float depth = textureLod(u_depthRt, uv, 1.0).r;
  14. float z = u_projectionParams.z / (u_projectionParams.w + depth);
  15. return z;
  16. }
  17. vec2 projectXy(in vec3 p)
  18. {
  19. vec4 a = u_projectionMat * vec4(p, 1.0);
  20. return a.xy / a.w;
  21. }
  22. vec3 doSslr(in vec3 r, in vec3 posVSpace, in vec2 uv, out float contribution)
  23. {
  24. contribution = 0.5;
  25. return vec3(1.0, 0.0, 1.0);
  26. #if 0
  27. vec3 color = vec3(0.0);
  28. vec3 p0 = posVSpace;
  29. contribution = 1.0;
  30. // Let p1 be the intersection of p0+r to the near plane, then
  31. // p1 = p0 + t*r or
  32. // p1.x = p0.x + t*r.x (1)
  33. // p1.y = p0.y + t*r.y (2) and
  34. // p1.z = p0.z + t*r.z (3)
  35. // p1.z is known to be something ~0.0 so if we solve (3) t becomes:
  36. float t = -p0.z / (r.z + 0.0000001);
  37. vec3 p1 = p0 + r * t;
  38. vec2 pp0 = uv * 2.0 - 1.0;
  39. vec2 pp1 = projectXy(p1);
  40. // Calculate the ray from p0 to p1 in 2D space and get the number of
  41. // steps
  42. vec2 dir = pp1 - pp0;
  43. vec2 path = dir * 0.5; // (pp1/2+1/2)-(pp0.xy/2+1/2)
  44. path *= vec2(float(WIDTH), float(HEIGHT));
  45. path = abs(path);
  46. float steps = max(path.x, path.y);
  47. // Calculate the step increase
  48. float len = length(dir);
  49. float stepInc = len / steps;
  50. dir /= len; // Normalize dir at last
  51. steps = min(steps, 300.0);
  52. for(float i = 0.0; i < steps; i += 1.0)
  53. {
  54. vec2 ndc = pp0 + dir * (i * stepInc);
  55. // Check if it's out of the view
  56. vec2 comp = abs(ndc);
  57. if(comp.x > ONE || comp.y > ONE)
  58. {
  59. //color = vec3(1, 0.0, 1);
  60. return color;
  61. }
  62. // 'a' is ray that passes through the eye and into ndc
  63. vec3 a;
  64. a.z = -1.0;
  65. a.xy = ndc * u_projectionParams.xy * a.z; // Unproject
  66. a = normalize(a);
  67. // Compute the intersection between 'a' (before normalization) and r 'k' is the value to multiply to 'a' to get
  68. // the intersection
  69. // c0 = cross(a, r);
  70. // c1 = cross(p0, r);
  71. // k = c1.x / c0.x; and the optimized:
  72. vec2 tmpv2 = a.yz * r.zy;
  73. float c0x = tmpv2.x - tmpv2.y;
  74. tmpv2 = p0.yz * r.zy;
  75. float c1x = tmpv2.x - tmpv2.y;
  76. float k = c1x / c0x;
  77. float intersectionZ = a.z * k; // intersectionXYZ = a * k;
  78. vec2 texCoord = ndc * 0.5 + 0.5;
  79. float depth = readZ(u_depthRt, texCoord);
  80. float diffDepth = depth - intersectionZ;
  81. if(diffDepth > 0.0)
  82. {
  83. if(diffDepth > 0.7)
  84. {
  85. return;
  86. }
  87. float factor = sin(length(ndc) * PI);
  88. factor *= 1.0 - length(pp0);
  89. //factor *= specColor;
  90. color = textureLod(u_isRt, texCoord, 0.0).rgb * factor;
  91. //color = vec3(1.0, 0.0, 1.0);
  92. //color = vec3(1.0 - abs(pp0.xy), 0.0);
  93. return color;
  94. }
  95. }
  96. return color;
  97. #endif
  98. }