cubes.fs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #version 430
  2. layout (binding = 0) uniform sampler3D noiseSamp;
  3. layout (binding = 1) uniform sampler2D textureSamp;
  4. layout (binding = 2) uniform sampler2D bumpSamp;
  5. layout (binding = 3) uniform sampler2D normalSamp;
  6. layout (binding = 4) uniform sampler2D depthSamp;
  7. layout (binding = 5) uniform sampler2D depthSamp2;
  8. uniform float height;
  9. uniform vec3 viewPos;
  10. uniform int steps;
  11. uniform int fineSteps;
  12. uniform float heightScale;
  13. uniform bool kajetan;
  14. in vec3 varPositionG;
  15. in vec4 varShadowPositionG;
  16. in vec2 varTextureG;
  17. out vec4 color;
  18. const vec3 light = vec3(-0.746, -0.373, -0.224);
  19. const float minVariance = 1.84582e-7;
  20. const float cutoff = 0.9999;
  21. float chebyshevUpperBound(vec2 moments, float t) {
  22. float variance = moments.y - (moments.x * moments.x);
  23. variance = max(variance, minVariance);
  24. float d = t - moments.x;
  25. float pMax = variance / (variance + d * d);
  26. return max(pMax, float(t < moments.x));
  27. }
  28. float linstep(float from, float to, float v) {
  29. return clamp((v - from) / (to - from), 0.0, 1.0);
  30. }
  31. float reduceLightBleeding(float light, float Amount) {
  32. return linstep(Amount, 1.0, light);
  33. }
  34. float getShadow(vec2 varTex, float distanceToLight) {
  35. vec2 Moments = texture(depthSamp, varTex).xy;
  36. return reduceLightBleeding(chebyshevUpperBound(Moments, distanceToLight), cutoff);
  37. }
  38. void main(void) {
  39. vec2 tex = varTextureG + vec2(0.0, height);
  40. vec2 eye = -normalize(viewPos - varPositionG).xy * heightScale;
  41. // kajetans variant
  42. if(kajetan) {
  43. eye *= texture(bumpSamp, tex).x;
  44. tex += eye;
  45. } else {
  46. // lecture
  47. float h = 1.0;
  48. float prev_hits = 0;
  49. float hit_h = 0;
  50. vec2 mTex = tex;
  51. float step = 1.0 / steps;
  52. vec2 stepEye = eye / steps;
  53. for (int it = 0; it < steps; it++) {
  54. h -= step;
  55. mTex += stepEye;
  56. float h_tex = texture(bumpSamp, mTex).x;
  57. float is_first_hit = float((h_tex - h - prev_hits) > 0.0);
  58. hit_h += is_first_hit * h;
  59. prev_hits += is_first_hit;
  60. }
  61. hit_h = max(0.0, hit_h - step);
  62. h = hit_h;
  63. prev_hits = 0;
  64. mTex = tex + stepEye * hit_h;
  65. hit_h = 0;
  66. step /= fineSteps;
  67. stepEye = eye / fineSteps;
  68. for (int it = 0; it < fineSteps; it++) {
  69. h -= step;
  70. mTex += stepEye;
  71. float h_tex = texture(bumpSamp, mTex).x;
  72. float is_first_hit = float((h_tex - h - prev_hits) > 0.0);
  73. hit_h += is_first_hit * h;
  74. prev_hits += is_first_hit;
  75. }
  76. tex += eye * hit_h;
  77. }
  78. vec3 normal = texture(normalSamp, tex).xyz;
  79. normal = normalize(normal * 2.0 - 1.0);
  80. float l = max(dot(-light, normal), 0.0) * 0.7 + 0.3;
  81. color = vec4(texture(textureSamp, tex).xyz * l, 1.0);
  82. vec3 pos = varShadowPositionG.xyz / varShadowPositionG.w;
  83. float shadow = getShadow(pos.xy, pos.z);
  84. //float shadow = float(texture(depthSamp2, pos.xy).r + 0.00001 > pos.z);
  85. shadow = shadow * 0.6 + 0.4;
  86. color *= shadow;
  87. }