暂无描述

deferred.frag 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 samplerposition;
  5. layout (binding = 2) uniform sampler2D samplerNormal;
  6. layout (binding = 3) uniform sampler2D samplerAlbedo;
  7. layout (location = 0) in vec2 inUV;
  8. layout (location = 0) out vec4 outFragcolor;
  9. struct Light {
  10. vec4 position;
  11. vec3 color;
  12. float radius;
  13. };
  14. layout (binding = 4) uniform UBO
  15. {
  16. Light lights[6];
  17. vec4 viewPos;
  18. } ubo;
  19. void main()
  20. {
  21. // Get G-Buffer values
  22. vec3 fragPos = texture(samplerposition, inUV).rgb;
  23. vec3 normal = texture(samplerNormal, inUV).rgb;
  24. vec4 albedo = texture(samplerAlbedo, inUV);
  25. #define lightCount 6
  26. #define ambient 0.0
  27. // Ambient part
  28. vec3 fragcolor = albedo.rgb * ambient;
  29. for(int i = 0; i < lightCount; ++i)
  30. {
  31. // Vector to light
  32. vec3 L = ubo.lights[i].position.xyz - fragPos;
  33. // Distance from light to fragment position
  34. float dist = length(L);
  35. // Viewer to fragment
  36. vec3 V = ubo.viewPos.xyz - fragPos;
  37. V = normalize(V);
  38. //if(dist < ubo.lights[i].radius)
  39. {
  40. // Light to fragment
  41. L = normalize(L);
  42. // Attenuation
  43. float atten = ubo.lights[i].radius / (pow(dist, 2.0) + 1.0);
  44. // Diffuse part
  45. vec3 N = normalize(normal);
  46. float NdotL = max(0.0, dot(N, L));
  47. vec3 diff = ubo.lights[i].color * albedo.rgb * NdotL * atten;
  48. // Specular part
  49. // Specular map values are stored in alpha of albedo mrt
  50. vec3 R = reflect(-L, N);
  51. float NdotR = max(0.0, dot(R, V));
  52. vec3 spec = ubo.lights[i].color * albedo.a * pow(NdotR, 16.0) * atten;
  53. fragcolor += diff + spec;
  54. }
  55. }
  56. outFragcolor = vec4(fragcolor, 1.0);
  57. }