代码之家  ›  专栏  ›  技术社区  ›  aybe

无法获取为该着色器属性工作的#pragma multi_compile

  •  0
  • aybe  · 技术社区  · 6 年前

    Shader "Unlit Vertex Color"
    {
        Properties
        {
            _MainTex ("Texture", 2D) = "white" {}
            _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
            _Brightness("Brightness", Range(0.0, 10.0)) = 1
            _Gamma("Gamma", Range(0.0, 10.0)) = 1
    
            [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
            [MaterialToggle] VertexColor("Vertex color", Float) = 1
            [MaterialEnum(Off, 0, Front, 1, Back, 2)] Culling("Culling", Int) = 2
            // BUG does not work
            [KeywordEnum(Default, Colors)] RenderMode("Render mode", Float) = 0
        }
        SubShader
        {
            Tags
            {
                "Queue"="AlphaTest"
                "IgnoreProjector"="True"
                "RenderType"="TransparentCutout"
            }
    
            LOD 100
            Lighting Off
            Cull[Culling]
    
            Pass
            {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma multi_compile _ PIXELSNAP_ON
                #pragma multi_compile _ VERTEXCOLOR_ON
                #pragma multi_compile   RENDERMODE_DEFAULT_ON RENDERMODE_COLORS_ON
    
                #include "UnityCG.cginc"
    
                struct appdata
                {
                    float4 vertex : POSITION;
                    float2 uv : TEXCOORD0;
                    float4 color : COLOR;
                };
    
                struct v2f
                {
                    float4 color : COLOR;
                    float2 uv : TEXCOORD0;
                    UNITY_FOG_COORDS(1)
                    float4 vertex : SV_POSITION;
                };
    
                sampler2D _MainTex;
                float4 _MainTex_ST;
                fixed _Cutoff;
                fixed _Brightness;
                fixed _Gamma;
    
                v2f vert (appdata v)
                {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                    o.color = v.color;
                    o.color.w = 1.0;
    
    #ifdef PIXELSNAP_ON
                    o.vertex = UnityPixelSnap(o.vertex);
    #endif
                    return o;
                }
    
                fixed4 frag (v2f i) : SV_Target
                {
                    fixed4 col = tex2D(_MainTex, i.uv);
                    clip(col.a - _Cutoff);
    
    #ifdef VERTEXCOLOR_ON
                    col *= i.color;
    #endif
    
                    // BUG show one or the other, always falls back to #1
    #ifdef RENDERMODE_DEFAULT_ON
                    col = float4(1,1,1,1);
    #elif RENDERMODE_COLORS_ON
                    col = float4(0,0,0,0);
    #endif
    
                    col.rgb *= _Brightness;
                    col.rgb = pow(col.rgb, 1.0 / _Gamma);
    
                    return col;
                }
                ENDCG
            }
        }
    }
    

    当我要求Unity显示编译的变体时,它们确实是正确的:

    i、 e.我也要 RENDERMODE_DEFAULT_ON RENDERMODE_COLORS_ON

    // Total snippets: 1
    // -----------------------------------------
    // Snippet #0 platforms ffffffff:
    Keywords always included into build: PIXELSNAP_ON RENDERMODE_COLORS_ON RENDERMODE_DEFAULT_ON VERTEXCOLOR_ON
    
    8 keyword variants used in scene:
    
    RENDERMODE_DEFAULT_ON
    RENDERMODE_COLORS_ON
    RENDERMODE_DEFAULT_ON VERTEXCOLOR_ON
    RENDERMODE_COLORS_ON VERTEXCOLOR_ON
    PIXELSNAP_ON RENDERMODE_DEFAULT_ON
    PIXELSNAP_ON RENDERMODE_COLORS_ON
    PIXELSNAP_ON RENDERMODE_DEFAULT_ON VERTEXCOLOR_ON
    PIXELSNAP_ON RENDERMODE_COLORS_ON VERTEXCOLOR_ON
    

    我一直在看唯一的官员 documentation 也有很多人 builtin shaders 渲染模式颜色

    问题:

    如何是一个 KeywordEnum 应该被执行和使用吗?

    0 回复  |  直到 6 年前
        1
  •  0
  •   aybe    6 年前

    答案是:

    删除 _ON 在这些定义上加上后缀就行了。