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

1. Introduction to Lighting and Rendering

1.介绍光照和渲染

Modern game lighting makes extensive use of ‘global illumination’.
Global illumination, or ‘GI’, is a term used to describe a range of techniques and mathematical models which attempt to simulate the complex behaviour of light as it bounces and interacts with the world. Simulating global illumination accurately is challenging and can be computationally expensive. Because of this, games use a range of approaches to handle these calculations beforehand, rather than during gameplay.

现在的游戏光照系统大部分都在使用“全局光照(GI)”。
全局光照是想对世界与光碰撞和交互的复杂行为用大量技术和数学模型来尝试模拟出来,但模拟出精确的全局光照也是一种挑战,并且计算量庞大。由此,很多游戏会提前用大量方式处理这些计算,而不是在运行时计算。

这里写图片描述
The same scene: With no lighting (Left), with ‘direct light’ only (Center) & with indirect ‘global illumination’ (Right). Note how colors are transferred as light ‘bounces’ between surfaces, giving a much more realistic result.
上图左为无光照,中间为只有直接光照,右图为全局光照。注意光照在表面的碰撞,表现的更加真实

In this document we will give you an overview of how GI works in Unity. We will walk you through the different lighting techniques available, explain how to setup your project for lighting and then explore the various tools at your disposal when considering how to light a scene.

此文档将概述GI是如何在Unity内部运行的。通过介绍不同的光照技术,便于你在考虑设计自己场景光照时如何建立光照和使用丰富的工具。


2. Choosing a Lighting Technique

2.选择光照技术

Broadly speaking, lighting in Unity can be considered as either ‘realtime’ or ‘precomputed’ in some way and both techniques can be used in combination to create immersive scene lighting.
In this section we will give a brief overview of what opportunities the different techniques offer, their relative advantages and individual performance characteristics.

扫描二维码关注公众号,回复: 1628232 查看本文章

Unity中光照一般使用包括 “实时”或“预计算”,或者两者结合达到光照逼真效果。下面将简单介绍这两种技术,及他们相对优势和各自的性能特征。

Realtime Lighting

实时光照

By default, lights in Unity - directional, spot and point, are realtime. This means that they contribute direct light to the scene and update every frame. As lights and GameObjects are moved within the scene, lighting will be updated immediately. This can be observed in both the scene and game views.

默认情况下,Unity提供的平行光,聚光灯,点光都是实时光照。这表示它们都提供直接光源并且每帧都在更新。一旦光源和游戏对象移动,光照将会立即更新。这在场景视图和游戏视图都可以观察得到。

这里写图片描述
The effect of realtime light alone. Note that shadows are completely black as there is no bounced light. Only surfaces falling within the cone of the Spotlight are affected.
只有实时光照的效果图。注意阴影处是完全黑暗并没有碰撞/反弹光照。只有落在聚光灯锥型区域内的面才有影响。

Realtime lighting is the most basic way of lighting objects within the scene and is useful for illuminating characters or other movable geometry.
Unfortunately, the light rays from Unity’s realtime lights do not bounce when they are used by themselves. In order to create more realistic scenes using techniques such as global illumination we need to enable Unity’s precomputed lighting solutions.

实时光照是大部分场景光源的基本方式,用于角色或可移动性几何体表现光照。
遗憾的是,仅仅用Unity的光源,实时光照并不碰撞/反弹。为了表现更加真实的场景需要使用像全局光照这样的技术,这就要求我们开启Unity的预计算光照解决方案了。

Baked GI Lighting

烘焙全局光照

When ‘baking’ a ‘lightmap’, the effects of light on static objects in the scene are calculated and the results are written to textures which are overlaid on top of scene geometry to create the effect of lighting.

当烘焙一张光照图时,场景中的静态对象的光照效果将被计算并将结果写入一张贴图,即光照图。然后把光照图覆盖到场景几何体上面达到想要的光照效果。

这里写图片描述
Left: A simple lightmapped scene. Right: The lightmap texture generated by Unity. Note how both shadow and light information is captured.
左:一个简单的光照场景。右:Unity生成的光照图。注意阴影和光照信息都被捕捉到了。

These ‘lightmaps’ can include both the direct light which strikes a surface and also the ‘indirect’ light that bounces from other objects or surfaces within the scene. This lighting texture can be used together with surface information like color (albedo) and relief (normals) by the ‘Shader’ associated with an object’s material.
With baked lighting, these light textures (lightmaps) cannot change during gameplay and so are referred to as ‘static’. Realtime lights can be overlaid and used additively on top of a lightmapped scene but cannot interactively change the lightmaps themselves.
With this approach, we trade the ability to move our lights at gameplay for a potential increase in performance, suiting less powerful hardware such as mobile platforms.

光照图包括直接照射到表面的直接光和从其他对象或场景表面碰撞/反弹的间接光。光照图可以用于包含颜色(反照率/漫反射)和浮雕(法线)的表面信息的着色器上并关联到一个对象的材质上。
被烘焙的光照图在运行时是不可被修改的,也被称为“静态的”。实时光照可以被覆盖并且可以叠加到光照图的上层,但不能够与光照图产生交互改变光照图本身。
有了这种方法,我们就可以在运行时移动我们的光源以提高性能,适应不那么强大的硬件,比如移动平台。

