light.glsl 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* light.glsl -- Contains everything you need to manage lights
  2. *
  3. * Copyright (c) 2025 Le Juez Victor
  4. *
  5. * This software is provided 'as-is', without any express or implied warranty.
  6. * For conditions of distribution and use, see accompanying LICENSE file.
  7. */
  8. #include "./math.glsl"
  9. #include "./pbr.glsl"
  10. /* === Defines === */
  11. #define LIGHT_FORWARD_COUNT 8
  12. #define LIGHT_DIR 0
  13. #define LIGHT_SPOT 1
  14. #define LIGHT_OMNI 2
  15. /* === Functions === */
  16. vec3 L_Diffuse(float cLdotH, float cNdotV, float cNdotL, float roughness)
  17. {
  18. float FD90_minus_1 = 2.0 * cLdotH * cLdotH * roughness - 0.5;
  19. float FdV = 1.0 + FD90_minus_1 * PBR_SchlickFresnel(cNdotV);
  20. float FdL = 1.0 + FD90_minus_1 * PBR_SchlickFresnel(cNdotL);
  21. return vec3(M_INV_PI * (FdV * FdL * cNdotL)); // Diffuse BRDF (Burley)
  22. }
  23. vec3 L_Specular(vec3 F0, float cLdotH, float cNdotH, float cNdotV, float cNdotL, float roughness)
  24. {
  25. roughness = max(roughness, 1e-3);
  26. float alphaGGX = roughness * roughness;
  27. float D = PBR_DistributionGGX(cNdotH, alphaGGX);
  28. float G = PBR_GeometryGGX(cNdotL, cNdotV, alphaGGX);
  29. float cLdotH5 = PBR_SchlickFresnel(cLdotH);
  30. float F90 = clamp(50.0 * F0.g, 0.0, 1.0);
  31. vec3 F = F0 + (F90 - F0) * cLdotH5;
  32. return cNdotL * D * F * G; // Specular BRDF (Schlick GGX)
  33. }