Unity Graphics (Unity 图形渲染 ) 官方教程文档笔记系列之九

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)(9)
    预处理实时全局光照(9)
  • Rendering and Shading
    渲染与着色
  • Cameras and Effects
    相机与效果
  • Geometry in Unity
    几何体

7.Optimizing Unity’s auto unwrapping

7.优化Unity的自动展开

When configuring unwrap parameters, the ideal outcome is to find a combination of values that results in the lowest number of Charts while having the least lightmap distortion. Remember that distortion is visualized as stretching of the checkerboard texture applied to objects in the Scene view when UV Charts draw mode is enabled.

在配置展开参数时,理想的结果是找到一组值的组合,这些值在最少的光照映射失真的情况下,图表的数量最少。请记住,当UV图表绘制模式开启时,在场景视图中扭曲将可视化,比如对象的棋盘纹理拉伸。
这里写图片描述
The checkerboard pattern can be used to visualize lightmap texel distribution. Notice here that the checkerboard is relatively consistent across our model - meaning there is little lightmap distortion.
棋盘图可以用来表示可视化光照图贴图的分布。注意这里的棋盘和我们的模型是相对一致的-这意味着更少的光照图扭曲。
这里写图片描述
Stretching and warping of the checkerboard indicates that there is some inconsistency in the distribution of lightmap texels.
棋盘格的拉伸和变形表明,光照图贴图的分布有一些不一致的地方。

In this example, we will apply what we have learned about UV unwrapping to optimize precompute times on an object in our tutorial project.
在本例中,我们将应用我们所学到的关于UV展开的知识,以优化我们的教程项目中的对象的预计算时间。

• Open the LightingTutorialStart Scene included in the example project.
• Select one of the objects called HouseBig02 in the Hierarchy window.These are grouped under Environment > Structures > Houses.
• Open the Lighting window (Window > Lighting) and select the Object tab.
• Select Charting from the drop-down menu in the top left of the tab.
• Expand the Preview area at the bottom of the tab. Note how many UV shells are displayed here and the number of corresponding colored Charts.
• 打开示例项目中包含的 LightingTutorialStart 场景
• 在层次结构窗口中选择名为HouseBig02的对象。这些在Environment > Structures > Houses 分组下。
• 打开光照窗口(窗口-灯光)并选择Object选项卡。
• 从选项卡左上角的下拉菜单中选择图表。
• 在选项卡的底部展开预览区域。请注意这里显示了多少个shells,以及相应的彩色图表的数量。
这里写图片描述
Image showing the Charting preview for the HouseBig02 object with default unwrap settings. This number of Charts can be easily reduced.
图像显示了带有默认展开设置的 HouseBig02 对象的图表预览。可以很容易地减少这一图表的数量。

HouseBig02 is a fairly complex object within our Scene, which may justify the use of multiple Charts. However, we may still be able to reduce this number by modifying our unwrap settings.
在我们的场景中,HouseBig02 是一个相当复杂的对象,这可能会证明使用多个图表是合理的。但是,我们仍然可以通过修改我们的展开设置来减少此数量。
这里写图片描述
The Object unwrap settings panel
对象展开设置面板

There is no one-size fits all solution for unwrapping objects. Unity’s unwrapping algorithms attempt to make sensible decisions based on appropriate defaults, but with some user guidance, we may be able to achieve a more optimal result.
- Select HouseBig02 in the Hierarchy window.
- Open the Lighting window (Window > Lighting) and select the Object tab.
- Lower the Auto UV Max Distance to its minimum value of 0.1.

