worldFragment.fs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #version 430
  2. layout (location = 0) out vec3 worldPosition;
  3. layout (location = 1) out vec3 worldNormal;
  4. layout (location = 2) out vec4 worldColor;
  5. layout (location = 3) out float worldShadow;
  6. layout (binding = 0) uniform sampler2D samp;
  7. //layout (binding = 1) uniform sampler2DShadow shadowSamp;
  8. layout (binding = 1) uniform sampler2D shadowSamp;
  9. uniform mat4 proj;
  10. uniform mat4 view;
  11. uniform mat4 model;
  12. uniform mat4 projViewShadow;
  13. in vec3 varPosition;
  14. in vec2 varTex;
  15. in vec3 varNormal;
  16. in vec4 varShadow;
  17. uniform float radius;
  18. uniform float bias;
  19. const int sampleAmount = 64;
  20. const vec2 samples[64] = {
  21. vec2(-0.000, -0.000), vec2(-0.007, 0.004), vec2(0.016, 0.001), vec2(-0.015, -0.018),
  22. vec2(-0.024, -0.020), vec2(0.036, -0.014), vec2(-0.015, -0.045), vec2(0.037, 0.041),
  23. vec2(-0.036, -0.051), vec2(-0.069, 0.014), vec2(0.068, -0.039), vec2(0.086, -0.001),
  24. vec2(0.062, -0.070), vec2(-0.066, -0.077), vec2(-0.010, 0.109), vec2(-0.013, 0.117),
  25. vec2(-0.086, 0.091), vec2(0.019, 0.131), vec2(-0.051, 0.131), vec2(0.133, 0.067),
  26. vec2(0.154, -0.024), vec2(0.151, -0.063), vec2(0.124, 0.119), vec2(0.133, -0.121),
  27. vec2(0.149, 0.113), vec2(-0.188, -0.054), vec2(0.030, 0.201), vec2(0.002, 0.211),
  28. vec2(-0.063, -0.209), vec2(0.013, 0.226), vec2(-0.052, 0.229), vec2(-0.093, -0.223),
  29. vec2(0.153, 0.198), vec2(0.258, -0.005), vec2(-0.188, -0.188), vec2(0.267, -0.061),
  30. vec2(0.150, -0.238), vec2(0.027, 0.288), vec2(-0.293, 0.049), vec2(-0.044, 0.301),
  31. vec2(-0.018, 0.312), vec2(-0.296, -0.121), vec2(0.026, -0.327), vec2(-0.248, 0.226),
  32. vec2(-0.344, -0.008), vec2(-0.042, 0.349), vec2(-0.357, -0.038), vec2(-0.156, -0.332),
  33. vec2(-0.342, 0.154), vec2(0.031, 0.382), vec2(-0.331, 0.207), vec2(0.209, -0.339),
  34. vec2(-0.068, -0.401), vec2(-0.409, -0.063), vec2(-0.249, 0.340), vec2(-0.041, 0.428),
  35. vec2(0.326, 0.292), vec2(-0.127, -0.427), vec2(0.226, 0.393), vec2(0.424, 0.180),
  36. vec2(-0.063, 0.465), vec2(0.446, -0.167), vec2(-0.477, -0.084), vec2(-0.433, -0.234)
  37. };
  38. const vec3 light = vec3(-0.280166, -0.573576, -0.769751);
  39. void main(void) {
  40. worldPosition = varPosition;
  41. worldNormal = normalize(varNormal);
  42. vec3 pos = varShadow.xyz / varShadow.w;
  43. float fbias = bias * (1.0 - max(dot(worldNormal, -light), 0.0));
  44. float shadow = 0;
  45. for(int i = 0; i < sampleAmount; i++) {
  46. shadow += float(texture(shadowSamp, pos.xy + vec2(samples[i].x, samples[i].y) * radius).r + fbias > pos.z);
  47. }
  48. shadow /= sampleAmount;
  49. /*if(shadow < 0.5) {
  50. shadow = 4 * pow(shadow, 3);
  51. } else {
  52. //shadow = -2 * shadow * shadow + 4 * shadow - 1;
  53. shadow = 1 - exp(-40 * shadow - 0.693147180559945 + 20);
  54. }*/
  55. //shadow = 1 - exp(-40 * shadow - 0.693147180559945 + 20);
  56. //shadow = float(shadow > 0.05);
  57. worldShadow = shadow;
  58. //worldShadow = textureProj(shadowSamp, varShadow);
  59. //float f = 1.0 - max(dot(worldNormal, -light), 0.0);
  60. //worldColor = vec4(f, f, f, 1.0);
  61. worldColor = texture(samp, varTex);
  62. }