着色器multi_compile笔记

概述

一句multi_compile后面写若干个关键字XXX,在代码里用#if XXX条件编译一段代码。关键字的开启关闭在材质debug界面。在Valid Keywords填的关键字如果在某句multi_compile里会自动进入Valid Keywords,否则进入Invalid。

哪里看变体数量

multi_compile A有几种变体

1种。A一定执行,不管有没有填A。

复制代码
Shader "学变体_一个关键字"
{
    Properties { }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile A

            struct appdata { float4 vertex : POSITION; };
            struct v2f { float4 pos : SV_POSITION; };

            v2f vert (appdata v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target {
                fixed4 col;
                #ifdef A
                col=fixed4(1,0,0,1);
                #endif
                #if !defined(A)
                col=fixed4(1,1,1,1);
                #endif
                return col;
            }
            ENDCG
        }
    }
}

multi_compile A会编译#if A的代码,就和#define A的效果是一样的。

multi_compile _ A有几种变体

2种,带A和不带的。

复制代码
Shader "学变体_一个关键字和下划线"
{
    Properties { }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile A _

            struct appdata { float4 vertex : POSITION; };
            struct v2f { float4 pos : SV_POSITION; };

            v2f vert (appdata v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target {
                fixed4 col;
                #ifdef A
                col=fixed4(1,0,0,1);
                #endif
                #if !defined(A)
                col=fixed4(1,1,1,1);
                #endif
                return col;
            }
            ENDCG
        }
    }
}

multi_compile A B C:允许从A B C选择一个关键字,都不开就执行第一个

如果没写任何关键字,则开启第一个关键字。

multi_compile A B _ C:允许从A B C选择一个关键字,或者不开

如果没写任何关键字,则不开。

总结

没有_,一句multi_compile就是多选一,必选一个,默认选第一个。

有_,就是多选1或0,默认不选。

做几个题吧

复制代码
#pragma multi_compile A
#pragma multi_compile B C D

Shader "学变体"
{
    Properties { }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile A
            #pragma multi_compile B C D

            struct appdata { float4 vertex : POSITION; };
            struct v2f { float4 pos : SV_POSITION; };

            v2f vert (appdata v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target {
                fixed4 col;
                #ifdef B
                col=fixed4(0,0,1,1);
                #endif
                #ifdef C
                col=fixed4(0,1,0,1);
                #endif
                #ifdef D
                col=fixed4(0,0,0,1);
                #endif
                #ifdef A
                col=fixed4(1,0,0,1);
                #endif
                #if !defined(A) && !defined(B) && !defined(C) && !defined(D)
                col=fixed4(1,1,1,1);
                #endif
                return col;
            }
            ENDCG
        }
    }
}

不开任何关键字:执行A,A必执行。3种。这里直接看

豆包说8个,deepseek说6个。

复制代码
#pragma multi_compile A _
#pragma multi_compile B C D

Shader "学变体"
{
    Properties { }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile A _
            #pragma multi_compile B C D

            struct appdata { float4 vertex : POSITION; };
            struct v2f { float4 pos : SV_POSITION; };

            v2f vert (appdata v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target {
                fixed4 col;
                #ifdef B
                col=fixed4(0,0,1,1);
                #endif
                #ifdef C
                col=fixed4(0,1,0,1);
                #endif
                #ifdef D
                col=fixed4(0,0,0,1);
                #endif
                #ifdef A
                col=fixed4(1,0,0,1);
                #endif
                #if !defined(A) && !defined(B) && !defined(C) && !defined(D)
                col=fixed4(1,1,1,1);
                #endif
                return col;
            }
            ENDCG
        }
    }
}

6种。

相关推荐
mxwin8 小时前
Unity URP 中的法线生成完全指南
unity·游戏引擎
游乐码8 小时前
Unity基础(十五)LineRender画线功能
unity·游戏引擎
小贺儿开发10 小时前
Unity3D 图片循环查看器
unity·工具·图片·列表·循环·ugui·互动
threelab11 小时前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
是果果呀儿17 小时前
Vuforia实现物体旋转、移动、缩放
unity·增强现实
不知名的老吴20 小时前
Unity3D 2022安装教程及全流程下载步骤指南
unity·游戏引擎
Thomas_YXQ20 小时前
Unity3D Addressable 深度优化热更性能消耗
开发语言·3d·unity·微信
程序员也有头发20 小时前
如何使用AI工具开发Unity
unity·游戏引擎·ai编程
隔窗听雨眠20 小时前
从零开始的游戏开发入门指南
unity
sinat_3845031121 小时前
【无标题】
unity·webgl