grass_pass2.frag 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifdef Use_Bindless_Texture
  2. layout(bindless_sampler) uniform sampler2D Albedo;
  3. layout(bindless_sampler) uniform sampler2D dtex;
  4. layout(bindless_sampler) uniform sampler2D SpecMap;
  5. #else
  6. uniform sampler2D Albedo;
  7. uniform sampler2D dtex;
  8. uniform sampler2D SpecMap;
  9. #endif
  10. in vec3 nor;
  11. in vec2 uv;
  12. out vec4 FragColor;
  13. vec3 getLightFactor(vec3 diffuseMatColor, vec3 specularMatColor, float specMapValue, float emitMapValue);
  14. void main(void)
  15. {
  16. vec4 color = texture(Albedo, uv);
  17. #ifdef Use_Bindless_Texture
  18. #ifdef SRGBBindlessFix
  19. color.xyz = pow(color.xyz, vec3(2.2));
  20. #endif
  21. #endif
  22. if (color.a < 0.5) discard;
  23. vec2 texc = gl_FragCoord.xy / screen;
  24. float z = texture(dtex, texc).x;
  25. vec4 xpos = 2.0 * vec4(texc, z, 1.0) - 1.0f;
  26. xpos = InverseProjectionMatrix * xpos;
  27. xpos /= xpos.w;
  28. vec3 eyedir = normalize(xpos.xyz);
  29. // Inspired from http://http.developer.nvidia.com/GPUGems3/gpugems3_ch16.html
  30. vec3 L = normalize((transpose(InverseViewMatrix) * vec4(sun_direction, 0.)).xyz);
  31. float fEdotL = clamp(dot(L, eyedir), 0., 1.);
  32. float fPowEdotL = pow(fEdotL, 4.);
  33. float fLdotNBack = max(0., - dot(nor, L) * 0.6 + 0.4);
  34. float scattering = mix(fPowEdotL, fLdotNBack, .5);
  35. float specmap = texture(SpecMap, uv).g;
  36. vec3 LightFactor = color.xyz * (scattering * 0.3) + getLightFactor(color.xyz, vec3(1.), specmap, 0.);
  37. FragColor = vec4(color.xyz * LightFactor, 1.);
  38. }