wiki/Cg Programming/Unity_shder/Debugging of Shaders

地址

https://en.wikibooks.org/wiki/Cg_Programming/Unity/Debugging_of_Shaders

重点

了解顶点出入参数

内置顶点参数

为了方便大家使用,unity 把常用的顶点参数封装成了一个个的结构体,这样我们用起来就不用在自己写结构体了,下面介绍最常用的
struct vertexInput {float4 vertex : POSITION; // position (in object coordinates, // i.e. local or model coordinates)float4 tangent : TANGENT;  // vector orthogonal to the surface normalfloat3 normal : NORMAL; // surface normal vector (in object// coordinates; usually normalized to unit length)float4 texcoord : TEXCOORD0;  // 0th set of texture // coordinates (a.k.a. “UV”; between 0 and 1) float4 texcoord1 : TEXCOORD1; // 1st set of texture // coordinates  (a.k.a. “UV”; between 0 and 1)fixed4 color : COLOR; // color (usually constant)};
只要查他们对应的绑定语义就可以知道他们的作用,毕竟GPU就是这么来从寄存器中找的
POSITION 位置信息 TANGENT 表面法线向量 NORMAL 法线向量 TEXCOORD0 贴图1 TEXCOORD0 贴图2
COLOR 颜色 其中POSITION 和 TANGENT 两种都是法线贴图,本人这方面的知识匮乏,借用别的连接 http://blog.csdn.net/candycat1992/article/details/41605257
这片文章的最后有详细的介绍,意思大致是这两种法线是基于不同的坐标系 其他的还有
struct appdata_base {float4 vertex : POSITION;float3 normal : NORMAL;float4 texcoord : TEXCOORD0;};struct appdata_tan {float4 vertex : POSITION;float4 tangent : TANGENT;float3 normal : NORMAL;float4 texcoord : TEXCOORD0;};struct appdata_full {float4 vertex : POSITION;float4 tangent : TANGENT;float3 normal : NORMAL;float4 texcoord : TEXCOORD0;float4 texcoord1 : TEXCOORD1;float4 texcoord2 : TEXCOORD2;float4 texcoord3 : TEXCOORD3;fixed4 color : COLOR;// and additional texture coordinates only on XBOX360};struct appdata_img {float4 vertex : POSITION;half2 texcoord : TEXCOORD0;};

上面的这些都大同小异,只要知道了绑定语义,就知道了它的作用

代码

如果unity没有内置的这些结构体,代码是这样的:
Shader "Cg shader with all built-in vertex input parameters" { SubShader { Pass { CGPROGRAM #pragma vertex vert  #pragma fragment frag struct vertexInput {float4 vertex : POSITION;float4 tangent : TANGENT;  float3 normal : NORMAL;float4 texcoord : TEXCOORD0;  float4 texcoord1 : TEXCOORD1; fixed4 color : COLOR; };struct vertexOutput {float4 pos : SV_POSITION;float4 col : TEXCOORD0;};vertexOutput vert(vertexInput input) {vertexOutput output;output.pos =  mul(UNITY_MATRIX_MVP, input.vertex);output.col = input.texcoord; // set the output color// other possibilities to play with:// output.col = input.vertex;// output.col = input.tangent;// output.col = float4(input.normal, 1.0);// output.col = input.texcoord;// output.col = input.texcoord1;// output.col = input.color;return output;}float4 frag(vertexOutput input) : COLOR {return input.col; }ENDCG  }}
}

可是现在已经有了,直接引入就好了
Shader "Cg shader with all built-in vertex input parameters" { SubShader { Pass { CGPROGRAM #pragma vertex vert  #pragma fragment frag #include "UnityCG.cginc"struct vertexOutput {float4 pos : SV_POSITION;float4 col : TEXCOORD0;};vertexOutput vert(appdata_full input) {vertexOutput output;output.pos =  mul(UNITY_MATRIX_MVP, input.vertex);output.col = input.texcoord;return output;}float4 frag(vertexOutput input) : COLOR {return input.col; }ENDCG  }}
}

其中加入了 #include "UnityCG.cginc"
需要知道的是cg语言其实是类c的,如果你知道c语言的话,这样看是不是很好懂,但要知道这句根本不需要什么c的知识,这句就相当于当入了一个包,这里叫引入宏



本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部