1、CG着色器首页用户手册组件手册脚本手册论坛传递顶点数据到顶点程序 Providing vertex data to vertex programsDate:2012-03-05 05:12For Cg vertex programs, the vertex data must be passed in as a structure. Several commonly used vertex structures are defined in UnityCG.cginc include file, and in most cases its enough just to use them. Th
2、e structures are:Cg顶点程序必须在结构中传递顶点数据。几种常用的顶点结构定义在文件UnityCG.cginc中。在大部分情况下仅仅使用它们就够了。结构如下: appdata_base: vertex consists of position, normal and one texture coordinate.appdata_base: 包含顶点位置,法线 和一个纹理坐标。 appdata_tan: vertex consists of position, tangent, normal and one texture coordinate.appdata_tan:包含顶点位
3、置,切线,法线 和一个纹理坐标。For example, this shader colors the mesh based on its normals and just uses appdata_base as vertex program input:例如:下面是这个着色器颜色网是基于它的法线,它仅仅在顶点程序中使用了appdata_base输入结构。Shader VertexInputSimple SubShader Pass CGPROGRAM#pragma vertex vert#include UnityCG.cgincstruct v2f float4 pos : SV_POS
4、ITION; fixed4 color : COLOR;v2f vert (appdata_base v) v2f o; o.pos = mul (UNITY_MATRIX_MVP, v.vertex); o.color.xyz = v.normal * 0.5 + 0.5; return o;ENDCG If you want to access different vertex data, you have to declare vertex structure yourself. The structure members must be from the following list:
5、如果你想访问个别的顶点数据,你必须自己声明顶点结构。结构中的成员必须是属于以下列表中的: float4 vertex is the vertex positionfloat4 vertex 是顶点位置 float3 normal is the vertex normalfloat3 normal 是顶点法线 float4 texcoord is the first UV coordinatefloat4 texcoord 是第一UV坐标 float4 texcoord1 is the second UV coordinatefloat4 texcoord1 是第二UV坐标 float4 tan
6、gent is the tangent vector (used for normal mapping)float4 tangent 是切线向量(用在法线贴图中) float4 color is per-vertex colorfloat4 color 是每个顶点(per-vertex)颜色Examples 范例Visualizing UVs 可视化UVThe following shader example uses vertex position and first texture coordinate as vertex shader input (defined in structur
7、e appdata). It is very useful to debug UV coordinates of the mesh. UV coordinates are visualized as red and green colors, and coordinates outside of 0.1 range have additional blue tint applied.以下着色器示例通过顶点着色器输入结构使用了顶点位置和第一纹理坐标。这在网格中调试UV坐标是非常有用的。UV坐标是可见的红色和绿色,坐标以外应用的是取值范围在0到1之间的蓝色值。Shader !Debug/UV 1
8、SubShader Pass Fog Mode Off CGPROGRAM#pragma vertex vert#pragma fragment frag/ vertex input: position, UVstruct appdata float4 vertex : POSITION; float4 texcoord : TEXCOORD0;struct v2f float4 pos : SV_POSITION; float4 uv : TEXCOORD0;v2f vert (appdata v) v2f o; o.pos = mul( UNITY_MATRIX_MVP, v.vertex
9、 ); o.uv = float4( v.texcoord.xy, 0, 0 ); return o;half4 frag( v2f i ) : COLOR half4 c = frac( i.uv ); if (any(saturate(i.uv) - i.uv) c.b = 0.5; return c;ENDCG Debug UV1 shader applied to a torus knot model将着色器应用到扣环模型上调式 UV1着色器的效果Similarly, this shader vizualizes the second UV set of the model:类似的,下
10、面这个着色器可视化使用的是第二UV模式:Shader !Debug/UV 2 SubShader Pass Fog Mode Off CGPROGRAM#pragma vertex vert#pragma fragment frag/ vertex input: position, second UVstruct appdata float4 vertex : POSITION; float4 texcoord1 : TEXCOORD1;struct v2f float4 pos : SV_POSITION; float4 uv : TEXCOORD0;v2f vert (appdata v)
11、 v2f o; o.pos = mul( UNITY_MATRIX_MVP, v.vertex ); o.uv = float4( v.texcoord1.xy, 0, 0 ); return o;half4 frag( v2f i ) : COLOR half4 c = frac( i.uv ); if (any(saturate(i.uv) - i.uv) c.b = 0.5; return c;ENDCG Visualizing vertex colors 可视化顶点颜色The following shader uses vertex position and per-vertex co
12、lors as vertex shader input (defined in structure appdata).下面这个着色器通过顶点着色器输入结构使用了顶点位置和每个顶点颜色。Shader !Debug/Vertex color SubShader Pass Fog Mode Off CGPROGRAM#pragma vertex vert/ vertex input: position, colorstruct appdata float4 vertex : POSITION; float4 color : COLOR;struct v2f float4 pos : SV_POSIT
13、ION; fixed4 color : COLOR;v2f vert (appdata v) v2f o; o.pos = mul( UNITY_MATRIX_MVP, v.vertex ); o.color = v.color; return o;ENDCG Debug Colors shader applied to a model that has illumination baked into colors将着色器应用到模型上调试颜色着色器,颜色有光照烘培效果Visualizing normals 可视化法线The following shader uses vertex positi
14、on and normal as vertex shader input (defined in structure appdata). Normals X,Y,Z components are visualized as R,G,B colors. Because the normal components are in -1.1 range, we scale and bias them so that the output colors are in displayable 0.1 range.下面这个着色器通过顶点着色器输入结构使用了顶点位置和法线。法线的X,Y,Z分量对应的是颜色的R
15、,G,B。因为法线的取值范围是-1到1,而颜色取值范围是0到1,所以我们在这里使用了一个小技巧来解决这个问题。Shader !Debug/Normals SubShader Pass Fog Mode Off CGPROGRAM#pragma vertex vert/ vertex input: position, normalstruct appdata float4 vertex : POSITION; float3 normal : NORMAL;struct v2f float4 pos : SV_POSITION; fixed4 color : COLOR;v2f vert (app
16、data v) v2f o; o.pos = mul( UNITY_MATRIX_MVP, v.vertex ); o.color.xyz = v.normal * 0.5 + 0.5; o.color.w = 1.0; return o;ENDCG Debug Normals shader applied to a model. You can see that the model has hard shading edges.将着色器应用到模型上调试法线着色器。 你可以看到模型的边缘有硬阴影。Visualizing tangents and binormals 可视化切线与副法线Tange
17、nt and binormal vectors are used for normal mapping. In Unity only the tangent vector is stored in vertices, and binormal is derived from normal and tangent.切线和副法线向量是用在法线贴图上的。在Unity里是只在顶点中存储切线向量的,副法线是通过法线和切线得到的。The following shader uses vertex position and tangent as vertex shader input (defined in
18、structure appdata). Tangents X,Y,Z components are visualized as R,G,B colors. Because the normal components are in -1.1 range, we scale and bias them so that the output colors are in displayable 0.1 range.下面这个着色器通过顶点着色器输入结构使用了顶点位置和切线。切线的X,Y,Z分量对应的是颜色的R,G,B。因为切线的取值范围是-1到1,而颜色取值范围是0到1,所以我们在这里使用了一个小技巧来
19、解决这个问题。Shader !Debug/Tangents SubShader Pass Fog Mode Off CGPROGRAM#pragma vertex vert/ vertex input: position, tangentstruct appdata float4 vertex : POSITION; float4 tangent : TANGENT;struct v2f float4 pos : SV_POSITION; fixed4 color : COLOR;v2f vert (appdata v) v2f o; o.pos = mul( UNITY_MATRIX_MVP
20、, v.vertex ); o.color = v.tangent * 0.5 + 0.5; return o;ENDCG Debug Tangents shader applied to a model.将着色器应用到模型上调试切线(Tangents)着色器。The following shader visualizes binormals. It uses vertex position, normal and tangent as vertex input. Binormal is calculated from normal and tangent. Just like normal
21、or tangent, it needs to be scaled and biased into a displayable 0.1 range.下面这个着色器是可视化副法线着色器。它通过顶点着色器输入结构使用了顶点位置,法线和切线。副法线是通过法线与切线计算得来。取值范围就像法线或者切线,需要通过缩放与裁减来使颜色最终的颜色值在0到1的区间内。Shader !Debug/Binormals SubShader Pass Fog Mode Off CGPROGRAM#pragma vertex vert/ vertex input: position, normal, tangentstru
22、ct appdata float4 vertex : POSITION; float3 normal : NORMAL; float4 tangent : TANGENT;struct v2f float4 pos : SV_POSITION; float4 color : COLOR;v2f vert (appdata v) v2f o; o.pos = mul( UNITY_MATRIX_MVP, v.vertex ); / calculate binormal float3 binormal = cross( v.normal, v.tangent.xyz ) * v.tangent.w; o.color.xyz = binormal * 0.5 + 0.5; o.color.w = 1.0; return o;ENDCG Debug Binormals shader applied to a model. Pretty!页面最后更新:2012-01-19标签: 着色器语法 ShaderLab Shader 着色器 顶点数据 顶点程序 分类:Components|评论: 0| 翻译: 风里疯语 投资合作请联系ceeger 英文部分版权属Unity公司所有,中文部分Unity圣典 版权所有,未经许可,严禁转载 。浙ICP备10041254号-1
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1