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

相关推荐
Sator18 小时前
Unity AStarPath的踩坑点
unity
星河耀银海15 小时前
Unity基础:摄像机Camera的参数设置与视角控制
unity·游戏引擎·lucene
星河耀银海15 小时前
Unity基础:Transform组件的位移、旋转与缩放详解
unity·游戏引擎·lucene
sp42a16 小时前
如何在 NativeScript 中使用 iOS 的 Metal 着色器
ios·着色器·nativescript
海清河晏1111 天前
数据结构 | 单链表
数据结构·unity·dreamweaver
mxwin2 天前
Unity URP 下 MatCap 技术详解 无视光照环境的卡通与质感渲染方案
unity·游戏引擎
山檐雾2 天前
OctreeNode
unity·c#·八叉树
WarPigs2 天前
Unity协程返回值的解决方案
unity·游戏引擎
WarPigs3 天前
Unity单例笔记
unity·游戏引擎
Allen74744 天前
ComfyUI 自动化生产 3D资产 工作流笔记
图像处理·opencv·unity·自然语言处理·3d模型生成·confyui