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

相关推荐
WarPigs21 分钟前
Unity人物翻越功能
unity·游戏引擎
游乐码36 分钟前
Unity基础(四)向量相关
游戏·unity·游戏引擎
threelab2 小时前
Three.js 3D 热力图效果 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
神码编程4 小时前
【Unity】MiniGame编辑器小游戏(十五)中国象棋局域网对战【Chinese Chess】(上)
unity·编辑器·游戏引擎·小游戏
伽蓝_游戏4 小时前
第四章:AssetBundle 核心机制与文件结构
unity·c#·游戏引擎·游戏程序
郝学胜-神的一滴4 小时前
中级OpenGL教程 006:高光反射原理与 Shader 实现
c++·unity·godot·图形渲染·three.js·opengl·unreal
神码编程7 小时前
【Unity】MiniGame编辑器小游戏(十六)中国象棋局域网对战【Chinese Chess】(下)
unity·编辑器·游戏引擎·小游戏
Maddie_Mo8 小时前
Unity 联动 Trae AI 项目开发基础教学
人工智能·unity·游戏引擎
XX風14 小时前
OpenGL中 为什么RBO 不能被着色器采样?
着色器
新手unity自用笔记20 小时前
unity简单新手上手动画系统讲解
unity·游戏引擎