您的位置:首页 > 移动开发 > Unity3D

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

2018-03-26 18:54 1066 查看
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)(8)

预处理实时全局光照(8)

Rendering and Shading

渲染与着色

Cameras and Effects

相机与效果

Geometry in Unity

几何体

6.Unwrapping and Chart reduction

展开和图表减少

Now that we have excluded suitable objects from the lighting precompute, we can begin the work of reducing the number of remaining Charts.

Charts are created to encompass the UV lightmap coordinates of a Static Mesh Renderer. The number of Charts that an object requires is therefore largely determined by the number of UV shells (pieces) needed to unwrap the object in question. Unwrapping is always a tradeoff between distortion of the texel distribution on geometry faces, and the number of shells required for sufficient texture coverage.

既然我们已经把光照预计算的合适对象用光照探头排除了,我们就可以开始减少剩余图表的数量了。

图表被创建出包含一个静态网格渲染器的UV光照映射坐标。因此,一个对象需要的图表数量很大程度上问题取决于展开对象所需的UV 组(块)的数量。展开包装通常是在几何图形上的纹理失真和足够的组数量来完成纹理覆盖之间的折衷。

Unity’s sophisticated unwrapping algorithms will often achieve good results without our intervention; however, sometimes we need to provide a little guidance. It is therefore important that we understand the theory and process behind automatic unwrapping.

Consider the following illustrations:

如果没有我们的干预,Unity的复杂的展开算法通常会取得不错的效果;然而,有时我们需要提供一些指导。因此,我们必须了解自动展开背后的理论和过程。

考虑下面的插图:



An unwrap without distortion, but requiring multiple UV shells.

一个没有扭曲,但需要多个UV组的展示。



UV coordinates which result in a single UV shell, but yield unacceptable texture distortion.

在一个的UV组下的UV坐标,会产生不可接受的纹理失真



An ideal result, with no texture distortion and a single UV shell.

这是一个理想的结果,没有纹理扭曲和一个的UV组。

In the images above, we show three different examples of how the UVs on an object can be projected or unwrapped.

在上面的图片中,我们展示了三个不同的例子,说明对象的UV如何进行投影或展开。

In the first image, we can see a result which is largely free of distortion. The checker pattern we are using as a texture is mapped in such a way that each tile of the texture remains square in aspect. If we imagine that this checkerboard was a lightmap (an ‘image’ of the lighting applied to the object), we would get a result that looked visually correct and without distortion. However, we require six UV shells to get this coverage. As a result, we would get six Charts using Unity’s Precomputed Realtime GI system. Regardless of the size of the object, each Chart would require a minimum of 4x4 texels, meaning that this object would use at least 96 texels irrespective of resolution.

在第一张图片中,我们可以看到一个很大程度上没有失真的结果。我们使用的检查器模式是用这样一种方式来映射纹理:每一块纹理每面都是正方形的。如果我们想象这个棋盘是一个光照图(一个被应用到物体上的光照的“图像”),我们就会得到一个看上去正确而不扭曲的结果。然而,我们需要6个UV组来达到这个目的。因此,我们将使用Unity的预先计算实时GI系统获得六张图表。无论对象的大小如何,每个图表都需要至少4x4的图素,这意味着这个对象将至少使用96个图素,而不管分辨率如何。

In the second image, we have a different problem. The object’s UV texture coordinates have been projected so that we have one complete UV shell covering all faces. While this would be optimal in terms of the number of Charts required to cover the object, the visual results would be unacceptably distorted. We would likely see ‘smearing’ of textures applied across the surface of the object. The faces of the object also overlap in UV texture space - meaning that if we we were referencing a lightmap, the lighting from one side of the object would be displayed on opposing faces. Clearly this method of unwrapping the object will not give us an acceptable result.

在第二个图像中,我们有一个不同的问题。这个物体的UV贴图坐标已经被投射出来了,所以我们有一个完整的UV组覆盖所有的面。虽然这将是最优的,但对于覆盖该对象所需的图表数量来说,扭曲的视觉效果是无法接受的。我们可能会看到在物体表面涂抹纹理的纹理。物体的表面也会在UV贴图空间中重叠——这意味着如果我们引用一个光照图,物体一侧的光线将会显示在其他的面上。显然,这种对对象进行展开的方法是一个不可接受的结果。