没有一种适合所有展开对象的解决方案。Unity的展开算法试图基于适当的默认值做出明智的决定,但是在一些用户导则下,我们可能会得到一个更优的结果。
- Hierarchy 窗口下选择HouseBig02
- 打开光照窗口,选择 Object 页签
- 把自动UV最大距离调低到最小于0.1
The precompute process will begin. Following a brief calculation, we can see in the Preview area of the Lighting window that number of Charts has noticeably increased. Checking the Scene view there seems to be little distortion, which is good. Unfortunately, however, this many Charts for an object of this complexity is unacceptable. Remember that more Charts means longer precompute times and reduced performance at run time.
预计算过程将开始。经过简单的计算,我们可以看到在灯光窗口的预览区域,图表的数量明显增加了。检查场景视图似乎没有什么失真,这很好。然而,不幸的是,这个复杂的对象的许多图表是不可接受的。请记住,更多的图表意味着更长的预计算时间和运行时性能的降低。

  • If HouseBig02 is not selected in the Hierarchy window, select it.
  • Navigate to the Lighting window (Window > Lighting) and select the Object tab.
  • Set the value of Auto UV Max Distance to 10.
  • Hierarchy 窗口下选择HouseBig02
  • 打开光照窗口,选择 Object 页签
  • 把自动UV最大距离调到最10

We now have the opposite problem. The number of Charts has decreased, but at the cost of introducing unacceptable distortion to our lightmaps. We can observe this distortion in the UV Charts Scene draw mode. The distortion appears as streaking or stretching of the checkerboard overlay.
我们现在面临着相反的问题。图表的数量减少了,但代价是在我们的光照图呈现出不可接受的失真。我们可以在UV图表绘制模式中观察到这种变形。这种扭曲表现为棋盘状覆盖的条纹或拉伸。
这里写图片描述

Selecting an Auto UV Max Distance which is too high can result in distortion of our lightmap UVs.
选择一个太高的自动UV最大值距离可能会导致我们的光照图UVs的变形。

We know that Unity’s unwrapping algorithm attempts to minimise the number of Charts by merging UV shells together. The Auto UV Max Distance setting specifies the maximum distance, in worldspace, permitted between UV shells when they are considered for merging. If the shells are further apart than this value, they will not be combined.
When we decreased our Auto UV Max Distance, fewer UV shells were eligible for merging. This meant that more individual Charts remained at the end of the process. When we increased our Auto UV Max Distance, UV shells that were very far apart in worldspace were then able to be merged. This resulted in fewer Charts, but this aggressive merging also caused warped lightmap UVs and therefore more distortion in our lightmap output.
Note that with large objects it can sometimes be necessary to increase the Auto UV Max Distance in order to accommodate the size of faces within the mesh. Conversely, with smaller objects decreasing the Auto UV Max Distance might yield better results.
我们知道,Unity的展开算法试图通过合并UV shell来最小化图表的数量。自动UV最大距离设置指定了在世界空间中,当被考虑合并时在UV shell之间允许的最大距离。如果这些shells 比这个值更大,它们就不会被合并。
当我们减少我们的自动UV最大距离时,更少的UV可以合并。这意味着在这个过程的最后,会有更多的单独的图表。当我们增加我们的自动UV最大距离时,在世界空间中相距很远的UV shells 就能被合并。这导致了更少的图表,但是这种侵略性的合并也导致了扭曲的光照映射UVs,因此在我们的光照图输出中会有更多的失真。
请注意,对于大的对象,有时为了适应网格内的面数大小,有时需要增加自动UV的最大距离。相反地,小的物体减少了自动UV最大距离可能产生更好的结果。

With the example of our object, HouseBig02, an Auto UV Max Distance value of 0.8 gives a good compromise between Chart count and distortion.
- If HouseBig02 is not selected in the Hierarchy window, select it.
- Navigate to the Lighting window (Window > Lighting) and select the Object tab.
- Set the value of Auto UV Max Distance to 0.8.
以我们的对象,HouseBig02的例子,一个自动UV最大距离值为0.8,在图表计数和失真之间提供了一个很好的折衷。
- Hierarchy 窗口下选择HouseBig02
- 打开光照窗口,选择 Object 页签
- 把自动UV最大距离调到最0.8
这里写图片描述
Using a Auto UV Max Distance of 0.8 gives a good result with our example object. However, we can still improve this further.
使用一个自动UV最大距离为0.8的例子,可以得到一个好的结果。然而,我们仍然可以进一步改进。

