着色器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种。

相关推荐
董董女友14 分钟前
unity mcp 配置指南
unity·游戏引擎
垂葛酒肝汤5 小时前
Unity的可视化网格和文字标签
unity·游戏引擎
魔士于安6 小时前
Unity UI图片 复活节UI,卡通风格
游戏·ui·unity·游戏引擎·材质·贴图
weixin_423995006 小时前
unity 团结开发小游戏,加载AssetBundles(第二种方法)
unity·游戏引擎
魔士于安6 小时前
unity 卡通风整套资源 小鸟N套带动作+一套卡通风村落 和 相关道具+落叶粒子效果 buildin
游戏·unity·游戏引擎·贴图·模型
伽蓝_游戏8 小时前
第一章:解构游戏资源
游戏·unity·性能优化·c#·游戏引擎·游戏程序·assetbundle
星辰徐哥9 小时前
Unity C#入门:Visual Studio与Unity的关联配置
unity·c#·visual studio
Sparkle Star9 小时前
Unity VRTK4包导入和依赖关系
unity·游戏引擎
Sparkle Star13 小时前
Unity VRTK4+SteamVR传送组件使用和层级关系
unity·游戏引擎
cheniie13 小时前
Windows下Unity开发VisionPro应用
windows·unity·vision pro