光照探针(LightProbe) 与 球谐(Spherical Harmonics)

需求:可以手动自定义,并且随Prefab保存的LightProbe

球谐函数 (Spherical Harmonics)

正经定义:
球谐函数是拉普拉斯方程的球坐标系形式解的角度部分

自分白话解释:
球谐函数 - 使用少量系数定义一个球面的整个表面:输入一个方向(球面上一点),输出该方向的颜色

Unity中使用的是二阶球谐函数 SphericalHarmonicsL2,保存为9个系数(27个float),使用时转换为7个Vector4提供给Shader(参考buitin shaders中的ShadeSH9)

Unity的实现参考的是 Stupid Spherical Harmonics (SH) Tricks

官方文档中关于光照探针的技术信息: Light Probes: Technical information

关于SH一篇非常好的文章: Benoît Mayaux (Patapom), Spherical Harmonics

还有企鹅学院的两篇:
(Unity Shader球谐光照解析)[https://gameinstitute.qq.com/community/detail/124147]
(球谐光照(spherical harmonic lighting)解析)[https://gameinstitute.qq.com/community/detail/123183]

自动布置LightProbe

将需要LightProbe的位置赋值给 LightProbeGroup.probePositions

官方例子:Placing probes using scripting

Unity中手动计算SH

SphericalHarmonicsL2提供两个方法

  • 添加环境光(AddAmbientLight)
  • 添加平行光(AddDirectionalLight)

Unity官方文档提供了一个例子:[LightProbes.bakedProbes(Unity文档)]https://docs.unity3d.com/ScriptReference/LightProbes-bakedProbes.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using UnityEngine;
using UnityEngine.Rendering;

public class ExampleClass : MonoBehaviour
{
public Color m_Ambient;
public Light[] m_Lights;

// On start add the contribution of the ambient light and all lights
// assigned to the lights array to all baked probes.
void Start()
{
SphericalHarmonicsL2[] bakedProbes = LightmapSettings.lightProbes.bakedProbes;
Vector3[] probePositions = LightmapSettings.lightProbes.positions;
int probeCount = LightmapSettings.lightProbes.count;

// Clear all probes
for (int i = 0; i < probeCount; i++)
bakedProbes[i].Clear();

// Add ambient light to all probes
for (int i = 0; i < probeCount; i++)
bakedProbes[i].AddAmbientLight(m_Ambient);

// Add directional and point lights' contribution to all probes
foreach (Light l in m_Lights)
{
if (l.type == LightType.Directional)
{
for (int i = 0; i < probeCount; i++)
bakedProbes[i].AddDirectionalLight(-l.transform.forward, l.color, l.intensity);
}
else if (l.type == LightType.Point)
{
for (int i = 0; i < probeCount; i++)
SHAddPointLight(probePositions[i], l.transform.position, l.range, l.color, l.intensity, ref bakedProbes[i]);
}
}
LightmapSettings.lightProbes.bakedProbes = bakedProbes;
}

void SHAddPointLight(Vector3 probePosition, Vector3 position, float range, Color color, float intensity, ref SphericalHarmonicsL2 sh)
{
// From the point of view of an SH probe, point light looks no different than a directional light,
// just attenuated and coming from the right direction.
Vector3 probeToLight = position - probePosition;
float attenuation = 1.0F / (1.0F + 25.0F * probeToLight.sqrMagnitude / (range * range));
sh.AddDirectionalLight(probeToLight.normalized, color, intensity * attenuation);
}
}

在已有LightProbe烘焙的环境中的指定位置获取LightProbe

使用方法LightProbes.GetInterpolatedProbe

LightProbe的SH差值


Unity官方在GDC 2012上的说明, Light Probe Interpolation Using Tetrahedral Tessellations

Unity中烘焙时计算四面体网络(TetrahedronGrid)并保存,运行时根据对象在某个四面体中的位置,利用这个四面体相关的几个SH插值出一个新的SH

更多阅读:
Spherical Harmonics Interpolation, Computation of Laplacians and Gauge Theory

将SH系数注入Renderer

Unity官方的Keijiro Takahashi 写的 LightProbeUtility

注意系数的组合方式在之后的版本中有变化,正确的组合如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Constant + Linear
for (var i = 0; i < 3; i++)
properties.SetVector(_idSHA[i], new Vector4(
sh[i, 3], sh[i, 1], sh[i, 2], sh[i, 0] - sh[i, 6]
));

// Quadratic polynomials
for (var i = 0; i < 3; i++)
properties.SetVector(_idSHB[i], new Vector4(
sh[i, 4], sh[i, 5], sh[i, 6] * 3, sh[i, 7]
));

// Final quadratic polynomial
properties.SetVector(_idSHC, new Vector4(
sh[0, 8], sh[1, 8], sh[2, 8], 1
));

UnityCG.cginc中相关的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// normal should be normalized, w=1.0
half3 SHEvalLinearL0L1 (half4 normal)
{
half3 x;

// Linear (L1) + constant (L0) polynomial terms
x.r = dot(unity_SHAr,normal);
x.g = dot(unity_SHAg,normal);
x.b = dot(unity_SHAb,normal);

return x;
}

// normal should be normalized, w=1.0
half3 SHEvalLinearL2 (half4 normal)
{
half3 x1, x2;
// 4 of the quadratic (L2) polynomials
half4 vB = normal.xyzz * normal.yzzx;
x1.r = dot(unity_SHBr,vB);
x1.g = dot(unity_SHBg,vB);
x1.b = dot(unity_SHBb,vB);

// Final (5th) quadratic (L2) polynomial
half vC = normal.x*normal.x - normal.y*normal.y;
x2 = unity_SHC.rgb * vC;

return x1 + x2;
}

// normal should be normalized, w=1.0
// output in active color space
half3 ShadeSH9 (half4 normal)
{
// Linear + constant polynomial terms
half3 res = SHEvalLinearL0L1 (normal);

// Quadratic polynomials
res += SHEvalLinearL2 (normal);

# ifdef UNITY_COLORSPACE_GAMMA
res = LinearToGammaSpace (res);
# endif

return res;
}
分享