pointlight.frag 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. uniform sampler2D ntex;
  2. uniform sampler2D dtex;
  3. flat in vec3 center;
  4. flat in float energy;
  5. flat in vec3 col;
  6. flat in float radius;
  7. out vec4 Diff;
  8. out vec4 Spec;
  9. vec3 DecodeNormal(vec2 n);
  10. vec3 SpecularBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
  11. vec3 DiffuseBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
  12. vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
  13. void main()
  14. {
  15. vec2 texc = gl_FragCoord.xy / screen;
  16. float z = texture(dtex, texc).x;
  17. vec3 norm = normalize(DecodeNormal(2. * texture(ntex, texc).xy - 1.));
  18. float roughness = texture(ntex, texc).z;
  19. vec4 xpos = getPosFromUVDepth(vec3(texc, z), InverseProjectionMatrix);
  20. vec3 eyedir = -normalize(xpos.xyz);
  21. vec4 pseudocenter = ViewMatrix * vec4(center.xyz, 1.0);
  22. pseudocenter /= pseudocenter.w;
  23. vec3 light_pos = pseudocenter.xyz;
  24. vec3 light_col = col.xyz;
  25. float d = distance(light_pos, xpos.xyz);
  26. float att = energy * 20. / (1. + d * d);
  27. att *= (radius - d) / radius;
  28. if (att <= 0.) discard;
  29. // Light Direction
  30. vec3 L = -normalize(xpos.xyz - light_pos);
  31. float NdotL = clamp(dot(norm, L), 0., 1.);
  32. vec3 Specular = SpecularBRDF(norm, eyedir, L, vec3(1.), roughness);
  33. vec3 Diffuse = DiffuseBRDF(norm, eyedir, L, vec3(1.), roughness);
  34. Diff = vec4(Diffuse * NdotL * light_col * att, 1.);
  35. Spec = vec4(Specular * NdotL * light_col * att, 1.);
  36. }