fragment.fs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. uniform float height;
  7. uniform vec3 viewPos;
  8. uniform int steps;
  9. uniform int fineSteps;
  10. uniform float heightScale;
  11. uniform bool kajetan;
  12. in vec3 varPositionG;
  13. in vec2 varTextureG;
  14. out vec4 color;
  15. const vec3 light = vec3(-0.746, -0.373, -0.224);
  16. void main(void) {
  17. vec2 tex = varTextureG + vec2(0.0, height);
  18. vec2 eye = -normalize(viewPos - varPositionG).xy * heightScale;
  19. // kajetans variant
  20. if(kajetan) {
  21. eye *= texture(bumpSamp, tex).x;
  22. tex += eye;
  23. } else {
  24. // lecture
  25. float h = 1.0;
  26. float prev_hits = 0;
  27. float hit_h = 0;
  28. vec2 mTex = tex;
  29. float step = 1.0 / steps;
  30. vec2 stepEye = eye / steps;
  31. for (int it = 0; it < steps; it++) {
  32. h -= step;
  33. mTex += stepEye;
  34. float h_tex = texture(bumpSamp, mTex).x;
  35. float is_first_hit = float((h_tex - h - prev_hits) > 0.0);
  36. hit_h += is_first_hit * h;
  37. prev_hits += is_first_hit;
  38. }
  39. hit_h = max(0.0, hit_h - step);
  40. h = hit_h;
  41. prev_hits = 0;
  42. mTex = tex + stepEye * hit_h;
  43. hit_h = 0;
  44. step /= fineSteps;
  45. stepEye = eye / fineSteps;
  46. for (int it = 0; it < fineSteps; it++) {
  47. h -= step;
  48. mTex += stepEye;
  49. float h_tex = texture(bumpSamp, mTex).x;
  50. float is_first_hit = float((h_tex - h - prev_hits) > 0.0);
  51. hit_h += is_first_hit * h;
  52. prev_hits += is_first_hit;
  53. }
  54. tex += eye * hit_h;
  55. }
  56. vec3 normal = texture(normalSamp, tex).xyz;
  57. normal = normalize(normal * 2.0 - 1.0);
  58. float l = max(dot(-light, normal), 0.0) * 0.7 + 0.3;
  59. color = vec4(texture(textureSamp, tex).xyz * l, 1.0);
  60. }