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