├── .gitattributes ├── README.md ├── Scripts ├── LightProbesTetrahedralGrid.cs └── LightProbesTetrahedralGrid.cs.meta └── imgs ├── AddingLP.png ├── LPG.png ├── addLPG.png ├── beforeLP.png ├── cbi.png ├── class-LightProbeGroup-12.png ├── class-LightProbeGroup-13.png ├── class-LightProbeGroup-14.png ├── class-LightProbeGroup-15.png ├── class-LightProbeGroup-20183.png ├── class-LightProbeGroup-Ringing.png ├── glpg.png ├── 全无.png ├── 全有.png └── 静态有动态无.png /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AutoGenerateLightProbeTool 2 | This is a tool for generate light probe in unity automatically. 3 | 4 | ## 1. 概念 5 | 6 | 首先,我们来回顾全局光照(Global Illumination )的设置,就是为了获取反射光影效果,增强场景真实性 7 | 8 | | 光照模式 | 全局光照设置 (Global Illumination) | 物体设置 | 间接(反射)光影效果 | 是否产生烘焙贴图 | 直接光影效果 | 9 | | -------- | ------------------------------------ | ---------- | -------------------- | -------------------------- | ------------ | 10 | | realtime | Realtime Global Illumination | not static | 有效果 | 无烘焙贴图,但会有实时贴图 | 有 | 11 | | realtime | Mixed Global Illumination | static | 无效果 | 有烘焙贴图 | 有 | 12 | | baked | Mixed Global Illumination | not static | 无效果 | 无烘焙贴图 | 无 | 13 | | baked | Mixed Global Illumination | static | 有效果 | 有烘焙贴图 | 有 | 14 | | Mixed | Mixed Global Illumination | not static | 无效果 | 无烘焙贴图 | 有 | 15 | | Mixed | Mixed Global Illumination | static | 有效果 | 有烘焙贴图 | 有 | 16 | 17 | 图1:静态、非静态物体,都没有配置好全局光照(间接光照)的效果 18 | 19 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/全无.png) 20 | 21 | 图2:静态物体有全局光照(间接光照)、非静态物体无间接光照的效果 22 | 23 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/静态有动态无.png) 24 | 25 | 图3:使用光照探针配合光照贴图,静态、非静态物体,都有间接光照效果 26 | 27 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/全有.png) 28 | 29 | > 注意: 30 | > * 实时光照,如果想要有间接光影效果,必须将全局光照配置为 Realtime Global Illumination 31 | > * 实时光照,如果想要有间接光影效果,还需要将物体设置为 Contribute Global Illumination 32 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/cbi.png) 33 | > * 但实际游戏中,是不会使用实时全局光照 Realtime Global Illumination(耗费大,而且效果差并非完全实时),所以要用 Mixed 光照配合 Mixed Global Illumination 34 | > * 为了弥补 非静态物体的间接光照效果,需要使用光照探针 35 | 36 | 间接光影效果最佳方案总结: 37 | 1. 场景光照设置为 Mixed Global Illumination ; 38 | 2. 光源一般如果同时覆盖动态和静态物体,一定要设置为 Mixed 模式; 39 | 3. 物体和光源都设置好后,在场景中添加光照探针组,并且 Generate LightMap 生成光照贴图 40 | 4. 享受最合理的间接光影效果~ 41 | 42 | ### 1.1 什么是光照探针 43 | 44 | * 实时 Realtime 光照太浪费资源,一般不会大范围使用在实际游戏场景中; 45 | * 烘焙 Baked 光照又不能作用在动态物体上,这就有了光照探针,来弥补光照贴图的不足 46 | * 对于 Mixed 光照,虽然可以将光影效果投射到移动物体上,但移动物体不会从静态环境中接收到反射光,所以也需要使用光照探针 47 | 48 | 未添加 光照探针: 49 | 50 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/beforeLP.png) 51 | 52 | 正在添加 光照探针: 53 | 54 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/AddingLP.png) 55 | 56 | 添加光照探针后: 57 | 58 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/全有.png) 59 | 60 | 光照探针大致原理:在某一光照探针的所在位置点上对光照信息进行采样,然后从该光照探针相邻的其他光照探针的位置上对光照信息进行采样,把这些采样得到的光照信息进行插值运算,便可算出这些光照探针之间某个位置的光照信息。 61 | 62 | ### 1.2 光照探针作用 63 | 64 | 光探测器(光照探针 Light Probe)存储有关场景中照明的“烘焙”信息。 65 | 66 | 光照贴图存储有关光线照射场景中表面的光照信息,但光照探测器存储有关光线穿过场景中空白空间的信息。 67 | 68 | 说白了,光照贴图只存储 mesh 表面的光影信息;而光照探针,存储空白空间的光影信息,包括直接光和间接光(反射光),是对光照贴图的补充 69 | 70 | ## 2. 使用 光照探针组 Light Probe Group 71 | 72 | ### 2.1 概念 73 | 74 | 光照探针组就是一组光照探针,默认是一个立方体,4*2一组,共八个光照探针组成。 75 | 76 | 可以通过编辑,增加或删除光照探针,也可以随意移动光照探针位置 77 | 78 | ### 2.2 使用方式 79 | 80 | 可以从菜单中添加 Light Probe Group 到当前场景中:Component > Rendering > Light Probe Group 81 | 82 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/addLPG.png) 83 | 84 | 光照探针组初始形态: 85 | 86 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/LPG.png) 87 | 88 | ### 2.3 属性 89 | 90 | 光照探针组组件在 Inspector 窗口如下: 91 | 92 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/class-LightProbeGroup-20183.png) 93 | 94 | * Edit Light Probes: 编辑光照探针,点击后,可以在场景中编辑光照探针组中的光照探针,进行增加、删除,移动位置等操作 95 | * Show Wireframe :显示光照探针之间的连线组成的线框,如果禁用,则只显示探针所在点,而不显示之间的连线; 96 | * Remove Ringing :去除振铃。启用此属性后,Unity 会自动从场景中移除 Light Probe 振铃。 97 | 在某些情况下,Light Probe 会表现出一种称为“振铃”的不良行为。当 Light Probe 周围的光线存在显着差异时,通常会发生这种情况。例如,如果光探头的一侧有亮光,而另一侧没有光,则光强度可能会在背面“过冲”。这种过冲会在背面产生一个光点。 98 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/class-LightProbeGroup-Ringing.png) 99 | 启用Remove Ringing。Unity 会自动移除不希望出现的光点。但是,这通常会降低 Light Probes 的准确度,并降低光线对比度,因此您必须检查视觉结果 100 | * Selected Probe Position : 选定光照探针所在位置 101 | 102 | ### 2.4 光探头放置故障排除 103 | 104 | 对 Light Probe 位置的选择必须考虑到光照是在 Light Probe 组之间插值的。如果您的 Light Probe 没有充分覆盖整个场景中的光照变化,则可能会出现问题。 105 | 106 | 下面的示例显示了一个夜间场景,两侧有两个明亮的路灯,中间有一个黑暗区域。如果 Light Probe 仅放置在路灯附近,而在黑暗区域没有放置,则来自灯的照明直接穿过黑暗的缝隙,照射到移动的物体上。这是因为照明从一个亮点插入到另一个亮点,中间没有关于暗区的信息。 107 | 108 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/class-LightProbeGroup-12.png) 109 | 110 | 如果您使用的是实时或混合光,则此问题可能不太明显,因为只有间接光会穿过间隙。如果您完全使用烘焙光,问题会更加明显 111 | ,因为在这种情况下,移动物体上的直射光也是由 Light Probes 插入的。在此示例场景中,两个灯被烘焙,因此移动对象从 Light Probes 获得直接光。在这里您可以看到结果 - 移动的物体(救护车)在穿过黑暗区域时保持明亮,这不是预期的效果。黄色线框四面体显示插值发生在街道明亮的一端与另一端之间。 112 | 113 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/class-LightProbeGroup-13.png) 114 | 115 | 这是一个不希望的效果 - 救护车在通过黑暗区域时仍然保持明亮,因为没有在黑暗区域放置光探头。 116 | 117 | 为了解决这个问题,你应该在黑暗区域放置更多的Light Probes,如下图: 118 | 119 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/class-LightProbeGroup-14.png) 120 | 121 | 现在场景在黑暗区域也有光探针。因此,移动的救护车在从场景的一侧行驶到另一侧时会呈现较暗的灯光。 122 | 123 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/class-LightProbeGroup-15.png) 124 | 125 | ## 3. 用代码添加光照探针 126 | 127 | 在比较大的场景中,如果需要光照探针时,还是使用手动添加,不仅位置不会很精确,而且会非常耗时。 128 | 129 | 这种情况下,就可以使用代码来自动防止光照探针: 130 | 1. 添加 LightProbeGroup 游戏对象; 131 | 2. 在代码中操作 LightProbeGroup 游戏对象,为其中用代码精准添加并布置光照探针 132 | 133 | 下面是核心功能代码: 134 | ``` C# 135 | //获取光照探针组件 136 | LightProbeGroup lightProbeGroup = GetComponent(); 137 | //建立要插入探针位置的列表 138 | List positions = new List(); 139 | // 下面就是向 positions 中添加合理的位置点 140 | ....... 141 | ....... 142 | //向光照探针组中,添加探针 143 | lightProbeGroup.probePositions = positions.ToArray(); 144 | ``` 145 | 146 | 完整实例:下面脚本可以将 Light Probes 放置在一个圆圈或一个环中 147 | 148 | ![](https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/main/imgs/glpg.png) 149 | 150 | ``` C# 151 | using System.Collections.Generic; 152 | using UnityEngine; 153 | 154 | [RequireComponent(typeof(LightProbeGroup))] 155 | public class LightProbesTetrahedralGrid : MonoBehaviour 156 | { 157 | // Common 158 | [Tooltip("边长 : 同层探针间连线长度")] 159 | public float m_Side = 1.0f; 160 | [Tooltip("半径 : 圆形探针组大圆半径")] 161 | public float m_Radius = 5.0f; 162 | [Tooltip("内半径 : 圆形探针组内部小圆半径")] 163 | public float m_InnerRadius = 0.1f; 164 | [Tooltip("高度 : 圆形探针组高度")] 165 | public float m_Height = 2.0f; 166 | [Tooltip("层数 : 圆形探针组层数")] 167 | public uint m_Levels = 3; 168 | //最小半径 169 | const float kMinSide = 0.05f; 170 | //最低高度 171 | const float kMinHeight = 0.05f; 172 | //最小内圆半径 173 | const float kMinInnerRadius = 0.1f; 174 | //最小迭代数 175 | const uint kMinIterations = 4; 176 | 177 | // 游戏运行时,先调用 Generate 方法,添加光照探针到场景中 178 | private void FixedUpdate() 179 | { 180 | Generate(); 181 | } 182 | 183 | // OnValidate可以用来验证一些数据,脚本加载或Inspector中的任何值被修改时会调用 184 | // 可以作为数据的保护,避免一些字段(或属性)被设置为不可用(不合理)的数据值 185 | public void OnValidate() 186 | { 187 | //保证 m_Side 不小于 kMinSide 188 | m_Side = Mathf.Max(kMinSide, m_Side); 189 | //保证 m_Height 不小于 kMinHeight 190 | m_Height = Mathf.Max(kMinHeight, m_Height); 191 | // 对 m_Radius 和 m_InnerRadius 进行限制 192 | if (m_InnerRadius < kMinInnerRadius) 193 | { 194 | TriangleProps props = new TriangleProps(m_Side); 195 | m_Radius = Mathf.Max(props.circumscribedCircleRadius + 0.01f, m_Radius); 196 | } 197 | else 198 | { 199 | m_Radius = Mathf.Max(0.1f, m_Radius); 200 | m_InnerRadius = Mathf.Min(m_Radius, m_InnerRadius); 201 | } 202 | } 203 | 204 | // 结构体 三角形探针 205 | struct TriangleProps 206 | { 207 | public TriangleProps(float triangleSide) 208 | { 209 | side = triangleSide; 210 | halfSide = side / 2.0f; 211 | height = Mathf.Sqrt(3.0f) * side / 2.0f; 212 | //内切圆半径 213 | inscribedCircleRadius = Mathf.Sqrt(3.0f) * side / 6.0f; 214 | //外切圆半径 215 | circumscribedCircleRadius = 2.0f * height / 3.0f; 216 | } 217 | public float side; 218 | public float halfSide; 219 | public float height; 220 | public float inscribedCircleRadius; 221 | public float circumscribedCircleRadius; 222 | }; 223 | //三角形探针位,用来辅助生成探针的位置 224 | private TriangleProps m_TriangleProps; 225 | //生成探针主方法 226 | public void Generate() 227 | { 228 | //获取光照探针组件 229 | LightProbeGroup lightProbeGroup = GetComponent(); 230 | //建立要插入探针位置的列表 231 | List positions = new List(); 232 | //三角形探针位,用来辅助生成探针的位置 233 | m_TriangleProps = new TriangleProps(m_Side); 234 | 235 | if (m_InnerRadius < kMinInnerRadius) 236 | //生成圆柱 237 | GenerateCylinder(m_TriangleProps, m_Radius, m_Height, m_Levels, positions); 238 | else 239 | //生成环 240 | GenerateRing(m_TriangleProps, m_Radius, m_InnerRadius, m_Height, m_Levels, positions); 241 | //向光照探针组中,添加探针 242 | lightProbeGroup.probePositions = positions.ToArray(); 243 | } 244 | //尝试添加 245 | static void AttemptAdding(Vector3 position, Vector3 center, float distanceCutoffSquared, List outPositions) 246 | { 247 | if ((position - center).sqrMagnitude < distanceCutoffSquared) 248 | outPositions.Add(position); 249 | } 250 | //计算圆柱体迭代 251 | uint CalculateCylinderIterations(TriangleProps props, float radius) 252 | { 253 | int iterations = Mathf.CeilToInt((radius + props.height - props.inscribedCircleRadius) / props.height); 254 | if (iterations > 0) 255 | return (uint)iterations; 256 | return 0; 257 | } 258 | //生成圆柱体 259 | void GenerateCylinder(TriangleProps props, float radius, float height, uint levels, List outPositions) 260 | { 261 | uint iterations = CalculateCylinderIterations(props, radius); 262 | float distanceCutoff = radius; 263 | float distanceCutoffSquared = distanceCutoff * distanceCutoff; 264 | Vector3 up = new Vector3(props.circumscribedCircleRadius, 0.0f, 0.0f); 265 | Vector3 leftDown = new Vector3(-props.inscribedCircleRadius, 0.0f, -props.halfSide); 266 | Vector3 rightDown = new Vector3(-props.inscribedCircleRadius, 0.0f, props.halfSide); 267 | for (uint l = 0; l < levels; l++) 268 | { 269 | float tLevel = levels == 1 ? 0 : (float)l / (float)(levels - 1); 270 | Vector3 center = new Vector3(0.0f, tLevel * height, 0.0f); 271 | if (l % 2 == 0) 272 | { 273 | for (uint i = 0; i < iterations; i++) 274 | { 275 | Vector3 upCorner = center + up + (float)i * up * 2.0f * 3.0f / 2.0f; 276 | Vector3 leftDownCorner = center + leftDown + (float)i * leftDown * 2.0f * 3.0f / 2.0f; 277 | Vector3 rightDownCorner = center + rightDown + (float)i * rightDown * 2.0f * 3.0f / 2.0f; 278 | AttemptAdding(upCorner, center, distanceCutoffSquared, outPositions); 279 | AttemptAdding(leftDownCorner, center, distanceCutoffSquared, outPositions); 280 | AttemptAdding(rightDownCorner, center, distanceCutoffSquared, outPositions); 281 | Vector3 leftDownUp = upCorner - leftDownCorner; 282 | Vector3 upRightDown = rightDownCorner - upCorner; 283 | Vector3 rightDownLeftDown = leftDownCorner - rightDownCorner; 284 | uint subdiv = 3 * i + 1; 285 | for (uint s = 1; s < subdiv; s++) 286 | { 287 | Vector3 leftDownUpSubdiv = leftDownCorner + leftDownUp * (float)s / (float)subdiv; 288 | AttemptAdding(leftDownUpSubdiv, center, distanceCutoffSquared, outPositions); 289 | Vector3 upRightDownSubdiv = upCorner + upRightDown * (float)s / (float)subdiv; 290 | AttemptAdding(upRightDownSubdiv, center, distanceCutoffSquared, outPositions); 291 | Vector3 rightDownLeftDownSubdiv = rightDownCorner + rightDownLeftDown * (float)s / (float)subdiv; 292 | AttemptAdding(rightDownLeftDownSubdiv, center, distanceCutoffSquared, outPositions); 293 | } 294 | } 295 | } 296 | else 297 | { 298 | for (uint i = 0; i < iterations; i++) 299 | { 300 | Vector3 upCorner = center + (float)i * (2.0f * up * 3.0f / 2.0f); 301 | Vector3 leftDownCorner = center + (float)i * (2.0f * leftDown * 3.0f / 2.0f); 302 | Vector3 rightDownCorner = center + (float)i * (2.0f * rightDown * 3.0f / 2.0f); 303 | AttemptAdding(upCorner, center, distanceCutoffSquared, outPositions); 304 | AttemptAdding(leftDownCorner, center, distanceCutoffSquared, outPositions); 305 | AttemptAdding(rightDownCorner, center, distanceCutoffSquared, outPositions); 306 | Vector3 leftDownUp = upCorner - leftDownCorner; 307 | Vector3 upRightDown = rightDownCorner - upCorner; 308 | Vector3 rightDownLeftDown = leftDownCorner - rightDownCorner; 309 | uint subdiv = 3 * i; 310 | for (uint s = 1; s < subdiv; s++) 311 | { 312 | Vector3 leftDownUpSubdiv = leftDownCorner + leftDownUp * (float)s / (float)subdiv; 313 | AttemptAdding(leftDownUpSubdiv, center, distanceCutoffSquared, outPositions); 314 | Vector3 upRightDownSubdiv = upCorner + upRightDown * (float)s / (float)subdiv; 315 | AttemptAdding(upRightDownSubdiv, center, distanceCutoffSquared, outPositions); 316 | Vector3 rightDownLeftDownSubdiv = rightDownCorner + rightDownLeftDown * (float)s / (float)subdiv; 317 | AttemptAdding(rightDownLeftDownSubdiv, center, distanceCutoffSquared, outPositions); 318 | } 319 | } 320 | } 321 | } 322 | } 323 | //生成环 324 | void GenerateRing(TriangleProps props, float radius, float innerRadius, float height, uint levels, List outPositions) 325 | { 326 | float chordLength = props.side; 327 | float angle = Mathf.Clamp(2.0f * Mathf.Asin(chordLength / (2.0f * radius)), 0.01f, 2.0f * Mathf.PI); 328 | uint slicesAtRadius = (uint)Mathf.FloorToInt(2.0f * Mathf.PI / angle); 329 | uint layers = (uint)Mathf.Max(Mathf.Ceil((radius - innerRadius) / props.height), 0.0f); 330 | for (uint level = 0; level < levels; level++) 331 | { 332 | float tLevel = levels == 1 ? 0 : (float)level / (float)(levels - 1); 333 | float y = height * tLevel; 334 | float iterationOffset0 = level % 2 == 0 ? 0.0f : 0.5f; 335 | for (uint layer = 0; layer < layers; layer++) 336 | { 337 | float tLayer = layers == 1 ? 1.0f : (float)layer / (float)(layers - 1); 338 | float tIterations = (tLayer * (radius - innerRadius) + innerRadius - kMinInnerRadius) / (radius - kMinInnerRadius); 339 | uint slices = (uint)Mathf.CeilToInt(Mathf.Lerp(kMinIterations, slicesAtRadius, tIterations)); 340 | float x = innerRadius + (radius - innerRadius) * tLayer; 341 | Vector3 position = new Vector3(x, y, 0.0f); 342 | float layerSliceOffset = layer % 2 == 0 ? 0.0f : 0.5f; 343 | for (uint slice = 0; slice < slices; slice++) 344 | { 345 | Quaternion rotation = Quaternion.Euler(0.0f, (slice + iterationOffset0 + layerSliceOffset) * 360.0f / (float)slices, 0.0f); 346 | outPositions.Add(rotation * position); 347 | } 348 | } 349 | } 350 | } 351 | 352 | } 353 | ``` 354 | 355 |
356 |
357 |
358 | 359 | > 参考资料: 360 | > * [光照探针文档 - Unity 官方](https://docs.unity3d.com/2022.2/Documentation/Manual/LightProbes.html) 361 | > * [Brackeys - youtube - HIGH QUALITY LIGHTING using Light Probes](https://www.youtube.com/watch?v=_E0JXOZDTKA) 362 | 363 |
364 |
365 |
-------------------------------------------------------------------------------- /Scripts/LightProbesTetrahedralGrid.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | 4 | [RequireComponent (typeof (LightProbeGroup))] 5 | public class LightProbesTetrahedralGrid : MonoBehaviour 6 | { 7 | // Common 8 | [Tooltip("边长 : 同层探针间连线长度")] 9 | public float m_Side = 1.0f; 10 | [Tooltip("半径 : 圆形探针组大圆半径")] 11 | public float m_Radius = 5.0f; 12 | [Tooltip("内半径 : 圆形探针组内部小圆半径")] 13 | public float m_InnerRadius = 0.1f; 14 | [Tooltip("高度 : 圆形探针组高度")] 15 | public float m_Height = 2.0f; 16 | [Tooltip("层数 : 圆形探针组层数")] 17 | public uint m_Levels = 3; 18 | //最小半径 19 | const float kMinSide = 0.05f; 20 | //最低高度 21 | const float kMinHeight = 0.05f; 22 | //最小内圆半径 23 | const float kMinInnerRadius = 0.1f; 24 | //最小迭代数 25 | const uint kMinIterations = 4; 26 | 27 | // 游戏运行时,先调用 Generate 方法,添加光照探针到场景中 28 | private void FixedUpdate() 29 | { 30 | Generate(); 31 | } 32 | 33 | // OnValidate可以用来验证一些数据,脚本加载或Inspector中的任何值被修改时会调用 34 | // 可以作为数据的保护,避免一些字段(或属性)被设置为不可用(不合理)的数据值 35 | public void OnValidate() 36 | { 37 | //保证 m_Side 不小于 kMinSide 38 | m_Side = Mathf.Max(kMinSide, m_Side); 39 | //保证 m_Height 不小于 kMinHeight 40 | m_Height = Mathf.Max(kMinHeight, m_Height); 41 | // 对 m_Radius 和 m_InnerRadius 进行限制 42 | if (m_InnerRadius < kMinInnerRadius) 43 | { 44 | TriangleProps props = new TriangleProps(m_Side); 45 | m_Radius = Mathf.Max(props.circumscribedCircleRadius + 0.01f, m_Radius); 46 | } 47 | else 48 | { 49 | m_Radius = Mathf.Max(0.1f, m_Radius); 50 | m_InnerRadius = Mathf.Min(m_Radius, m_InnerRadius); 51 | } 52 | } 53 | 54 | // 结构体 三角形探针 55 | struct TriangleProps 56 | { 57 | public TriangleProps(float triangleSide) 58 | { 59 | side = triangleSide; 60 | halfSide = side / 2.0f; 61 | height = Mathf.Sqrt(3.0f) * side / 2.0f; 62 | //内切圆半径 63 | inscribedCircleRadius = Mathf.Sqrt(3.0f) * side / 6.0f; 64 | //外切圆半径 65 | circumscribedCircleRadius = 2.0f * height / 3.0f; 66 | } 67 | public float side; 68 | public float halfSide; 69 | public float height; 70 | public float inscribedCircleRadius; 71 | public float circumscribedCircleRadius; 72 | }; 73 | //三角形探针位,用来辅助生成探针的位置 74 | private TriangleProps m_TriangleProps; 75 | //生成探针主方法 76 | public void Generate() 77 | { 78 | //获取光照探针组件 79 | LightProbeGroup lightProbeGroup = GetComponent(); 80 | //建立要插入探针位置的列表 81 | List positions = new List(); 82 | //三角形探针位,用来辅助生成探针的位置 83 | m_TriangleProps = new TriangleProps(m_Side); 84 | 85 | if (m_InnerRadius < kMinInnerRadius) 86 | //生成圆柱 87 | GenerateCylinder(m_TriangleProps, m_Radius, m_Height, m_Levels, positions); 88 | else 89 | //生成环 90 | GenerateRing(m_TriangleProps, m_Radius, m_InnerRadius, m_Height, m_Levels, positions); 91 | //向光照探针组中,添加探针 92 | lightProbeGroup.probePositions = positions.ToArray(); 93 | } 94 | //尝试添加 95 | static void AttemptAdding(Vector3 position, Vector3 center, float distanceCutoffSquared, List outPositions) 96 | { 97 | if ((position - center).sqrMagnitude < distanceCutoffSquared) 98 | outPositions.Add(position); 99 | } 100 | //计算圆柱体迭代 101 | uint CalculateCylinderIterations(TriangleProps props, float radius) 102 | { 103 | int iterations = Mathf.CeilToInt((radius + props.height - props.inscribedCircleRadius) / props.height); 104 | if (iterations > 0) 105 | return (uint)iterations; 106 | return 0; 107 | } 108 | //生成圆柱体 109 | void GenerateCylinder(TriangleProps props, float radius, float height, uint levels, List outPositions) 110 | { 111 | uint iterations = CalculateCylinderIterations(props, radius); 112 | float distanceCutoff = radius; 113 | float distanceCutoffSquared = distanceCutoff * distanceCutoff; 114 | Vector3 up = new Vector3(props.circumscribedCircleRadius, 0.0f, 0.0f); 115 | Vector3 leftDown = new Vector3(-props.inscribedCircleRadius, 0.0f, -props.halfSide); 116 | Vector3 rightDown = new Vector3(-props.inscribedCircleRadius, 0.0f, props.halfSide); 117 | for (uint l = 0; l < levels; l++) 118 | { 119 | float tLevel = levels == 1 ? 0 : (float)l / (float)(levels - 1); 120 | Vector3 center = new Vector3(0.0f, tLevel * height, 0.0f); 121 | if (l % 2 == 0) 122 | { 123 | for (uint i = 0; i < iterations; i++) 124 | { 125 | Vector3 upCorner = center + up + (float)i * up * 2.0f * 3.0f / 2.0f; 126 | Vector3 leftDownCorner = center + leftDown + (float)i * leftDown * 2.0f * 3.0f / 2.0f; 127 | Vector3 rightDownCorner = center + rightDown + (float)i * rightDown * 2.0f * 3.0f / 2.0f; 128 | AttemptAdding(upCorner, center, distanceCutoffSquared, outPositions); 129 | AttemptAdding(leftDownCorner, center, distanceCutoffSquared, outPositions); 130 | AttemptAdding(rightDownCorner, center, distanceCutoffSquared, outPositions); 131 | Vector3 leftDownUp = upCorner - leftDownCorner; 132 | Vector3 upRightDown = rightDownCorner - upCorner; 133 | Vector3 rightDownLeftDown = leftDownCorner - rightDownCorner; 134 | uint subdiv = 3 * i + 1; 135 | for (uint s = 1; s < subdiv; s++) 136 | { 137 | Vector3 leftDownUpSubdiv = leftDownCorner + leftDownUp * (float)s / (float)subdiv; 138 | AttemptAdding(leftDownUpSubdiv, center, distanceCutoffSquared, outPositions); 139 | Vector3 upRightDownSubdiv = upCorner + upRightDown * (float)s / (float)subdiv; 140 | AttemptAdding(upRightDownSubdiv, center, distanceCutoffSquared, outPositions); 141 | Vector3 rightDownLeftDownSubdiv = rightDownCorner + rightDownLeftDown * (float)s / (float)subdiv; 142 | AttemptAdding(rightDownLeftDownSubdiv, center, distanceCutoffSquared, outPositions); 143 | } 144 | } 145 | } 146 | else 147 | { 148 | for (uint i = 0; i < iterations; i++) 149 | { 150 | Vector3 upCorner = center + (float)i * (2.0f * up * 3.0f / 2.0f); 151 | Vector3 leftDownCorner = center + (float)i * (2.0f * leftDown * 3.0f / 2.0f); 152 | Vector3 rightDownCorner = center + (float)i * (2.0f * rightDown * 3.0f / 2.0f); 153 | AttemptAdding(upCorner, center, distanceCutoffSquared, outPositions); 154 | AttemptAdding(leftDownCorner, center, distanceCutoffSquared, outPositions); 155 | AttemptAdding(rightDownCorner, center, distanceCutoffSquared, outPositions); 156 | Vector3 leftDownUp = upCorner - leftDownCorner; 157 | Vector3 upRightDown = rightDownCorner - upCorner; 158 | Vector3 rightDownLeftDown = leftDownCorner - rightDownCorner; 159 | uint subdiv = 3 * i; 160 | for (uint s = 1; s < subdiv; s++) 161 | { 162 | Vector3 leftDownUpSubdiv = leftDownCorner + leftDownUp * (float)s / (float)subdiv; 163 | AttemptAdding(leftDownUpSubdiv, center, distanceCutoffSquared, outPositions); 164 | Vector3 upRightDownSubdiv = upCorner + upRightDown * (float)s / (float)subdiv; 165 | AttemptAdding(upRightDownSubdiv, center, distanceCutoffSquared, outPositions); 166 | Vector3 rightDownLeftDownSubdiv = rightDownCorner + rightDownLeftDown * (float)s / (float)subdiv; 167 | AttemptAdding(rightDownLeftDownSubdiv, center, distanceCutoffSquared, outPositions); 168 | } 169 | } 170 | } 171 | } 172 | } 173 | //生成环 174 | void GenerateRing(TriangleProps props, float radius, float innerRadius, float height, uint levels, List outPositions) 175 | { 176 | float chordLength = props.side; 177 | float angle = Mathf.Clamp(2.0f * Mathf.Asin(chordLength / (2.0f * radius)), 0.01f, 2.0f * Mathf.PI); 178 | uint slicesAtRadius = (uint)Mathf.FloorToInt(2.0f * Mathf.PI / angle); 179 | uint layers = (uint)Mathf.Max(Mathf.Ceil((radius - innerRadius) / props.height), 0.0f); 180 | for (uint level = 0; level < levels; level++) 181 | { 182 | float tLevel = levels == 1 ? 0 : (float)level / (float)(levels - 1); 183 | float y = height * tLevel; 184 | float iterationOffset0 = level % 2 == 0 ? 0.0f : 0.5f; 185 | for (uint layer = 0; layer < layers; layer++) 186 | { 187 | float tLayer = layers == 1 ? 1.0f : (float)layer / (float)(layers - 1); 188 | float tIterations = (tLayer * (radius - innerRadius) + innerRadius - kMinInnerRadius) / (radius - kMinInnerRadius); 189 | uint slices = (uint)Mathf.CeilToInt(Mathf.Lerp(kMinIterations, slicesAtRadius, tIterations)); 190 | float x = innerRadius + (radius - innerRadius) * tLayer; 191 | Vector3 position = new Vector3(x, y, 0.0f); 192 | float layerSliceOffset = layer % 2 == 0 ? 0.0f : 0.5f; 193 | for (uint slice = 0; slice < slices; slice++) 194 | { 195 | Quaternion rotation = Quaternion.Euler(0.0f, (slice + iterationOffset0 + layerSliceOffset) * 360.0f / (float)slices, 0.0f); 196 | outPositions.Add(rotation * position); 197 | } 198 | } 199 | } 200 | } 201 | 202 | } -------------------------------------------------------------------------------- /Scripts/LightProbesTetrahedralGrid.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 72984f527f7b0484a94fa46770dcf794 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /imgs/AddingLP.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/AddingLP.png -------------------------------------------------------------------------------- /imgs/LPG.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/LPG.png -------------------------------------------------------------------------------- /imgs/addLPG.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/addLPG.png -------------------------------------------------------------------------------- /imgs/beforeLP.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/beforeLP.png -------------------------------------------------------------------------------- /imgs/cbi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/cbi.png -------------------------------------------------------------------------------- /imgs/class-LightProbeGroup-12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/class-LightProbeGroup-12.png -------------------------------------------------------------------------------- /imgs/class-LightProbeGroup-13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/class-LightProbeGroup-13.png -------------------------------------------------------------------------------- /imgs/class-LightProbeGroup-14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/class-LightProbeGroup-14.png -------------------------------------------------------------------------------- /imgs/class-LightProbeGroup-15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/class-LightProbeGroup-15.png -------------------------------------------------------------------------------- /imgs/class-LightProbeGroup-20183.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/class-LightProbeGroup-20183.png -------------------------------------------------------------------------------- /imgs/class-LightProbeGroup-Ringing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/class-LightProbeGroup-Ringing.png -------------------------------------------------------------------------------- /imgs/glpg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/glpg.png -------------------------------------------------------------------------------- /imgs/全无.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/全无.png -------------------------------------------------------------------------------- /imgs/全有.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/全有.png -------------------------------------------------------------------------------- /imgs/静态有动态无.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gaolingx/AutoGenerateLightProbeTool/939bc018d16b3c5cdb54a0456b9b2e3ee962abb4/imgs/静态有动态无.png --------------------------------------------------------------------------------