Unity 4.3 new "MaterialPropertyDrawer" feature

Hi,

I just tried to use the new “MaterialPropertyDrawer” features of the 4.3 release to write a shader that would have enabled/disabled options but while i succeeded in using the enum i could not have the checkbox to work as expected.

Here is a simple shader i wrote to test this feature, it is supposed to change the color based on the value of the _Invert Property.
If someone can point out what i may have done wrong here, that would be great.

Thanks in advance !

Shader "Basic"
{
	Properties 
	{
		_Color1 ("Color 1", color) = (1,1,1,1)
		_Color2 ("Color 2", color) = (0,0,0,1)

		[Toggle] _Invert ("Invert Color ?", Float) = 0
	}

	SubShader 
	{
		Tags { "Queue"="Geometry" }
		Cull Off
		Lighting Off

		Pass
		{
			CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#pragma multi_compile _INVERT_ON _INVERT_OFF
				#include "UnityCG.cginc"
	
				uniform fixed4 _Color1;
				uniform fixed4 _Color2;
					
				struct vertexInput
				{
					float4 vertex : POSITION;
					half2 texcoord : TEXCOORD0;
				};
				struct vertexOutput 
				{
					float4 pos : SV_POSITION;
					half2 tex : TEXCOORD0;
				};
				
				vertexOutput vert(vertexInput input)
				{
					vertexOutput output;
					output.tex = input.texcoord;
					output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
					return output;
				}
				
				fixed4 frag(vertexOutput input) : COLOR
				{
					fixed4 color = fixed4(1.0,0.0,0.0,1.0);
					
					#if _INVERT_ON
						color = _Color1;
					#else
						color = _Color2;
					#endif

					return color;
				}
			ENDCG
		}
	}
	CustomEditor "CustomMaterialInspector"
}

Change line 8:
[Toggle] _Invert (“Invert Color ?”, Float) = 0
to this:
[MaterialToggle(_INVERT_OFF)] _Invert (“Invert Color ?”, Float) = 0

1 Like

Hi Arkhivrag

Thanks for your reply.
That worked :slight_smile:

I tried to do the same, to make this work, [Toggle] or [MaterialToggle] is indifferent but you need to specify inside the toggle parentheses the Keyword defined as THE LAST in the #pragma multi_compile line or it will not work at all.

In this case you defined:
#pragma multi_compile _INVERT_ON _INVERT_OFF
So if you write [MaterialToggle(_INVERT_OFF)] or [Toggle(_INVERT_OFF)] it will work if you use [MaterialToggle(_INVERT_ON)] or [Toggle(_INVERT_ON)] it will not work at all.

Here is the problem, the KeywordEnum MaterialPropertyDrawer doesn’t work I tried changing the combinations back to front it doesn’t seem to work the same:

My Test Shader:

Shader "Custom/ColorizeMe" {    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _Color("Color", Color) = (1.0,0.0,1.0,1.0)
        [KeywordEnum(BLACK, GREY, WHITE)] _AlphaValue ("Alpha Value", Float) = 0
        [Toggle(COLOR_ON)] _Colorize ("Colorize Me ?", Float) = 1
    }
    SubShader {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Lambert
        #pragma multi_compile  BLACK GREY WHITE        
        #pragma multi_compile  COLOR_OFF COLOR_ON

        #include "UnityCG.cginc"

        uniform sampler2D _MainTex;
        uniform fixed4 _Color;

        struct Input {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o) {
            half4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Alpha = c.a;

            #if COLOR_ON
                o.Albedo *=  _Color.rgb; // Colorize Me
            #else
                o.Albedo *= fixed3(1.0,0.0,0.0); // Redify Me
            #endif
            
            #if WHITE
                o.Alpha = 1.0; // White Alpha
            #elif GREY
                o.Alpha = 0.5; // Grey Alpha
            #else
                o.Alpha = 0.0; // Black Alpha
            #endif
        }
        ENDCG
    } 
    FallBack "Diffuse"
    CustomEditor "CustomMaterialInspector"
}

It only shows the ALPHA color set as the FIRST Keyword in the multicompile directive.

Any Idea ?
Maybe I’m missing something or I’ve written this the wrong way. :face_with_spiral_eyes:

Okay reading back the reference it looks like I’ve read wrong the part on the Enums:

The keywords will be:
_ALPHAVALUE_BLACK _ALPHAVALUE_GREY _ALPHAVALUE_WHITE

Gonna test and report if it works.

EDIT: Works, indipendently on the order of the Keys in the MultiCompile directive.