Unity Graphics (Unity 图形渲染 ) 官方教程文档笔记系列之七
2018-03-21 17:40
736 查看
Unity Graphics (Unity 图形渲染 )
- Everything for Lighting and Rendering in Unity
- 主要涉及到光照与渲染方面的知识
本文档主要是对Unity官方教程的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于 Unity2017.3版本
参考链接:https://unity3d.com/cn/learn/tutorials/s/graphics
下面是将要介绍的章节,黑体是本次内容:
Introduction to Lighting and Rendering
介绍光照和渲染
Precomputed Realtime GI (Realtime Global Illumination)(7)
预处理实时全局光照(7)
Rendering and Shading
渲染与着色
Cameras and Effects
相机与效果
Geometry in Unity
几何体
4.Starting the precompute process
Organizing and grouping GameObjects within the Hierarchy view makes it quick and easy to select GameObjects for inclusion in the lighting precompute. There are numerous strategies for organizing the hierarchy of GameObjects. Discussing these different strategies in detail is outside the scope of this tutorial.
4000
However, what is important is to understand that having some scheme for organizing GameObjects will greatly improve productivity when making repeated selections in the Scene.
In our example Scene, we have organised our GameObjects into classifications underneath a parent object called Environment. This group contains all the visible, static MeshRenderers which make up the environment of our Scene. Underneath this we have sub-groups of objects which have similar characteristics in terms of size or topology.
为了使用预计算实时GI生成一个光照解决方案,我们必须启动预计算处理。为此,我们必须在场景中至少有一个被标记为Lightmap Static 的对象。
在层次视图中组织和分组游戏对象使其能够快速且容易地选择包含光照预计算的游戏对象。有许多策略来组织游戏对象的层次结构。详细讨论这些不同的策略超出了本教程的范围。然而,重要的一点是:在场景中重复选择时,有计划地组织游戏对象将极大地提高生产力。
在我们的示例场景中,我们将我们的游戏对象分类到一个称为“环境”的父对象下。这个组包含所有可见的、静态的网格渲染器,它们构成了我们场景的环境。在这下面,我们有一些子组,子组以大小或拓扑结构具有相似的特征为组织。
![](https://unity3d.com/sites/default/files/hierarchy.png)
Grouping of objects within the Scene hierarchy can help when making repeated selection of objects
场景层次结构中的对象分组可以在重复选择对象时有所帮助
It is most efficient to begin by applying lighting settings in broad strokes and then progressively refine the details as we move forwards.
Select the Environment GameObject.
In the Inspector window, check the box marked Static
When asked if you wish to enable the Static flags of the child
objects as well, select Yes, change children. Doing this ensures the
GameObject and its children will receive the Lightmap Static flag.
They will then be included in the lighting precompute.
最有效的方法是先将光照设置应用于大致框架下,然后在后续改进的过程中逐步完善细节。
选择环境组下的游戏对象
在属性窗口,选择Static标记
当被问及是否希望开启子对象的静态标志和本身一致时,选择Yes,改变子对象。这样确保了游戏对象和子对象接收都置为 光照静态 标记。它们将被包括在光照预计算中。
Now that our Scene has some Static GameObjects, we can begin the precompute process.
Open the Lighting window (Window > Lighting) and then select the
Scene tab.
Ensure that the Auto checkbox is checked. The Auto checkbox can be
found next to the Build button, which will be greyed out if the Auto
checkbox is checked.
The precompute process will now begin. The progress of the precompute
process is shown in the bottom right of the Unity Editor and is
represented by a blue progress bar. The current task (written as
x/xx) and the number of jobs remaining in that task are displayed
within the progress bar.
If the Auto checkbox is not enabled, the process can be manually
initiated by clicking the Build button. When working through this
tutorial, it is recommended that Auto mode is left on.
现在我们的场景中有一些静态的游戏对象,我们可以开始预计算处理了。
打开光照窗口(窗口-灯光),然后选择 场景 选项卡。
确保选中了“自动”复选框。自动复选框可以是在“构建”按钮旁边找到它,如果“自动”复选框被选中,“构建”按钮将变灰色。
预计算过程将会开始。预计算的进展过程显示在Unity编辑器的右下方用蓝色进度条表示。当前任务(写成x/xx)和该任务中剩余的作业数量在进度条显示。
如果没有启用”自动“复选框,则可以手动进行该处理单击”Build“按钮启动。通过这个工作时教程,建议”自动“模式被开启。
![](https://unity3d.com/sites/default/files/jobs.png)
Progress bar showing the current task, remaining tasks and remaining jobs of the precompute.
进度条显示当前任务、剩余任务和预计算的剩余作业。
5.Probe lighting
在理解了我们场景中图表的数量对预计算时间不利的情况下,我们可以通过选择将某些对象排除在预计算之外,从而在照明性能上取得很大的进步。这将减少生成的图表数量。相反,我们可以在这些物体上使用探头照明,这是一种从场景中接收间接光照的有效方法。虽然探测点的物体不会向场景提供反射的光线,但通常这不会产生明显的影响。大部分适合探测光线的物体往往是较小的物体,对反射光线的影响很小。
What is probe lighting?
Probe lighting works by sampling the incoming lighting at a specific point in 3D space and encoding this information across a sphere using mathematical functions known as spherical harmonics. These coefficients have low storage cost and can be then be quickly ‘unpacked’ at gameplay and used by shaders within the Scene to approximate surface lighting. In Unity, this functionality is offered by Light Probes.
探头光照是一项快速的技术,用于接近于实时渲染应用程序中的光照效果,例如游戏。它通常用于游戏世界中的有光照的角色和其他非静态(动态)对象。探测器的照明在运行时效率是非常高的,并且有快速地进行预计算的额外的好处。
探头光照的工作原理是,在3D空间的特定点采样入射光线,并利用被称为球谐函数的数学方法将这些信息编码到一个球体上。这些系数的存储成本很低,可以在游戏中快速“解包”,并在场景中使用着色器来近似表面光照。在Unity中,这个功能是由光照探头提供的。
There are limitations to using probe lighting. One such limitation is that it is difficult to represent high frequency or dappled lighting across the spherical range without increasing the order (size) of these special numbers. Unfortunately the costs quickly escalate as the precision increases, meaning that for performance reasons we are limited to lower order spherical harmonics.
The fact that we only have one spherical representation of lighting for one 3D position in the world makes probe lighting unsuitable for representing lighting across very large objects where lots of lighting variation could be expected. Another limitation is that - as spherical harmonics encode values on a sphere - they are generally unsuitable for large planar objects or those with significant concavity. Note the Unity Manual offers some information on Light Probe Proxy Volumes (LPPV) if you are considering using probe lighting with large objects.
Despite these limitations, probe lighting will yield suitably convincing results at relatively low cost when used with suitable smaller, convex objects. Later in the tutorial we will explore the setup and placement of Light Probes in more depth, but for now, we need only understand that we can reduce the number of lightmap Charts in our Scene by instead choosing to use probe lighting where appropriate.
使用探头光照是有限制的。一个这样的限制是,在不增加这些球形谐波的阶的情况下,很难在球面范围内表示高频或有斑纹的光照。不幸的是,随着精度的增加,成本迅速上升,这意味着,由于性能的原因,我们被限制为低阶的球形谐波。
事实上,在这个世界的一个3D位置,我们只有一个球形的光照表示,使得探头光照不适合在非常大的物体上光照表示,大物体会有大量的光照变化。另一个限制是,当球谐函数在球面上编码值时,它们通常不适合大平面物体或具有显著凹性的物体。请注意,如果您正在考虑使用大型对象的探头光照,Unity手册提供了有关光照探头代理卷(LPPV)的一些信息。
尽管有这些限制,但当使用合适的较小的凸面物体时,探头光照将会以相对较低的成本产生适当的令人信服的结果。在本教程的后面,我们将深入探讨光探测的设置和放置,但是现在,我们只需要知道,我们可以减少我们场景中的光照图图表的数量,代替选择去适当地使用探头光照。
Choosing objects to light with probes
虽然在“环境”组中为了起步时设置光照而设置我们的游戏对象是静态的总是有用的,但是在这个父节点下面有很多对象可以很好地用探头光照来代替。清除这些对象的静态标志意味着它们不再被Unity的预计算实时GI系统考虑,因此将减少光照图图表的数量。请记住,减少我们场景中的图表数量是提高预计算时间的关键。
![](https://unity3d.com/sites/default/files/probelighting.png)
Small, convex debris objects make perfect cases for probe lighting.
小而凸的碎片物体为探头光照提供了完美的案例
Looking at the Hierarchy panel of the Unity Editor, let’s examine the children of the Environment GameObject. If we look specifically at the objects we find under Props, we find that many of these are small, debris style objects and Scene ‘dressing’ such as stones, buckets and wooden planks. These objects are numerous, and many of these relatively small objects would be complex to unwrap. Getting lightmap UVs that are free of distortion would most likely result in a large number of UV shells. More UV shells require extra Charts, more Charts mean more lightmap texels to compute, and so on.
查看Unity编辑器的层次结构面板,让我们来检查一下“环境”游戏对象的子元素。如果我们仔细观察我们在“小道具”组下的物体,会发现其中很多都是小的、碎片化的物体和场景的“装饰”,比如石头、水桶和木板。这些对象数量众多,而这些相对较小的对象中有许多展开是复杂的。获得不变形的光照UV很可能会产生大量的UV组。越多的UV组需要额外的图表,更多的图表意味着更多的光照图素参与计算,等等。
Given that these objects are so small, they are unlikely to contribute much to the indirect lighting within the Scene. Equally, their size means that there is little surface area in which detailed lighting variation can be displayed. This makes these objects great candidates for probe lighting. Not only will our precompute times benefit, but performance at run time will also be improved due to the reduced number of lightmaps we need to store in memory and decode in our shaders.
考虑到这些物体如此之小,它们不太可能对场景中的间接照明做出很大贡献。同样,它们的大小也意味着,几乎没有任何表面区域可以显示出详细的光照变化。这使得这些物体成为探头光照的绝佳候选者。不仅我们的预计算时间会受益,而且运行时的性能也会得到改善,因为减少了大量需要存储在内存中,并在我们的着色器中进行解码的光照图。
Select the Props GameObject in the Hierarchy view.
In the Inspector panel, uncheck the Static checkbox.
Choose Yes, change children when prompted.
在层次视图中选择“Props”游戏对象。
在属性面板中,取消选中静态复选框。
选择是,提示时同时修改子对象。
At this point, if we were to leave the precompute long enough, we would eventually see our lighting results in the Scene view. We would probably notice that our probe-lit, non-Static objects are lit differently to the objects around them and do not match the lighting of the Scene. This is because we have not yet set up our Light Probes and therefore these objects are falling-back to the Scene’s Ambient Probe. The Ambient Probe is essentially a hidden probe within the Scene, invisible to the user, which only samples the Ambient Source set in the Lighting window.
这时,如果我们让预计算足够长的时间,我们最终会看到我们的光照结果在场景视图中。我们可能会注意到,我们的探头光照,非静态的物体与周围的物体有不同的亮度,并且与场景的光照不匹配。这是因为我们还没有设置我们的光照探头,因此这些物体就会下降到场景的环境探头上。环境探头本质上是一个隐藏在场景中的探头,用户不可见,它只对光照窗口中的环境光源进行采样。
![](https://unity3d.com/sites/default/files/ambientprobe.png)
Image showing how, without light probes, non-Static objects can look out of place in their environment.
图像显示,在没有光照探头的情况下,非静态对象在其环境中会看着不合适。
In order to place our non-Static objects more convincingly into their lit surroundings we need to spend some time placing Light Probes around the Scene in order to sample indirect lighting throughout the world.
为了使我们的非静态物体更有说服力地进入他们的光照环境,我们需要花些时间在场景周围放置光照探头,以便在整个世界范围内采样间接照明。
Placing Light Probes
ca96
that they create a 3D volume, or cage.
非静态对象根据邻近的探头而接收光照。关于一个物体可能从哪个探头上“读出”的决定是通过将光照探头的空间划分成四面体的体积,然后再检查确定给定物体落在哪个四面体。因此,为了制造这些四面体,探头必须被放置好位置,这样它们就能创造出一个三维的体积,或者说是一个笼子。
![](https://unity3d.com/sites/default/files/probevisualisation.png)
Scene showing the placement of Light Probes - visualized in the editor as lit spheres.
显示光照探头的位置-在编辑器中显示为亮的球体。
Probe lighting is relatively inexpensive at run time and is quick to precompute. However, to maximise performance, some care does need to be taken when placing probes. Although quicker to setup, dense grid arrangements of Light Probes are potentially wasteful as many of these probes will not be sampling much variance in the local lighting conditions. For efficiency, it is better practice to position probes in greater density around areas where there is pronounced change in the lighting. This may include areas where there is transition from light into shadow for example, or perhaps where there are likely to be strong colors created by light bounce.
探头照明在运行时相对廉价,并且可以快速进行预计算。然而,为了最大限度地提高性能,在放置探测器时还是需要注意一些问题。尽管创建速度比较快,但密集排列的光照探头可能会造成浪费,因为许多探头在局部光照条件下不会产生太大的差异。为了提高效率,最好将探头放置在有明显变化的光照区域周围。这可能包括从光照到阴影的过渡区域,或者可能是由光反射产生的强烈的颜色的区域。
To set up Light Probes, begin by creating a Light Probe Group from the GameObject menu (GameObject > Light > Light Probe Group).
Now we will begin placing our probes. Select the newly created Light Probe Group in the Hierarchy view.
In the Inspector panel, select Edit Light Probes from the Light Probe Group component.
You can select the probes themselves in the Scene view. Delete all but one of the corner probes from the default cube arrangement which is created.
Position the remaining probe to be just above the Terrain in the environment and then duplicate it by pressing Ctrl+D (Cmd+D on Mac).
Using the Translate tool (W), move this second probe up in the Y axis to be around 2 meters above the first.
Now duplicate the probe again and move the new copy to be much higher, perhaps another 5 meters up in the Y axis.
要设置光照探头,首先要从GameObject 菜单中创建一个 Light Probe Group (GameObject > Light > Light Probe Group).
现在,我们将开始放置探头。在Hierarchy 视图中选择新创建的 Light Probe Group。
在Inspector面板中,选择Light Probe Group 组件中的Edit Light Probes。
您可以在Scene 视图中自己选择探头。从创建的默认立方排列中把除边上的所有探头删除。(Unity2017.3默认只有边角有探头)
将剩余的探头放置在地型上环境中,然后按Ctrl+D复制(Mac上是Cmd+D).
使用Translate工具(W),将第二个探头沿Y轴向上移动,大约在第一个对象的上方2米处。
现在再次复制探头,并将新拷贝移动得更高,也许在Y轴上再增加5米。
![](https://unity3d.com/sites/default/files/lightprobegroup.png)
The Edit Light Probes button within the Light Probe Group component.
在Light Probe Group组件中的Edit Light Probes按钮。
The reason for creating a vertical arrangement like this is so that we can sample the indirect lighting bounced from the ground, from around head height and also in the air (it is feasible that objects might leave the ground). When we copy these Light Probes around the Scene in order to create our volumes, we want to ensure objects anywhere in the playable area will fall within one of the tetrahedrons created between the probes. These volumes are visualized as magenta lines drawn between the probes.
垂直排列的原因是为了让我们能够对间接光线进行采样,可能从地面,头部高度还有空中的反射光照(物体可能离开地面)。当我们在场景周围复制这些光探测器来创建我们的体积时,我们要确保在可玩区域的任何地方的对象都将落在探头之间创建的一个四面体内。这些体积被可视化为在探头之间绘制的品红线。
![](https://unity3d.com/sites/default/files/tetrahedrons.png)
Image showing position of Light Probes and the tetrahedrons made up between their positions.
图像显示光照探头的位置和在它们的位置之间的四面体。
Select all 3 probes you just created by holding Shift and clicking each probe individually, or by dragging a selection.
Duplicate the whole vertical chain we have created and move this to a nearby area of lighting interest in the Scene.
选择您刚才创建的3个探头,通过按住Shift键,分别单击每个探针,或者拖动选区。
复制我们所创建的整个垂直链条,并将其移动到附近的光照区域。
When deciding what is an area of lighting interest, look for areas of shadow, or perhaps where there is a strong change in the color of the Terrain material. Remember we are aiming to sample indirect, or bounced, lighting throughout the Scene. In order to justify the cost of adding a new probe, we must ensure that it is sampling some noticeable change in the lighting. If we were to use our Light Probes to sample areas with general or consistent lighting, then we are unlikely to see much change as the receiving object passes these probes. Like many aspects of game optimisation, we have to ensure every item we include in our Scene offers some benefit.
在决定什么是光照感兴趣的区域时,要寻找阴影区域,或者是地形材质的颜色有很大变化的地方。请记住,我们的目标是在整个场景中采样间接或反射的光照。为了证明增加一个新探头的成本是合理的,我们必须确保它是在对照明进行一些明显的改变。如果我们使用我们的光照探头来采样具有一般或一致照明的区域,那么当通过这些探头接收对象时,我们不太可能看到太多的变化。就像游戏优化的许多方面一样,我们必须确保在我们的场景中包含的每一项都能带来一些好处。
Continue repeating this process, positioning probes in greater density around areas of lighting change until you have created a sparse cage layout which encapsulates the playable area.
While positioning our probes, always remember to check the bottom-most Light Probe remains just off the ground.
继续重复这一过程,在照明改变的区域内放置更密集的探头,直到你创建了一个封装可玩的区域稀疏的笼子布局。
在定位探测器的位置时,要记得检查底部的光照探头是否在地面上。
In our example LightingTutorialOptimal Scene we have created two groups of Light Probes: one for the more densely populated village area called VillageLightProbeGroup, and one for the sparse non-playable area outside the village called ExtentsLightProbeGroup. This second group covers the extents of the game world in case any non-Static MeshRenderers leave the playable area. We don’t need the same degree of fidelity in these unreachable parts of the Scene therefore this group can be much less dense. Two probes in the vertical axis is sufficient.
Splitting the Light Probe Groups into two GameObjects makes it easier to position the probes. Individual Light Probe Groups can be enabled or disabled to offer a clearer view when working in the Scene. These groups will be automatically combined at run time, along with a de-duplication pass to remove any overlapping probes.
在我们的 LightingTutorialOptimal 场景中,我们创造了两组光照探头:一组是在人口密度更大的村庄叫做VillageLightProbeGroup,另一组是在村庄外的一个叫ExtentsLightProbeGroup的非可玩区域。第二组涵盖了游戏世界的范围,以防任何非静态的网格渲染器离开可玩区域。这些不可到达的地方我们并不需要相同程度的精度,因此这个群体的密度会小得多。垂直轴上的两个探头就足够了。
将光照探头组分割为两个子对象,使得定位探头更容易。在场景中工作时,可以启用或禁用单个光照探头组来提供更清晰的视图。这些组将在运行时自动组合,同时还可以通过重复数据删除操作来删除任何重叠的探头。
To see our Light Probes lit according to their environment, we must wait for a lighting precompute to complete. If Auto mode is enabled in the Lighting window, this will happen automatically. If Auto mode is not enabled, we must manually begin a precompute by navigating to the Lighting window (Window > Lighting) and pressing the Build button.
Once this process has completed, you will notice that the non-Static GameObjects in the Scene now read from the Light Probes and look better matched to the Scene lighting. To see Light Probes in their lit state, deselect the Light Probe Group in the Hierarchy view.
Now that we have set up Light Probes in our Scene, we get the visual benefits of indirect lighting on our smaller ‘prop’ objects without the expense of extra Charts created by the inclusion of these objects in the precompute.
根据他们的环境要看到我们的光照探头光照效果,我们必须等待光照预计算完成。如果在照明窗口中启用自动模式,这将自动发生。如果没有启用自动模式,我们必须手动启动预计算,通过导航到光照窗口(Window > Lighting)并按下Build按钮。
一旦这个过程完成,你会注意到场景中的非静态的游戏对象现在从光照探头中读取,并且看起来与场景的灯光效果配合很好。要在它们的光照状态下看到光照探头点亮效果,在Hierarchy 视图中取消选择光照探头组。
既然我们已经在我们的场景中设置了光照探头,我们就可以得到我们较小“Prop”游戏对象组的间接照明的视觉效果,而不用在预计算中包含这些对象的额外的图表花费。
- Everything for Lighting and Rendering in Unity
- 主要涉及到光照与渲染方面的知识
本文档主要是对Unity官方教程的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于 Unity2017.3版本
参考链接:https://unity3d.com/cn/learn/tutorials/s/graphics
下面是将要介绍的章节,黑体是本次内容:
Introduction to Lighting and Rendering
介绍光照和渲染
Precomputed Realtime GI (Realtime Global Illumination)(7)
预处理实时全局光照(7)
Rendering and Shading
渲染与着色
Cameras and Effects
相机与效果
Geometry in Unity
几何体
4.Starting the precompute process
4.开始预计算处理
To generate a lighting solution using Precomputed Realtime GI, we must initiate the precompute process. For this to begin, we must have at least one object marked as Lightmap Static in our Scene.Organizing and grouping GameObjects within the Hierarchy view makes it quick and easy to select GameObjects for inclusion in the lighting precompute. There are numerous strategies for organizing the hierarchy of GameObjects. Discussing these different strategies in detail is outside the scope of this tutorial.
4000
However, what is important is to understand that having some scheme for organizing GameObjects will greatly improve productivity when making repeated selections in the Scene.
In our example Scene, we have organised our GameObjects into classifications underneath a parent object called Environment. This group contains all the visible, static MeshRenderers which make up the environment of our Scene. Underneath this we have sub-groups of objects which have similar characteristics in terms of size or topology.
为了使用预计算实时GI生成一个光照解决方案,我们必须启动预计算处理。为此,我们必须在场景中至少有一个被标记为Lightmap Static 的对象。
在层次视图中组织和分组游戏对象使其能够快速且容易地选择包含光照预计算的游戏对象。有许多策略来组织游戏对象的层次结构。详细讨论这些不同的策略超出了本教程的范围。然而,重要的一点是:在场景中重复选择时,有计划地组织游戏对象将极大地提高生产力。
在我们的示例场景中,我们将我们的游戏对象分类到一个称为“环境”的父对象下。这个组包含所有可见的、静态的网格渲染器,它们构成了我们场景的环境。在这下面,我们有一些子组,子组以大小或拓扑结构具有相似的特征为组织。
![](https://unity3d.com/sites/default/files/hierarchy.png)
Grouping of objects within the Scene hierarchy can help when making repeated selection of objects
场景层次结构中的对象分组可以在重复选择对象时有所帮助
It is most efficient to begin by applying lighting settings in broad strokes and then progressively refine the details as we move forwards.
Select the Environment GameObject.
In the Inspector window, check the box marked Static
When asked if you wish to enable the Static flags of the child
objects as well, select Yes, change children. Doing this ensures the
GameObject and its children will receive the Lightmap Static flag.
They will then be included in the lighting precompute.
最有效的方法是先将光照设置应用于大致框架下,然后在后续改进的过程中逐步完善细节。
选择环境组下的游戏对象
在属性窗口,选择Static标记
当被问及是否希望开启子对象的静态标志和本身一致时,选择Yes,改变子对象。这样确保了游戏对象和子对象接收都置为 光照静态 标记。它们将被包括在光照预计算中。
Now that our Scene has some Static GameObjects, we can begin the precompute process.
Open the Lighting window (Window > Lighting) and then select the
Scene tab.
Ensure that the Auto checkbox is checked. The Auto checkbox can be
found next to the Build button, which will be greyed out if the Auto
checkbox is checked.
The precompute process will now begin. The progress of the precompute
process is shown in the bottom right of the Unity Editor and is
represented by a blue progress bar. The current task (written as
x/xx) and the number of jobs remaining in that task are displayed
within the progress bar.
If the Auto checkbox is not enabled, the process can be manually
initiated by clicking the Build button. When working through this
tutorial, it is recommended that Auto mode is left on.
现在我们的场景中有一些静态的游戏对象,我们可以开始预计算处理了。
打开光照窗口(窗口-灯光),然后选择 场景 选项卡。
确保选中了“自动”复选框。自动复选框可以是在“构建”按钮旁边找到它,如果“自动”复选框被选中,“构建”按钮将变灰色。
预计算过程将会开始。预计算的进展过程显示在Unity编辑器的右下方用蓝色进度条表示。当前任务(写成x/xx)和该任务中剩余的作业数量在进度条显示。
如果没有启用”自动“复选框,则可以手动进行该处理单击”Build“按钮启动。通过这个工作时教程,建议”自动“模式被开启。
![](https://unity3d.com/sites/default/files/jobs.png)
Progress bar showing the current task, remaining tasks and remaining jobs of the precompute.
进度条显示当前任务、剩余任务和预计算的剩余作业。
5.Probe lighting
5.探头光照
Having understood that the number of Charts in our Scene is detrimental to precompute times, we are able to make big gains in lighting performance by choosing to exclude certain objects from the precompute altogether. This will reduce the number of Charts generated. Instead, we can use probe lighting on these objects, which is an efficient means of receiving indirect lighting from the Scene. Although probe lit objects will not contribute bounced lighting to the Scene, often this is has little noticeable impact. The objects most suitable for probe lighting tend to be smaller objects which would have little effect on bounced lighting anyway.在理解了我们场景中图表的数量对预计算时间不利的情况下,我们可以通过选择将某些对象排除在预计算之外,从而在照明性能上取得很大的进步。这将减少生成的图表数量。相反,我们可以在这些物体上使用探头照明,这是一种从场景中接收间接光照的有效方法。虽然探测点的物体不会向场景提供反射的光线,但通常这不会产生明显的影响。大部分适合探测光线的物体往往是较小的物体,对反射光线的影响很小。
What is probe lighting?
什么是探头光照?
Probe lighting is a fast technique for approximating lighting in realtime rendering applications such as games. It is commonly used for lighting characters and other non-Static (dynamic) objects in the game world. Probe lighting is very performant at run time and has the added benefit that it is quick to precompute.Probe lighting works by sampling the incoming lighting at a specific point in 3D space and encoding this information across a sphere using mathematical functions known as spherical harmonics. These coefficients have low storage cost and can be then be quickly ‘unpacked’ at gameplay and used by shaders within the Scene to approximate surface lighting. In Unity, this functionality is offered by Light Probes.
探头光照是一项快速的技术,用于接近于实时渲染应用程序中的光照效果,例如游戏。它通常用于游戏世界中的有光照的角色和其他非静态(动态)对象。探测器的照明在运行时效率是非常高的,并且有快速地进行预计算的额外的好处。
探头光照的工作原理是,在3D空间的特定点采样入射光线,并利用被称为球谐函数的数学方法将这些信息编码到一个球体上。这些系数的存储成本很低,可以在游戏中快速“解包”,并在场景中使用着色器来近似表面光照。在Unity中,这个功能是由光照探头提供的。
There are limitations to using probe lighting. One such limitation is that it is difficult to represent high frequency or dappled lighting across the spherical range without increasing the order (size) of these special numbers. Unfortunately the costs quickly escalate as the precision increases, meaning that for performance reasons we are limited to lower order spherical harmonics.
The fact that we only have one spherical representation of lighting for one 3D position in the world makes probe lighting unsuitable for representing lighting across very large objects where lots of lighting variation could be expected. Another limitation is that - as spherical harmonics encode values on a sphere - they are generally unsuitable for large planar objects or those with significant concavity. Note the Unity Manual offers some information on Light Probe Proxy Volumes (LPPV) if you are considering using probe lighting with large objects.
Despite these limitations, probe lighting will yield suitably convincing results at relatively low cost when used with suitable smaller, convex objects. Later in the tutorial we will explore the setup and placement of Light Probes in more depth, but for now, we need only understand that we can reduce the number of lightmap Charts in our Scene by instead choosing to use probe lighting where appropriate.
使用探头光照是有限制的。一个这样的限制是,在不增加这些球形谐波的阶的情况下,很难在球面范围内表示高频或有斑纹的光照。不幸的是,随着精度的增加,成本迅速上升,这意味着,由于性能的原因,我们被限制为低阶的球形谐波。
事实上,在这个世界的一个3D位置,我们只有一个球形的光照表示,使得探头光照不适合在非常大的物体上光照表示,大物体会有大量的光照变化。另一个限制是,当球谐函数在球面上编码值时,它们通常不适合大平面物体或具有显著凹性的物体。请注意,如果您正在考虑使用大型对象的探头光照,Unity手册提供了有关光照探头代理卷(LPPV)的一些信息。
尽管有这些限制,但当使用合适的较小的凸面物体时,探头光照将会以相对较低的成本产生适当的令人信服的结果。在本教程的后面,我们将深入探讨光探测的设置和放置,但是现在,我们只需要知道,我们可以减少我们场景中的光照图图表的数量,代替选择去适当地使用探头光照。
Choosing objects to light with probes
用探头选择对象
While it was broadly useful to set our GameObjects under the Environment group to be Static in order to get our lighting setup underway, there are many objects under this parent which would make good candidates for probe lighting instead. Clearing the Static flags for these objects will mean that they are no longer considered by Unity’s Precomputed Realtime GI system and therefore the number of lightmap Charts will be reduced. Remember that reducing the number of Charts in our Scene is key to improving precompute times.虽然在“环境”组中为了起步时设置光照而设置我们的游戏对象是静态的总是有用的,但是在这个父节点下面有很多对象可以很好地用探头光照来代替。清除这些对象的静态标志意味着它们不再被Unity的预计算实时GI系统考虑,因此将减少光照图图表的数量。请记住,减少我们场景中的图表数量是提高预计算时间的关键。
![](https://unity3d.com/sites/default/files/probelighting.png)
Small, convex debris objects make perfect cases for probe lighting.
小而凸的碎片物体为探头光照提供了完美的案例
Looking at the Hierarchy panel of the Unity Editor, let’s examine the children of the Environment GameObject. If we look specifically at the objects we find under Props, we find that many of these are small, debris style objects and Scene ‘dressing’ such as stones, buckets and wooden planks. These objects are numerous, and many of these relatively small objects would be complex to unwrap. Getting lightmap UVs that are free of distortion would most likely result in a large number of UV shells. More UV shells require extra Charts, more Charts mean more lightmap texels to compute, and so on.
查看Unity编辑器的层次结构面板,让我们来检查一下“环境”游戏对象的子元素。如果我们仔细观察我们在“小道具”组下的物体,会发现其中很多都是小的、碎片化的物体和场景的“装饰”,比如石头、水桶和木板。这些对象数量众多,而这些相对较小的对象中有许多展开是复杂的。获得不变形的光照UV很可能会产生大量的UV组。越多的UV组需要额外的图表,更多的图表意味着更多的光照图素参与计算,等等。
Given that these objects are so small, they are unlikely to contribute much to the indirect lighting within the Scene. Equally, their size means that there is little surface area in which detailed lighting variation can be displayed. This makes these objects great candidates for probe lighting. Not only will our precompute times benefit, but performance at run time will also be improved due to the reduced number of lightmaps we need to store in memory and decode in our shaders.
考虑到这些物体如此之小,它们不太可能对场景中的间接照明做出很大贡献。同样,它们的大小也意味着,几乎没有任何表面区域可以显示出详细的光照变化。这使得这些物体成为探头光照的绝佳候选者。不仅我们的预计算时间会受益,而且运行时的性能也会得到改善,因为减少了大量需要存储在内存中,并在我们的着色器中进行解码的光照图。
Select the Props GameObject in the Hierarchy view.
In the Inspector panel, uncheck the Static checkbox.
Choose Yes, change children when prompted.
在层次视图中选择“Props”游戏对象。
在属性面板中,取消选中静态复选框。
选择是,提示时同时修改子对象。
At this point, if we were to leave the precompute long enough, we would eventually see our lighting results in the Scene view. We would probably notice that our probe-lit, non-Static objects are lit differently to the objects around them and do not match the lighting of the Scene. This is because we have not yet set up our Light Probes and therefore these objects are falling-back to the Scene’s Ambient Probe. The Ambient Probe is essentially a hidden probe within the Scene, invisible to the user, which only samples the Ambient Source set in the Lighting window.
这时,如果我们让预计算足够长的时间,我们最终会看到我们的光照结果在场景视图中。我们可能会注意到,我们的探头光照,非静态的物体与周围的物体有不同的亮度,并且与场景的光照不匹配。这是因为我们还没有设置我们的光照探头,因此这些物体就会下降到场景的环境探头上。环境探头本质上是一个隐藏在场景中的探头,用户不可见,它只对光照窗口中的环境光源进行采样。
![](https://unity3d.com/sites/default/files/ambientprobe.png)
Image showing how, without light probes, non-Static objects can look out of place in their environment.
图像显示,在没有光照探头的情况下,非静态对象在其环境中会看着不合适。
In order to place our non-Static objects more convincingly into their lit surroundings we need to spend some time placing Light Probes around the Scene in order to sample indirect lighting throughout the world.
为了使我们的非静态物体更有说服力地进入他们的光照环境,我们需要花些时间在场景周围放置光照探头,以便在整个世界范围内采样间接照明。
Placing Light Probes
放置光照探头
Non-Static objects receive lighting based on proximity to nearby probes. The decision about which probe an object might ‘read’ from is made by dividing the space between the Light Probes into tetrahedral volumes and then checking to see which tetrahedron the given object falls within. In order to make these tetrahedrons, the probes must therefore be positioned suchca96
that they create a 3D volume, or cage.
非静态对象根据邻近的探头而接收光照。关于一个物体可能从哪个探头上“读出”的决定是通过将光照探头的空间划分成四面体的体积,然后再检查确定给定物体落在哪个四面体。因此,为了制造这些四面体,探头必须被放置好位置,这样它们就能创造出一个三维的体积,或者说是一个笼子。
![](https://unity3d.com/sites/default/files/probevisualisation.png)
Scene showing the placement of Light Probes - visualized in the editor as lit spheres.
显示光照探头的位置-在编辑器中显示为亮的球体。
Probe lighting is relatively inexpensive at run time and is quick to precompute. However, to maximise performance, some care does need to be taken when placing probes. Although quicker to setup, dense grid arrangements of Light Probes are potentially wasteful as many of these probes will not be sampling much variance in the local lighting conditions. For efficiency, it is better practice to position probes in greater density around areas where there is pronounced change in the lighting. This may include areas where there is transition from light into shadow for example, or perhaps where there are likely to be strong colors created by light bounce.
探头照明在运行时相对廉价,并且可以快速进行预计算。然而,为了最大限度地提高性能,在放置探测器时还是需要注意一些问题。尽管创建速度比较快,但密集排列的光照探头可能会造成浪费,因为许多探头在局部光照条件下不会产生太大的差异。为了提高效率,最好将探头放置在有明显变化的光照区域周围。这可能包括从光照到阴影的过渡区域,或者可能是由光反射产生的强烈的颜色的区域。
To set up Light Probes, begin by creating a Light Probe Group from the GameObject menu (GameObject > Light > Light Probe Group).
Now we will begin placing our probes. Select the newly created Light Probe Group in the Hierarchy view.
In the Inspector panel, select Edit Light Probes from the Light Probe Group component.
You can select the probes themselves in the Scene view. Delete all but one of the corner probes from the default cube arrangement which is created.
Position the remaining probe to be just above the Terrain in the environment and then duplicate it by pressing Ctrl+D (Cmd+D on Mac).
Using the Translate tool (W), move this second probe up in the Y axis to be around 2 meters above the first.
Now duplicate the probe again and move the new copy to be much higher, perhaps another 5 meters up in the Y axis.
要设置光照探头,首先要从GameObject 菜单中创建一个 Light Probe Group (GameObject > Light > Light Probe Group).
现在,我们将开始放置探头。在Hierarchy 视图中选择新创建的 Light Probe Group。
在Inspector面板中,选择Light Probe Group 组件中的Edit Light Probes。
您可以在Scene 视图中自己选择探头。从创建的默认立方排列中把除边上的所有探头删除。(Unity2017.3默认只有边角有探头)
将剩余的探头放置在地型上环境中,然后按Ctrl+D复制(Mac上是Cmd+D).
使用Translate工具(W),将第二个探头沿Y轴向上移动,大约在第一个对象的上方2米处。
现在再次复制探头,并将新拷贝移动得更高,也许在Y轴上再增加5米。
![](https://unity3d.com/sites/default/files/lightprobegroup.png)
The Edit Light Probes button within the Light Probe Group component.
在Light Probe Group组件中的Edit Light Probes按钮。
The reason for creating a vertical arrangement like this is so that we can sample the indirect lighting bounced from the ground, from around head height and also in the air (it is feasible that objects might leave the ground). When we copy these Light Probes around the Scene in order to create our volumes, we want to ensure objects anywhere in the playable area will fall within one of the tetrahedrons created between the probes. These volumes are visualized as magenta lines drawn between the probes.
垂直排列的原因是为了让我们能够对间接光线进行采样,可能从地面,头部高度还有空中的反射光照(物体可能离开地面)。当我们在场景周围复制这些光探测器来创建我们的体积时,我们要确保在可玩区域的任何地方的对象都将落在探头之间创建的一个四面体内。这些体积被可视化为在探头之间绘制的品红线。
![](https://unity3d.com/sites/default/files/tetrahedrons.png)
Image showing position of Light Probes and the tetrahedrons made up between their positions.
图像显示光照探头的位置和在它们的位置之间的四面体。
Select all 3 probes you just created by holding Shift and clicking each probe individually, or by dragging a selection.
Duplicate the whole vertical chain we have created and move this to a nearby area of lighting interest in the Scene.
选择您刚才创建的3个探头,通过按住Shift键,分别单击每个探针,或者拖动选区。
复制我们所创建的整个垂直链条,并将其移动到附近的光照区域。
When deciding what is an area of lighting interest, look for areas of shadow, or perhaps where there is a strong change in the color of the Terrain material. Remember we are aiming to sample indirect, or bounced, lighting throughout the Scene. In order to justify the cost of adding a new probe, we must ensure that it is sampling some noticeable change in the lighting. If we were to use our Light Probes to sample areas with general or consistent lighting, then we are unlikely to see much change as the receiving object passes these probes. Like many aspects of game optimisation, we have to ensure every item we include in our Scene offers some benefit.
在决定什么是光照感兴趣的区域时,要寻找阴影区域,或者是地形材质的颜色有很大变化的地方。请记住,我们的目标是在整个场景中采样间接或反射的光照。为了证明增加一个新探头的成本是合理的,我们必须确保它是在对照明进行一些明显的改变。如果我们使用我们的光照探头来采样具有一般或一致照明的区域,那么当通过这些探头接收对象时,我们不太可能看到太多的变化。就像游戏优化的许多方面一样,我们必须确保在我们的场景中包含的每一项都能带来一些好处。
Continue repeating this process, positioning probes in greater density around areas of lighting change until you have created a sparse cage layout which encapsulates the playable area.
While positioning our probes, always remember to check the bottom-most Light Probe remains just off the ground.
继续重复这一过程,在照明改变的区域内放置更密集的探头,直到你创建了一个封装可玩的区域稀疏的笼子布局。
在定位探测器的位置时,要记得检查底部的光照探头是否在地面上。
In our example LightingTutorialOptimal Scene we have created two groups of Light Probes: one for the more densely populated village area called VillageLightProbeGroup, and one for the sparse non-playable area outside the village called ExtentsLightProbeGroup. This second group covers the extents of the game world in case any non-Static MeshRenderers leave the playable area. We don’t need the same degree of fidelity in these unreachable parts of the Scene therefore this group can be much less dense. Two probes in the vertical axis is sufficient.
Splitting the Light Probe Groups into two GameObjects makes it easier to position the probes. Individual Light Probe Groups can be enabled or disabled to offer a clearer view when working in the Scene. These groups will be automatically combined at run time, along with a de-duplication pass to remove any overlapping probes.
在我们的 LightingTutorialOptimal 场景中,我们创造了两组光照探头:一组是在人口密度更大的村庄叫做VillageLightProbeGroup,另一组是在村庄外的一个叫ExtentsLightProbeGroup的非可玩区域。第二组涵盖了游戏世界的范围,以防任何非静态的网格渲染器离开可玩区域。这些不可到达的地方我们并不需要相同程度的精度,因此这个群体的密度会小得多。垂直轴上的两个探头就足够了。
将光照探头组分割为两个子对象,使得定位探头更容易。在场景中工作时,可以启用或禁用单个光照探头组来提供更清晰的视图。这些组将在运行时自动组合,同时还可以通过重复数据删除操作来删除任何重叠的探头。
To see our Light Probes lit according to their environment, we must wait for a lighting precompute to complete. If Auto mode is enabled in the Lighting window, this will happen automatically. If Auto mode is not enabled, we must manually begin a precompute by navigating to the Lighting window (Window > Lighting) and pressing the Build button.
Once this process has completed, you will notice that the non-Static GameObjects in the Scene now read from the Light Probes and look better matched to the Scene lighting. To see Light Probes in their lit state, deselect the Light Probe Group in the Hierarchy view.
Now that we have set up Light Probes in our Scene, we get the visual benefits of indirect lighting on our smaller ‘prop’ objects without the expense of extra Charts created by the inclusion of these objects in the precompute.
根据他们的环境要看到我们的光照探头光照效果,我们必须等待光照预计算完成。如果在照明窗口中启用自动模式,这将自动发生。如果没有启用自动模式,我们必须手动启动预计算,通过导航到光照窗口(Window > Lighting)并按下Build按钮。
一旦这个过程完成,你会注意到场景中的非静态的游戏对象现在从光照探头中读取,并且看起来与场景的灯光效果配合很好。要在它们的光照状态下看到光照探头点亮效果,在Hierarchy 视图中取消选择光照探头组。
既然我们已经在我们的场景中设置了光照探头,我们就可以得到我们较小“Prop”游戏对象组的间接照明的视觉效果,而不用在预计算中包含这些对象的额外的图表花费。
相关文章推荐
- Unity Graphics (Unity 图形渲染 ) 官方教程文档笔记系列之一
- Unity Graphics (Unity 图形渲染 ) 官方教程文档笔记系列之三
- Unity Graphics (Unity 图形渲染 ) 官方教程文档笔记系列之二
- Unity Graphics (Unity 图形渲染 ) 官方教程文档笔记系列之五
- Unity Graphics (Unity 图形渲染 ) 官方教程文档笔记系列之六
- Unity Graphics (Unity 图形渲染 ) 官方教程文档笔记系列之八
- Unity Graphics (Unity 图形渲染 ) 官方教程文档笔记系列之四
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之摄像机介绍Cameras
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之网格渲染器和过滤器Mesh renderers and filters
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之网格Meshes
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之材质了解Materials
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之纹理Textures
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之灯光介绍Lights
- [Unity2D] Unity官方 Native 2D中文系列教程总共36讲 更新至15讲
- Unity5.3官方VR教程重磅登场-系列4 VR中的用户界面
- Unity游戏图形渲染效果系列之基于物理的渲染
- Unity官方游戏实例视频中文教程之太空射击学习笔记
- Unity5.3官方VR教程-系列1
- Unity5.3官方VR教程重磅登场-系列5 VR中的运动
- Unity官方教程学习笔记之Roll A Ball篇---(一)创建工程和保存场景