The third image shows an example of an ideal unwrap. We have a result which is free of distortion: the tiles of our checkerboard texture have remained square in aspect. We have also succeeded in covering all faces of the object while only outputting one UV shell. This has been achieved by joining, or stitching UV edges which correspond to contiguous geometry edges on the model.

第三张图片展示了一个理想的展开的例子。我们有一个不被扭曲的结果:我们棋盘纹理的纹理块在样式是正方形的。我们还成功地覆盖了物体的所有面,同时只输出了一个UV组。这是通过连接,或拼接UV边来实现的,这些边对应于模型上的相邻几何边。

If we think about this programmatically, what have we done to achieve an ideal unwrap? First, we have made our UV projections orthographically onto the object in order to create our individual shells. We have then evaluated these shells for edges which are shared in the geometry of the corresponding object. Where shared edges are found, we have moved and stitched those sh
4000
ells to the partner edges of their neighbouring shell. This is what Unity’s unwrapping algorithms attempt to do automatically with Static geometry.

如果我们以编程方式思考这个问题,我们做什么可以实现这个理想的展开呢?首先,我们将UV正交投影在物体上,以创造出我们的独立的UV组。然后我们对这些UV组进行了评估,以获得相应对象的几何体所共享的边。在找到共享边的地方,我们移动并将这些UV组缝到相邻的UV组的边缘。这是Unity的展开算法尝试使用静态几何来自动处理的。

Visualising Charts

图表可视化

Before working to optimize our unwrapping and lightmap Charts, we need a way to visualize them in the Editor. Charts are generated during the unwrapping stage of the mesh import pipeline. For Precomputed Realtime GI, these Charts are then packed into atlasses during the Geometry stage of the precompute process. This is to ensure that they don’t overlap. We can only see a preview of our Charts once the Geometry stage of the precompute has completed and the visualisation data has been saved.

If you are working with version control as part of a team, please be aware that this information is cached locally. This means that a precompute must be carried out on your local machine before the various diagnostic draw modes become available.

在优化我们的展开和光照映射图表之前,我们需要一种在编辑器中可视化它们的方法。图表是在网格导入管道的展开阶段生成的。对于预计算实时GI,在预计算的几何阶段,这些图表被打包到图集中。这是为了确保它们不会重叠。一旦预计算的几何阶段完成,可视化数据被保存,我们便能看到图表的预览。

如果您正在使用版本控制作为团队的一部分,请注意此信息在本地缓存。这意味着在各种特征的绘制模式可用之前,必须在本地机器上进行预计算。



UV Charts draw mode representing the different Charts used in the Scene as colored regions and lightmap resolution as a checkerboard overlay.

在场景中UV图表绘制模式显示出使用不同的图表颜色区域和光照图纹理作为一个棋盘式覆盖

One way to quickly visualize your Charts is to use the UV Charts draw mode in the Scene view.

- In the top left of the Scene view, use the Draw Mode drop-down menu to select UV Charts.

Using this mode, Charts can be seen as different-colored panels overlaid with a checkerboard texture representing the corresponding lightmap resolution. If you have Auto mode enabled (Window > Lighting > Auto), changes to unwrap parameters will be calculated automatically and the Scene view will be updated with the results.

一种快速可视化图表的方式是在场景视图中用UV图表渲染模式。

- 在场景视图的左上角,用渲染模式的下拉菜单去选择UV图表。

使用这种模式,图表可以被看作是不同颜色的面板,上面覆盖了一个棋盘式纹理,代表相应的光照映射分辨率。如果启用了自动模式(Window > Lighting > Auto),修改展开参数将自动计算,场景视图也将一起更新。



The Charting mode of the object Preview window showing Charts as colored squares and lightmap UVs as light blue wireframe.

对象预览窗口的绘图模式显示出图表为彩色方块和光照映射UV为淡蓝色线框。

It can be easy to miss Charts in the Scene view when working with complex objects. By using the Preview pane in the Lighting window, we can view all the Charts used by an individual object. This can help us to more accurately assess the unwrapping of these objects, which will help us to reduce Chart counts in our Scene.