Precomputed Realtime GI Lighting

预计算实时全局光照

Whilst traditional, static lightmaps are unable to react to changes in lighting conditions within the scene, Precomputed Realtime GI does offer us a technique for updating complex scene lighting interactively.
With this approach it is possible to create lit environments featuring rich global illumination with bounced light which responds, in realtime, to lighting changes. A good example of this would be a time of day system - where the position and color of the light source changes over time. With traditional baked lighting, this is not possible.

然而传统的静态光照图不能够与场景中光照变化做出调整,预计算实时全局光照提供给我们一种可更新的复杂场景光照交互技术。
有了这种方法,我们就可以创造出具有丰富反射光照的全局光照的照明环境,并能实时的对光源变化做出反应。一个好的示例就是“天系统”:随时间变化的光源位置和颜色。用传统烘焙光照方式是做不到这点的。

这里写图片描述
A simple example of time of day using Precomputed Realtime GI.
预计算实时全局光照的“天系统”的简单示例

In order to deliver these effects at playable framerates, we need to shift some of the lengthy number-crunching from being a realtime process, to one which is ‘precomputed’.
Precomputing shifts the burden of calculating complex light behaviour from something that happens during gameplay, to something which can be calculated when time is no longer so critical. We refer to this as an ‘offline’ process.

为了在合理帧率内实现这种效果,我们需要从实时处理中提取一些冗长的计算处理,这种处理被称为“预计算”。
预计算把从运行时发生的一些处理任务提取并承担了复杂光照行为的计算,那些与时间关联不是很重要的计算。我们称之为“离线”处理。

So how does this work?
Most frequently it is indirect (bounced) light that we want to store in our lightmaps when trying to create realism in our scene lighting. Fortunately, this tends to be soft with few sharp, or ‘high frequency’ changes in color. Unity’s Precomputed Realtime GI solution exploits these ‘diffuse’ characteristics of indirect light to our advantage.
Finer lighting details, such as crisp shadowing, are usually better generated with realtime lights rather than baking them into lightmaps. By assuming we don’t need to capture these intricate details we can greatly reduce the resolution of our global illumination solution.
By making this simplification during the precompute, we effectively reduce the number of calculations we need to make in order to update our GI lighting during gameplay. This is important if we were to change properties of our lights - such as color, rotation or intensity, or even make change to surfaces in the scene.
To speed up the precompute further Unity doesn’t directly work on lightmaps texels, but instead creates a low resolution approximation of the static geometry in the world, called ‘clusters’.

如何工作的呢?
在我们尝试在场景创建真实感光照效果时,大部分频繁的存储在我们的光照图中的是间接光照。幸运的是,间接光是比较少会被修改颜色值的,或者说不太明显。Unity的预计算实时全局光照解决方案利用间接光的这个“漫反射”特性转换成其优势。
像洒落的阴影,更精细的光照细节一般实时光照生成的会好于烘焙出的光照图。如果我们不需要捕获这种错综复杂的细节,完全可以把全局光照解决方案的分辨率降低。
通过在预计算时的简化,我们可以有效地降低在运行时全局光照大量的计算。这预计算对于光源属性修改,比如颜色值,旋转,强度,甚至改变场景表面都是非常重要的。
为了提高预计算速度Unity并不直接在光照图素上工作,而是创建一个近似场景静态几何体的低分辨率图,被称之为“集群/簇”。
这里写图片描述
Left: With scene view set to ‘Albedo’ the texels generated by Unity’s Precomputed Realtime GI can clearly be seen. By default a texel in this view is roughly the size of a cluster. Right: The scene as it appears in-game once the lighting has been calculated and the results converted to lightmap textures and applied.
左:由Unity预计算实时全局光照生成的纹素可在【场景视图】的【Albedo】清晰地看到。默认情况下,一个纹素和一个集群的大小近似相等。
右:一旦光照被计算完成,并把结果转换到光照图及应用后,场景视图和游戏视图表现一致。

Traditionally when calculating global illumination, we would ‘ray trace’ light rays as they bounce around the static scene. This is very processing intensive and therefore too demanding to be updated in realtime. Instead, Unity uses ray tracing to calculate the relationships between these surface clusters beforehand - during the ‘Light Transport’ stage of the precompute.
By simplifying the world into a network of relationships, we remove the need for expensive ray tracing during the performance-critical gameplay processes.
We have effectively created a simplified mathematical model of the world which can be fed different input during gameplay. This means we can make modifications to lights, or surface colors within the scene and quickly see the effects of GI in scene lighting update at interactive framerates. The resulting output from our lighting model can then be turned into lightmap textures for rendering on the GPU, blended with other lighting and surface maps, processed for effects and finally output to the screen.

