Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SixWayLighting.hlsl 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #ifndef UNITY_SIX_WAY_COMMON_INCLUDED
  2. #define UNITY_SIX_WAY_COMMON_INCLUDED
  3. #define ABSORPTION_EPSILON max(REAL_MIN, 1e-5)
  4. real3 ComputeDensityScales(real3 absorptionColor)
  5. {
  6. absorptionColor.rgb = max(ABSORPTION_EPSILON, absorptionColor.rgb);
  7. // Empirical value used to parametrize absorption from color
  8. const real absorptionStrength = 0.2f;
  9. return 1.0f + log2(absorptionColor.rgb) / log2(absorptionStrength);
  10. }
  11. real3 GetTransmissionWithAbsorption(real transmission, real3 densityScales, real absorptionRange)
  12. {
  13. // Recompute transmission based on density scaling
  14. return pow(saturate(transmission / absorptionRange), densityScales);
  15. }
  16. real3 GetTransmissionWithAbsorption(real transmission, real4 absorptionColor, real absorptionRange, bool alphaPremultiplied)
  17. {
  18. #if defined(_SIX_WAY_COLOR_ABSORPTION)
  19. real3 densityScales = ComputeDensityScales(absorptionColor.rgb);
  20. if(alphaPremultiplied)
  21. absorptionRange *= (absorptionColor.a > 0) ? absorptionColor.a : 1.0f;
  22. real3 outTransmission = GetTransmissionWithAbsorption(transmission, densityScales, absorptionRange);
  23. outTransmission *= absorptionRange;
  24. return outTransmission;
  25. #else
  26. return transmission.xxx * absorptionColor.rgb; // simple multiply
  27. #endif
  28. }
  29. real3 GetSixWayDiffuseContributions(real3 rightTopBack, real3 leftBottomFront, real4 baseColor, real3 L0, real3 diffuseGIData[3], real absorptionRange, bool alphaPremultiplied)
  30. {
  31. real3 giColor = real3(0,0,0);
  32. // Scale to be energy conserving: Total energy = 4*pi; divided by 6 directions
  33. real scale = 4.0f * PI / 6.0f;
  34. #if defined(_SIX_WAY_COLOR_ABSORPTION)
  35. real3 densityScales = ComputeDensityScales(baseColor.rgb);
  36. if(alphaPremultiplied)
  37. absorptionRange *= (baseColor.a > 0) ? baseColor.a : 1.0f;
  38. for(int i = 0; i < 3; i++)
  39. {
  40. real3 bakeDiffuseLighting = L0 + diffuseGIData[i];
  41. giColor += GetTransmissionWithAbsorption(rightTopBack[i], densityScales, absorptionRange) * bakeDiffuseLighting;
  42. bakeDiffuseLighting = L0 - diffuseGIData[i];
  43. giColor += GetTransmissionWithAbsorption(leftBottomFront[i], densityScales, absorptionRange) * bakeDiffuseLighting;
  44. }
  45. giColor *= absorptionRange;
  46. #else
  47. for(int i = 0; i < 3; i++)
  48. {
  49. real3 bakeDiffuseLighting = L0 + diffuseGIData[i];
  50. giColor += rightTopBack[i] * bakeDiffuseLighting;
  51. bakeDiffuseLighting = L0 - diffuseGIData[i];
  52. giColor += leftBottomFront[i] * bakeDiffuseLighting;
  53. }
  54. giColor *= baseColor.rgb;
  55. #endif
  56. return giColor * scale;
  57. }
  58. #endif // UNITY_SIX_WAY_COMMON_INCLUDED