Open the Lighting window (Window > Lighting) and select the Object tab.

Select the object that we wish to view in the Hierarchy window.

In the top left of the Preview pane of the Lighting window, choose Charting from the drop-down menu.

The number of Charts used by that object will be represented by colored squares overlaid with the corresponding UV coordinates in light blue.

当处理复杂对象时,很容易忽略场景视图中的图表。通过在光照窗口中使用预览窗格,我们可以查看单个对象使用的所有图表。这可以帮助我们更准确地评估这些对象的展开,这将帮助我们减少在我们的场景中的图表计数。

打开照明窗口(窗口-灯光)并选择Object选项卡。

选择我们希望在层级窗口中查看的对象。

在窗口的预览窗格的左上角,从下拉菜单中选择图表。

该物体所使用的图表的数量将用彩色的方格表示,用淡蓝色的相应的UV坐标覆盖。

Unwrapping parameters explained

展开参数理解

There are a number of settings that we can tweak to optimize UV unwrapping. All of these settings are per-object. We can access these settings through the Lighting window, as follows:

- Open the Lighting window (Window > Lighting) and select the Object tab.

- Select the object that we wish to view in the Hierarchy window.

我们可以调整一些设置来优化UV展开。所有这些设置都是针对每个对象的。我们可以通过照明窗口来访问这些设置:

打开照明窗口(窗口-灯光)并选择Object选项卡。

选择我们希望在层级窗口中查看的对象。

Auto UV Max Distance

自动UV最大距离

Unity’s unwrapping algorithms attempt to simplify lightmap UVs by moving shells and stitching UV edges together. UV shells will only be considered for this operation if shells are within the distance defined by Auto UV Max Distance. This range is defined in Unity’s world space units. Remember that in our tutorial Scene we are assuming that a unit is 1 meter.

Unity的展开算法试图通过移动UV组和将UV边缘拼接在一起来简化光照映射UV。只有当UV组在自动UV最大距离内的距离时才会考虑UV组展开算法。这个范围是以Unity的世界空间单位来定义的。记住,在我们的教程中我们假设一个单位是1米。



The Auto UV Max Distance setting found in the Object tab of the Lighting window.

在光照窗口的对象标签中找到的自动UV最大距离设置。(Unity2017.3是在游戏对象属性面板MeshRenderer组件中控制的)

In many cases the default value of 0.5 units will give acceptable results. For particularly large objects with large faces, it may be necessary to increase this value. This is to prevent suitable candidate UVs from being excluded from selection by the stitching algorithm.

Increasing the Auto UV Max Distance will often reduce the number of Charts required by the selected object. Decreasing this value is useful in situations where there is visible stretching of lightmap texels and we may actually need more Charts in order to get the required texel coverage. The results of these changes can be easily assessed using the checkerboard overlay in the UV Charts Scene draw mode. Finding the right balance can often require a little experimentation.

在很多情况下,0.5单位的默认值将给出可接受的结果。对于那些拥有大表面的大型对象,可能有必要增加这个值。这是为了防止在缝合算法时把合适的候选UV排除在外。

增加自动UV最大距离通常会减少所选对象所需的图表数量。在有可见的光照映射纹理被拉伸的情况下减少自动UV最大距离是有用的,我们可能需要更多的图表来获得所需的图素覆盖。这些变化的结果可以很容易地通过在UV图表场景绘制模式下的棋盘式覆盖来评估。想要找到正确的平衡点往往需要做一些实验。

Auto UV Max Angle

自动UV最大角度

Lighting UV shells are also evaluated for stitching based on the angle between neighbouring faces of the corresponding mesh. Auto UV Max Angle defines the maximum angle permitted between faces sharing a UV edge and is calculated using the internal angle. If the angle between the backfaces is greater than this amount, these UV shells will not be considered for stitching.

根据相应网格的相邻面之间的夹角,也可以对光照UV组的缝合进行评估。自动UV最大角度定义了在共享UV边沿的表面之间允许的最大角度,并使用内部角度计算。如果背面之间的角度大于这个值,这些UV组就不会被认为可缝合的。



