IrradianceProjectSH.bsl 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "$ENGINE$\PPBase.bslinc"
  2. #include "$ENGINE$\ReflectionCubemapCommon.bslinc"
  3. #define SH_ORDER 5
  4. #include "$ENGINE$\SHCommon.bslinc"
  5. technique IrradianceProjectSH
  6. {
  7. mixin PPBase;
  8. mixin ReflectionCubemapCommon;
  9. mixin SHCommon;
  10. code
  11. {
  12. [internal]
  13. cbuffer Params
  14. {
  15. int gCubeFace;
  16. }
  17. StructuredBuffer<SHVectorRGB> gSHCoeffs;
  18. float evaluateLambert(SHVector coeffs)
  19. {
  20. // Multiply irradiance SH coefficients by cosine lobe (Lambert diffuse) and evaluate resulting SH
  21. // See: http://cseweb.ucsd.edu/~ravir/papers/invlamb/josa.pdf for derivation of the
  22. // cosine lobe factors
  23. float output = 0.0f;
  24. // Band 0 (factor 1.0)
  25. output += coeffs.v[0];
  26. // Band 1 (factor 2/3)
  27. float f = (2.0f/3.0f);
  28. for(int i = 1; i < 4; i++)
  29. output += coeffs.v[i] * f;
  30. // Band 2 (factor 1/4)
  31. f = (1.0f/4.0f);
  32. for(int i = 4; i < 9; i++)
  33. output += coeffs.v[i] * f;
  34. // Band 3 (factor 0)
  35. // Band 4 (factor -1/24)
  36. f = (-1.0f/24.0f);
  37. for(int i = 16; i < 25; i++)
  38. output += coeffs.v[i] * f;
  39. return output;
  40. }
  41. float4 fsmain(VStoFS input) : SV_Target0
  42. {
  43. float2 scaledUV = input.uv0 * 2.0f - 1.0f;
  44. float3 dir = getDirFromCubeFace(gCubeFace, scaledUV);
  45. dir = normalize(dir);
  46. SHVector shBasis = SHBasis(dir);
  47. SHVectorRGB coeffs = gSHCoeffs[0];
  48. SHMultiply(coeffs.R, shBasis);
  49. SHMultiply(coeffs.G, shBasis);
  50. SHMultiply(coeffs.B, shBasis);
  51. float3 output = 0;
  52. output.r = evaluateLambert(coeffs.R);
  53. output.g = evaluateLambert(coeffs.G);
  54. output.b = evaluateLambert(coeffs.B);
  55. return float4(output.rgb, 1.0f);
  56. }
  57. };
  58. };