Skip to content

Commit 67cb3b3

Browse files
Feature/shader override update (#306)
- shader override functionality rework to support other property types
1 parent f811caf commit 67cb3b3

File tree

8 files changed

+1734
-973
lines changed

8 files changed

+1734
-973
lines changed

Editor/Core/Scripts/UI/CustomEditors/AvatarConfigEditor.cs

Lines changed: 91 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ public class AvatarConfigEditor : UnityEditor.Editor
3838
private VisualElement root;
3939
private Action textureChannelChanged;
4040

41+
private SerializedProperty shaderPropertyMappingList;
42+
4143
public override VisualElement CreateInspectorGUI()
4244
{
4345
root = new VisualElement();
4446
visualTreeAsset.CloneTree(root);
45-
4647
avatarConfigTarget = (AvatarConfig) target;
47-
48+
shaderPropertyMappingList = serializedObject.FindProperty("ShaderPropertyMapping");
4849
SetupLod();
4950
SetupPose();
5051
SetupTextureAtlas();
@@ -255,65 +256,106 @@ private void SetupShader()
255256
var shader = root.Q<ObjectField>("ShaderOverride");
256257
shader.SetValueWithoutNotify(avatarConfigTarget.Shader);
257258

259+
shader.RegisterValueChangedCallback(x =>
260+
{
261+
avatarConfigTarget.Shader = (Shader) x.newValue;
262+
Save();
263+
SetupShader();
264+
}
265+
);
258266
var shaderPropertiesContainer = root.Q<VisualElement>("ShaderProperties");
259-
CreateShaderProperties(shaderPropertiesContainer);
260-
261-
textureChannelChanged += () => ShowShaderProperties(shaderPropertiesContainer);
262-
if (shader.value == null)
267+
shaderPropertiesContainer.Clear();
268+
shaderPropertiesContainer.style.display = DisplayStyle.Flex;
269+
shaderPropertiesContainer.style.flexDirection = FlexDirection.Column;
270+
if (avatarConfigTarget.Shader == null)
263271
{
264272
shaderPropertiesContainer.style.display = DisplayStyle.None;
265273
}
266274
else
267275
{
268-
ShowShaderProperties(shaderPropertiesContainer);
269-
}
270-
271-
shader.RegisterValueChangedCallback(x =>
276+
shaderPropertiesContainer.style.display = DisplayStyle.Flex;
277+
shaderPropertiesContainer.style.marginTop = 10;
278+
shaderPropertiesContainer.style.left = 10;
279+
shaderPropertiesContainer.style.right = 10;
280+
var titleRowContainer = new VisualElement();
281+
titleRowContainer.style.flexDirection = FlexDirection.Row;
282+
titleRowContainer.style.marginBottom = 7;
283+
titleRowContainer.style.marginTop = 7;
284+
titleRowContainer.style.left = 10;
285+
titleRowContainer.style.right = 10;
286+
var sourceTitleField = new Label("Source Property")
272287
{
273-
avatarConfigTarget.Shader = (Shader) x.newValue;
274-
Save();
275-
if (x.newValue == null)
288+
style =
276289
{
277-
shaderPropertiesContainer.style.display = DisplayStyle.None;
290+
width = 200, unityFontStyleAndWeight = FontStyle.Bold, // Make the text bold
291+
unityTextAlign = TextAnchor.MiddleLeft
278292
}
279-
else
280-
{
281-
ShowShaderProperties(shaderPropertiesContainer);
282-
}
283-
}
284-
);
285-
}
286-
287-
private void ShowShaderProperties(VisualElement shaderPropertiesContainer)
288-
{
289-
shaderPropertiesContainer.style.display = DisplayStyle.Flex;
290-
foreach (var child in shaderPropertiesContainer.Children())
291-
{
292-
if (avatarConfigTarget.TextureChannel.Contains((TextureChannel) Enum.Parse(typeof(TextureChannel), child.name)))
293+
};
294+
titleRowContainer.Add(sourceTitleField);
295+
var targetTitleField = new Label("Target Property")
293296
{
294-
child.style.display = DisplayStyle.Flex;
295-
}
296-
else
297+
style = { width = 200, marginRight = 10, flexGrow = 0.8f, unityFontStyleAndWeight = FontStyle.Bold, }
298+
};
299+
titleRowContainer.Add(targetTitleField);
300+
var typeTitleField = new Label("Type")
297301
{
298-
child.style.display = DisplayStyle.None;
299-
}
300-
}
301-
}
302-
303-
private void CreateShaderProperties(VisualElement shaderPropertiesContainer)
304-
{
305-
foreach (TextureChannel textureChannel in Enum.GetValues(typeof(TextureChannel)))
306-
{
307-
var field = new TextField(textureChannel.ToString());
308-
field.name = textureChannel.ToString();
309-
var property = avatarConfigTarget.ShaderProperties.FindIndex(x => x.TextureChannel == textureChannel);
310-
field.SetValueWithoutNotify(avatarConfigTarget.ShaderProperties[property].PropertyName);
311-
field.RegisterValueChangedCallback(x =>
302+
style = { width = 70, alignSelf = Align.FlexEnd, unityFontStyleAndWeight = FontStyle.Bold, }
303+
};
304+
titleRowContainer.Add(typeTitleField);
305+
shaderPropertiesContainer.Add(titleRowContainer);
306+
for (int i = 0; i < shaderPropertyMappingList.arraySize; i++)
312307
{
313-
avatarConfigTarget.ShaderProperties[property].PropertyName = x.newValue;
314-
Save();
315-
});
316-
shaderPropertiesContainer.Add(field);
308+
SerializedProperty mapping = shaderPropertyMappingList.GetArrayElementAtIndex(i);
309+
310+
var propertyContainer = new VisualElement();
311+
propertyContainer.style.flexDirection = FlexDirection.Column;
312+
//propertyContainer.style.marginBottom = 10;
313+
314+
var horizontalContainer = new VisualElement();
315+
horizontalContainer.style.flexDirection = FlexDirection.Row;
316+
horizontalContainer.style.marginBottom = 7;
317+
horizontalContainer.style.marginTop = 7;
318+
horizontalContainer.style.left = 10;
319+
horizontalContainer.style.right = 10;
320+
// Alternating background colors
321+
propertyContainer.style.backgroundColor = i % 2 == 0 ? new StyleColor(new Color(0.25f, 0.25f, 0.25f)) : new StyleColor(new Color(0.3f, 0.3f, 0.3f));
322+
323+
var sourcePropertyField = new Label(mapping.FindPropertyRelative("SourceProperty").stringValue)
324+
{
325+
style =
326+
{
327+
width = 200, unityFontStyleAndWeight = FontStyle.Bold, // Make the text bold
328+
unityTextAlign = TextAnchor.MiddleLeft
329+
}
330+
};
331+
horizontalContainer.Add(sourcePropertyField);
332+
333+
var targetPropertyField = new TextField
334+
{
335+
value = mapping.FindPropertyRelative("TargetProperty").stringValue,
336+
//style = { flexGrow = 1, marginTop = 5 }
337+
style = { width = 200, marginRight = 10, flexGrow = 0.8f }
338+
};
339+
targetPropertyField.RegisterValueChangedCallback(evt =>
340+
{
341+
mapping.FindPropertyRelative("TargetProperty").stringValue = evt.newValue;
342+
Save();
343+
});
344+
horizontalContainer.Add(targetPropertyField);
345+
346+
var propertyTypeField = new EnumField((ShaderPropertyType) mapping.FindPropertyRelative("Type").enumValueIndex)
347+
{
348+
style = { width = 70, alignSelf = Align.FlexEnd }
349+
};
350+
propertyTypeField.RegisterValueChangedCallback(evt =>
351+
{
352+
mapping.FindPropertyRelative("Type").enumValueIndex = (int) (ShaderPropertyType) evt.newValue;
353+
Save();
354+
});
355+
horizontalContainer.Add(propertyTypeField);
356+
propertyContainer.Add(horizontalContainer);
357+
shaderPropertiesContainer.Add(propertyContainer);
358+
}
317359
}
318360
}
319361

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
The MIT License (MIT)
22
=====================
33

4-
Copyright © 2022 Ready Player Me
4+
Copyright © 2024 Ready Player Me
55

66
Permission is hereby granted, free of charge, to any person
77
obtaining a copy of this software and associated documentation

0 commit comments

Comments
 (0)