-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathLwguiShaderPropertyPreset.cs
More file actions
315 lines (279 loc) · 8.57 KB
/
Copy pathLwguiShaderPropertyPreset.cs
File metadata and controls
315 lines (279 loc) · 8.57 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (c) Jason Ma
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEditor;
using Object = UnityEngine.Object;
namespace LWGUI
{
[CreateAssetMenu(fileName = "LWGUI_ShaderPropertyPreset.asset", menuName = "LWGUI/Shader Property Preset", order = 84)]
public class LwguiShaderPropertyPreset : ScriptableObject
{
public enum PropertyType
{
Color,
Vector,
Float,
Range,
Texture,
Integer,
}
[SerializeField]
public int order;
[Serializable]
public class PropertyValue
{
public PropertyValue(MaterialProperty prop)
{
CopyFromMaterialProperty(prop);
}
public string propertyName;
public PropertyType propertyType;
public float floatValue;
public int intValue;
public Color colorValue;
public Vector4 vectorValue;
public Texture textureValue;
private int propertyNameID = -1;
public void Apply(Material material, bool isDefaultMaterial, PerMaterialData perMaterialData = null)
{
if (propertyNameID == -1 || !material.HasProperty(propertyNameID))
propertyNameID = Shader.PropertyToID(propertyName);
if (!material.HasProperty(propertyNameID))
{
// Legacy
var propertyNameLower = propertyName.ToLower();
switch (propertyNameLower)
{
case "renderqueue":
material.renderQueue = (int)floatValue;
return;
default:
// Debug.LogWarning("Unable to find Preset Property: " + propertyName + " in Material: " + material + "!");
return;
}
}
if (isDefaultMaterial)
{
switch (propertyType)
{
case PropertyType.Color:
material.SetColor(propertyNameID, colorValue);
break;
case PropertyType.Vector:
material.SetVector(propertyNameID, vectorValue);
break;
case PropertyType.Float:
case PropertyType.Range:
material.SetFloat(propertyNameID, floatValue);
break;
case PropertyType.Integer:
material.SetInteger(propertyNameID, intValue);
break;
case PropertyType.Texture:
material.SetTexture(propertyNameID, textureValue);
break;
}
UnityEditorExtension.ApplyMaterialPropertyAndDecoratorDrawers(material);
}
// is Property Primary Material
else if (perMaterialData != null)
{
var propDynamicData = perMaterialData.propDynamicDatas[propertyName];
var prop = propDynamicData.property;
switch (propertyType)
{
case PropertyType.Color:
prop.colorValue = colorValue;
break;
case PropertyType.Vector:
prop.vectorValue = vectorValue;
break;
case PropertyType.Float:
case PropertyType.Range:
prop.floatValue = floatValue;
break;
case PropertyType.Integer:
prop.intValue = intValue;
break;
case PropertyType.Texture:
prop.textureValue = textureValue;
break;
}
propDynamicData.hasRevertChanged = true;
}
}
public void CopyFromMaterialProperty(MaterialProperty prop)
{
propertyName = prop.name;
switch (prop.GetPropertyType())
{
case ShaderPropertyType.Color:
propertyType = PropertyType.Color;
colorValue = prop.colorValue;
break;
case ShaderPropertyType.Vector:
propertyType = PropertyType.Vector;
vectorValue = prop.vectorValue;
break;
case ShaderPropertyType.Float:
propertyType = PropertyType.Float;
floatValue = prop.floatValue;
break;
case ShaderPropertyType.Int:
propertyType = PropertyType.Integer;
intValue = prop.intValue;
break;
case ShaderPropertyType.Range:
propertyType = PropertyType.Range;
floatValue = prop.floatValue;
break;
case ShaderPropertyType.Texture:
propertyType = PropertyType.Texture;
textureValue = prop.textureValue;
break;
}
}
public void OnValidate()
{
propertyNameID = -1;
}
}
[Serializable]
public class Preset
{
public string presetName;
public List<PropertyValue> propertyValues = new List<PropertyValue>();
public List<string> enabledKeywords = new List<string>();
public List<string> disabledKeywords = new List<string>();
public List<string> enabledPasses = new List<string>();
public List<string> disabledPasses = new List<string>();
public int renderQueue = -1;
public int order = -1;
public void ApplyToDefaultMaterial(Material material)
{
foreach (var propertyValue in propertyValues)
propertyValue.Apply(material, true);
foreach (var enabledKeyword in enabledKeywords)
material.EnableKeyword(enabledKeyword);
foreach (var disabledKeyword in disabledKeywords)
material.DisableKeyword(disabledKeyword);
Helper.SetShaderPassEnabled(new Object[] { material }, enabledPasses.Select(s => s.ToUpper()).ToArray(), true);
Helper.SetShaderPassEnabled(new Object[] { material }, disabledPasses.Select(s => s.ToUpper()).ToArray(), false);
if (renderQueue >= 0)
material.renderQueue = renderQueue;
}
public void ApplyToEditingMaterial(MaterialEditor editor, PerMaterialData perMaterialData)
{
for (int i = 0; i < editor.targets.Length; i++)
{
var material = editor.targets[i] as Material;
foreach (var propertyValue in propertyValues)
propertyValue.Apply(material, false, i == 0 ? perMaterialData : null);
foreach (var enabledKeyword in enabledKeywords)
{
material.EnableKeyword(enabledKeyword);
}
foreach (var disabledKeyword in disabledKeywords)
{
material.DisableKeyword(disabledKeyword);
}
if (renderQueue >= 0)
material.renderQueue = renderQueue;
}
Helper.SetShaderPassEnabled(editor.targets, enabledPasses.Select(s => s.ToUpper()).ToArray(), true);
Helper.SetShaderPassEnabled(editor.targets, disabledPasses.Select(s => s.ToUpper()).ToArray(), false);
}
public void ApplyKeywordsAndPassesToMaterials(Object[] materials)
{
for (int i = 0; i < materials.Length; i++)
{
var material = materials[i] as Material;
foreach (var enabledKeyword in enabledKeywords)
material.EnableKeyword(enabledKeyword);
foreach (var disabledKeyword in disabledKeywords)
material.DisableKeyword(disabledKeyword);
}
Helper.SetShaderPassEnabled(materials, enabledPasses.Select(s => s.ToUpper()).ToArray(), true);
Helper.SetShaderPassEnabled(materials, disabledPasses.Select(s => s.ToUpper()).ToArray(), false);
}
public PropertyValue GetPropertyValue(string propName)
{
PropertyValue result = null;
if (propertyValues != null)
{
foreach (var propertyValue in propertyValues)
{
if (propertyValue.propertyName == propName)
{
result = propertyValue;
break;
}
}
}
return result;
}
public void AddOrUpdate(MaterialProperty prop)
{
var propertyValue = GetPropertyValue(prop.name);
if (propertyValue != null)
propertyValue.CopyFromMaterialProperty(prop);
else
propertyValues.Add(new PropertyValue(prop));
}
public void AddOrUpdateIncludeExtraProperties(LWGUIMetaDatas metaDatas, MaterialProperty prop)
{
AddOrUpdate(prop);
foreach (var extraPropName in metaDatas.GetPropStaticData(prop).extraPropNames)
{
AddOrUpdate(metaDatas.GetProperty(extraPropName));
}
}
public void Remove(string propName)
{
var propertyValue = GetPropertyValue(propName);
if (propertyValue != null)
propertyValues.Remove(propertyValue);
}
public void RemoveIncludeExtraProperties(LWGUIMetaDatas metaDatas, string propName)
{
Remove(propName);
foreach (var extraPropName in metaDatas.GetPropStaticData(propName).extraPropNames)
{
Remove(metaDatas.GetProperty(extraPropName).name);
}
}
}
[SerializeField]
private List<Preset> presets;
public List<Preset> GetPresets() => presets;
public int GetPresetCount() => presets?.Count ?? 0;
public Preset GetPreset(int index)
{
if (presets == null)
return null;
if (index < presets.Count)
{
return presets[index];
}
else
{
Debug.LogError($"LWGUI: Index ({ index }) is out of range when accessing PresetFile: { name }");
return null;
}
}
public Preset GetPreset(float index) => GetPreset((int)index);
private void OnValidate()
{
// Update All Material Default Values
MetaDataHelper.ReleaseAllShadersMetadataCache();
}
private void OnEnable()
{
if (PresetHelper.IsInitComplete)
PresetHelper.AddPreset(this);
}
}
}