CG语言中:
不支持 switch 语句(可以写,但不能很好的执行。)
循环语句中, 循环次数不能大于 1024 ,否则会报错。
If...ELSE 条件判断语句:
if (true) {
} else {
}
If...ELSE IF...ELSE... 嵌套语句
if (true) {
} else if (true) {
} else {
}
While语句:
while (bool) {
}
Do...While语句:
do {
} while (bool)
For 循环语句:
for (int i=0; i < xxx; i++) {
}
示例代码
Shader "Custom/001" {
SubShader { pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag void vert(in float2 objPos: POSITION, out float4 pos: POSITION, out float4 col: COLOR) {
pos = float4(objPos,,);
if (pos.x < && pos.y < ) {
col = float4(,,,);
} else if (pos.x < ) {
col = float4(,,,);
} else if (pos.y > ) {
col = float4(,,,);
} else
col = float4(,,,);
} void frag(inout float4 col: COLOR) {
int i = ;
while (i < ) {
i++;
}
if (i == )
col = float4(,,,); i = ;
do {
i++;
} while (i < );
if (i == )
col = float4(,,,); for (i=; i < ; i++) { }
if (i == )
col = float4(0.5,0.5,,); } ENDCG
}
} }