目录索引
译文
纹理类型的属性允许我们在着色器中使用纹理。
如果我们想要在3D模型(例如角色模型)上使用纹理,那么我们首先需要为纹理创建一个2D属性,然后通过有两个输入(纹理与UV坐标)的“tex2D”函数将纹理映射到模型上。
有一个我们经常在游戏中用到的贴图属性是立方体贴图,这种贴图对生成反射贴图来说非常有用。反射可以被应用到诸多场合,例如角色的盔甲、金属物体的表面等等。
其余我们能找到的纹理类型都是3D类型的了。它们相较于前面提及的两种纹理来说使用的场景更少,因为它们是立体的,有额外的坐标轴和空间计算。
声明纹理的语法如下所示:
// name ("display name", 2D) = "defaultColorTexture"
// name ("display name", Cube) = "defaultColorTexture"
// name ("display name", 3D) = "defaultColorTexture"
Shader "InspectorPath/shaderName"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Reflection ("Reflection", Cube) = "black" {}
_3DTexture ("3D Texture", 3D) = "white" {}
}
}
当我们声明一个变量的时候,需要注意的是我们是用ShaderLab来声明它的,但在具体的着色器程序中,我们将用Cg或HLSL来使用它。因为Cg/HLSL和ShaderLab是不同的语言,所以我们需要创建“连接变量”。
这些变量使用“uniform”进行全局声明,然而这一步是可以被跳过的,因为程序知道它们是全局变量。所以如果我们想要在一个“.shader”文件中添加一个变量,我们需要先在Shader Lab中声明它,然后在Cg或HLSL中使用同名的全局变量,最后我们就可以在程序中使用它了。
Shader "InspectorPath/shaderName"
{
Properties
{
// declare the properties
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Pass
{
CGPROGRAM
…
// add connection variables
sampler2D _MainTex;
float4 _Color;
…
half4 frag (v2f i) : SV_Target
{
// use the variables
half4 col = tex2D(_MainTex, i.uv);
return col * _Color;
}
ENDCG
}
}
}
在上面给出的例子中,我们声明了两个变量:_MainTex 和 _Color。接着,我们在CGPROGRAM语义块中创建了两个连接变量(“sampler2D _MainTex” 和 “float4 _Color”)。属性和连接变量的名字必须一致,这样我们才能在代码中正确地使用。
在第3.2.7小节中,我们在介绍数据类型时会详细了解2D采样的过程。
原文对照
These properties allow us to implement textures within our shader.
If we want to place a texture on our object (e.g. a 3D character), then we would have to create a 2D property for its texture and then pass it through a function called “tex2D” which will ask us for two parameters: the texture and the UV coordinates of our object.
A property that we will use frequently in our video games is the “Cube” which itself refers to a “Cubemap”. This type of texture is quite useful for generating reflection maps, e.g., reflections in our character’s armor or to metallic elements in general.
Other types of textures that we can find are those of the 3D type. They are used less frequently than the previous ones since they are volumetric and have an additional coordinate for their spatial calculation.
The following syntax declares textures in our shader:
// name ("display name", 2D) = "defaultColorTexture"
// name ("display name", Cube) = "defaultColorTexture"
// name ("display name", 3D) = "defaultColorTexture"
Shader "InspectorPath/shaderName"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Reflection ("Reflection", Cube) = "black" {}
_3DTexture ("3D Texture", 3D) = "white" {}
}
}
When declaring a property it is very important to consider that it will be written in ShaderLab declarative language while our program will be written in either Cg or HLSL language. As they are two different languages, we have to create “connection variables”.
These variables are declared globally using the word “uniform”, however, this step can be skipped because the program recognizes them as global variables. So, to add a property to a “.shader” we must first declare the property in ShaderLab, then the global variable using the same name in Cg or HLSL, and then we can finally use it.
Shader "InspectorPath/shaderName"
{
Properties
{
// declare the properties
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Pass
{
CGPROGRAM
…
// add connection variables
sampler2D _MainTex;
float4 _Color;
…
half4 frag (v2f i) : SV_Target
{
// use the variables
half4 col = tex2D(_MainTex, i.uv);
return col * _Color;
}
ENDCG
}
}
}
In the previous example, we declared two properties: _MainTex and _Color. Then we created two connection variables within our CGPROGRAM, these correspond to “sampler2D _MainTex” and “float4 _Color”. Both the properties and the connection variables must have the same name so that the program can recognize them.
In section 3.2.7 we will detail the operation of a 2D sampler when we talk about data types.
暂无评论内容