fragment.fs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir) {
  17. float height = texture(bumpSamp, texCoords, 0.0).r;
  18. vec2 p = viewDir.xy / viewDir.z * (height * heightScale);
  19. return texCoords - p;
  20. }
  21. void main(void) {
  22. vec2 tex = varTextureG + vec2(0.0, height);
  23. vec2 eye = normalize(viewPos - varPositionG).xy * heightScale;
  24. // kajetans variant
  25. if(kajetan) {
  26. eye *= texture(bumpSamp, tex).x;
  27. tex += -eye;
  28. } else {
  29. // lecture
  30. float h = 1.0;
  31. float prev_hits = 0;
  32. float hit_h = 0;
  33. vec2 mTex = tex;
  34. float step = 1.0 / steps;
  35. vec2 stepEye = eye / steps;
  36. for (int it = 0; it < steps; it++) {
  37. h -= step;
  38. mTex += stepEye;
  39. float h_tex = texture(bumpSamp, mTex).x;
  40. float is_first_hit = float((h_tex - h - prev_hits) > 0.0);
  41. hit_h += is_first_hit * h;
  42. prev_hits += is_first_hit;
  43. }
  44. hit_h = max(0.0, hit_h);
  45. h = hit_h;
  46. prev_hits = 0;
  47. mTex = tex + stepEye * hit_h;
  48. hit_h = 0;
  49. step /= fineSteps;
  50. stepEye = eye / fineSteps;
  51. for (int it = 0; it < fineSteps; it++) {
  52. h -= step;
  53. mTex += stepEye;
  54. float h_tex = texture(bumpSamp, mTex).x;
  55. float is_first_hit = float((h_tex - h - prev_hits) > 0.0);
  56. hit_h += is_first_hit * h;
  57. prev_hits += is_first_hit;
  58. }
  59. tex += eye * hit_h;
  60. }
  61. vec3 normal = texture(normalSamp, tex).xyz;
  62. normal = normalize(normal * 2.0 - 1.0);
  63. float l = max(dot(-light, normal), 0.0) * 0.7 + 0.3;
  64. color = vec4(texture(textureSamp, tex).xyz * l, 1.0);
  65. }