Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity 4.3 new "MaterialPropertyDrawer" feature

Discussion in 'Shaders' started by melong, Nov 14, 2013.

  1. melong

    melong

    Joined:
    Sep 2, 2011
    Posts:
    22
    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 !

    Code (csharp):
    1.  
    2. Shader "Basic"
    3. {
    4.     Properties
    5.     {
    6.         _Color1 ("Color 1", color) = (1,1,1,1)
    7.         _Color2 ("Color 2", color) = (0,0,0,1)
    8.  
    9.         [Toggle] _Invert ("Invert Color ?", Float) = 0
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags { "Queue"="Geometry" }
    15.         Cull Off
    16.         Lighting Off
    17.  
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.                 #pragma vertex vert
    22.                 #pragma fragment frag
    23.                 #pragma multi_compile _INVERT_ON _INVERT_OFF
    24.                 #include "UnityCG.cginc"
    25.    
    26.                 uniform fixed4 _Color1;
    27.                 uniform fixed4 _Color2;
    28.                    
    29.                 struct vertexInput
    30.                 {
    31.                     float4 vertex : POSITION;
    32.                     half2 texcoord : TEXCOORD0;
    33.                 };
    34.                 struct vertexOutput
    35.                 {
    36.                     float4 pos : SV_POSITION;
    37.                     half2 tex : TEXCOORD0;
    38.                 };
    39.                
    40.                 vertexOutput vert(vertexInput input)
    41.                 {
    42.                     vertexOutput output;
    43.                     output.tex = input.texcoord;
    44.                     output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    45.                     return output;
    46.                 }
    47.                
    48.                 fixed4 frag(vertexOutput input) : COLOR
    49.                 {
    50.                     fixed4 color = fixed4(1.0,0.0,0.0,1.0);
    51.                    
    52.                     #if _INVERT_ON
    53.                         color = _Color1;
    54.                     #else
    55.                         color = _Color2;
    56.                     #endif
    57.  
    58.                     return color;
    59.                 }
    60.             ENDCG
    61.         }
    62.     }
    63.     CustomEditor "CustomMaterialInspector"
    64. }
    65.  
     
  2. Arkhivrag

    Arkhivrag

    Joined:
    Apr 25, 2012
    Posts:
    2,972
    Change line 8:
    [Toggle] _Invert ("Invert Color ?", Float) = 0
    to this:
    [MaterialToggle(_INVERT_OFF)] _Invert ("Invert Color ?", Float) = 0
     
    Seyed_Morteza_Kamaly likes this.
  3. melong

    melong

    Joined:
    Sep 2, 2011
    Posts:
    22
    Hi Arkhivrag

    Thanks for your reply.
    That worked :)
     
  4. mayorc1978

    mayorc1978

    Joined:
    Dec 30, 2013
    Posts:
    36
    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:

    Code (csharp):
    1. Shader "Custom/ColorizeMe" {    Properties {
    2.         _MainTex ("Base (RGB)", 2D) = "white" {}
    3.         _Color("Color", Color) = (1.0,0.0,1.0,1.0)
    4.         [KeywordEnum(BLACK, GREY, WHITE)] _AlphaValue ("Alpha Value", Float) = 0
    5.         [Toggle(COLOR_ON)] _Colorize ("Colorize Me ?", Float) = 1
    6.     }
    7.     SubShader {
    8.         Tags { "RenderType"="Opaque" }
    9.  
    10.         CGPROGRAM
    11.         #pragma surface surf Lambert
    12.         #pragma multi_compile  BLACK GREY WHITE        
    13.         #pragma multi_compile  COLOR_OFF COLOR_ON
    14.  
    15.         #include "UnityCG.cginc"
    16.  
    17.         uniform sampler2D _MainTex;
    18.         uniform fixed4 _Color;
    19.  
    20.         struct Input {
    21.             float2 uv_MainTex;
    22.         };
    23.  
    24.         void surf (Input IN, inout SurfaceOutput o) {
    25.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    26.             o.Albedo = c.rgb;
    27.             o.Alpha = c.a;
    28.  
    29.             #if COLOR_ON
    30.                 o.Albedo *=  _Color.rgb; // Colorize Me
    31.             #else
    32.                 o.Albedo *= fixed3(1.0,0.0,0.0); // Redify Me
    33.             #endif
    34.            
    35.             #if WHITE
    36.                 o.Alpha = 1.0; // White Alpha
    37.             #elif GREY
    38.                 o.Alpha = 0.5; // Grey Alpha
    39.             #else
    40.                 o.Alpha = 0.0; // Black Alpha
    41.             #endif
    42.         }
    43.         ENDCG
    44.     }
    45.     FallBack "Diffuse"
    46.     CustomEditor "CustomMaterialInspector"
    47. }
    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. :confused:

    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.
     
    Last edited: Mar 2, 2014