-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added replacement in animations and added a checkbox to replace all o…
…r only ones that actualy get used twice
- Loading branch information
1 parent
ea863a2
commit 62df5b0
Showing
16 changed files
with
351 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,100 @@ | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using cc.dingemans.bigibas123.MaterialDedup.Editor.Model; | ||
using cc.dingemans.bigibas123.MaterialDedup.Runtime; | ||
using JetBrains.Annotations; | ||
using UnityEditor; | ||
using UnityEditor.UIElements; | ||
using UnityEngine; | ||
using UnityEngine.UIElements; | ||
using VRC; | ||
using VRC.SDK3.Avatars.Components; | ||
|
||
namespace cc.dingemans.bigibas123.MaterialDedup.Editor | ||
{ | ||
[CustomEditor(typeof(MaterialDeduplicatorBehavior))] | ||
public class MaterialDeduplicatorBehaviorEditor : UnityEditor.Editor | ||
{ | ||
SerializedProperty _replaceEvenIfOnlyOneProp; | ||
|
||
[CanBeNull] private GroupBox _box; | ||
|
||
void OnEnable() | ||
{ | ||
_replaceEvenIfOnlyOneProp = | ||
serializedObject.FindProperty(nameof(MaterialDeduplicatorBehavior.replaceEvenIfOnlyOne)); | ||
} | ||
|
||
private void ReDrawMaterials(MaterialDeduplicatorBehavior behavior) | ||
{ | ||
_box.contentContainer.Clear(); | ||
var list = behavior | ||
.AsMaterialRefs(behavior.gameObject.GetComponentInParent<VRCAvatarDescriptor>()) | ||
.AsDedupList(); | ||
var matBlock = GetMaterialFoldouts(list, behavior.replaceEvenIfOnlyOne); | ||
foreach (var foldout in matBlock) | ||
{ | ||
_box.contentContainer.Add(foldout); | ||
} | ||
} | ||
|
||
public override VisualElement CreateInspectorGUI() | ||
{ | ||
VisualElement inspector = new VisualElement(); | ||
var list = ((MaterialDeduplicatorBehavior)target).AsMaterialRefs().AsDedupList().ToList(); | ||
|
||
list.ForEach(dedup => | ||
var behavior = (MaterialDeduplicatorBehavior)target; | ||
|
||
var checkBox = new PropertyField(_replaceEvenIfOnlyOneProp, "Always replace materials"); | ||
checkBox.RegisterValueChangeCallback(evt => { ReDrawMaterials(behavior); }); | ||
|
||
inspector.Add(checkBox); | ||
|
||
var list = behavior | ||
.AsMaterialRefs(behavior.gameObject.GetComponentInParent<VRCAvatarDescriptor>()) | ||
.AsDedupList(); | ||
|
||
var matBlock = GetMaterialFoldouts(list, behavior.replaceEvenIfOnlyOne); | ||
_box ??= new GroupBox(); | ||
foreach (var foldout in matBlock) | ||
{ | ||
var rendererFold = new Foldout | ||
{ | ||
text = dedup.Name, | ||
}; | ||
var gb = new GroupBox() { focusable = false }; | ||
gb.SetEnabled(false); | ||
var of = new ObjectField() { focusable = false, objectType = typeof(Material) }; | ||
of.value = dedup.Material; | ||
gb.contentContainer.Add(of); | ||
foreach (var dest in dedup.Destinations) | ||
{ | ||
var rendObjField = new ObjectField() { focusable = false, objectType = typeof(Renderer) }; | ||
rendObjField.label = dest.Slot.ToString(); | ||
rendObjField.value = dest.Renderer; | ||
gb.contentContainer.Add(rendObjField); | ||
} | ||
rendererFold.contentContainer.Add(gb); | ||
inspector.Add(rendererFold); | ||
}); | ||
|
||
|
||
_box.contentContainer.Add(foldout); | ||
} | ||
inspector.Add(_box); | ||
|
||
return inspector; | ||
} | ||
|
||
public IEnumerable<Foldout> GetMaterialFoldouts(IEnumerable<DeduplicatedMaterial> list, | ||
bool replaceEvenIfOnlyOne) | ||
{ | ||
return list | ||
.Where(dedup => replaceEvenIfOnlyOne || dedup.DestinationCount > 1) | ||
.ToList() | ||
.Select(dedup => | ||
{ | ||
var rendererFold = new Foldout | ||
{ | ||
text = dedup.Name, | ||
}; | ||
var gb = new GroupBox() { focusable = false }; | ||
gb.SetEnabled(false); | ||
var of = new ObjectField() { focusable = false, objectType = typeof(Material) }; | ||
of.value = dedup.Material; | ||
gb.contentContainer.Add(of); | ||
foreach (var dest in dedup.Destinations) | ||
{ | ||
var rendObjField = new ObjectField | ||
{ | ||
focusable = false, | ||
objectType = dest.Target.GetType(), | ||
label = dest.Name, | ||
value = dest.Target | ||
}; | ||
gb.contentContainer.Add(rendObjField); | ||
} | ||
|
||
rendererFold.contentContainer.Add(gb); | ||
return rendererFold; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace cc.dingemans.bigibas123.MaterialDedup.Editor.Model | ||
{ | ||
public class AnimationMaterialTarget : MaterialTarget | ||
{ | ||
private readonly AnimationClip _clip; | ||
private readonly EditorCurveBinding _binding; | ||
private readonly int _keyFrameId; | ||
|
||
public AnimationMaterialTarget(AnimationClip clip, EditorCurveBinding binding, int keyFrame) | ||
{ | ||
_clip = clip; | ||
_binding = binding; | ||
_keyFrameId = keyFrame; | ||
} | ||
|
||
private ObjectReferenceKeyframe Keyframe => | ||
AnimationUtility.GetObjectReferenceCurve(_clip, _binding)[_keyFrameId]; | ||
|
||
public override Material Material => Keyframe.value as Material; | ||
public override string Name => $"{_clip.name}:{_binding.propertyName}[{_keyFrameId}]"; | ||
|
||
public override TargetType Type => TargetType.AnimationClip; | ||
public override Object Target => _clip; | ||
|
||
public override void SetNewMat(Material mat) | ||
{ | ||
var curve = AnimationUtility.GetObjectReferenceCurve(_clip, _binding); | ||
curve[_keyFrameId].value = mat; | ||
AnimationUtility.SetObjectReferenceCurve(_clip, _binding, curve); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using UnityEngine; | ||
|
||
namespace cc.dingemans.bigibas123.MaterialDedup.Editor.Model | ||
{ | ||
public abstract class MaterialTarget : MaterialContainer | ||
{ | ||
public abstract TargetType Type { get; } | ||
public abstract Object Target { get; } | ||
public abstract void SetNewMat(Material mat); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.