Unity Graphics (Unity 图形渲染 )
- Everything for Lighting and Rendering in Unity
- 主要涉及到光照与渲染方面的知识
本文档主要是对Unity官方教程的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于 Unity2018.3版本
参考链接:https://unity3d.com/cn/learn/tutorials/s/graphics
下面是将要介绍的章节,黑体是本次内容:
7.Reflections
By default, objects in a scene are rendered using Unity’s ‘Standard Shader’. The Standard Shader is a ‘physically based shader’ (PBS). This attempts to accurately represent the behavior of light on materials by mimicking physical properties such as reflectivity and the principles of ‘energy conservation’ which exist in the real world.
默认场景的对象使用Unity的标准着色器渲染。标准着色器是基于物理的着色器(PBS)。它尝试去精确呈现模拟物理属性材质的光照行为,比如反射性和真实世界存在的能量守衡原则。
When using the Standard Shader, every material has a degree of reflectivity based on its ‘specularity’, or ‘metalness’. Without hardware powerful enough to ray trace reflections in realtime we have to rely on pre-rendering reflections. We do this with a cubemap - a 6-sided image of the world derived either from the sky, or from a ‘Reflection Probe’ which renders the environment from a specific point in space, writing the results to a texture. This is then blended with other lighting and surface data by a material’s shader in order to approximate the effects of reflectivity, like we see in the real world.
当使用标准着色器,每个材质都有一定的反射率,这取决于它的高光性或金属性。如果硬件性能不足以实时光线追踪反射光,我们必须依靠预渲染的反射光。我们用一个6面都有贴图的立方体贴图实现。贴图或来自天空,或空间中某一位置渲染周围环境的“反射探头”把渲染结果写入到贴图上。然后,和其他光照及材质的着色器的表面数据融合,以接近反射效果,像我们在真实世界看到的一样。
By default, highly Specular/Metal materials in your scene will reflect the Skybox, defined by the Reflection Source property of the Environment Lighting Panel. This behaviour can be changed by choosing a different source, or by adding a Reflection Probe to your scene.
默认,在场景中高光/金属材质将反射天空盒,可由环境光面板的反应源属性来定义。这个行为也可以选择不同的源,或加一个反应探头到场景中得以改变。
By default, objects in a Unity scene will reflect the Skybox. However, this behaviour can be changed globally in the Lighting window using the ‘Reflection Source’ property. The Skybox, or alternatively, a custom cubemap may be used. This ‘Reflection Source’ can be thought of as a scene-wide cubemap used by all objects in the scene, unless overridden - by adding a Reflection Probe.
默认,在Unity场景中对象将反射天空盒。然而,这个行为也可以在光照窗口用“反射源(‘Reflection Source)”全局修改。天空盒或自定义的立方体图都可以被使用。这个“Reflection Source”可以被认为是场景中所有物体都会使用的cubemap (场景级的),除非通过添加一个反射探头而被覆盖。
Reflection Probes
Often it is undesirable for objects to simply reflect the Skybox in a Unity scene. In many instances, objects may be blocked or ‘occluded’ from the sky. They may be indoors or underneath an architectural feature like a bridge or tunnel . To create more accurate reflections we need to sample what objects ‘see’ using a ‘Reflection Probe’. These probes render the world from their position in 3D space and write the results to a cubemap. This can then be used by nearby objects to give the impression that they are reflecting the world around them.
一般情况下,并不希望Unity场景对所有对象都简单地反射天空盒。在许多应用中,对象可能被阻挡或“遮挡”在天空下。它们可能在室内或地下的建筑特征,比如桥或隧道。为了创建更精确的反射,我们需要用一个反射探头采样看到的对象。这些探头从它们的位置渲染周围3D空间,并把结果写入立方体贴图。这可以用于附近的对象,让其产生在反射周围世界的效果。
A reflection probe can be added via (GameObject>Light>Reflection Probe).
一个反射探头可以通过【GameObject>Light>Reflection Probe】添加。
The position of a Reflection Probe will determine what the generated cubemap looks like, and therefore what is ‘seen’ in the reflections. Generally, for performance reasons it is better to use as few probes as possible. Remember, reflection probes are not meant to give physically accurate results, but instead give the impression of reflections in the game world. A few well placed probes throughout your scene will be sufficient in most cases.
反射探头的位置将决定生成的cubemap的内容,好像是在反射位置看到的内容。一般出于性能原因,越少使用探头越好。注意,反射探头不是为了提供精确的物理结果,而是给游戏世界反射的效果。大部分情况,只需要在场景中放置一些合适位置的探头就足够了。
Left: Our scene with default reflections. Right: With a Reflection Probe added.
左:默认反射光。右:添加一个反射探头
In the Inspector panel for a Reflection Probe we can set the ‘Type’ property of the probe to choose between ‘Baked’, ‘Custom’ or ‘Realtime’. It should be noted that Realtime Reflection Probes are extremely detrimental to performance as we are effectively rendering the scene an additional 6 times for each probe. There are specific cases where a realtime reflection probe is required and this expense is justified, but as a general rule, Baked Reflection Probes are preferable as they are considerably more performant.
在反射探头的属性面板,可以设置探头的类型属性为“烘焙”,“自定义”或“实时”。注意:实时反射探头对性能花费是极其不利的,因为对每个探头来说需要消耗6倍的成本去有效地渲染场景。有一些特殊案例需要实时反射探头,并接受昂贵的成本,但一般来说,烘焙反射探头更可取,因为它们性能更合理。
Note that GameObjects are only visible to Baked Reflection Probes if marked as ‘Reflection Probe Static’ from the ‘Static’ drop-down at the top of the Inspector panel. Conversely, ‘Realtime’ probes render all visible GameObjects in the world unless a culling mask is applied.
注意:游戏对象只要对烘焙反射探头可见,并标记为“反射探头静态”(属性面板顶部“Static”的下拉列表中)。相反,实时探头将渲染世界中可见的全部游戏对象,除非被标记为剔除。
8.Ambient Lighting
An important contributor to the overall look and brightness of a scene is ‘ambient lighting’. This can be thought of as a global light source affecting objects in the scene from every direction.
一个场景的整体外观和亮度的一个重要贡献者是“环境光”。可以认为是一个从任意方向照射影响场景中对象的全局光源。
Ambient light can be useful in a number of cases, depending upon your chosen art style. An example would be bright, cartoon-style rendering where dark shadows may be undesirable or where lighting is perhaps hand-painted into textures. Ambient light can also be useful if you need to increase the overall brightness of a scene without adjusting individual lights.
环境光在大部分案例都很有用,并取决于你选择的美术风格。比如:明亮的卡通渲染风格不需要黑色阴影,或手绘光照到贴图中。如果你需要提高整体明亮度,但不调整单独的光源,环境光也是比较有用的。
Without using one of Unity’s precomputed lighting solutions, ambient light will not be occluded and therefore will not be physically accurate. However if either Baked GI or Precomputed Realtime GI are enabled in your scene then this ‘skylight’ will be blocked by objects in your scene - giving a more realistic result.
不用Unity的预计算光照系统,环境光没有遮挡关系,因此没有物理上的精确性。但如果开启了烘焙GI或预计算实时GI,环境光将会在场景中被对象所阻挡,产生更真实感的结果。
The same scene with no light whatsoever (Left) and with only Ambient Light (Right). Notice how the visible Skybox does not change when changes are made to the Ambient Intensity.
左:没有任何光照,右:只有环境光。注意:当调整环境光强度时,天空盒并不受影响。
Now using Precomputed Realtime GI by marking the objects as static. Notice how light is now occluded in areas of contact between surfaces.
现在用预计算实时GI并把对象标记为静态。注意几何体交接区域光照是怎样遮挡的。
A significant advantage of using ambient light is that it is cheap to render and so particularly useful for mobile applications where perhaps it is desirable to minimize the number of lights in your scene.
用环境光的重要优势是渲染成本低,尤其对于希望场景中最小化光照数量的手机应用来说。
Ambient Lighting can be controlled in the Lighting window from the ‘Environment Lighting’ section (Lighting>Scene>Ambient Source).
环境光可以从光照面板的【Lighting>Scene>Ambient Source】的【Environment Lighting】来控制。
The default value is for the ‘Ambient Source’ property to be set to ‘Skybox’. The Skybox in this case is the default procedural Skybox that - with default settings - contributes a blue tint to the Ambient Lighting of the scene. The other options for the ‘Ambient Source’ include a solid color, or a ‘Gradient’ which is a simple color ramp applied over the hemisphere.
环境光源默认值是天空盒。天空盒是默认的程序化天空盒,为场景的环境光提供蓝色的色调。另外选择是固定色,或一个渐变色:在整个半球上的简单色的渐变。由天空色,赤道色,地面色计算得出。
Note that changing the color of the ambient source does not affect the visible Skybox, instead it only affects the color of lighting within the scene.
注意,修改环境光源的颜色并不会影响到天空盒的可视性,只是影响到场景的光照色。
9.Light Types
We have now covered some of the project settings which need to be considered before beginning work on lighting your scenes in Unity. Hopefully at this point you should have your project configured appropriately for your target platform (generally Baked GI and Gamma Color Space for mobile, Precomputed Realtime GI and Linear Color Space for Standalone PC or recent games consoles).
我们现在已经介绍了一些在开始Unity光照方面工作之前需要考虑的项目设置。希望现在你有了接近目标平台的项目设置(一般手机用烘焙GI和Gamma颜色空间,PC或游戏主机用预计算实时GI和线性颜色空间)。
Let’s move on to looking at the tools available for achieving the lighting you want in your game.
让我们继续寻找达到你想要的可用来实现光照的工具。
Directional Lights
‘Directional Lights’ are very useful for creating effects such as sunlight in your scenes. Behaving in many ways like the sun, Directional Lights can be thought of as distant light sources which exist infinitely far away.
平行光在场景中对创建像太阳光这样的效果是非常有用的。行为很像太阳,平行光像无限远距离的光源。
Light rays emitted from Directional Lights are parallel to one another and do not diverge like those from other light types. As a result, shadows cast by Directional Lights look the same, regardless of their position relative to the source. This is useful to us, especially when lighting outdoor scenes.
从平行光射出的光线是平行的,不像其他光源类型光线偏离。因此,无论他们相对光源的位置在哪里,通过平行光投射的阴影都是一致的。这对于我们是有用的,尤其室外场景光照。
As Directional Lights do not have a source position, they can be placed anywhere in your scene without changing the effect of the light. Rotating the light however does greatly affect the visual result.
平行光并没有源位置,它们可以放在场景的任何位置而不改变光照效果。但旋转光源将会有比较大的视觉影响。
With other light types where there is an obvious source position, such as Spotlights, character shadows will change as the character moves closer to the light source. This can be a problem when trying to generate character shadows in interior levels. Directional lights are advantageous in these situations as shadows remain consistent regardless of proximity to the light source.
像其他的光源类型有一个明显的源位置信息,像聚光灯,随着角色移动的靠近他的影子将产生变化。如果光源在角色内部生成阴影将会出错。在这些情况下,平行光是有优势的,因为无论如何靠近光源,阴影始终保持一致。
Directional Lights do not diminish over distance. As they affect all surfaces in your scene (unless culled), they pose a performance cost when using the Deferred Rendering path. Remember that when using this rendering technique, the performance cost of a light is relative to the number of pixels it illuminates. However, despite the cost, performance will at least be consistent and as such, easier to balance.
平行光不会因为距离而减弱。它会影响到场景的全部表面(除非剔除掉了),用于延迟渲染有一定的性能成本,这取决于光照到的像素数。但抛却成本不谈,性能至少是稳定的,比较容易平衡。
By default, every new Unity scene contains a Directional Light. In Unity 5, this is linked to the procedural sky system defined in the Environment Lighting section of the Lighting Panel (Lighting>Scene>Skybox). You can change this behaviour by deleting the default Directional Light and creating a new light or simply by specifying a different GameObject from the ‘Sun’ parameter (Lighting>Scene>Sun).
默认,Unity新场景都含有一个平行光。在Unity 5中它关联到程序化天空系统。其可在光照面板的环境光部分看到(【Lighting>Scene>Skybox】)。可删除默认平行光再创建一个新光源或者从“太阳”参数指定一个不同的游戏对象(【Lighting>Scene>Sun】)。
Rotating the default Directional Light (or ‘Sun’) causes the ‘Skybox’ to update. With the light angled to the side, parallel to the ground, sunset effects can be achieved. Additionally, pointing the light upwards causes the sky to turn black, as if it’s nighttime. With the light angled from above, the sky will resemble daylight.
旋转默认平行光(太阳)引起天空盒更新。随着光照角度的倾斜,平行到地面,日落效果将会出现。再者,光照朝上,天空将会变黑,好像夜晚效果。再把角度从上向下,天空如白天日照。
If the Skybox is selected as the ambient source, Ambient Lighting will change in relation to these colors.
如果天空盒被作为环境光源,环境光将随着这些颜色而变化。
Point Lights
A Point Light can be thought of as a point in 3D space from which light is emitted in all directions. These are useful for creating effects like light bulbs, weapon glow or explosions where you expect light to radiate out from an object.
The intensity of of Point Lights in Unity diminishes quadratically from full intensity at the centre of the light, to zero at the limit of the light’s reach defined by the ‘Range’ property of the component in the Inspector. Light intensity is inversely proportional to the square of the distance from the source. This is known as ‘inverse square law’ and is similar to how light behaves in the real world.
点光源可以被看成一个3D空间的一个点,它可以向所有方向照射。适用于作为电灯泡,武器辉光或者爆炸时期望从一个物体放射出来的光照。
Unity中点光的光照强度是从中心从全部强度平方性递减,直到光照的范围边界下降到0,范围定义可从属性面板的组件上设置。光照强度和到光源距离的平方成反比。这就是所谓的“平方反比定律”,和真实世界的光照表现是相似的。
Point Lights emit light in all directions from their position in the world. The spherical gizmo represents the ‘Range’ of the Light. Direct Light will ‘Falloff’ to zero when it reaches this limit, however bounced - or indirect light can continue much further.
点光从位置上发射各个方向的光照。球形线框显示光照的范围。光照强度到达边界时下降为0,但碰撞或间接光可以影响更远。
Enabling shadows for Point Lights can be expensive and so must be used sparingly. Point Lights require that shadows have to be rendered six times for the six world directions and on slower hardware this can be an unacceptable performance cost.
When adding Point Lights to a scene it’s worth noting that currently they do not support indirect bounce light shadowing.This means that the light created by Point Lights will continue through objects and bounce on the other side unless attenuated by range. This can lead to light ‘leaks’ through walls and floors and therefore lights must be carefully placed to avoid such problems. This however is not a problem when using Baked GI.
对点光开启阴影是很耗性能的,必须谨慎使用。点光必须从6个方向渲染6次完成阴影,对于硬件差的是不可接受的性能成本。
当加点光到场景时,注意他们不支持间接碰撞光照阴影。这意味着点光光照会继续穿过对象并在另一外碰撞,除非范围减弱。这会导致墙和地板会泄露光照,因此必须小心放置来避免这些问题。然而当使用烘焙GI这将不会出错。
Spotlights
Spotlights project a cone of light in their forward (+Z) direction. The width of this cone is defined by the light’s ‘Spot Angle’ parameter. Light will ‘falloff’ from the source position towards the extent of the light’s range, where it will eventually diminish to zero. Light also diminishes at the edges of the Spotlight’s cone. Widening the Spot Angle increases the width of the cone and with it, increases the size of this fade, known as the ‘penumbra’.
聚光灯向Z方向投射一个圆锥体光照。圆锥体宽通过光照的“投射角度”来定义。光照从源位置衰减到范围边界为0。在圆锥体的边框也将衰减。拉大投射角度将增大圆锥体的宽度,衰减范围也将增大,被称为“半影”。
Spotlights have many useful applications for scene lighting. They can be used to great effect as street lights, wall downlights or used dynamically, for creating effects like a flashlight. As their area of influence can be precisely controlled, Spotlights are extremely useful for creating focus on a character or for creating dramatic stage lighting effects.
聚光灯有很多应用场景。比如街灯,墙射灯,或为了动态地营造效果的闪光灯。因为他们的影响范围可以被精确控制,聚光灯对创造焦点到角色上或创造引人注目的舞台光效尤其有用。
Light will diminish as distance increases from the source. Notice how light also fades towards the edges of the cone. We call this the Light’s penumbra and this increases as the angle of the cone increases.
越远离光源越衰减明显。注意圆锥体边框也在衰减。我们称为“半影”,随着圆锥体角度增加而增加。
Like Point Lights, Spotlights do not presently support indirect shadowing when using Precomputed Realtime GI. This means that light produced by Spotlights will travel through geometry and will bounce on the other side. Placement therefore needs to be carefully considered.
和点光源一样,当用预计算实时GI时,聚光灯也不支持直接阴影。这表示聚光灯将会穿过几何体在另一边碰撞。放置位置因此需要谨慎。
Area Lights
Area Lights can be thought of as similar to a photographer’s softbox. In Unity they are defined as rectangles from which light is emitted in all directions, from one side only - the object’s +Z direction. Presently only available in Baked GI, these Area Lights illuminate uniformly across their surface area. There is no manual control for the range of an Area Light, however intensity will diminish at inverse square of the distance as it travels away from the source.
区域光可以被认为类似于摄影师的柔光箱。Unity中定义为一个可以向任意方向照射的矩形光源,只在Z方向一面。只在烘焙GI中有效显示,光照均匀地照射在表面区域。区域光不可手动控制范围,但距离光源越远,强度随之以“平方反比定律”衰减。
Light is emitted across the surface of an Area Light producing a diffuse light with soft shadowing.
区域光的表面发出光,产生一种有柔和阴影的漫反射光照。
Area Lights are useful in situations where you wish to create soft lighting effects. As light is emitted in all directions across the surface of the light, the rays produced travel in many directions - creating a diffuse lighting effect across a subject. A common use for this might be a ceiling striplight or a backlit panel.
区域光在你希望创造柔光效果时是比较有用的。光从光源表面向各个方向发射,光线在很多方向传播,在被影响的物体上生成漫反射光照效果。一个通常的使用是天花板上的带状灯或背光面板。
In order to achieve this, we must fire a number of rays from each lightmap texel in the world, back towards the light in order to determine whether the light can be seen. This means that Area Lights can be quite computationally expensive and can increase bake times. However, used well, they can add a great depth of realism to your scene lighting and this extra precomputation may be justified. Note that as they are baked only, gameplay performance is not affected.
为了达到这个效果,我们必须从每个光照图纹素发射大量的光线,并反馈到光源来确定光源是否被看到。这表示区域光需要相对大的计算量,会加大烘焙的时间。然而,相当好用,它可以大大增强场景的真实感,并且这额外的预处理可能合理的。注意这只是在烘焙时,运行时并不受其影响。