The Auto Max Angle settings can be found within the Object tab of the Lighting window.

可以在光照窗口的Object选项卡中找到自动最大角度设置。(Unity2017.3是在游戏对象属性面板MeshRenderer组件中控制的)

Increasing this value will make it more likely that lighting UVs will be combined by Unity’s unwrapping algorithm. Auto UV Max Angle can therefore can be a good way to reduce the number of Charts required by a selected object. However, sometimes stretched lightmaps can occur when this tolerance is too relaxed. Decreasing the Auto UV Max Angle will make the unwrapper less likely to combine UV edges, which will result in more Charts but less distortion. Again, using the checkerboard overlay in the UV Charts Scene draw mode is a good way of assessing the suitability of the values we are using.

增加这个值将使更有可能通过Unity的展开算法将光照UV组合起来。因此,自动UV最大角度可以减少被选中对象所需要的图表的数量。然而,当这种容差过于宽松时,有时会出现被拉伸的光照图。减少自动UV最大角度,会使展开器不太可能组合UV边,这样就会产生更多的图表,但会减少失真。同样,在UV图表绘制模式下使用棋盘式覆盖是评估我们所使用的值是否合适的好方法。

Preserve UVs

保留UV

In some cases it may be impossible to get an ideal unwrap using the auto unwrapper. We may end up with too many Charts or unacceptable distortion in our lightmaps (visualized as stretched checkering when in GI Charts draw mode). In these cases it may be necessary to manually author UVs in the UV01 channel of our model file. This is work that needs to be done while in our chosen content creation package.

If this is the case, we can use the Preserve UVs option to force Unity’s unwrapping algorithms to preserve the shape of the UV shells defined in your model’s UV01 channel.

在某些情况下,使用自动展开可能无法获得理想的展开效果。在我们的光照图中,我们可能会有太多的图表或无法接受的失真(在GI图表绘制模式下可以看到其拉伸的横盘式方格)。在这些情况下,可能需要在模型文件的UV01通道中手动编写UVs。这是在我们选择内容创作资源中需要完成的工作。

如果是这样的话,我们可以使用“保留UVs”选项来强制Unity的展开算法来保留模型的UV01通道中定义的UV组的形状。



The Preserve UVs option is useful when it is desirable to keep your manual lighting UVs.

当你想要保留手动光照UV的时候,Preserve UVs选项可被使用。(Unity2017.3是在游戏对象属性面板MeshRenderer组件中 “优化实时UV”开关控制的)

It is important to note that these shells will always be be repacked to save lightmap space. It is the manner in which the shells are unwrapped, individually, which will be preserved rather than the shell’s position within the lightmap.

We must be cautious when using this approach. In cases where our lightmap UVs contain a large number of UV shells, this option can actually increase precompute times. This is because the UV merging step offered by Unity’s auto unwrapper is bypassed and our manual UV layout is preserved. Remember that the desired outcome is as few UV shells - and therefore as few Charts - as possible, while keeping an acceptable level of distortion.

需要注意的是,这些shells (UV组)将始终要被重新打包以保存光照图空间。这是一种将shells 展开的方式,而不是保留shells在光照图中的位置信息。

在使用这种方法时,我们必须谨慎。在我们的光照映射UVs包含大量的UV shell的情况下,这个选项实际上会增加预计算时间。这是因为Unity的自动展开器提供的UV合并步骤被忽略了,我们的手动UV布局也得以保留。请记住,所期望的结果是尽可能少的UV shells——因此尽可能少的图表——同时保持可接受的失真程度。

Ignore Normals

忽略法线

In certain cases the mesh importer may decide to split geometry. This will also affect Charts. For example, if a mesh has an extremely high triangle count it may be more performant for Unity to split it into separate sub-meshes. Often this is for hardware specific requirements, such as reducing the number of triangles in each draw call. Where these splits occur is decided based on areas where there is large variance in the normal angle between adjacent mesh faces, such as hard edges . Splitting meshes in this way happens during the mesh import process for your models. Charts can potentially be split during this process as edges that fall within a Chart may be separated, resulting in multiple shells which in turn will require additional Charts.

