目录索引
译文
现实世界中随处可见光影的踪迹,光和影是给物体带来体积感的重要元素之一。有趣的是,所有不发光的物体本质都是黑暗的,而我们之所以能够分辨出物体的一个面和另一个面,靠的是光作用于物体表面产生的效果。光照属性的初始值为“0”,因为 0 等于“黑色”。
![图片[1]-《Unity着色器圣经》7.0.2 | 环境光颜色-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-86-1024x398.jpeg)
环境光颜色是指由多个光源反射产生的照明色调。在计算机图形学中,这一名词来自于一种称为全局光照(GI)的技术。全局光照是一种能够计算间接光照的算法,可以模拟光线反射的自然现象。
Unity 允许我们通过菜单“窗口 / 渲染 / 照明”打开照明(Lighting)窗口,我们可以在其中找到项目的全局照明属性。在“环境(Environment)”选项卡中,我们将在着色器中直接使用其中的两个属性,它们分别是
- 1. 光源
- 2. 环境光颜色
为了演示如何使用它们,让我们创建一个名为 USB_ambient_color 的无光照着色器,并关注名为 UNITY_LIGHTMODEL_AMBIENT 的内部变量。
让我们从在着色器中创建一个用于增加或减少环境光总量的范围(range)开始。让我们将范围设置成 0 ~ 1,其中 0 代表环境光为 0%;1 表示环境光为 100%。这样,我们就可以动态更改环境光的值。
Shader "USB/USB_ambient_color"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Ambient ("Ambient Color", Range(0, 1)) = 1
}
SubShader { ... }
}
我们将环境光属性 _Ambient 的默认值设为 1,代表环境光默认为 100%。
接着,我们需要为刚才创建的属性声明连接变量,这样才能在着色器程序中使用它们:
Pass
{
CGPROGRAM
...
sampler2D _MainTex;
float4 _MainTex_ST;
float _Ambient;
...
ENDCG
}
现在来到片元着色器,我们按照下图代码所示使用内置变量 UNITY_ LIGHTMODEL_AMBIENT :
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
float3 ambient_color = UNITY_LIGHTMODEL_AMBIENT * _Ambient;
col.rgb += ambient_color;
return col;
}
_Ambient 和 内置变量 UNITY_ LIGHTMODEL_AMBIENT 的乘积被储存到了一个三维向量 ambient_color 中。最后,将环境光颜色的 RGB 通道与贴图颜色相加。到这一步,我们的环境光颜色已经在工作了。现在,我们只需要来到光照窗口的环境选项卡,将“光源(Source)”属性设置为“颜色(Color)”,接着为“环境光颜色(Ambient Color)”选一个你喜欢的颜色!
![图片[2]-《Unity着色器圣经》7.0.2 | 环境光颜色-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-85-1024x400.jpeg)
值得一提的是,“环境光颜色”属性默认是高动态范围(HDR)颜色,这代表高精度。出于这个原因,在前面例子中的三维向量 ambient_color 被声明成了“float3”类型。
原文对照
A default value that we can find in real life is darkness. Lighting is the reason why we can perceive volume. This is an interesting point because all objects in their nature are dark and the reason we can differentiate between one surface and another, is due to how lighting interacts with the properties of such a surface. This is due to the initialization of “zero” of a light property because zero equals “black”, that is, its default value.
![图片[1]-《Unity着色器圣经》7.0.2 | 环境光颜色-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-86-1024x398.jpeg)
Ambient color refers to the hue of lighting that is generated by the bouncing of multiple light sources. In Computer Graphics, this property derives from a technique known as global illumination, which itself corresponds to an algorithm capable of calculating indirect illumination to simulate the natural phenomenon of light reflection.
In Unity, we can easily access the environment color, for this we must follow the menu Windows / Rendering / Lighting. This displays the Lighting window and in it, we can find the global lighting properties for our project. In the “Environment” tab there are two properties that we will use in our shader directly, these refer to:
- 1. Source
- 2. Ambient color
To illustrate this, we will create a new Unlit Shader and name it USB_ambient_color and focus on an internal variable called UNITY_LIGHTMODEL_AMBIENT.
We will start by creating a property to increase or decrease the amount of ambient color in our shader. To do this, we can use a range between zero and one [0, 1], this is because “zero” refers to 0% illumination and “one” to 100%, therefore, if we want to dynamically modify our property, we will have to add a numerical range.
Shader "USB/USB_ambient_color"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Ambient ("Ambient Color", Range(0, 1)) = 1
}
SubShader { ... }
}
As we can see, we declared a floating type property called _Ambient, which has a range between zero and one [0, 1]. Its default value equals one [1], which means that the ambient color will be initialized at 100% illumination.
Next, we must define its connection variable so that this property can communicate with the program.
Pass
{
CGPROGRAM
...
sampler2D _MainTex;
float4 _MainTex_ST;
float _Ambient;
...
ENDCG
}
Now we simply go to the fragment shader stage and use the internal variable UNITY_ LIGHTMODEL_AMBIENT as follows:
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
float3 ambient_color = UNITY_LIGHTMODEL_AMBIENT * _Ambient;
col.rgb += ambient_color;
return col;
}
The factor between the ambient color and the property _Ambient has been saved in the vector ambient_color. Finally, the RGB ambient color has been added to the col texture. Up to this point, our ambient color is already working. Now we simply need to go to the Lighting window, Environment tab, and set the “Source” property to “Color”, and then select an ambient color from the “Ambient Color” property.
![图片[2]-《Unity着色器圣经》7.0.2 | 环境光颜色-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-85-1024x400.jpeg)
It is worth mentioning that the “Ambient Color” property corresponds to an HDR (High Dynamic Range) color by default, that is, high precision. For that reason, the threedimensional vector ambient_color was declared as a “float3” type vector in the previous example.
暂无评论内容