zoukankan      html  css  js  c++  java
  • [Unity] Shader

    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
            }
        }
    
    }
  • 相关阅读:
    LeetCode 172. Factorial Trailing Zeroes
    C++primer 练习12.27
    C++primer 练习12.6
    C++primer 练习11.33:实现你自己版本的单词转换程序
    77. Combinations
    75. Sort Colors
    74. Search a 2D Matrix
    73. Set Matrix Zeroes
    71. Simplify Path
    64. Minimum Path Sum
  • 原文地址:https://www.cnblogs.com/yangyxd/p/5403838.html
Copyright © 2011-2022 走看看