Nenhuma descrição

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #version 450
  2. #extension GL_ARB_separate_shader_objects : enable
  3. #extension GL_ARB_shading_language_420pack : enable
  4. layout (binding = 1) uniform sampler2D shadowMap;
  5. layout (location = 0) in vec3 inNormal;
  6. layout (location = 1) in vec3 inColor;
  7. layout (location = 2) in vec3 inViewVec;
  8. layout (location = 3) in vec3 inLightVec;
  9. layout (location = 4) in vec4 inShadowCoord;
  10. layout (constant_id = 0) const int enablePCF = 0;
  11. layout (location = 0) out vec4 outFragColor;
  12. #define ambient 0.1
  13. float textureProj(vec4 P, vec2 off)
  14. {
  15. float shadow = 1.0;
  16. vec4 shadowCoord = P / P.w;
  17. if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 )
  18. {
  19. float dist = texture( shadowMap, shadowCoord.st + off ).r;
  20. if ( shadowCoord.w > 0.0 && dist < shadowCoord.z )
  21. {
  22. shadow = ambient;
  23. }
  24. }
  25. return shadow;
  26. }
  27. float filterPCF(vec4 sc)
  28. {
  29. ivec2 texDim = textureSize(shadowMap, 0);
  30. float scale = 1.5;
  31. float dx = scale * 1.0 / float(texDim.x);
  32. float dy = scale * 1.0 / float(texDim.y);
  33. float shadowFactor = 0.0;
  34. int count = 0;
  35. int range = 1;
  36. for (int x = -range; x <= range; x++)
  37. {
  38. for (int y = -range; y <= range; y++)
  39. {
  40. shadowFactor += textureProj(sc, vec2(dx*x, dy*y));
  41. count++;
  42. }
  43. }
  44. return shadowFactor / count;
  45. }
  46. void main()
  47. {
  48. float shadow = (enablePCF == 1) ? filterPCF(inShadowCoord / inShadowCoord.w) : textureProj(inShadowCoord / inShadowCoord.w, vec2(0.0));
  49. vec3 N = normalize(inNormal);
  50. vec3 L = normalize(inLightVec);
  51. vec3 V = normalize(inViewVec);
  52. vec3 R = normalize(-reflect(L, N));
  53. vec3 diffuse = max(dot(N, L), ambient) * inColor;
  54. // vec3 specular = pow(max(dot(R, V), 0.0), 50.0) * vec3(0.75);
  55. outFragColor = vec4(diffuse * shadow, 1.0);
  56. }