Unity Lighting - Choosing a Lighting Technique 选择照明技术(一)
Choosing a Lighting Technique 选择照明技术
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.
一般来说,Unity中的灯光可以被认为是“实时”或“预先计算”的,并且两种技术都可以结合使用来创建逼真的场景照明。
In this section we will give a brief overview of what opportunities the different techniques offer, their relative advantages and individual performance characteristics.
在本节中,我们将简要概述不同技术提供的机会,其相对优势和个人性能特征。
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中的灯光 - 方向,光点和点是实时的。这意味着他们为场景提供直接照明并更新每一帧。当灯光和GameObjects在场景内移动时,照明将立即更新。这可以在场景和游戏视图中观察到。
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 烘焙GI照明
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 预计算的实时GI照明
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.
虽然传统的静态光照贴图无法对场景内照明条件的变化做出反应,但预先计算好的实时GI确实为我们提供了一种交互式更新复杂场景照明的技术。
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.
一天的时间光照变化,使用预先计算的实时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.
大多数情况下,我们希望在我们的光照贴图中存储间接光线(反射光),以便在我们的场景光照中创建真实感。幸运的是,这种颜色趋于柔和,几乎没有尖锐的或“高频率”的颜色变化。 Unity的预先计算的实时GI解决方案利用了这些间接光的“漫射”特性,以达到我们的优势。
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.
通过在预计算期间进行的这种简化,我们可以有效减少我们在玩游戏期间更新GI照明所需的计算次数,尤其是我们要改变灯的属性(例如颜色,旋转或强度),甚至改变场景中的曲面,这一点非常重要。
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不直接在光照贴图上工作,而是在世界中创建一个低分辨率的静态几何图形近似值,称为“集群”。
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.
在场景视图设置为“Albedo”时,可以清楚地看到Unity预先计算好的实时GI生成的纹理。默认情况下,该视图中的纹理元素大致是集群的大小。
Right: The scene as it appears in-game once the lighting has been calculated and the results converted to lightmap textures and applied. 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.
右:光照计算完成后场景出现在游戏中,并将结果转换为光照贴图并应用。传统上计算全局照明时,我们会在静态场景发生关系反弹时使用“光线追踪”。这是非常强大的处理,因此要求实时更新。而Unity会在预计算的“光传输”阶段通过光线追踪来计算这些表面集群之间的关系 。
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.
我们有效地创建了一个简化的世界数学模型,可以在游戏过程中输入不同的输入。这意味着我们可以对场景中的灯光或表面颜色进行修改,并以交互式帧率快速查看GI在场景灯光更新中的效果。然后,我们的照明模型生成的输出可以转化为光照贴图纹理,用于在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.
虽然可以同时使用烘焙GI照明和预计算实时GI,但要小心同时渲染两个系统的性能成本恰好是它们的总和。我们不仅需要将两组光照贴图存储在视频内存中,还要支付在着色器中解码的处理成本。
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.
您可能希望选择一种照明方法而不是另一种照明方法的情况取决于项目的性质以及预期硬件的性能。例如,在视频内存和处理能力更加有限的移动设备上,烘焙GI照明方法可能更具性能。在具有专用图形硬件的独立计算机或最近的游戏控制台上,可以使用预先计算好的实时GI,甚至可以同时使用这两种系统。
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.
必须根据您的特定项目的性质和期望的目标平台来评估采取哪种方法。请记住,当瞄准一系列不同的硬件时,通常这是决定哪种方法是需要的性能最差的。
Enabling Baked GI or Precomputed Realtime GI
启用烘焙GI或预先计算的实时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).
默认情况下,在Unity的“灯光”面板(“灯光”>“场景”)中启用了预计算实时GI和烘焙GI。启用后,使用哪种技术可以分别控制每个灯(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.
在场景中同时使用烘焙GI和预计算实时GI可能会对性能造成不利影响。一个好的做法是确保一次只使用一个系统,通过全局禁用另一个系统。这可以通过取消选中Unity的照明面板中的预计算实时GI或烘焙GI旁边的框(光照>场景)来完成。现在只有选中的选项会出现在你的场景中,并且任何配置为每个灯的设置都将被覆盖。
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.
每个灯的默认烘焙模式是“实时”。这意味着选定的灯光仍然会为您的场景提供直射光线,并通过Unity的预计算实时GI系统处理间接灯光。
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.
但是,如果烘焙模式设置为“Bake(烘焙)”,那么该灯光将仅为Unity的烘焙GI系统提供照明。来自所选灯光的直接和间接光线将被“烘烤”成光照贴图,并且在游戏过程中无法更改。
Point light with the per-light Baking mode set to ‘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.
选择“混合”烘焙模式,标记为静态的GameObjects仍然会在烘焙的GI光照贴图中包含此光源。然而,与标记为“烘焙”的灯光不同,混合灯光仍然会为您场景中的非静态GameObjects提供实时直接照明。这对于在静态环境中使用光照贴图的情况很有用,但您仍然希望角色使用这些相同的光源将实时阴影投射到光照贴图几何体上。