StandardPBR_LightingBrdf.azsli 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. // This #define magic lets you use the GetDiffuseLighting & GetSpecularLighting functions in this file without making them the final functions
  10. // used in your shader. Simply #define GetDiffuseLighting & GetSpecularLighting to your custom definition before including this file
  11. //
  12. #ifndef GetDiffuseLighting
  13. #define GetDiffuseLighting(surface, lightingData, lightIntensity, dirToLight) GetDiffuseLighting_StandardPBR(surface, lightingData, lightIntensity, dirToLight)
  14. #endif
  15. #ifndef GetSpecularLighting
  16. #define GetSpecularLighting(surface, lightingData, lightIntensity, dirToLight, viewIndex) GetSpecularLighting_StandardPBR(surface, lightingData, lightIntensity, dirToLight, viewIndex)
  17. #endif
  18. #include <Atom/Features/PBR/Microfacet/Brdf.azsli>
  19. #include <Atom/Features/PBR/LightingOptions.azsli>
  20. #ifndef LightingData
  21. #error LightingData must be defined before including this file.
  22. #endif
  23. #ifndef Surface
  24. #error Surface must be defined before including this file.
  25. #endif
  26. // Then define the Diffuse and Specular lighting functions
  27. real3 GetDiffuseLighting_StandardPBR(Surface surface, LightingData lightingData, real3 lightIntensity, real3 dirToLight)
  28. {
  29. real3 diffuse = DiffuseLambertian(surface.albedo, surface.GetDiffuseNormal(), dirToLight, lightingData.diffuseResponse);
  30. #if ENABLE_CLEAR_COAT
  31. if(o_clearCoat_feature_enabled)
  32. {
  33. // Attenuate diffuse term by clear coat's fresnel term to account for energy loss
  34. real HdotV = saturate(dot(normalize(dirToLight + lightingData.dirToCamera[0]), lightingData.dirToCamera[0]));
  35. diffuse *= 1.0 - (FresnelSchlick(HdotV, 0.04) * surface.clearCoat.factor);
  36. }
  37. #endif
  38. diffuse *= lightIntensity;
  39. return diffuse;
  40. }
  41. real3 GetSpecularLighting_StandardPBR(Surface surface, LightingData lightingData, const real3 lightIntensity, const real3 dirToLight, uint viewIndex)
  42. {
  43. #if ENABLE_MOBILEBRDF
  44. real3 specular = SpecularGGXMobile(lightingData.dirToCamera[viewIndex], dirToLight, surface.GetSpecularNormal(), surface.GetSpecularF0(), surface.roughnessA2, surface.roughnessA, surface.roughnessLinear);
  45. #else
  46. real3 specular = SpecularGGX(lightingData.dirToCamera[viewIndex], dirToLight, surface.GetSpecularNormal(), surface.GetSpecularF0(), lightingData.GetSpecularNdotV(viewIndex), surface.roughnessA2, lightingData.multiScatterCompensation);
  47. #endif
  48. #if ENABLE_CLEAR_COAT
  49. if(o_clearCoat_feature_enabled)
  50. {
  51. specular = ClearCoatSpecular(dirToLight, lightingData.dirToCamera[viewIndex], surface.clearCoat.normal, surface.clearCoat.factor, surface.clearCoat.roughness, specular);
  52. }
  53. #endif
  54. specular *= lightIntensity;
  55. return specular;
  56. }