Now we will look at the Auto UV Max Angle parameter. Like Auto UV Max Distance, this value affects the UV merging step of Unity’s unwrapping algorithms. In addition to being tested for distance before UV shells are combined, the unwrapper also tests the angle between adjacent mesh faces. Only edges with an angle below this value will be considered for merging.
A smaller Auto UV Max Angle means that fewer edges will pass the test. As a result, fewer UV shells will be merged and the number of Charts will increase. Conversely, a larger Auto UV Max Angle means that the merging algorithm will be more tolerant of angles between faces. More UV shells are likely to be merged, the result of which will likely be fewer UV shells and therefore fewer Charts. As with the Auto UV Max Distance, if unsuitable edges are combined due to overly wide tolerances, distortion may occur in the final lightmaps UVs.
现在我们来看一下自动UV最大角度参数。就像自动UV最大距离一样,这个值会影响Unity的展开算法的UV合并步骤。除了在UV shells合并之前进行远距离测试外,展开器还测试相邻网格面之间的夹角。只有边沿的角小于这个值才会考虑合并。
更小的自动UV最大角度意味着更少的边会通过测试。因此,将会有更少的UV shells被合并,图表的数量将会增加。相反,一个较大的自动UV最大角度意味着合并算法将更能容忍面与面之间的角度。更多的UV shells可能会被合并,其结果可能是更少的UV shells,因此更少的图表。与自动UV最大距离一样,如果不合适的边由于过大的容差而组合在一起,则在最终的光照映射UVs中可能出现失真。

  • If HouseBig02 is not selected in the Hierarchy window, select it.
  • Navigate to the Lighting window (Window > Lighting) and select the Object tab.
  • Set the value of Auto UV Max Angle to 0.

    • Hierarchy 窗口下选择HouseBig02
    • 打开光照窗口,选择 Object 页签
    • 把自动UV最大角度调到最0

When we look at the Preview area of the Lighting window, we can see that the number of Charts created is unacceptably high. This is because we are only allowing a deviation of 0 degrees or less between faces before they are considered for merging. This almost certainly means that no UV shells will pass the test and therefore many Charts will remain following this merging process.
当我们查看光照窗口的预览区域时,我们可以看到创建的图表的数量是不可接受的高。这是因为在考虑合并条件是,我们只允许在不同的面之间有0度或更低的偏差。这几乎肯定意味着,没有UV shells会通过测试,因此许多图表将在这个合并过程中继续存在。
这里写图片描述
Without some tolerance, a low Auto UV Max Angle will result in an excessive number of Charts.
如果没有一些容忍公差,低的自动UV最大角度将导致大量的图表。

Next we will try increasing the Auto UV Max angle.
- If HouseBig02 is not selected in the Hierarchy window, select it.
- Navigate to the Lighting window (Window > Lighting) and select the Object tab.
- Slowly increase the value of Auto UV Max Angle, observing effects of each change in the Preview area of the Lighting window.
接下来我们将尝试增加自动UV的最大角度。
- 如果不在Hierarchy 窗口中选中HouseBig02 ,请选择它。
- 导航到Lighting 窗口(Window > Lighting)并选择Object选项卡。
- 慢慢增加自动UV最大角度的值,观察每个变化在Lighting 窗口的预览区域的效果。

We can see that the number of Charts decreases as the value goes up. This is because the angle permitted between mesh faces considered for merging is more tolerant. More UV shells pass the test and will be merged. Consequently, the number of lightmap UV shells is reduced and the number of corresponding Charts decreases.
我们可以看到,随着值的增加,图表的数量会减少。这是因为在考虑合并的网格面之间允许的角度更加宽容。更多的UV shells通过测试,并将被合并。因此,光照图UV shells的数量减少了,相应的图表数量也减少了。
这里写图片描述
Increasing the Auto UV Max Angle can help reduce Charts - particularly on objects with rounded surfaces.
增加自动 UV最大角度可以帮助减少图表——特别是在有圆形表面的物体上。

An Auto UV Max Angle of 93 yields good results with our example object, HouseBig02.
- If HouseBig02 is not selected in the Hierarchy window, select it.
- Navigate to the Lighting window (Window > Lighting) and select the
Object tab.
- Set the value of Auto UV Max Angle to 93
自动 UV最大角度设为 93,与我们的示例对象HouseBig02有良好的结果。
- 如果在Hierarchy 窗口中没有选择HouseBig02 ,请选择它。
- 导航到Lighting 窗口(Window > Lighting)并选择 对象选项卡。
- 将自动UV最大角的值设置为93

