instanced_grass_pass2.frag 1.5 KB

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