[Unity] Shader - CG语言 流程控制语句

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
        }
    }

}

 

转载于:https://www.cnblogs.com/yangyxd/p/5403838.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值