目录索引
译文
与顶点/片元着色器一样,标准表面着色器在默认情况下包含了两个结构类型的函数,它们分别是:
- Input.
- SurfaceOutputStandard.
这个结构体与我们在前几节所接触的 appdata(顶点输入)有所不同,为什么呢?
在 appdata 中,我们可以将模型的语义为输入;另一方面,在 Input 中,我们可以确定着色器用于光照计算的预定义函数,这意味着什么?
在 appdata 中我们可以使用语义 POSITION[n] 去使用模型空间下网格顶点的位置;在 Input 中我们可以定义输入的 viewDir,正如我们已经知道的,它与世界空间中的视角方向相对应,用于计算不同的光照函数。
struct Input
{
float2 uv_MainTex; // TEXCOORD0
float3 viewDir; // world - space view direction
float4 color : COLOR; // vertex color
float3 worldPos; // world - space vertices
float3 worldNormal; // world - space normals
};
正如我们在前面几个小节中所学习的反射计算,Input 结构体中的 viewDir 与我们用来确定世界空间中视角方向的函数相同。
viewDir = normalize(_WorldSpaceCameraPos - i.vertex_world);
worldPos 和 worldNormal 也是如此,它们指的是顶点和法线在世界空间中的位置。
worldPos = mul(unity_ObjectToWorld, v.vertex);
worldNormal = normalize(mul(unity_ObjectToWorld, float4(v.normal, 0))).xyz;
我们可以看到,在表面着色器中,我们可以使用与顶点/片元着色器中相同的属性。不同的是,在这种情况下,这些属性是内部预定义的。
原文对照
As in a vertex/fragment shader, in a standard surface by default we can find two struct type functions, these are:
- Input.
- SurfaceOutputStandard.
The struct Input is different from the struct appdata (vertex input) which we reviewed in the previous chapter, why?
In appdata we can define our object’s semantics as an input, on the other hand, in Input we can determine our shader’s predefined functions for the lighting calculation, what does this mean?
In appdata we can use the semantics POSITION[n] to use the position of the mesh vertices in object-space, on the other hand, in Input we can define the input viewDir which, as we already know, corresponds to the view direction in world-space and is used for the calculation of different lighting functions.
struct Input
{
float2 uv_MainTex; // TEXCOORD0
float3 viewDir; // world - space view direction
float4 color : COLOR; // vertex color
float3 worldPos; // world - space vertices
float3 worldNormal; // world - space normals
};
As in the reflection calculation in the previous sections, the Input viewDir is the same as the function that we use to determine the view direction in world-space.
viewDir = normalize(_WorldSpaceCameraPos - i.vertex_world);
The same goes for worldPos and worldNormal, which refer to the position of the vertices and normals in world-space.
worldPos = mul(unity_ObjectToWorld, v.vertex);
worldNormal = normalize(mul(unity_ObjectToWorld, float4(v.normal, 0))).xyz;
As we can see, in a surface shader we can use the same properties as in a vertex/fragment shader, with the difference that in this case, they are predefined internally.
暂无评论内容