Skip to content

Commit

Permalink
Merge pull request #165 from Azukimochi/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Azukimochi authored May 21, 2024
2 parents 9c4b00e + 23ee64f commit 350f7e7
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 171 deletions.
162 changes: 15 additions & 147 deletions Editor/LightLimitChanger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,35 @@

namespace io.github.azukimochi
{
public sealed class LightLimitChanger : EditorWindow
public static class LightLimitChanger
{
public const string Title = "Light Limit Changer For MA";
private const string ContextMenuPath = "GameObject/ModularAvatar/Light Limit Changer";
private const string ContextMenuPath = "GameObject/Light Limit Changer/Setup";
private const int ContextMenuPriority = 130;

public VRCAvatarDescriptor TargetAvatar;
private VRCAvatarDescriptor _prevTargetAvatar;
private GameObject _temp;
private LightLimitChangerSettingsEditor _editor;

[MenuItem("Tools/Modular Avatar/LightLimitChanger")]
public static void CreateWindow() => GetWindow<LightLimitChanger>(Title);

[MenuItem(ContextMenuPath, true, ContextMenuPriority)]
public static bool ValidateApplytoAvatar() => Selection.gameObjects.Any(ValidateCore);
public static bool ValidateApplytoAvatar(MenuCommand command) => Selection.gameObjects.Any(x => x.TryGetComponent<VRCAvatarDescriptor>(out _));

[MenuItem(ContextMenuPath, false, ContextMenuPriority)]
public static void ApplytoAvatar()
public static void ApplytoAvatar(MenuCommand command)
{
List<GameObject> objectToCreated = new List<GameObject>();
foreach (var x in Selection.gameObjects)
var target = command.context as GameObject;
if (target == null || !target.TryGetComponent<VRCAvatarDescriptor>(out _))
{
if (!ValidateCore(x))
continue;

var prefab = GeneratePrefab(x.transform);

objectToCreated.Add(prefab);
return;
}
if (objectToCreated.Count == 0)

if (target.TryGetComponentInChildren<LightLimitChangerSettings>(out _))
{
EditorUtility.DisplayDialog("Light Limit Changer for MA Setup", string.Format(Localization.S("Window.info.error.already_setup"), target.name), "OK");
return;
}

EditorGUIUtility.PingObject(objectToCreated[0]);
Selection.objects = objectToCreated.ToArray();
var prefab = GeneratePrefab(target.transform);
EditorGUIUtility.PingObject(prefab);
Selection.objects = Selection.gameObjects.Where(x => x.TryGetComponent<LightLimitChangerSettings>(out _)).Append(prefab).ToArray();
}

// 選択されているものがアバター本体かつ、LLCが含まれていないときに実行可能
private static bool ValidateCore(GameObject obj) => obj != null && obj.GetComponent<VRCAvatarDescriptor>() != null && obj.GetComponentInChildren<LightLimitChangerSettings>() == null;

[MenuItem("CONTEXT/LightLimitChangerSettings/Manual Bake (For Advanced User)")]
public static void ManualBake(MenuCommand command)
{
Expand All @@ -65,127 +54,6 @@ public static void ManualBake(MenuCommand command)
AssetDatabase.SaveAssets();
}

private void OnDestroy()
{
if (_temp != null)
{
DestroyImmediate(_temp);
}
if (_editor != null)
{
DestroyImmediate(_editor);
}
}

private void OnGUI()
{
EditorGUIUtility.labelWidth = 280;
Utils.ShowVersionInfo();
EditorGUILayout.Separator();

var style = new GUIStyle(EditorStyles.helpBox);
style.richText = true;
EditorGUILayout.LabelField($"<size=12>{Localization.S("window.info.deprecated")}\n<a href=\"https://azukimochi.github.io/LLC-Docs/docs/howtouse/howtouse-basic/\">https://azukimochi.github.io/LLC-Docs/docs/howtouse/howtouse-basic</a></size>", style);
EditorGUILayout.Space(8);

TargetAvatar = EditorGUILayout.ObjectField(Localization.G("label.avatar"), TargetAvatar, typeof(VRCAvatarDescriptor), true) as VRCAvatarDescriptor;
EditorGUILayout.Separator();

UpdateInnerEditor();

if (_editor != null)
{
try
{
// InspectorのGUI処理を使いまわす
_editor.IsWindowMode = true;
_editor.OnInspectorGUI();
EditorGUILayout.Separator();

// 操作対象が一時オブジェクトなら生成ボタンを表示する
if ((_editor.target as Component).gameObject.hideFlags.HasFlag(HideFlags.HideInHierarchy))
{
if (GUILayout.Button(Localization.G("info.generate")))
{
var prefab = GeneratePrefab(TargetAvatar.transform);
EditorUtility.CopySerialized(_editor.target, prefab.GetComponent<LightLimitChangerSettings>());
}
}
}
catch { }
}

EditorGUILayout.Separator();
Localization.ShowLocalizationUI();
}

private void UpdateInnerEditor()
{
if (_editor == null)
{
if (TargetAvatar != null)
{
var settings = TargetAvatar.GetComponentInChildren<LightLimitChangerSettings>();
if (settings != null)
{
_editor = Editor.CreateEditor(settings, typeof(LightLimitChangerSettingsEditor)) as LightLimitChangerSettingsEditor;
}
else
{
_editor = Editor.CreateEditor(GetTempSettings(), typeof(LightLimitChangerSettingsEditor)) as LightLimitChangerSettingsEditor;
}
}
else
{
// nanimo sinai...
}
}
else
{
if (TargetAvatar != _prevTargetAvatar)
{
_editor.Destroy();
_editor = null;
}
else if (TargetAvatar != null)
{
if (_editor.target == null)
{
_editor.Destroy();
_editor = null;
_editor = Editor.CreateEditor(GetTempSettings(), typeof(LightLimitChangerSettingsEditor)) as LightLimitChangerSettingsEditor;
}
else
{
var settings = TargetAvatar.GetComponentInChildren<LightLimitChangerSettings>();
if (settings != null && (_editor.target as Component).gameObject.hideFlags.HasFlag(HideFlags.HideInHierarchy))
{
_editor.Destroy();
_editor = null;
_editor = Editor.CreateEditor(settings, typeof(LightLimitChangerSettingsEditor)) as LightLimitChangerSettingsEditor;
_temp?.Destroy();
_temp = null;
}
}
}
}
_prevTargetAvatar = TargetAvatar;
if (_temp != null)
{
// ApplyとかRevertすると何故かHideFlagsが剥がれるので
_temp.hideFlags = HideFlags.HideAndDontSave & ~HideFlags.NotEditable;
}

LightLimitChangerSettings GetTempSettings()
{
_temp?.Destroy();
_temp = GeneratePrefab();
_temp.name = "_INTERNAL_LLC_PREFAB_";

return _temp.GetComponent<LightLimitChangerSettings>();
}
}

private static GameObject GeneratePrefab(Transform parent = null)
{
var prefabObj = LightLimitChangerPrefab.Object;
Expand Down
9 changes: 9 additions & 0 deletions Editor/Localization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ internal static class Localization
"載入全域設定"
}
},
{
"Window.info.error.already_setup", new []
{
//[0] = Avatar Name
"Light Limit Changer has already been installed in the avatar \"{0}\"",
"アバター「{0}」にはすでにLight Limit Changerが導入されています",
"Light Limit Changer has already been installed in the avatar \"{0}\""
}
},
{
"Window.info.cancel", new []
{
Expand Down
1 change: 1 addition & 0 deletions Editor/NDMF/Passes.CloningMaterialsPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ internal bool TryClone(Material material, out Material clonedMaterial)
if (ShaderInfo.TryGetShaderInfo(material, out var info) && Session.Parameters.TargetShaders.Contains(info.Name))
{
clonedMaterial = material.Clone();
ObjectRegistry.RegisterReplacedObject(material, clonedMaterial);
Cache.Register(material, clonedMaterial);
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Editor/NDMF/Passes.CollectTargetRenderersPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected override void Execute(BuildContext context, Session session, LightLimi
{
IError error = new ErrorMessage("NDMF.info.non_generated", ErrorSeverity.NonFatal);
ErrorReport.ReportError(error);
session.IsNoTargetRenderer = true;
session.Cancel = true;
}
}

Expand Down
4 changes: 3 additions & 1 deletion Editor/NDMF/Passes.FinalizePass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ internal sealed class FinalizePass : LightLimitChangerBasePass<FinalizePass>
private Session _session;
private LightLimitChangerObjectCache _cache;

protected override bool IsForceRun => true;

protected override void Execute(BuildContext context, Session session, LightLimitChangerObjectCache cache)
{
Run(context.AvatarRootObject, session, cache);
Expand All @@ -33,7 +35,7 @@ internal static void Run(GameObject avatarObject, Session session, LightLimitCha

var maParameters = obj.GetOrAddComponent<ModularAvatarParameters>();

if (!session.IsValid() || session.IsNoTargetRenderer)
if (session.Cancel)
{
addMAParamaters(session, maParameters);

Expand Down
6 changes: 1 addition & 5 deletions Editor/NDMF/Passes.GenerateAnimationsPass.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using gomoru.su;
using nadena.dev.modular_avatar.core;
using nadena.dev.ndmf;
Expand Down Expand Up @@ -58,8 +58,6 @@ renderer.sharedMaterials[0] is Material mat &&
toggleTree.Parameters = new[] { ParameterName_Toggle };
var animationTree = toggleTree.AddDirectBlendTree(DirectBlendTree.Target.ON, "Animation");

session.AddParameter(new ParameterConfig() { nameOrPrefix = ParameterName_Toggle, defaultValue = parameters.IsDefaultUse ? 1 : 0, syncType = ParameterSyncType.Bool });

foreach (ref readonly var container in animationContainers)
{
if (session.TargetControl.HasFlag(container.ControlType))
Expand All @@ -68,8 +66,6 @@ renderer.sharedMaterials[0] is Material mat &&
var puppet = animationTree.AddRadialPuppet(container.Name);
puppet.ParameterName = container.ParameterName;
puppet.Animation = container.Control;

session.AddParameter(new ParameterConfig() { nameOrPrefix = container.ParameterName, defaultValue = container.DefaultValue, syncType = ParameterSyncType.Float });
}
}
}
Expand Down
31 changes: 21 additions & 10 deletions Editor/NDMF/Passes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public static void RunningPasses(Sequence sequence)
.Run(Finalize);
}

public readonly static CollectTargetRenderersPass CollectTargetRenderers = new CollectTargetRenderersPass();
public readonly static CloningMaterialsPass CloningMaterials = new CloningMaterialsPass();
public readonly static NormalizeMaterialsPass NormalizeMaterials = new NormalizeMaterialsPass();
public readonly static GenerateAdditionalControlPass GenerateAdditionalControl = new GenerateAdditionalControlPass();
public readonly static GenerateAnimationsPass GenerateAnimations = new GenerateAnimationsPass();
public readonly static FinalizePass Finalize = new FinalizePass();
public readonly static CollectTargetRenderersPass CollectTargetRenderers = CollectTargetRenderersPass.Instance;
public readonly static CloningMaterialsPass CloningMaterials = CloningMaterialsPass.Instance;
public readonly static NormalizeMaterialsPass NormalizeMaterials = NormalizeMaterialsPass.Instance;
public readonly static GenerateAdditionalControlPass GenerateAdditionalControl = GenerateAdditionalControlPass.Instance;
public readonly static GenerateAnimationsPass GenerateAnimations = GenerateAnimationsPass.Instance;
public readonly static FinalizePass Finalize = FinalizePass.Instance;

internal const string ParameterName_Toggle = "LightLimitEnable";
internal const string ParameterName_Value = "LightLimitValue";
Expand Down Expand Up @@ -62,10 +62,12 @@ private static LightLimitChangerObjectCache GetObjectCache(BuildContext context)
private LightLimitChangerObjectCache _cache;
private Session _session;

protected virtual bool IsForceRun { get; } = false;

protected override void Execute(BuildContext context)
{
var session = GetSession(context);
if ((!session.IsValid() || session.IsNoTargetRenderer) && (typeof(TPass) != typeof(FinalizePass) && typeof(TPass) != typeof(GenerateAnimationsPass)))
if (session.Settings == null || (!IsForceRun && session.Cancel))
return;
_session = session;
var cache = _cache = GetObjectCache(context);
Expand All @@ -86,14 +88,13 @@ internal sealed class Session
public HashSet<Renderer> TargetRenderers;
public DirectBlendTree DirectBlendTree;
public List<ParameterConfig> AvatarParameters;
public bool IsNoTargetRenderer;

public bool Cancel { get; set; } = false;

public HashSet<Object> Excludes;

private bool _initialized;

public bool IsValid() => Settings != null;

public void InitializeSession(BuildContext context)
{
InitializeSession(context.AvatarRootObject.GetComponentInChildren<LightLimitChangerSettings>(), GetObjectCache(context));
Expand Down Expand Up @@ -155,6 +156,16 @@ public void InitializeSession(LightLimitChangerSettings settings, LightLimitChan

TargetRenderers = new HashSet<Renderer>();

AddParameter(new ParameterConfig() { nameOrPrefix = ParameterName_Toggle, defaultValue = parameters.IsDefaultUse ? 1 : 0, syncType = ParameterSyncType.Bool });

foreach (ref readonly var container in Controls.AsSpan())
{
if (TargetControl.HasFlag(container.ControlType))
{
AddParameter(new ParameterConfig() { nameOrPrefix = container.ParameterName, defaultValue = container.DefaultValue, syncType = ParameterSyncType.Float });
}
}

_initialized = true;
}

Expand Down
4 changes: 3 additions & 1 deletion Editor/ObjectMapping/AnimatorControllerMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using io.github.azukimochi;
using nadena.dev.ndmf;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
Expand Down Expand Up @@ -74,7 +75,7 @@ private Object CustomClone(Object o)
newClip.frameRate = clip.frameRate;
newClip.localBounds = clip.localBounds;
AnimationUtility.SetAnimationClipSettings(newClip, AnimationUtility.GetAnimationClipSettings(clip));

ObjectRegistry.RegisterReplacedObject(clip, newClip);
return newClip;
}
else if (o is RuntimeAnimatorController controller)
Expand Down Expand Up @@ -193,6 +194,7 @@ private T DefaultDeepClone<T>(T original, Func<Object, Object> visitor) where T
so.ApplyModifiedPropertiesWithoutUndo();
}

ObjectRegistry.RegisterReplacedObject(original, obj);
return (T)obj;
}

Expand Down
9 changes: 7 additions & 2 deletions Editor/ShaderInfo/ShaderInfo.LilToon.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using nadena.dev.ndmf;
using UnityEngine;

namespace io.github.azukimochi
Expand Down Expand Up @@ -118,7 +119,9 @@ private bool BakeMainTex(Material material, LightLimitChangerObjectCache cache,
// Run Bake
if (bakeFlag)
{
material.SetTexture(PropertyIDs.MainTex, cache.Register(textureBaker.Bake()));
var baked = cache.Register(textureBaker.Bake());
ObjectRegistry.RegisterReplacedObject(tex, baked);
material.SetTexture(PropertyIDs.MainTex, baked);
}

return bakeFlag;
Expand Down Expand Up @@ -148,7 +151,9 @@ private bool Bake2ndOr3rdTex(Material material, LightLimitChangerObjectCache cac

if (bakeFlag)
{
material.SetTexture(propertyIds.Texture, cache.Register(textureBaker.Bake()));
var baked = cache.Register(textureBaker.Bake());
ObjectRegistry.RegisterReplacedObject(tex, baked);
material.SetTexture(propertyIds.Texture, baked);
}

return bakeFlag;
Expand Down
Loading

0 comments on commit 350f7e7

Please sign in to comment.