pbrPreLightingFunctions.fx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Pre Light Computing
  2. struct preLightingInfo
  3. {
  4. // Pre Falloff Info
  5. vec3 lightOffset;
  6. float lightDistanceSquared;
  7. float lightDistance;
  8. // Falloff Info
  9. float attenuation;
  10. // Lighting Info
  11. vec3 L;
  12. vec3 H;
  13. float NdotV;
  14. float NdotL;
  15. float VdotH;
  16. float roughness;
  17. };
  18. preLightingInfo computePointAndSpotPreLightingInfo(vec4 lightData, vec3 V, vec3 N) {
  19. preLightingInfo result;
  20. // Attenuation data.
  21. result.lightOffset = lightData.xyz - vPositionW;
  22. result.lightDistanceSquared = dot(result.lightOffset, result.lightOffset);
  23. // Roughness.
  24. result.lightDistance = sqrt(result.lightDistanceSquared);
  25. // Geometry Data.
  26. result.L = normalize(result.lightOffset);
  27. result.H = normalize(V + result.L);
  28. result.NdotL = clamp(dot(N, result.L), 0.000000000001, 1.0);
  29. result.VdotH = clamp(dot(V, result.H), 0.0, 1.0);
  30. return result;
  31. }
  32. preLightingInfo computeDirectionalPreLightingInfo(vec4 lightData, vec3 V, vec3 N) {
  33. preLightingInfo result;
  34. // Roughness
  35. result.lightDistance = length(-lightData.xyz);
  36. // Geometry Data.
  37. result.L = normalize(-lightData.xyz);
  38. result.H = normalize(V + result.L);
  39. result.NdotL = clamp(dot(N, result.L), 0.00000000001, 1.0);
  40. result.VdotH = clamp(dot(V, result.H), 0.0, 1.0);
  41. return result;
  42. }
  43. preLightingInfo computeHemisphericPreLightingInfo(vec4 lightData, vec3 V, vec3 N) {
  44. preLightingInfo result;
  45. // Geometry Data.
  46. result.NdotL = dot(N, lightData.xyz) * 0.5 + 0.5;
  47. result.NdotL = clamp(result.NdotL, 0.000000000001, 1.0);
  48. #ifdef SPECULARTERM
  49. result.L = normalize(lightData.xyz);
  50. result.H = normalize(V + result.L);
  51. result.VdotH = clamp(dot(V, result.H), 0.0, 1.0);
  52. #endif
  53. return result;
  54. }