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 normal
         float3 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的知识,这句就相当于当入了一个包,这里叫引入宏



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值