《Unity着色器圣经》4.0.2 | 我们的第一个Cg/HLSL着色器

目录索引

译文

我们将继续使用我们在本章开头创建的“USB_simple_color”着色器。正如我们已经知道的,我们的默认着色器有一个名为_MainTex的纹理,该纹理在属性中进行了配置(这里的配置是指,在properties部分声明的变量,在shader代码部门又做了声明,这俩声明就对上了)。我们接下来要做的是添加颜色来改变纹理的色调。在开始之前,我们必须记住,我们不能将着色器直接应用于场景中的对象。为此,我们需要创建一个材质并将着色器指定给它,以便以图形方式进行渲染。要创建材质,我们必须转到我们的项目,右键单击我们正在工作的文件夹,转到“创建”并选择“材质”(这里有个特别快捷的方式:就是右键点击你写的shader,来创建材质)。材质的默认配置取决于我们刚刚创建的渲染管线。通常,当我们在Built-in RP中创建材质时,它的默认shader是Standard Surface shader,此shader允许我们可视化光的方向、阴影和其他相关计算。因此,我们进创建一个三维对象。我们将着色器USB_simple_color指定给刚才创建的材质,然后将该材质应用于场景中的对象。此时,我们可以为该对象上的材质指定纹理。返回我们的USB_simple_color着色器并创建一个颜色属性,如下例所示:

Shader "USB/USB_simple_color"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color ("Texture Color", Color) = (1, 1, 1, 1)
    }
    SubShader { ... }
}

我们刚刚做的是为着色器声明一个颜色属性。如果我们保存并返回Unity,我们可以看到该属性出现在材料检查器中,但它还不起作用,因为我们尚未在CGPROGRAM中声明它。我们接下来需要做的是添加_Color连接变量,以便在程序中使用它。为此,我们进入顶点着色器阶段;其中_MainTex已声明为sampler2D,并以以下方式创建连接变量:

uniform sampler2D _Maintex;
uniform float4 _MainTex_ST;
uniform float4 _Color; // connection variable

正如我们所看到的,我们已经在CGPROGRAM或HLSLPROGRAM中为_Color声明了一个全局变量,但是,我们仍然没有使用它。要更改纹理的色调,我们将进入片段着色器阶段,并使用乘法符号将col(纹理颜色)乘以_color。操作应如下所示:

// CGPROGRAM
fixed4 frag (v2f i) : SV_Target
{
    fixed4 col = tex2D(_MainTex, i.uv);
    return col * _Color;
}

// HLSLPROGRAM
half4 frag (v2f i) : SV_Target
{
    half4 col = tex2D(_MainTex, i.uv);
    return col * _Color;
}

如果我们保存着色器并返回Unity,我们现在可以从材质检查器更改色调纹理。


原文对照

We will continue working with our “USB_simple_color” shader that we created at the beginning of this chapter.
As we already know, our default shader has a texture called _MainTex which is configured in the properties. What we will do next is add color to change the tint of the texture.
Before we begin, we must remember that we cannot directly apply a shader to an object in our scene. For this, we need to create a material and assign the shader to it so that it can be rendered graphically. To create a material we must go to our project, right-click on the folder in which we are working, go to Create and select Material. The default configuration of the material depends on the rendering pipeline we have just created. Generally, when we create a material in Built-in RP it comes with a Standard Surface shader type which allows us to visualize the direction of lighting, shadows and other calculations associated with it.
So, we go to the hierarchy and create a 3D object. We assign the shader USB_simple_color to the material we have recently created and then apply the material to the object in our scene. At this point, we can assign a texture to our material to be projected onto the object.
Go back to our USB_simple_color shader and create a color property, as we will see in the following example:

Shader "USB/USB_simple_color"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color ("Texture Color", Color) = (1, 1, 1, 1)
    }
    SubShader { ... }
}

What we just did was declare a color property for our shader. If we save and return to Unity we can see that this property appears in the Material Inspector, however, it is not working yet because we have not declared it within our CGPROGRAM. What we need to do next is add the _Color connection variable so that we can use it within the program. For this we go to the vertex shader stage; where _MainTex has been declared as sampler2D and create the connection variable in the following way:

uniform sampler2D _Maintex;
uniform float4 _MainTex_ST;
uniform float4 _Color; // connection variable

As we can see, we have declared a global variable for _Color inside our CGPROGRAM or HLSLPROGRAM, however, we are still not using it yet. To change the tint of our texture, we will go to the fragment shader stage and multiply col (texture color) by _Color using the multiplication sign. The operation should look like this:

// CGPROGRAM
fixed4 frag (v2f i) : SV_Target
{
    fixed4 col = tex2D(_MainTex, i.uv);
    return col * _Color;
}

// HLSLPROGRAM
half4 frag (v2f i) : SV_Target
{
    half4 col = tex2D(_MainTex, i.uv);
    return col * _Color;
}

If we save our shader and go back to Unity, we can now change the tint texture from the material inspector.

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容