├── BlendModeDrawer.cs ├── MaterialAlphaRangeDrawer.cs ├── MaterialNormalizeDrawer.cs ├── MaterialTextureToggleDrawer.cs ├── MaterialToggleLeftDrawer.cs ├── MaterialVectorFieldDrawer.cs └── README.md /BlendModeDrawer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | using UnityEditor.Animations; 4 | 5 | using UnityEngine; 6 | 7 | namespace UnityEditor 8 | { 9 | internal class BlendModeDrawer : MaterialPropertyDrawer 10 | { 11 | const string _ALPHATEST_ON = "_ALPHATEST_ON"; 12 | const string _ALPHABLEND_ON = "_ALPHABLEND_ON"; 13 | const string _ALPHAPREMULTIPLY_ON = "_ALPHAPREMULTIPLY_ON"; 14 | 15 | const string RenderType = "RenderType"; 16 | const string Transparent = "Transparent"; 17 | const string TransparentCutout = "TransparentCutout"; 18 | 19 | string srcblend; 20 | string dstblend; 21 | string zwrite; 22 | string cull; 23 | 24 | public BlendModeDrawer() 25 | { 26 | this.srcblend = "_SrcBlend"; 27 | this.dstblend = "_DstBlend"; 28 | this.zwrite = "_ZWrite"; 29 | this.cull = "_Cull"; 30 | } 31 | 32 | public BlendModeDrawer(string srcblend, string dstblend, string zwrite = "_ZWrite", string cull = "_Cull") 33 | { 34 | this.srcblend = srcblend; 35 | this.dstblend = dstblend; 36 | this.zwrite = zwrite; 37 | this.cull = cull; 38 | } 39 | 40 | public enum BlendMode 41 | { 42 | Opaque, 43 | Cutout, 44 | Fade, // Old school alpha-blending mode, fresnel does not affect amount of transparency 45 | Transparent, // Physically plausible transparency mode, implemented as alpha pre-multiply 46 | } 47 | 48 | private bool IsDefaultRenderQueue( int renderQueue ) 49 | { 50 | return renderQueue == -1 51 | || renderQueue == (int) UnityEngine.Rendering.RenderQueue.Geometry 52 | || renderQueue == (int) UnityEngine.Rendering.RenderQueue.AlphaTest 53 | || renderQueue == (int) UnityEngine.Rendering.RenderQueue.Transparent; 54 | } 55 | 56 | public void SetupMaterialWithBlendMode( Material material, BlendMode blend ) 57 | { 58 | switch( blend ) 59 | { 60 | case BlendMode.Opaque: 61 | material.SetOverrideTag( RenderType, string.Empty ); 62 | material.SetInt( this.srcblend, (int) UnityEngine.Rendering.BlendMode.One ); 63 | material.SetInt( this.dstblend, (int) UnityEngine.Rendering.BlendMode.Zero ); 64 | material.SetInt( this.zwrite, 1 ); 65 | material.SetInt( this.cull, (int) UnityEngine.Rendering.CullMode.Back ); 66 | material.DisableKeyword( _ALPHATEST_ON ); 67 | material.DisableKeyword( _ALPHABLEND_ON ); 68 | material.DisableKeyword( _ALPHAPREMULTIPLY_ON ); 69 | if( IsDefaultRenderQueue( material.renderQueue ) ) 70 | material.renderQueue = -1; // 'From Shader' 71 | break; 72 | case BlendMode.Cutout: 73 | material.SetOverrideTag( RenderType, TransparentCutout ); 74 | material.SetInt( this.srcblend, (int) UnityEngine.Rendering.BlendMode.One ); 75 | material.SetInt( this.dstblend, (int) UnityEngine.Rendering.BlendMode.Zero ); 76 | material.SetInt( this.zwrite, 1 ); 77 | material.SetInt( this.cull, (int) UnityEngine.Rendering.CullMode.Back ); 78 | material.EnableKeyword( _ALPHATEST_ON ); 79 | material.DisableKeyword( _ALPHABLEND_ON ); 80 | material.DisableKeyword( _ALPHAPREMULTIPLY_ON ); 81 | if( IsDefaultRenderQueue( material.renderQueue ) ) 82 | material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.AlphaTest; 83 | break; 84 | case BlendMode.Fade: //AlphaBlend 85 | material.SetOverrideTag( RenderType, Transparent ); 86 | material.SetInt( this.srcblend, (int) UnityEngine.Rendering.BlendMode.SrcAlpha ); 87 | material.SetInt( this.dstblend, (int) UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha ); 88 | material.SetInt( this.zwrite, 0 ); 89 | material.SetInt( this.cull, (int) UnityEngine.Rendering.CullMode.Off ); 90 | material.DisableKeyword( _ALPHATEST_ON ); 91 | material.EnableKeyword( _ALPHABLEND_ON ); 92 | material.DisableKeyword( _ALPHAPREMULTIPLY_ON ); 93 | if( IsDefaultRenderQueue( material.renderQueue ) ) 94 | material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.Transparent; 95 | break; 96 | case BlendMode.Transparent: 97 | material.SetOverrideTag( RenderType, Transparent ); 98 | material.SetInt( this.srcblend, (int) UnityEngine.Rendering.BlendMode.One ); 99 | material.SetInt( this.dstblend, (int) UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha ); 100 | material.SetInt( this.zwrite, 0 ); 101 | material.SetInt( this.cull, (int) UnityEngine.Rendering.CullMode.Off ); 102 | material.DisableKeyword( _ALPHATEST_ON ); 103 | material.DisableKeyword( _ALPHABLEND_ON ); 104 | material.EnableKeyword( _ALPHAPREMULTIPLY_ON ); 105 | if( IsDefaultRenderQueue( material.renderQueue ) ) 106 | material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.Transparent; 107 | break; 108 | } 109 | } 110 | 111 | // Draw the property inside the given rect 112 | public override void OnGUI( Rect position, MaterialProperty prop, String label, MaterialEditor editor ) 113 | { 114 | EditorGUI.showMixedValue = prop.hasMixedValue; 115 | var mode = (BlendMode) prop.floatValue; 116 | 117 | EditorGUI.BeginChangeCheck(); 118 | mode = (BlendMode)EditorGUI.EnumPopup( position, label, mode ); 119 | if( EditorGUI.EndChangeCheck() ) 120 | { 121 | editor.RegisterPropertyChangeUndo( prop.displayName ); 122 | prop.floatValue = (float) mode; 123 | 124 | UnityEngine.Object[] targets = prop.targets; 125 | for( int i = 0; i < targets.Length; i++ ) 126 | { 127 | Material material = (Material) targets[i]; 128 | SetupMaterialWithBlendMode( material, mode ); 129 | } 130 | } 131 | EditorGUI.showMixedValue = false; 132 | } 133 | } 134 | } -------------------------------------------------------------------------------- /MaterialAlphaRangeDrawer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace UnityEditor 5 | { 6 | internal class MaterialAlphaRangeDrawer : MaterialPropertyDrawer 7 | { 8 | private readonly GUIContent alphaLabel; 9 | private readonly float minAlpha, maxAlpha; 10 | private float height; 11 | 12 | public MaterialAlphaRangeDrawer() : this(null) {} 13 | public MaterialAlphaRangeDrawer(string a) : this(a, 1) {} 14 | public MaterialAlphaRangeDrawer(string a, float maxA) : this(a, 0, maxA) {} 15 | 16 | public MaterialAlphaRangeDrawer(string a, float minA, float maxA) 17 | { 18 | alphaLabel = new GUIContent( a ); 19 | minAlpha = minA; 20 | maxAlpha = maxA; 21 | } 22 | 23 | 24 | public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor) 25 | { 26 | height = base.GetPropertyHeight( prop, label, editor ); 27 | 28 | if( prop.type == MaterialProperty.PropType.Color ) 29 | return height * 2 + 1; 30 | else 31 | return height; 32 | } 33 | 34 | public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) 35 | { 36 | if( prop.type == MaterialProperty.PropType.Color ) 37 | { 38 | position = EditorGUI.IndentedRect(position); 39 | var v = prop.colorValue; 40 | position.height = height; 41 | GUI.Label( position, label ); 42 | 43 | EditorGUI.BeginChangeCheck(); 44 | v = EditorGUI.ColorField( position, label, v, true, false, false, null); 45 | 46 | position.y += height; 47 | v.a = EditorGUI.Slider( position, alphaLabel, v.a, minAlpha, maxAlpha ); 48 | 49 | if( EditorGUI.EndChangeCheck() ) 50 | prop.colorValue = v; 51 | } 52 | else 53 | editor.DefaultShaderProperty( prop, label.text ); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /MaterialNormalizeDrawer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace UnityEditor 5 | { 6 | internal class MaterialNormalizeDrawer : MaterialPropertyDrawer 7 | { 8 | int count = 3; 9 | 10 | public MaterialNormalizeDrawer() 11 | {} 12 | 13 | public MaterialNormalizeDrawer( int count ) 14 | { 15 | this.count = count; 16 | } 17 | 18 | public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor) 19 | { 20 | float height = base.GetPropertyHeight( prop, label, editor ); 21 | 22 | if( prop.type == MaterialProperty.PropType.Vector ) 23 | return height * 2; 24 | else 25 | return height; 26 | } 27 | 28 | private Vector4 Round( Vector4 v, int digits ) 29 | { 30 | float d = Mathf.Pow( 10, digits ); 31 | float rcp_d = 1f / d; 32 | v.x = Mathf.Round( v.x * d ) * rcp_d; 33 | v.y = Mathf.Round( v.y * d ) * rcp_d; 34 | v.z = Mathf.Round( v.z * d ) * rcp_d; 35 | v.w = Mathf.Round( v.w * d ) * rcp_d; 36 | return v; 37 | } 38 | 39 | public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) 40 | { 41 | if( prop.type == MaterialProperty.PropType.Vector ) 42 | { 43 | position = EditorGUI.IndentedRect( position ); 44 | var v = prop.vectorValue; 45 | 46 | EditorGUI.BeginChangeCheck(); 47 | switch( count ) 48 | { 49 | case 2: 50 | v = EditorGUI.Vector2Field( position, label, v ); 51 | break; 52 | case 3: 53 | v = EditorGUI.Vector3Field( position, label, v ); 54 | break; 55 | default: 56 | v = EditorGUI.Vector4Field( position, label, v ); 57 | break; 58 | } 59 | if( EditorGUI.EndChangeCheck() ) 60 | { 61 | v = Round( v.normalized, 5 ); 62 | prop.vectorValue = v; 63 | } 64 | } 65 | else 66 | editor.DefaultShaderProperty( prop, label.text ); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /MaterialTextureToggleDrawer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace UnityEditor 5 | { 6 | internal class MaterialTextureToggleDrawer : MaterialPropertyDrawer 7 | { 8 | protected readonly string keyword; 9 | 10 | public MaterialTextureToggleDrawer() 11 | { 12 | } 13 | 14 | public MaterialTextureToggleDrawer(string keyword) 15 | { 16 | this.keyword = keyword; 17 | } 18 | 19 | private static bool IsPropertyTypeSuitable(MaterialProperty prop) 20 | { 21 | return prop.type == MaterialProperty.PropType.Texture; 22 | } 23 | 24 | protected virtual void SetKeyword(MaterialProperty prop, Texture target) 25 | { 26 | this.SetKeywordInternal(prop, target, "_ON"); 27 | } 28 | 29 | protected void SetKeywordInternal(MaterialProperty prop, Texture target, string defaultKeywordSuffix) 30 | { 31 | string text = (!string.IsNullOrEmpty(this.keyword)) ? this.keyword : (prop.name.ToUpperInvariant() + defaultKeywordSuffix); 32 | UnityEngine.Object[] targets = prop.targets; 33 | for (int i = 0; i < targets.Length; i++) 34 | { 35 | Material material = (Material)targets[i]; 36 | bool on = material.GetTexture( target.name ); 37 | if (on) 38 | { 39 | material.EnableKeyword(text); 40 | } 41 | else 42 | { 43 | material.DisableKeyword(text); 44 | } 45 | } 46 | } 47 | 48 | public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) 49 | { 50 | if (!MaterialTextureToggleDrawer.IsPropertyTypeSuitable(prop)) 51 | { 52 | GUIContent label2 = new GUIContent("Toggle used on a non-texture property: " + prop.name); 53 | EditorGUI.LabelField(position, label2, EditorStyles.helpBox); 54 | } 55 | else 56 | { 57 | EditorGUI.BeginChangeCheck(); 58 | prop.textureValue = editor.TextureProperty(position, prop, label.text); 59 | if (EditorGUI.EndChangeCheck()) 60 | { 61 | this.SetKeyword(prop, prop.textureValue); 62 | } 63 | } 64 | } 65 | 66 | public override void Apply(MaterialProperty prop) 67 | { 68 | base.Apply(prop); 69 | if (MaterialTextureToggleDrawer.IsPropertyTypeSuitable(prop)) 70 | { 71 | this.SetKeyword(prop, prop.textureValue); 72 | } 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /MaterialToggleLeftDrawer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace UnityEditor 5 | { 6 | internal class MaterialToggleLeftDrawer : MaterialPropertyDrawer 7 | { 8 | protected readonly string keyword; 9 | 10 | public MaterialToggleLeftDrawer() 11 | { 12 | } 13 | 14 | public MaterialToggleLeftDrawer(string keyword) 15 | { 16 | this.keyword = keyword; 17 | } 18 | 19 | private static bool IsPropertyTypeSuitable(MaterialProperty prop) 20 | { 21 | return prop.type == MaterialProperty.PropType.Float || prop.type == MaterialProperty.PropType.Range; 22 | } 23 | 24 | protected virtual void SetKeyword(MaterialProperty prop, bool on) 25 | { 26 | this.SetKeywordInternal(prop, on, "_ON"); 27 | } 28 | 29 | protected void SetKeywordInternal(MaterialProperty prop, bool on, string defaultKeywordSuffix) 30 | { 31 | string text = (!string.IsNullOrEmpty(this.keyword)) ? this.keyword : (prop.name.ToUpperInvariant() + defaultKeywordSuffix); 32 | UnityEngine.Object[] targets = prop.targets; 33 | for (int i = 0; i < targets.Length; i++) 34 | { 35 | Material material = (Material)targets[i]; 36 | if (on) 37 | { 38 | material.EnableKeyword(text); 39 | } 40 | else 41 | { 42 | material.DisableKeyword(text); 43 | } 44 | } 45 | } 46 | 47 | public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor) 48 | { 49 | float result; 50 | if (!MaterialToggleLeftDrawer.IsPropertyTypeSuitable(prop)) 51 | { 52 | result = 40f; 53 | } 54 | else 55 | { 56 | result = base.GetPropertyHeight(prop, label, editor); 57 | } 58 | return result; 59 | } 60 | 61 | public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) 62 | { 63 | if (!MaterialToggleLeftDrawer.IsPropertyTypeSuitable(prop)) 64 | { 65 | EditorGUI.HelpBox(position, "Toggle used on a non-float property: " + prop.name, MessageType.Error); 66 | } 67 | else 68 | { 69 | EditorGUI.BeginChangeCheck(); 70 | bool flag = Math.Abs(prop.floatValue) > 0.001f; 71 | EditorGUI.showMixedValue = prop.hasMixedValue; 72 | flag = EditorGUI.ToggleLeft(position, label, flag); 73 | EditorGUI.showMixedValue = false; 74 | if (EditorGUI.EndChangeCheck()) 75 | { 76 | prop.floatValue = ((!flag) ? 0f : 1f); 77 | this.SetKeyword(prop, flag); 78 | } 79 | } 80 | } 81 | 82 | public override void Apply(MaterialProperty prop) 83 | { 84 | base.Apply(prop); 85 | if (MaterialToggleLeftDrawer.IsPropertyTypeSuitable(prop)) 86 | { 87 | if (!prop.hasMixedValue) 88 | { 89 | this.SetKeyword(prop, Math.Abs(prop.floatValue) > 0.001f); 90 | } 91 | } 92 | } 93 | } 94 | } // namespace UnityEditor 95 | -------------------------------------------------------------------------------- /MaterialVectorFieldDrawer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace UnityEditor 5 | { 6 | internal class MaterialVectorFieldDrawer : MaterialPropertyDrawer 7 | { 8 | private readonly GUIContent[] labels; 9 | private float height; 10 | 11 | public MaterialVectorFieldDrawer(string x) : this(new string[] { x }) {} 12 | public MaterialVectorFieldDrawer(string x, string y) : this(new string[] { x, y }) {} 13 | public MaterialVectorFieldDrawer(string x, string y, string z) : this(new string[] { x, y, z }) {} 14 | public MaterialVectorFieldDrawer(string x, string y, string z, string w) : this(new string[] { x, y, z, w}) {} 15 | public MaterialVectorFieldDrawer(params string[] labels) 16 | { 17 | this.labels = new GUIContent[labels.Length]; 18 | for (int i = 0; i < labels.Length; i++) 19 | { 20 | this.labels[i] = new GUIContent(labels[i]); 21 | } 22 | } 23 | 24 | public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor) 25 | { 26 | height = base.GetPropertyHeight( prop, label, editor ); 27 | 28 | if( prop.type == MaterialProperty.PropType.Vector ) 29 | return height * (this.labels.Length + 1); 30 | else 31 | return height; 32 | } 33 | 34 | public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) 35 | { 36 | if( prop.type == MaterialProperty.PropType.Vector ) 37 | { 38 | position = EditorGUI.IndentedRect(position); 39 | var v = prop.vectorValue; 40 | position.height = height; 41 | GUI.Label( position, label ); 42 | 43 | EditorGUI.BeginChangeCheck(); 44 | EditorGUI.indentLevel += 1; 45 | for( int i = 0; i < this.labels.Length; i++ ) 46 | { 47 | position.y += height; 48 | v[i] = EditorGUI.FloatField( position, this.labels[i], v[i] ); 49 | } 50 | EditorGUI.indentLevel -= 1; 51 | if( EditorGUI.EndChangeCheck() ) 52 | prop.vectorValue = v; 53 | } 54 | else 55 | editor.DefaultShaderProperty( prop, label.text ); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## [MaterialToggleLeft] 2 | Unity [MaterialToggle] attribute has checkbox on right.
3 | New [MaterialToggleLeft] attribute seems like GUI [ToggleLeft].
4 |
5 | [MaterialToggle] => label □
6 | [MaterialToggleLeft] => □ label
7 |
8 | It can use also [ToggleLeft]
9 |
10 | Reference
11 | https://github.com/MattRix/UnityDecompiled/blob/5.6.0f3/UnityEditor/UnityEditor/MaterialToggleDrawer.cs 12 |
13 | 14 | ## [BlendMode] 15 | Unity Standard Shader use 'Rendering Mode' popup.
16 | But, it built in StandardShaderGUI.cs
17 | // Blending state
18 | [HideInInspector] _Mode ("__mode", Float) = 0.0
19 | [HideInInspector] _SrcBlend ("__src", Float) = 1.0
20 | [HideInInspector] _DstBlend ("__dst", Float) = 0.0
21 | [HideInInspector] _ZWrite ("__zw", Float) = 1.0
22 |
23 | It change to
24 | [BlendMode] _Mode("Rendering Mode", Float) = 0.0
25 | or,
26 | [BlendMode(_SrcBlend, _DstBlend, _ZWrite)] _Mode("Rendering Mode", Float) = 0.0
27 |
28 | 29 | ## [Normalize] 30 | Vector normalize in shader inspector.
31 | [Normalize] _Direction("Normalized Direction",Vector) = (0,1,0,0) // Vector3 Normalize
32 | [Normalize(3)] _Direction("Normalized Direction",Vector) = (0,1,0,0) // Vector3 Normalize
33 | [Normalize(2)] _Direction("Normalized Direction",Vector) = (0,1,0,0) // Vector2 Normalize
34 | [Normalize(4)] _Direction("Normalized Direction",Vector) = (0,1,0,0) // Vector4 Normalize
35 |
36 | 37 | ## [VectorField(x,y,z,w)] 38 | It label to vector member.
39 | [VectorField(param1, param2, param3, param4] _Param("Params",Vector) = (1,2,3,4)
40 |
41 | Params x[ 1 ] y[ 2 ] z[ 3 ] w[ 4 ]
42 | change to:
43 | Params
44 |    param1 [ 1 ]
45 |    param2 [ 2 ]
46 |    param3 [ 3 ]
47 |    param4 [ 4 ]
48 |
49 | 50 | ## [TextureToggle] 51 | It support materialToggle when texture exist.
52 | [TextureToggle(KEYWORD)] _MainTex("Main", 2D) = "white" {}
53 | if you set _MainTex in inspector, material set EnableKeyword("KEYWORD_ON").
54 |
55 | 56 | ## [AlphaRange] 57 | It show Color(RGB) picker and Slider of Alpha Value.
58 | Usage:
59 | [AlphaRange] _Color("Color", Color) = (1, 1, 1, 0.5)
60 | [AlphaRange(Name)] ...
61 | [AlphaRange(Name, maxValue)] ...
62 | [AlphaRange(Name, minValue, maxValue)] ...
--------------------------------------------------------------------------------