对于 Cg/HLSL 顶点程序,
网格顶点数据作为输入传递给顶点
着色器函数。每个输入都需要有指定的语义:例如,POSITION
输入表示顶点位置,NORMAL
表示顶点法线。
通常,顶点数据输入在结构中声明,而不是 逐个列出。在 UnityCG.cginc include 文件中 定义了几个常用的顶点结构,在大多数情况下, 仅使用它们就足够了。这些结构为:
appdata_base
:位置、法线和一个纹理坐标。appdata_tan
:位置、切线、法线和一个纹理坐标。appdata_full
:位置、切线、法线、四个纹理坐标和颜色。示例:以下着色器根据法线为网格着色,并使用 appdata_base
作为顶点程序输入:
Shader "VertexInputSimple" {
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
fixed4 color : COLOR;
};
v2f vert (appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.color.xyz = v.normal * 0.5 + 0.5;
o.color.w = 1.0;
return o;
}
fixed4 frag (v2f i) : SV_Target { return i.color; }
ENDCG
}
}
}
要访问不同的顶点数据,您需要自己声明 顶点结构,或者将输入参数添加到 顶点着色器。顶点数据由 Cg/HLSL 语义标识,并且必须来自 以下列表:
POSITION
是顶点位置,通常为 float3
或 float4
。NORMAL
是顶点法线,通常为 float3
。TEXCOORD0
是第一个 UV 坐标,通常为 float2
、float3
或 float4
。TEXCOORD1
、TEXCOORD2
和 TEXCOORD3
分别是第 2、第 3 和第 4 个 UV 坐标。TANGENT
是切线矢量(用于法线贴图),通常为 float4
。COLOR
是每顶点颜色,通常为 float4
。当网格数据包含的分量少于顶点着色器输入所需
的分量时,其余部分用零填充,但默认值为 1 的 .w
分量除外。例如,网格纹理坐标
通常是仅包含 x 和 y 分量的 2D 矢量。如果
顶点着色器使用 TEXCOORD0
语义声明一个 float4
输入,则
顶点着色器接收的值将包含 (x,y,0,1)。
有关使用这些技术在内置渲染管线中可视化顶点数据的示例,请参阅可视化顶点数据。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.