传统计算全局光照,我们会用光线追踪与场景上静态对象碰撞。因为在运行时要求及时更新,处理将会非常密集。相反,Unity在预计算的光照传输阶段就通过光线追踪计算好了表面集群间的关系。
通过将场景简化成一个关系网络,我们从性能关键的运行时抽离出性能昂贵的光线追踪处理。
我们有效地创建一个可以在运行时填充不同输入值的简化场景数学模型。这意味着我们可以在场景上修改光源,或表面颜色值,然后在合理帧率下可以很快地看到全局光照效果。从光照模型的输出结果能被转换到GPU渲染的光照贴图上,然后和其他光照和表面图融合,加上后期效果最终输出到屏幕上。

Benefits and Costs

效益与成本

Although it is possible to simultaneously use Baked GI lighting and Precomputed Realtime GI, be wary that the performance cost of rendering both systems simultaneously is exactly the sum of them both. Not only do we have to store both sets of lightmaps in video memory, but we also pay the processing cost of decoding both in shaders.
The cases in which you may wish to choose one lighting method over another depend on the nature of your project and the performance capabilities of your intended hardware. For example, on mobile where video memory and processing power is more limited, it is likely that a Baked GI lighting approach would be more performant. On ‘standalone computers’ with dedicated graphics hardware, or recent games consoles, it is quite possible to use Precomputed Realtime GI or even to use both systems simultaneously.
The decision on which approach to take will have to be evaluated based on the nature of your particular project and desired target platform. Remember that when targeting a range of different hardware, that often it is the least performant which will determine which approach is needed.

虽然可以同时使用烘焙全局光照和预计算实时全局光照,但渲染的性能花费也将是它们的总和。不但要存储光照图到显存,还要对着色器的解码耗费处理成本。
您可能希望选择其中一种光照方法取决于您的项目的性质和您打算使用的硬件的性能。比如,在手机上显存和处理能力比较有限,烘焙全局光照方式将更合理。在拥有独立显卡的PC或当前的游戏主机,可以使用预计算实时全局光照,或两个系统同时使用。
决定采用哪一种方法必须基于项目的特殊性和希望的目标平台。注意,在你广泛的目标平台上,性能最差的往往决定了哪种方式合适。

Enabling Baked GI or Precomputed Realtime GI

开启烘焙全局光照或预计算实时全局光照

By default, both Precomputed Realtime GI and Baked GI are enabled in Unity’s Lighting panel (Lighting>Scene). With both enabled, which technique is used can then be controlled by each light individually (Inspector>Light>Baking).
Using both Baked GI and Precomputed Realtime GI together in your scene can be detrimental to performance. A good practise is to ensure that only one system is used at a time, by disabling the other globally. This can be done by unchecking the box next to either Precomputed Realtime GI or Baked GI from Unity’s lighting panel (Lighting>Scene). Now only the checked option will be present in your scene, and any settings configured per-light will be overridden.

默认下,预计算实时全局光照和烘焙全局光照在Unity的光照面板都是开启的(【Window>Lighting>Settings>Scene】)。在都开启的情况下,哪个技术被使用取决于每个独立光源的控制(【Inspector>Light>Mode】)。
同时使用两个系统对性能是不利的。一个好的建议是确保一定时间内只有一个系统被使用,另一个全局被关闭。这可以通过在Unity光照面板上取消勾选复选框(【Realtime Lighting>Realtime Global Illumination】或(【Mixed Lighting>Baked Global Illumination】))。不但在场景中复选项被显示,所有的单光源的设置也将被修改。

Per-Light Settings

单光源设置

The default baking mode for each light is ‘Realtime’. This means that the selected light(s) will still contribute direct light to your scene, with indirect light handled by Unity’s Precomputed Realtime GI system.
However, if the baking mode is set to ‘Baked’ then that light will contribute lighting solely to Unity’s Baked GI system. Both direct and indirect light from those lights selected will be ‘baked’ into lightmaps and cannot be changed during gameplay.

默认的单光源的烘焙模式是“Realtime’”。这表示被选的光源的直接光对场景将仍影响着,间接光被Unity的预计算实时全局光照处理。
然而,如果烘焙模式是“Baked’”,那光源只影响着Unity的烘焙全局光照系统。直接光和间接光只被烘焙到光照图,在运行时不能被修改。
这里写图片描述
Point light with the per-light Baking mode set to ‘Realtime’.
单个点光源的烘焙模式设置成“Realtime”。

Selecting the ‘Mixed’ baking mode, GameObjects marked as static will still include this light in their Baked GI lightmaps. However, unlike lights marked as ‘Baked’, Mixed lights will still contribute realtime, direct light to non-static GameObjects within your scene. This can be useful in cases where you are using lightmaps in your static environment, but you still want a character to use these same lights to cast realtime shadows onto lightmapped geometry.

烘焙模式设置为“Mixed”,被标记为静态的游戏对象仍包括在烘焙全局光照图中。然而,和烘焙模式设置为“Baked”不同,Mixed模式直接光仍实时影响着场景上的非静态游戏对象。当你希望把光照图应用到静态场景,同时想让角色在同样的光源投射阴影到光照图几何体上,这种模式将会很有用了。

猜你喜欢

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