Notice how the number of Charts is reduced in the Preview area of the Lighting window following a brief precompute.
请注意,在一个简单的预计算之后,在照明窗口的预览区域中,图表的数量是如何减少的。
这里写图片描述
The Charting preview for HouseBig02 following some optimisation of its unwrap settings.
在对其展开的设置进行一些优化之后,HouseBig02 的图表预览

At this stage, our optimized unwrap settings can be taken back into the working Scene.
- If HouseBig02 is not selected in the Hierarchy window, select it.
- In the Inspector panel, locate the MeshRenderer component and click the cog icon in the top right of its display.
- Click Copy Component.
- Open the LightingTutorialStart Scene.
- Find all the objects in the Hierarchy named HouseBig02.
- Hold down Ctrl (Cmd on the Mac) and click each of the HouseBig02 GameObjects in the Hierarchy window to select them all.
- Navigate to the Lighting window (Window > Lighting) and select the Object tab.
- Select the the cog icon in the top right.
- Choose Paste Component Values to apply the settings you previously copied.
在这个阶段,我们的优化的打开的设置可以被带回到工作场景中。
- 如果在Hierarchy 窗口中没有选择HouseBig02 ,请选择它。
- 在Inspector 面板中,定位MeshRenderer组件,并单击其显示在右上方的齿轮图标。
- 点击复制组件。
- 打开LightingTutorialStart场景。
- 在Hierarchy中找出名字为HouseBig02的所有对象。
- 按住Ctrl(Mac上的Cmd),然后点击Hierarchy 窗口中的每个HouseBig02 游戏对象 来选择它们。
- 导航到Lighting 窗口((Window > Lighting)并选择Object选项卡。
- 选择右上方的齿轮图标。
- 选择Paste 组件值来应用您之前复制的设置。
这里写图片描述
Copy Component is an easy way to take your unwrap settings back to the main Scene.
复制组件是一种简单的方法,可以将展开的设置返回到主场景。

The modest savings these settings give us can add up to much more significant optimisations when applied across a Scene of several hundred objects.
这些设置所带来的适度节省,可以在几百个对象的场景中应用到更重要的优化中。

Unwrap settings: per-instance or per-Prefab?

展开设置:每个实例还是预制体?

Unwrap settings can be stored in a Prefab by applying them to any Static MeshRenderer component that lives within that Prefab’s hierarchy. Alternatively, unwrap settings can also be applied to individual GameObjects within the Scene. Unwrap settings applied to an instance of a Prefab in a Scene will override the settings stored within the Prefab. This way of working offers us some useful approaches to setting up Scene lighting.
展开的设置可以存储在一个预制体中,将它们应用到任何静态的MeshRenderer组件中,这些组件位于Prefab的hierarchy结构中。另外,展开的设置也可以应用于场景中的单个GameObjects 。在场景中应用于一个预制体的实例的展开设置将覆盖存储在预制件中的设置。这种工作方式为我们提供了一些为设置场景光照有用的方法。

It is often useful to configure a default unwrapping and lighting setup for your Prefabs. If the Prefabs are to be instantiated numerous times, it can save effort to have the unwrapping for that object preconfigured rather than having to specify this repeatedly throughout the Scene.
为您的预制体配置默认的展开和光照设置通常是很有用的。如果要多次实例化Prefabs,它可以节省大量的工作,以便预先配置该对象的展开,而不必在整个场景中重复指定这一点。

On the other hand, it is frequently desirable to configure lighting settings based on the context in which an object is used within the overall Scene. For example, if an instance of a Prefab will be seen up close within the playable area then it makes sense to use higher fidelity setting on that instance. If the instance of the Prefab is far away, then it would not make sense to use our lighting budget maintaining the same settings. Here, we would likely want to lower the quality of our lightmaps and use more aggressive stitching parameters in our unwrap.
另一方面,通常需要根据在整个场景中使用对象的上下文来配置光照设置。例如,如果一个预制件的实例在可玩区域内被发现,那么在该实例上使用更高的保真度是有意义的。如果预制件的实例离得很远,那么使用我们的照明预算来维护相同的设置是没有意义的。在这里,我们可能想要降低我们的lightmaps的质量,并在我们的展开中使用更激进的拼接参数。

The ability to configure default settings that suitable for most use cases and then override these settings in special cases can be very useful. In the case of this lighting tutorial, we will be applying our settings per-instance, in the Scene.
能够配置适合大多数实例的默认设置,然后在特殊情况下覆盖这些设置,这是非常有用的。在这个光照教程的例子中,我们将在场景中应用我们的设置在每个实例中。

Applying unwrap settings throughout the Scene

在整个场景中应用展开设置

It is time to start applying what we have learned about Unity’s automatic unwrapping algorithm to the remaining Static geometry within the LightingTutorialStart Scene. Working systematically, it should not take long to set up the Scene for significantly improved precompute times.

  • Open the LightingTutorialStart Scene included in the example project.
  • In the Hierarchy window, select the top GameObject found under Environment > Structures.
  • Copy the GameObject to an empty Scene.
    现在是时候开始将我们所学到的关于Unity的自动展开算法应用到LightingTutorialStart场景中剩下的静态几何体中。系统地工作,不需要花很长时间就可以建立一个显著提高预计算时间的场景。
  • 打开示例项目中包含的LightingTutorialStart场景。
  • 在Hierarchy 结构窗口中,选择在Environment>Structures 结构下找到顶部的游戏对象。
  • 把游戏对象复制到一个空的场景中。

At this point we should apply what we have learned about the unwrap parameters and feel free to experiment with different values. Remember to use the UV Charts Scene draw mode in combination with the Charting mode of the Preview area of the Lighting window. This will enable us to find the right balance between lightmap distortion and the number of Charts required.
在这一点上,我们应该应用我们已经学过的关于展开参数的知识,并且可以自由地尝试不同的值。请记住使用UV图表场景绘制模式与光照窗口的预览区域的图表模式相结合。这将使我们能够在光照图扭曲和所需的图表数量之间找到正确的平衡。

The LightingTutorialOptimal Scene can be used as a reference in cases where we are unsure. Note that in some cases it may even be unnecessary to change the default unwrap values at all. The default values may actually provide the best outcome.
在我们不确定的情况下,LightingTutorialOptimal 场景可以作为参考。注意,在某些情况下,甚至没有必要更改默认的展开值。默认值实际上可能提供最好的结果。

  • Experiment with different values for the unwrap parameters until a
    balance is reached between number of Charts and lightmap distortion.

对展开参数的不同值进行试验,直到在图表数量和光照图扭曲之间达到平衡。

  • Copy the settings, return to the LightingTutorialStart Scene and
    paste them into all other instances of the same GameObject.
    Multi-selecting all the corresponding objects in the hierarchy will
    enable you to use Paste Component Values to quickly apply the
    settings across multiple objects.

复制设置,返回到LightingTutorialStart场景,并将它们粘贴到同一个GameObject的所有其他实例中。在层次结构中多选所有对应的对象将使您能够使用粘贴组件值快速地在多个对象上应用这些设置。

  • Repeat this process for every uniquely named GameObject in the
    Hierarchy view.
    在Hierarchy 视图中为每一个唯一命名的GameObject重复这个过程。

Once all of the GameObjects under the Structures group have been optimized, move onto the remaining Static geometry under the Rocks group. Working methodically in this way, it should take no longer than a few minutes to set the unwrap values for all of the Static geometry within your Scene. The outcome of this manual preparation will be faster iteration when lighting the Scene and better performance at run time.

一旦Structures 组下的所有游戏对象都经过了优化,就移到Rocks 组下面的静态几何图形中。以这种方式有条不紊地工作,不需要花费几分钟的时间来设置场景中所有静态几何体的展开值。这个手动准备的结果将是在光照时更快速的迭代,在运行时更好的性能。

猜你喜欢

转载自blog.csdn.net/cangod/article/details/79898755