cubes.fs 2.2 KB

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