sunlightshadowesm.frag 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. uniform sampler2D ntex;
  2. uniform sampler2D dtex;
  3. uniform sampler2DArray shadowtex;
  4. uniform float split0;
  5. uniform float split1;
  6. uniform float split2;
  7. uniform float splitmax;
  8. in vec2 uv;
  9. out vec4 Diff;
  10. out vec4 Spec;
  11. vec3 DecodeNormal(vec2 n);
  12. vec3 SpecularBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
  13. vec3 DiffuseBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
  14. vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
  15. vec3 SunMRP(vec3 normal, vec3 eyedir);
  16. float getShadowFactor(vec3 pos, int index)
  17. {
  18. vec4 shadowcoord = (ShadowViewProjMatrixes[index] * InverseViewMatrix * vec4(pos, 1.0));
  19. shadowcoord.xy /= shadowcoord.w;
  20. vec2 shadowtexcoord = shadowcoord.xy * 0.5 + 0.5;
  21. float z = texture(shadowtex, vec3(shadowtexcoord, float(index))).x;
  22. float d = shadowcoord.z;
  23. return min(pow(exp(-32. * d) * z, 8.), 1.);
  24. }
  25. void main() {
  26. vec2 uv = gl_FragCoord.xy / screen;
  27. float z = texture(dtex, uv).x;
  28. vec4 xpos = getPosFromUVDepth(vec3(uv, z), InverseProjectionMatrix);
  29. vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
  30. float roughness =texture(ntex, uv).z;
  31. vec3 eyedir = -normalize(xpos.xyz);
  32. vec3 Lightdir = SunMRP(norm, eyedir);
  33. float NdotL = clamp(dot(norm, Lightdir), 0., 1.);
  34. vec3 Specular = SpecularBRDF(norm, eyedir, Lightdir, vec3(1.), roughness);
  35. vec3 Diffuse = DiffuseBRDF(norm, eyedir, Lightdir, vec3(1.), roughness);
  36. // Shadows
  37. float factor;
  38. if (xpos.z < split0)
  39. factor = getShadowFactor(xpos.xyz, 0);
  40. else if (xpos.z < split1)
  41. factor = getShadowFactor(xpos.xyz, 1);
  42. else if (xpos.z < split2)
  43. factor = getShadowFactor(xpos.xyz, 2);
  44. else if (xpos.z < splitmax)
  45. factor = getShadowFactor(xpos.xyz, 3);
  46. else
  47. factor = 1.;
  48. Diff = vec4(factor * NdotL * Diffuse * sun_col, 1.);
  49. Spec = vec4(factor * NdotL * Specular * sun_col, 1.);
  50. }