AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
CustomMaterialEditor.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEditor;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity
8 {
12  public static class CustomMaterialEditor
13  {
14  public static Rect TextureWithToggleableColorSingleLine
15  (
16  MaterialEditor matEditor,
17  GUIContent label,
18  MaterialProperty textureProp,
19  MaterialProperty colorToggleProp,
20  MaterialProperty colorProp
21  )
22  {
23  var lineRect = GetControlRectForSingleLine();
24  var controlRect = lineRect;
25 
26  //TexturePropertyMiniThumbnail handles begin and end animation checks
27  matEditor.TexturePropertyMiniThumbnail(lineRect, textureProp, label.text, label.tooltip);
28 
29  controlRect.x += EditorGUIUtility.labelWidth;
30  controlRect.width = EditorGUIUtility.fieldWidth;
31 
32  var toggleTooltip = new GUIContent
33  {
34  text = string.Empty,
35  tooltip = "Enable/Disable color"
36  };
37 
38  //label indent of -1 is the secret sauce to make it aligned with right aligned toggles that come after labels
39  //ShaderProperty handles begin and end animation checks
40  matEditor.ShaderProperty(controlRect, colorToggleProp, toggleTooltip, -1);
41 
42  if (colorToggleProp.floatValue != 0.0f)
43  {
44  controlRect.x += EditorStyles.toggle.fixedWidth;
45  controlRect.x += EditorStyles.toggle.padding.right;
46 
47  //size it to take up the remainder of the space
48  controlRect.width = lineRect.width - controlRect.x;
49 
50  var tooltipOnly = new GUIContent
51  {
52  text = string.Empty,
53  tooltip = label.tooltip
54  };
55 
56  EditorGUI.showMixedValue = colorProp.hasMixedValue;
57  EditorGUI.BeginChangeCheck();
58  var color = EditorGUI.ColorField(controlRect, tooltipOnly, colorProp.colorValue);
59  if (EditorGUI.EndChangeCheck())
60  {
61  colorProp.colorValue = color;
62  }
63  EditorGUI.showMixedValue = false;
64  }
65 
66  return lineRect;
67  }
68 
69  public static void SetScaleOffsetKeywords
70  (
71  MaterialEditor matEditor,
72  MaterialProperty textureProp,
73  MaterialProperty scaleOffsetProp
74  )
75  {
76  var texScaleOffset = scaleOffsetProp.vectorValue;
77  bool usesScale = texScaleOffset.x != 1.0f || texScaleOffset.y != 1.0f;
78  bool usesOffset = texScaleOffset.z != 0.0f || texScaleOffset.w != 0.0f;
79 
80  var mat = matEditor.target as Material;
81 
82  var scaleKeyword = textureProp.name + "_SCALE_ON";
83  var offsetKeyword = textureProp.name + "_OFFSET_ON";
84 
85  ShaderGUIUtils.SetKeyword(mat, scaleKeyword, usesScale);
86  ShaderGUIUtils.SetKeyword(mat, offsetKeyword, usesOffset);
87  }
88 
89  public static Rect TextureWithToggleableColorAutoScaleOffsetSingleLine
90  (
91  MaterialEditor matEditor,
92  GUIContent label,
93  MaterialProperty textureProp,
94  MaterialProperty colorToggleProp, MaterialProperty colorProp,
95  MaterialProperty scaleOffsetProp
96  )
97  {
98  var rect = TextureWithToggleableColorSingleLine(matEditor, label, textureProp, colorToggleProp, colorProp);
99 
100  SetScaleOffsetKeywords(matEditor, textureProp, scaleOffsetProp);
101 
102  return rect;
103  }
104 
105  public static void TextureScaleOffsetVector4Property(MaterialEditor matEditor, MaterialProperty scaleOffsetProp)
106  {
107  matEditor.BeginAnimatedCheck(
108 #if UNITY_2017_1_OR_NEWER
109  GetControlRectForSingleLine(),
110 #endif
111  scaleOffsetProp);
112 
113  EditorGUI.showMixedValue = scaleOffsetProp.hasMixedValue;
114  EditorGUI.BeginChangeCheck();
115 
116  Vector4 scaleOffsetVector = scaleOffsetProp.vectorValue;
117 
118  var textureScale = new Vector2(scaleOffsetVector.x, scaleOffsetVector.y);
119  textureScale = EditorGUILayout.Vector2Field(Styles.scale, textureScale);
120 
121  var textureOffset = new Vector2(scaleOffsetVector.z, scaleOffsetVector.w);
122  textureOffset = EditorGUILayout.Vector2Field(Styles.offset, textureOffset);
123 
124  if (EditorGUI.EndChangeCheck())
125  {
126  scaleOffsetProp.vectorValue = new Vector4(textureScale.x, textureScale.y, textureOffset.x, textureOffset.y);
127  }
128 
129  EditorGUI.showMixedValue = false;
130 
131  matEditor.EndAnimatedCheck();
132  }
133 
134  public static Rect GetControlRectForSingleLine()
135  {
136  return EditorGUILayout.GetControlRect(true, 18f, EditorStyles.layerMaskField);
137  }
138 
139  private static class Styles
140  {
141  public static GUIContent scale = new GUIContent("Tiling", "Scale of texture - multiplied by texture coordinates from vertices");
142  public static GUIContent offset = new GUIContent("Offset", "Offset of texture - added to texture coordinates from vertices");
143  }
144  }
145 }
Helper class for custom material editors
static void TextureScaleOffsetVector4Property(MaterialEditor matEditor, MaterialProperty scaleOffsetProp)
static void SetKeyword(Material mat, string keyword, bool state)
Helper class for custom shader editors