pbr.glsl 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* pbr.glsl -- Contains everything you need for PBR
  2. *
  3. * Copyright (c) 2025-2026 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. /* === Functions === */
  10. float PBR_DistributionGGX(float cosTheta, float alpha)
  11. {
  12. // Standard GGX/Trowbridge-Reitz distribution - optimized form
  13. float a = cosTheta * alpha;
  14. float k = alpha / (1.0 - cosTheta * cosTheta + a * a);
  15. return k * k * (1.0 / M_PI);
  16. }
  17. float PBR_GeometryGGX(float NdotL, float NdotV, float roughness)
  18. {
  19. // Hammon's optimized approximation for GGX Smith geometry term
  20. // This version is an efficient approximation that:
  21. // 1. Avoids expensive square root calculations
  22. // 2. Combines both G1 terms into a single expression
  23. // 3. Provides very close results to the exact version at a much lower cost
  24. // SEE: https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX
  25. return 0.5 / mix(2.0 * NdotL * NdotV, NdotL + NdotV, roughness);
  26. }
  27. float PBR_SchlickFresnel(float u)
  28. {
  29. float m = 1.0 - u;
  30. float m2 = m * m;
  31. return m2 * m2 * m; // pow(m,5)
  32. }
  33. vec3 PBR_ComputeF0(float metallic, float specular, vec3 albedo)
  34. {
  35. // use (albedo * metallic) as colored specular reflectance at 0 angle for metallic materials
  36. // SEE: https://google.github.io/filament/Filament.md.html
  37. float dielectric = 0.16 * specular * specular;
  38. return mix(vec3(dielectric), albedo, vec3(metallic));
  39. }