目录索引
译文
顾名思义,该函数通常用于颜色转换,它允许在两个值之间进行线性插值,例如,我们可以通过lerp,使一个角色的皮肤渐变到另一个皮肤。
其语法如下:
float lerp (float a, float b, float n)
{
return a + n * (b - a);
}
float2 lerp (float2 a, float2 b, float2 n);
float3 lerp (float3 a, float3 b, float3 n);
float4 lerp (float4 a, float4 b, float4 n);
我们将创建一个类型为“Unlit shader”的着色器来举例说明该函数,我们将其称为USB_function_LERP。我们将首先声明两个纹理,我们稍后将使用它们作为“skins”,再加上一个数字范围来执行交叉渐变。
Shader "USB/USB_function_LERP"
{
Properties
{
_Skin01 ("Skin 01", 2D) = "white" {}
_Skin02 ("Skin 02", 2D) = "white" {}
_Lerp ("Lerp", Range(0, 1)) = 0.5
}
SubShader
{
...
Pass
{
...
sampler2D _Skin01;
float4 _Skin01_ST;
sampler2D _Skin02;
float4 _Skin02_ST;
float _Lerp;
...
}
}
}
由于我们将使用两种纹理,因此在每种情况下都需要使用UV坐标。这些必须在顶点输入和输出中声明,主要有两个原因:
1.因为纹理会受到TRANSFORM_TEX函数中“tiling and offset”的影响。
2.因为我们稍后将在片段着色器阶段使用它们。
struct appdata
{
float4 vertex : POSITION;
// create the UV coordinates for each case 01 and 02
float2 uv_s01 : TEXCOORD0;
float2 uv_s02 : TEXCOORD1;
};
struct v2f
{
float4 vertex : SV_POSITION;
// we will use the UV coordinates in the fragment shader stage
float2 uv_s01 : TEXCOORD0;
float2 uv_s02 : TEXCOORD1;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
// add tiling and offset for each case
o.uv_s01 = TRANSFORM_TEX(v.uv_s01, _Skin01);
o.uv_s02 = TRANSFORM_TEX(v.uv_s02, _Skin02);
return o;
}
在片段着色器阶段,我们可以声明两个四维向量,在每种情况下使用tex2D函数,然后使用lerp函数在它们之间执行线性插值。
fixed4 frag (v2f i) : SV_Target
{
// create a vector for each skin
fixed4 skin01 = tex2D(_Skin01, i.uv_s01);
fixed4 skin02 = tex2D(_Skin02, i.uv_s02);
// make a linear interpolation between each color
fixed4 render = lerp(skin01, skin02, _Lerp);
return render;
}
如果我们注意_Lerp属性,我们会注意到它的值被限制在0.0f和1.0f之间。默认情况下,它的值为0.5f;因此,如果我们为_Skin[n]的每个属性指定两个不同的纹理,则在这每种情况下,每个纹理都会有0.5f的透明度,最终混合在一起。
![图片[1]-《Unity着色器圣经》4.1.8. | Lerp function.-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-72-1024x468.jpeg)
原文对照
Commonly used in color transitions, as the name suggests, this function allows a linear interpolation between two values, e.g., we could use lerp on one of our characters to go from one skin to another through a crossfade.
Its syntax is as follows:
float lerp (float a, float b, float n)
{
return a + n * (b - a);
}
float2 lerp (float2 a, float2 b, float2 n);
float3 lerp (float3 a, float3 b, float3 n);
float4 lerp (float4 a, float4 b, float4 n);
We will create a small shader of type “Unlit Shader” to exemplify the function, which we will call USB_function_LERP. We will start by declaring two textures that we will use later as “skins” in effect, plus a numeric range to perform the cross-fading.
Shader "USB/USB_function_LERP"
{
Properties
{
_Skin01 ("Skin 01", 2D) = "white" {}
_Skin02 ("Skin 02", 2D) = "white" {}
_Lerp ("Lerp", Range(0, 1)) = 0.5
}
SubShader
{
...
Pass
{
...
sampler2D _Skin01;
float4 _Skin01_ST;
sampler2D _Skin02;
float4 _Skin02_ST;
float _Lerp;
...
}
}
}
Since we will use two textures, it will be necessary to use UV coordinates in each case. These must be declared in both the vertex input and output for two main reasons:
1. Because the textures will be affected by the “tiling and offset” through the TRANSFORM_TEX function.
2. Because we will use them later in the fragment shader stage.
struct appdata
{
float4 vertex : POSITION;
// create the UV coordinates for each case 01 and 02
float2 uv_s01 : TEXCOORD0;
float2 uv_s02 : TEXCOORD1;
};
struct v2f
{
float4 vertex : SV_POSITION;
// we will use the UV coordinates in the fragment shader stage
float2 uv_s01 : TEXCOORD0;
float2 uv_s02 : TEXCOORD1;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
// add tiling and offset for each case
o.uv_s01 = TRANSFORM_TEX(v.uv_s01, _Skin01);
o.uv_s02 = TRANSFORM_TEX(v.uv_s02, _Skin02);
return o;
}
In the fragment shader stage, we can declare two four-dimensional vectors, use the tex2D function in each case, and then perform a linear interpolation between them using the lerp function.
fixed4 frag (v2f i) : SV_Target
{
// create a vector for each skin
fixed4 skin01 = tex2D(_Skin01, i.uv_s01);
fixed4 skin02 = tex2D(_Skin02, i.uv_s02);
// make a linear interpolation between each color
fixed4 render = lerp(skin01, skin02, _Lerp);
return render;
}
If we pay attention to the _Lerp property, we notice that its value has been limited between 0.0f and 1.0f. By default, it has the value 0.5f; therefore, if we assign two different textures to each property of _Skin[n], the result will be equal to transparency or fading of 0.5f in each case.
![图片[1]-《Unity着色器圣经》4.1.8. | Lerp function.-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-72-1024x468.jpeg)
暂无评论内容