설명 없음
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.

AmbientProbe.hlsl 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef __AMBIENTPROBE_HLSL__
  2. #define __AMBIENTPROBE_HLSL__
  3. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SphericalHarmonics.hlsl"
  4. // Ambient Probe is preconvolved with clamped cosinus
  5. // In case we use a diffuse power, we have to edit the coefficients to change the convolution
  6. // This is currently not used because visual difference is really minor
  7. void ReconvolveAmbientProbeWithPower(float diffusePower, inout float4 SHCoefficients[7])
  8. {
  9. if (diffusePower == 0.0f)
  10. return;
  11. // convolution coefs
  12. float w = diffusePower + 1;
  13. float kModifiedLambertian0 = 1.0f;
  14. float kModifiedLambertian1 = (w + 1.0f) / (w + 2.0f);
  15. float kModifiedLambertian2 = w / (w + 3.0f);
  16. // ambient probe is pre-convolved by clamped cosine - we have to undo pre-convolution and
  17. // convolve again with coefs for modified lambertian
  18. float wrapScaling0 = kModifiedLambertian0 / kClampedCosine0;
  19. float wrapScaling1 = kModifiedLambertian1 / kClampedCosine1;
  20. float wrapScaling2 = kModifiedLambertian2 / kClampedCosine2;
  21. // handle coeficient packing - see AmbientProbeConvolution.compute : PackSHFromScratchBuffer
  22. float3 ambient6 = float3(SHCoefficients[3].z, SHCoefficients[4].z, SHCoefficients[5].z) / 3.0f;
  23. float3 ambient0 = float3(SHCoefficients[0].a, SHCoefficients[1].a, SHCoefficients[2].a) + ambient6;
  24. SHCoefficients[0].xyz *= wrapScaling1;
  25. SHCoefficients[1].xyz *= wrapScaling1;
  26. SHCoefficients[2].xyz *= wrapScaling1;
  27. SHCoefficients[3] *= wrapScaling2;
  28. SHCoefficients[4] *= wrapScaling2;
  29. SHCoefficients[5] *= wrapScaling2;
  30. SHCoefficients[6] *= wrapScaling2;
  31. SHCoefficients[0].a = ambient0.r * wrapScaling0 - ambient6.r * wrapScaling2;
  32. SHCoefficients[1].a = ambient0.g * wrapScaling0 - ambient6.g * wrapScaling2;
  33. SHCoefficients[2].a = ambient0.b * wrapScaling0 - ambient6.b * wrapScaling2;
  34. }
  35. // We need to define this before including ProbeVolume.hlsl as that file expects this function to be defined.
  36. // AmbientProbe Data is fetch directly from a compute buffer to remain on GPU and is preconvolved with clamped cosinus
  37. real3 EvaluateAmbientProbe(real3 normalWS)
  38. {
  39. #if AMBIENT_PROBE_BUFFER
  40. return SampleSH9(_AmbientProbeData, normalWS);
  41. #else
  42. // Linear + constant polynomial terms
  43. real3 res = SHEvalLinearL0L1(normalWS, unity_SHAr, unity_SHAg, unity_SHAb);
  44. // Quadratic polynomials
  45. res += SHEvalLinearL2(normalWS, unity_SHBr, unity_SHBg, unity_SHBb, unity_SHC);
  46. return res;
  47. #endif
  48. }
  49. real3 EvaluateAmbientProbeSRGB(real3 normalWS)
  50. {
  51. real3 res = EvaluateAmbientProbe(normalWS);
  52. #ifdef UNITY_COLORSPACE_GAMMA
  53. res = LinearToSRGB(res);
  54. #endif
  55. return res;
  56. }
  57. real3 SampleSH(real3 normalWS)
  58. {
  59. return EvaluateAmbientProbeSRGB(normalWS);
  60. }
  61. real3 EvaluateAmbientProbeL1(real3 normalWS)
  62. {
  63. #if AMBIENT_PROBE_BUFFER
  64. real4 SHCoefficients[3];
  65. SHCoefficients[0] = _AmbientProbeData[0];
  66. SHCoefficients[1] = _AmbientProbeData[1];
  67. SHCoefficients[2] = _AmbientProbeData[2];
  68. return SampleSH4_L1(SHCoefficients, normalWS);
  69. #else
  70. return real3(0.0, 0.0, 0.0);
  71. #endif
  72. }
  73. real3 EvaluateAmbientProbeL0()
  74. {
  75. #if AMBIENT_PROBE_BUFFER
  76. return real3(_AmbientProbeData[0].w, _AmbientProbeData[1].w, _AmbientProbeData[2].w);
  77. #else
  78. return real3(0.0, 0.0, 0.0);
  79. #endif
  80. }
  81. #endif