本指南是以下 Unity 博客文章的更新版本:聚光灯团队最佳实践:设置光照管线 (Spotlight Team Best Practices: Setting up the Lighting Pipeline) - Pierre Yves Donzallaz。
首先,让我们来看看在本文中经常遇到的几个重要图形渲染术语的定义。
根据帧率,这些操作每秒钟重复很多次。
下面的流程图从内容创建者的角度,大致显示了 Unity 中的整个光照管线。
首先要选择一个渲染管线。然后决定如何产生间接光照,并相应地选择一个全局光照系统。确保为您的项目适当地调整了所有全局光照设置之后,您可以继续添加光源、发光表面、反射探针、光照探针和光照探针代理体 (LPPV)。所有这些光照对象的用法和特性的详细介绍超出了本文的范围,因此,建议您阅读手册中的“光照”部分,以学习如何在项目中正确地使用光照。
在 2018 年初之前,Unity 中只有一个渲染管线:内置渲染管线 (Built-In Render Pipeline)。此渲染管线提供了可选择的渲染路径:前向渲染路径和延迟渲染路径。
2018 年 1 月,Unity 推出了可编程渲染管线 (SRP),允许通过 C# 脚本来自定义渲染循环。这实际上是游戏引擎领域的一次小革命:用户终于无需使用像 C++ 这样的低级编程语言便可以个性化设置对象剔除、绘制和帧后期处理。
Unity 当前提供两个预先构建的 SRP:
瓦片是帧的一个小型二维方形像素部分,而聚类则是摄像机视锥体中的一个三维体积。瓦片和聚类渲染技术都依赖于影响每个瓦片和聚类的光源的列表,然后可以用相应的已知光源列表在一个通道中计算其光照。不透明对象很可能使用瓦片系统进行着色,而透明对象则依赖于聚类系统。该渲染器的主要优点是,与内置渲染管线(延迟)相比,光照处理速度更快,带宽消耗也大大减少,因为内置渲染管线依赖于更慢的多通道光照积累。
您可以使用下面的决策图表来快速确认您应该根据几个关键标准选择哪一种渲染管线。
可以通过 Unity Package Manager (Window > Package Manager) 来下载最新版本的 HDRP 和 URP。要使用这些 SRP 的其中之一,最简单的方法是使用 Unity Hub 来创建一个新项目并使用其中一个对应的模板。
如果要为项目设置 HDRP,请确保安装了所需的包。然后,使用 HD Render Pipeline Wizard (Window > Render Pipeline > HD Render Pipeline Wizard) 来一键设置您的项目。
如果您略懂渲染方面的知识,熟悉 C#,并需要为项目完全定制渲染器,您可以试着使用 SRP 概念来创建您自己的自定义可编程渲染管线。因为通用渲染管线的着色器库较小并且能够快速注入、删除和交换渲染通道,因此可以对通用渲染管线进行轻松扩展。
在 Unity 中将项目的材质从内置渲染管线移植到 HDRP 或 URP 相对比较容易,这要归功于 Edit > Render Pipeline > Upgrade… 下的一键材质转换器。请注意,这种操作是不可逆的。强烈建议提前备份您的项目!
尽管如此,必须手动移植自定义着色器,因此在生产过程中从内置渲染管线过渡到 HDRP 或 URP 可能会耗费时间,具体时间取决于必须重新编写的自定义着色器数量。
此外,由于 HDRP 在物理上比内置渲染管线更正确,特别是在光衰减和分布方面,因此在切换到 HDRP 之后,项目应该会有所不同。
此外,HDRP 和 URP 并不是交叉兼容的,因为它们没有共同的渲染特征。可以将项目从 HDRP 移植到 URP,反过来也可以,但并不是一键操作,而是需要手动重新制作光照、材质和着色器!
Two of the Global Illumination systems available in Unity are:
Realtime Global Illumination: This system is built on Enlighten, a third-party middleware solution. It enables you to adjust your lighting in real-time if you do a precompute and do not modify GameObjects in your scene with the ContributeGI setting enabled. See the HDRP and URP for compatibility information specific to scriptable render pipelines. Unless otherwise specified, the Built-In Render Pipeline supports all features described in this article.
Baked Global Illumination: When you use this system, Unity bakes lighting data into textures called lightmaps, and into Light Probes and into reflection probes. There are two lightmappers: Enlighten Baked Global Illumination and the Progressive Lightmapper (CPU or GPU). See the HDRP and URP documentation for compatibility information specific to scriptable render pipelines. Unless otherwise specified, the Built-In Render Pipeline supports all features described in this article.
The Progressive Lightmapper calculates indirect lighting values using path tracing. It can prioritize precomputing lighting that affects objects visible to the scene view camera. Although only updating lighting for parts of lightmaps increases the overall bake time, it also enables you to more quickly iterate on your lighting design.
For more information about lighting features in the Scriptable Render Pipelines, see the Universal Render Pipeline and High Definition Render Pipeline Documentation.
无论使用哪种全局光照系统,在烘焙/预计算光照过程中,Unity 只会考虑标记为“Contribute GI”的对象。动态(即非静态)对象必须依赖于放置在整个场景中的光照探针来获得间接光照。
由于光照的烘焙/预计算是一个相对缓慢的过程,所以只有具有不同光照变化(例如凹度和自我阴影)的大型和复杂资源才应被标记为“Contribute GI”。获得均匀光照的较小和凸面网格不应进行这样的标记,因此它们应该从光照探针获得间接光照;光照探针中存储了更简单的光照近似值。较大的动态对象可以依赖 LPPV,以便获得更好的本地化间接光照。要最大程度减少烘焙时间并同时保持足够的光照质量,最重要的就是限制场景中标记为“Contribute GI”的对象数量。可在本教程中详细了解这个优化过程和探针光照的重要性。
The Unity Editor and Player allow you to use both Enlighten Realtime Global Illumination and baked lighting at the same time.
However, simultaneously enabling these features greatly increases baking time and memory usage at runtime, because they do not use the same data sets. You can expect visual differences between indirect light you have baked and indirect light provided by Enlighten Realtime Global Illumination, regardless of the lightmapper you use for baking. This is because Enlighten Realtime Global Illumination often operates at a significantly different resolution than Unity’s baking backends, and relies on different techniques to simulate indirect lighting.
If you wish to use both Enlighten Realtime Global Illumination and baked lighting at the same time, limit your simultaneous use of both global illumination systems to high-end platforms and/or to projects that have tightly controlled scenes with predictable costs. Only expert users who have a very good understanding of all lighting settings can effectively use this approach. Consequently, picking one of the two global illumination systems is usually a safer strategy for most projects. Using both systems is rarely recommended.
Light 组件的 Mode 属性是一种常常让人混淆的属性。
Light Inspector 中提供了三种光照模式:
It is important to note that the mode of a light is only relevant if the Baked Global Illumination system is enabled. If you do not use any global illumination system or only use Enlighten Realtime Global Illumination system, then all Baked and Mixed lights will behave as though their Mode property was set to Realtime.
下面的图表结合了一个决策流程图和一个比较表;可以帮助您决定每次在场景中添加新光源时哪种光源模式合适。
如前面的图表中所示,场景中的所有混合光源具有特定的烘焙和实时功能,具体取决于您在 Lighting 窗口中选择的光照模式 (Lighting Mode)。
有三种模式可供选择:
Shadowmask 光照模式有两个质量设置:
使用 HDRP 的阴影遮罩光照模式 (Shadowmask Lighting Mode) 时,在 Graphics 设置中分配的 HDRP 资源中会启用阴影遮罩功能;然后必须通过 Frame Settings 专门为您的摄像机激活该功能。
See the HDRP and URP documentation for compatibility information specific to scriptable render pipelines. Unless otherwise specified, the Built-In Render Pipeline supports all features described in this article.
我们已经介绍了渲染管线和主要光照功能,现在让我们看看几个项目的例子,看看可以使用哪些设置来提供光照。由于每个项目都是独一无二的,因此您可能会根据自己的需求使用稍微不同的选项。
如果您严重依赖 Asset Store 来构建原型,则内置渲染管线可能是唯一合适的渲染管线,因为 Asset Store 中的大多数资源并不完全兼容 HDRP 和 URP;尽管如此,资源兼容性将随着时间推移而改善。如果您要从头开始构建所有资源,并且已经清楚了解了自己项目的需求,那么您可以从这两个 SRP(即 URP 或 HDRP)中选择一个,甚至创建一个自定义 SRP。
When you are in the early stage of (pre-)production and need a quick turnaround and maximum flexibility for the lighting, you might prefer a full real-time approach that does not require any precomputation, therefore you might want to turn off both Baked Global Illumination and Enlighten Realtime Global Illumination. To alleviate the lack of proper indirect lighting, you can enable Screen Space Ambient Occlusion: it can help ground the object in the scene by offering cheap real-time contact shadows.
If you are targeting mobile devices, URP could be a great candidate to ensure solid performance for your game. It is in many cases possible to customise URP to suit your game’s specific needs, with help from a graphics programmer.
The Built-In Render Pipeline and URP both support Shadowmask Lighting Mode which makes it possible for you to bake shadows for static objects while still enabling dynamic objects to cast real-time shadows. If Shadowmasks are too expensive for your project, you can fall back to the cheapest Subtractive mode. Finally, the forward rendering path is probably the best option if you have a very small number of lights in your level(s), and if you’re targeting older hardware.
如果目标是在 PC 和游戏主机上实现线性第一人称射击游戏的 AAA 级质量视觉效果,HDRP 应该是首选的渲染管线。同样,还可以在图形开发程序员的帮助下开发自定义 SRP。
If your levels contain many real-time shadow casting lights (e.g. destructible light props and moving lights), then using the Baked Global Illumination system with the Baked Indirect mode should ensure you get great looking indirect lighting from the Mixed directional light and the Baked lights in static light props. If your levels consist of a larger proportion of fixed shadow casting lights, then an approach with Shadowmasks could be recommended because HDRP offers a great hybrid Shadowmask mode which gives you more control over the blend between real-time and baked shadows.
如果您还计划支持 Nintendo Switch,则建议您使用 URP,这样您就可以支持市场上的大多数游戏平台,而不必经历将项目从 HDRP 移植到 URP(或反之)的繁琐过程。
如果您计划发布一款面向 PC 和游戏主机的大逃杀类型游戏(特征是包含大规模环境和完全动态光照),那么应该选择 HDRP,或者进行扩展以便为项目量身定制渲染管线。如果目标不是实现 AAA 级逼真视觉效果,而是面向低规格的移动设备或系统,那么可以考虑使用 URP。
For this particular scenario, if you are using the Built-in Render Pipeline, activating both the Enlighten Realtime Global Illumination and a Baked Global Illumination system is not recommended, because the resulting overhead in terms of performance and scene management for an immense level could be problematic. Another argument against the use of both global illumination systems is the unpredictable nature of such large-scale multiplayer games: performance estimations are for instance more difficult than in a highly-scripted linear level.
由于引入了可编程渲染管线,Unity 中的渲染架构在过去的几年中发生了翻天覆地的变化。因此,要弄清楚所有这些变化及其对光照管线的影响可能会令人筋疲力尽。
希望本指南和及其多个插图能让您更好地理解每个渲染管线的功能,从而能在 Unity 中满怀信心地使用合适的渲染和光照设置让您项目大放异彩!
可通过以下页面来详细了解 Unity 中的光照和渲染管线: