UniUnlitEditor.cs
6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
namespace UniGLTF.UniUnlit
{
public class UniUnlitEditor : ShaderGUI
{
private MaterialProperty _mainTex;
private MaterialProperty _color;
private MaterialProperty _cutoff;
private MaterialProperty _blendMode;
private MaterialProperty _cullMode;
private MaterialProperty _vColBlendMode;
// private MaterialProperty _srcBlend;
// private MaterialProperty _dstBlend;
// private MaterialProperty _zWrite;
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
{
_mainTex = FindProperty(Utils.PropNameMainTex, properties);
_color = FindProperty(Utils.PropNameColor, properties);
_cutoff = FindProperty(Utils.PropNameCutoff, properties);
_blendMode = FindProperty(Utils.PropNameBlendMode, properties);
_cullMode = FindProperty(Utils.PropNameCullMode, properties);
_vColBlendMode = FindProperty(Utils.PropeNameVColBlendMode, properties);
// _srcBlend = FindProperty(PropNameSrcBlend, properties);
// _dstBlend = FindProperty(PropNameDstBlend, properties);
// _zWrite = FindProperty(PropNameZWrite, properties);
var materials = materialEditor.targets.Select(x => x as Material).ToArray();
EditorGUI.BeginChangeCheck();
{
DrawRenderingBox(materialEditor, materials);
DrawColorBox(materialEditor, materials);
DrawOptionsBox(materialEditor, materials);
}
EditorGUI.EndChangeCheck();
}
public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
{
var blendMode = UniUnlitRenderMode.Opaque;
if (material.HasProperty(Utils.PropNameStandardShadersRenderMode)) // from Standard shader
{
blendMode = (UniUnlitRenderMode) Math.Min(2f, material.GetFloat(Utils.PropNameStandardShadersRenderMode));
}
// assigns UniUnlit's properties...
base.AssignNewShaderToMaterial(material, oldShader, newShader);
// take over old value
material.SetFloat(Utils.PropNameBlendMode, (float) blendMode);
Utils.ValidateProperties(material, isRenderModeChangedByUser: true);
}
private void DrawRenderingBox(MaterialEditor materialEditor, Material[] materials)
{
EditorGUILayout.LabelField("Rendering", EditorStyles.boldLabel);
EditorGUILayout.BeginVertical(GUI.skin.box);
{
if (PopupEnum<UniUnlitRenderMode>("Rendering Type", _blendMode, materialEditor))
{
ModeChanged(materials, isRenderModeChangedByUser: true);
}
if (PopupEnum<UniUnlitCullMode>("Cull Mode", _cullMode, materialEditor))
{
ModeChanged(materials, isRenderModeChangedByUser: true);
}
EditorGUILayout.Space();
switch ((UniUnlitRenderMode) _blendMode.floatValue)
{
case UniUnlitRenderMode.Cutout:
materialEditor.ShaderProperty(_cutoff, "Cutoff");
break;
case UniUnlitRenderMode.Opaque:
case UniUnlitRenderMode.Transparent:
break;
}
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
}
private void DrawColorBox(MaterialEditor materialEditor, Material[] materials)
{
EditorGUILayout.LabelField("Color", EditorStyles.boldLabel);
EditorGUILayout.BeginVertical(GUI.skin.box);
{
materialEditor.TexturePropertySingleLine(new GUIContent("Main Tex", "(RGBA)"), _mainTex, _color);
materialEditor.TextureScaleOffsetProperty(_mainTex);
EditorGUILayout.Space();
if (PopupEnum<UniUnlitVertexColorBlendOp>("Vertex Color Blend Mode", _vColBlendMode, materialEditor))
{
ModeChanged(materials, isRenderModeChangedByUser: true);
}
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
}
private void DrawOptionsBox(MaterialEditor materialEditor, Material[] materials)
{
EditorGUILayout.LabelField("Options", EditorStyles.boldLabel);
EditorGUILayout.BeginVertical(GUI.skin.box);
{
#if UNITY_5_6_OR_NEWER
// materialEditor.EnableInstancingField();
materialEditor.DoubleSidedGIField();
#endif
materialEditor.RenderQueueField();
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
}
private static bool PopupEnum<T>(string name, MaterialProperty property, MaterialEditor editor) where T : struct
{
if (!typeof(T).IsEnum) return false;
EditorGUI.showMixedValue = property.hasMixedValue;
EditorGUI.BeginChangeCheck();
var values = (T[]) Enum.GetValues(typeof(T));
var names = Enum.GetNames(typeof(T));
var currInt = (int) property.floatValue;
var currValue = (T) Enum.ToObject(typeof(T), currInt);
var currIndex = Array.IndexOf(values, currValue);
var nextIndex = EditorGUILayout.Popup(name, currIndex, names);
var changed = EditorGUI.EndChangeCheck();
if (changed)
{
editor.RegisterPropertyChangeUndo("EnumPopUp");
var nextValue = values[nextIndex];
var nextInt = (int) (object) nextValue;
property.floatValue = nextInt;
}
EditorGUI.showMixedValue = false;
return changed;
}
private static void ModeChanged(Material[] materials, bool isRenderModeChangedByUser = false)
{
foreach (var material in materials)
{
Utils.ValidateProperties(material, isRenderModeChangedByUser);
}
}
}
}