基本数据类型
- int
- bool
- float
- half
- fixd
- sampler*
向量
- float2
- float3
- float4
- half2
- half3
- half4
- fixd2
- fixd3
- fixd4
向量Swizzle操作
float4 fl=float4(1,0,0,1);
fl.xyzw==fl.rgba
//xyzw 和 rgba 作用相同,不能混用,取其一
//xyzw可以任意组合,如:fl.xy,fl.xyz,fl.xxx,fl.xxy
矩阵
float 2x2 M2x2={1,0, 1,0};
float 2x4 M2x4={1,0,1,0, 1,0,1,0};
float4 col=M2x4[0];
float4 col=float4(M2x4[1]);
数组
float arr[4]={1,0.5,0.5,1};
float4 col=float4(arr[0],arr[1],arr[2],arr[3]);
结构体
//定义
struct v2f{
float4 pos;
float2 uv;
} ;
//声明
v2f o;
o.pos=float4(1,0,0,1);
o.uv=float2(1,0);
宏定义
和C语言类似
- #define MacroName MacroBody
#define MACRORFL float4(1,0,0,1)
void frag()
{
float4 fl=MACRORFL;
}
流程控制
循环次数需要小于1024次
//if-else嵌套
float4 pos=float4(1,0,0,1);
if(pos.x<0 && pos.y<0)
{
col=float4(1,0,0,1);
}else if(pos.x<0)
{
col=float4(0,1,0,1);
}
//条件表达式
bool bl=true;
float4 col= bl ? float4(1,0,0,1) : float4(0,1,0,1);
//while循环
int i=0;
while(i<10)
{
i++;
}
//do-while循环
int i=0;
do{
i++;
}while(i<10);
//for循环
for(int i=0;i<10;i++)
{
}
类型定义符
typedef float4 FL4;
FL4 fl=FL4(1,0,0,1);
函数
函数参数总是通过值拷贝传递
参数传递标记:in out inout
//使用前定义
void Func()
{
}
void frag()
{
Func();
}
//预定义
void Func(inout float4 c);
void frag()
{
float4 col=float4(1,0,0,1);
Func(col);
}
void Func(inout float4 c)
{
}
//数组作为参数必须制定数组维度
float Func(float arr[3])
{
float sum=0;
for(int i=0;i<arr.Length;i++)
{
sum+=attr[i];
}
return sum;
}
void frag()
{
float col[3]={1,1,2};
float sum=Func(col);
}
文件包含
#include "unity.cginc"