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,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); } else if (pos.y > 0) { col = float4(1,1,0,1); } else col = float4(0,0,1,1); } void frag(inout float4 col: COLOR) { int i = 0; while (i < 10) { i++; } if (i == 10) col = float4(0,0,0,1); i = 0; do { i++; } while (i < 10); if (i == 10) col = float4(1,1,1,1); for (i=0; i < 10; i++) { } if (i == 10) col = float4(0.5,0.5,0,1); } ENDCG } } }