2
0

IrradianceProjectSH.bsl 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "$ENGINE$\PPBase.bslinc"
  2. #include "$ENGINE$\ReflectionCubemapCommon.bslinc"
  3. #include "$ENGINE$\SHCommon.bslinc"
  4. Technique
  5. : inherits("PPBase")
  6. : inherits("ReflectionCubemapCommon")
  7. : inherits("SHCommon") =
  8. {
  9. Language = "HLSL11";
  10. Pass =
  11. {
  12. Fragment =
  13. {
  14. cbuffer Params
  15. {
  16. int gCubeFace;
  17. }
  18. StructuredBuffer<SHVector5RGB> gSHCoeffs;
  19. float evaluateLambert(SHVector5 coeffs)
  20. {
  21. // Multiply irradiance SH coefficients by cosine lobe (Lambert diffuse) and evaluate resulting SH
  22. // See: http://cseweb.ucsd.edu/~ravir/papers/invlamb/josa.pdf for derivation of the
  23. // cosine lobe factors
  24. float output = 0.0f;
  25. // Band 0 (factor 1.0)
  26. output += coeffs.v0[0];
  27. // Band 1 (factor 2/3)
  28. float f = (2.0f/3.0f);
  29. float4 f4 = float4(f, f, f, f);
  30. output += dot(coeffs.v0.gba, f4.rgb);
  31. // Band 2 (factor 1/4)
  32. f = (1.0f/4.0f);
  33. f4 = float4(f, f, f, f);
  34. output += dot(coeffs.v1, f4);
  35. output += coeffs.v2.r * f;
  36. // Band 3 (factor 0)
  37. // Band 4 (factor -1/24)
  38. f = (-1.0f/24.0f);
  39. f4 = float4(f, f, f, f);
  40. output += dot(coeffs.v4, f4);
  41. output += dot(coeffs.v5, f4);
  42. output += coeffs.v6 * f;
  43. return output;
  44. }
  45. float4 main(VStoFS input) : SV_Target0
  46. {
  47. float2 scaledUV = input.uv0 * 2.0f - 1.0f;
  48. float3 dir = getDirFromCubeFace(gCubeFace, scaledUV);
  49. dir = normalize(dir);
  50. SHVector5 shBasis = SHBasis5(dir);
  51. SHVector5RGB coeffs = gSHCoeffs[0];
  52. SHMultiply(coeffs.R, shBasis);
  53. SHMultiply(coeffs.G, shBasis);
  54. SHMultiply(coeffs.B, shBasis);
  55. float3 output = 0;
  56. output.r = evaluateLambert(coeffs.R);
  57. output.g = evaluateLambert(coeffs.G);
  58. output.b = evaluateLambert(coeffs.B);
  59. return float4(output.rgb, 1.0f);
  60. }
  61. };
  62. };
  63. };