在某些情况下,网格导入器可能会分割几何体。这也会影响到图表。例如,如果一个网格有一个非常高的三角形数,那么它可能会花费更多的性能分割成单独的子网格。通常这是针对特定于硬件的需求,例如减少每次渲染调用中的三角形数量。在这些分割发生的地方,是基于相邻的网格面的法线角度的平方差大小,如硬边。以这种方式分割网格是在模型的网格导入过程中发生的。在此过程中,图表可能会被分割,因为在图表中可能会分离出一些边,从而导致多个shell,从而需要额外的图表。



The Ignore Normals checkbox prevents charts being split during the import pipeline.

在导入管道中勾选 忽略法线 复选框可阻止图表被分割。

Sometimes it is not desirable to split your Charts in this way. The resulting increase in the number of Charts may be detrimental to precompute times, and lighting seams can cause unwanted visual artefacts in the resulting lightmaps. In these cases, enabling the Ignore Normals checkbox will prevent the Charts being split for Precomputed Realtime GI lighting.

Note that only Precomputed Realtime GI is affected by this option. Splits in the selected meshes are still preserved for other uses in Unity.

有时候,用这种方式分割你的图表是不可取的。由此产生的图表数量的增加可能会对预计算时间造成不利影响,而且在产生的光照图中,光线的接缝会造成不必要的视觉伪影。在这些情况下,启用忽略Normals复选框将会防止图表被分
ad9b
割,这有利于预计算实时GI光照。

注意,只有预计算实时GI才会受到这个选项的影响。在被选择的网格中,分割仍然在Unity保留其他用途。

Faster iteration in larger Scenes

大场景中更快的迭代

Complex Scenes can contain hundreds - even thousands - of Static objects. Generating the Chart atlases for all these objects can result in slow lighting precomputes and this can negatively affect the speed with which we are able to iterate on our Scenes.

When experimenting with unwrap settings for objects, sometimes it can be useful to isolate an object of interest in an empty Scene where we can iterate quickly with minimal precompute times. The unwrap settings we determine can then be taken back and applied to other objects of that type in our original Scene. Working in this way can add up to big time savings when preparing Scenes for lighting.

复杂的场景可以包含数百甚至数千个静态对象。为所有这些对象生成图表图集会导致较慢的光照预计算,这可能会对我们在场景中迭代的速度产生负面影响。

当尝试对对象进行展开相关设置时,有时在一个空的场景中独立出感兴趣的对象是很有用的,在这个场景中,我们可以用最少的预计算时间快速迭代。我们使用的展开的设置可以被收回并应用到我们原来的场景中的其他对象。用这种方式工作可以节省大量的时间,在为照明做准备的时候。

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.

Copy this object to the clipboard by pressing Ctrl+C (Cmd+C on Mac).

Create a new Scene by pressing Ctrl+N (Cmd+N on Mac).

If prompted to save your changes, select Yes if you are happy with your current Scene progress or No if you wish to discard it.

In the newly created Scene, paste HouseBig02 from the clipboard by pressing Ctrl+V (Cmd+V on Mac).

Open the Lighting window (Window > Lighting) and then select the Scene tab.

Enable Auto precompute mode by enabling the Auto checkbox.

Now select the Object tab.

In the top left of the Object tab, select Charting from the drop-down menu.

Expand the Preview area to see how the object is unwrapped.

打开示例项目的 LightingTutorialStart 场景。

在Hierarchy 窗口选中叫 HouseBig02 对象。在 Environment > Structures > Houses 分组下

按Ctrl+C复制对象到剪切板

按Ctrl+N新建场景

如果提示保存您的更改,如果您对当前场景的进展感到满意选择Yes,或者如果您希望丢弃它选择No。

在新创建的场景中,通过按Ctrl+V(Mac上的Cmd+V)粘贴来自剪贴板的HouseBig02 。

打开光照窗口(窗口 >灯光),然后选择“场景”选项卡。

通过启用 自动 复选框,启用自动预计算模式

现在选择Object选项卡。

在Object选项卡的左上角,从下拉菜单中选择图表。

展开预览区域,以查看对象是如何展开的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息