查看: 4873|回复: 0
打印 上一主题 下一主题

编写顶点和片段着色器:传递顶点数据到顶点程序

[复制链接]
.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

活跃会员 优秀版主 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2013-2-24 09:56:22 |只看该作者 |倒序浏览
For Cg vertex programs, the vertex data must be passed in as a s***cture. Several commonly used vertex s***ctures are defined in UnityCG.cginc include file, and in most cases it's enough just to use them. The s***ctures 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:包含顶点位置,切线,法线 和一个纹理坐标。
For example, this shader colors the mesh based on it's normals and just uses appdata_base as vertex program input:
例如:下面是这个着色器颜色网是基于它的法线,它仅仅在顶点程序中使用了appdata_base输入结构。
Shader "VertexInputSimple" {

SubShader {

Pass {

CGPROGRAM

#pragma vertex vert

#include "UnityCG.cginc"
s***ct v2f {

    float4 pos : SV_POSITION;

    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 s***cture yourself. The s***cture members must be from the following list:
如果你想访问个别的顶点数据,你必须自己声明顶点结构。结构中的成员必须是属于以下列表中的:
    float4 vertex is the vertex position

    float4 vertex 是顶点位置

    float3 normal is the vertex normal

    float3 normal 是顶点法线

    float4 texcoord is the first UV coordinate

    float4 texcoord 是第一UV坐标

    float4 texcoord1 is the second UV coordinate

    float4 texcoord1 是第二UV坐标

    float4 tangent is the tangent vector (used for normal mapping)

    float4 tangent 是切线向量(用在法线贴图中)

    float4 color is per-vertex color

    float4 color 是每个顶点(per-vertex)颜色
【Examples 范例】

[Visualizing UVs 可视化UV]
The following shader example uses vertex position and first texture coordinate as vertex shader input (defined in s***cture 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" {

SubShader {

    Pass {

        Fog { Mode Off }

CGPROGRAM

#pragma vertex vert

#pragma fragment frag
// vertex input: position, UV

s***ct appdata {

    float4 vertex : POSITION;

    float4 texcoord : TEXCOORD0;

};
s***ct v2f {

    float4 pos : SV_POSITION;

    float4 uv : TEXCOORD0;

};

v2f vert (appdata v) {

    v2f o;

    o.pos = mul( UNITY_MATRIX_MVP, v.vertex );

    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 t***s knot model
将着色器应用到扣环模型上调式 UV1着色器的效果
Similarly, this shader vizualizes the second UV set of the model:
类似的,下面这个着色器可视化使用的是第二UV模式:
Shader "!Debug/UV 2" {

SubShader {

    Pass {

        Fog { Mode Off }

CGPROGRAM

#pragma vertex vert

#pragma fragment frag
// vertex input: position, second UV

s***ct appdata {

    float4 vertex : POSITION;

    float4 texcoord1 : TEXCOORD1;

};
s***ct v2f {

    float4 pos : SV_POSITION;

    float4 uv : TEXCOORD0;

};

v2f vert (appdata v) {

    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 colors as vertex shader input (defined in s***cture appdata).
下面这个着色器通过顶点着色器输入结构使用了顶点位置和每个顶点颜色。
Shader "!Debug/Vertex color" {

SubShader {

    Pass {

        Fog { Mode Off }

CGPROGRAM

#pragma vertex vert
// vertex input: position, color

s***ct appdata {

    float4 vertex : POSITION;

    float4 color : COLOR;

};
s***ct v2f {

    float4 pos : SV_POSITION;

    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 position and normal as vertex shader input (defined in s***cture appdata). Normal's 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,所以我们在这里使用了一个小技巧来解决这个问题。
Shader "!Debug/Normals" {

SubShader {

    Pass {

        Fog { Mode Off }

CGPROGRAM

#pragma vertex vert
// vertex input: position, normal

s***ct appdata {

    float4 vertex : POSITION;

    float3 normal : NORMAL;

};
s***ct v2f {

    float4 pos : SV_POSITION;

    fixed4 color : COLOR;

};

v2f vert (appdata 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 可视化切线与副法线]
Tangent 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 s***cture appdata). Tangent's 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,所以我们在这里使用了一个小技巧来解决这个问题。
Shader "!Debug/Tangents" {

SubShader {

    Pass {

        Fog { Mode Off }

CGPROGRAM

#pragma vertex vert
// vertex input: position, tangent

s***ct appdata {

    float4 vertex : POSITION;

    float4 tangent : TANGENT;

};
s***ct v2f {

    float4    pos : SV_POSITION;

    fixed4    color : COLOR;

};

v2f vert (appdata v) {

    v2f o;

    o.pos = mul( UNITY_MATRIX_MVP, 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 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, tangent

s***ct appdata {

    float4 vertex : POSITION;

    float3 normal : NORMAL;

    float4 tangent : TANGENT;

};
s***ct 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!


【来源:互联网】

更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-9-20 10:45 , Processed in 0.092776 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部