Skip to content

Commit

Permalink
シンプルな相対解決
Browse files Browse the repository at this point in the history
  • Loading branch information
pandrabox committed Sep 21, 2024
1 parent 2d2cd90 commit 691c2cf
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Editor/CreateClip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using VRC.SDK3.Dynamics.PhysBone.Components;
using static com.github.pandrabox.emoteprefab.runtime.Generic;
using static com.github.pandrabox.emoteprefab.editor.EmoteManager;
using static com.github.pandrabox.emoteprefab.editor.RelativeResolver;

namespace com.github.pandrabox.emoteprefab.editor
{
Expand All @@ -36,7 +37,7 @@ public CreateClip()
for (int n = 0; n < _emote.UnitMotions.Count; n++)
{
_clip = EmotePrefabs[m].UnitMotions[n].Clip;
_original = UnityEngine.Object.Instantiate(_clip.Original);
_original = RelativeResolve(_emote.transform, _clip.Original, ResolveMode.AutoByRelative);
CreateHumanoidClip();
CreateUnhumanoidClip();
CreateBodyShapeBlockerClip();
Expand Down
56 changes: 56 additions & 0 deletions Editor/RelativeResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using com.github.pandrabox.emoteprefab.editor;
using com.github.pandrabox.emoteprefab.runtime;
using nadena.dev.modular_avatar.core;
using nadena.dev.modular_avatar.core.editor;
using nadena.dev.ndmf;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
using VRC.SDK3.Avatars.Components;
using VRC.SDK3.Dynamics.PhysBone.Components;
using static com.github.pandrabox.emoteprefab.runtime.Generic;
using static com.github.pandrabox.emoteprefab.editor.EmoteManager;

namespace com.github.pandrabox.emoteprefab.editor
{
public static class RelativeResolver
{
public enum ResolveMode
{
Absolute,
AutoByRelative,
AutoByAbsolute
}
public static AnimationClip RelativeResolve(Transform Root, AnimationClip Original, ResolveMode mode)
{
var original = UnityEngine.Object.Instantiate(Original);
var clip = UnityEngine.Object.Instantiate(Original);
if (mode == ResolveMode.Absolute) return original;
var RootPath = FindPathRecursive(Descriptor.transform, Root);

// CleanUp
foreach (var binding in AnimationUtility.GetCurveBindings(clip))
{
AnimationUtility.SetEditorCurve(clip, binding, null);
}

// Create ResolvedAnim
foreach (var binding in AnimationUtility.GetCurveBindings(original))
{
AnimationCurve curve = AnimationUtility.GetEditorCurve(original, binding);
var path = binding.path;
if (Root.Find(path) && path.Length > 0)
{
path = $@"{RootPath}/{path}";
}
clip.SetCurve(path, binding.type, binding.propertyName, curve);
}

return clip;
}
}
}
11 changes: 11 additions & 0 deletions Editor/RelativeResolver.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 11 additions & 14 deletions Runtime/Generic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,21 @@ public static T FindComponentFromParent<T>(GameObject currentObject)

return null;
}
public static string FindPathRecursive(Transform parent, Transform target)
public static string FindPathRecursive(Transform root, Transform child)
{
if (parent == target)
{
return "";
}
if (root == child) return "";

// 子要素を再帰的に調べる
foreach (Transform child in parent)
List<string> pathSegments = new List<string>();
while (child != root && child != null)
{
string childPath = FindPathRecursive(child, target);
if (childPath != null)
{
// 親の名前を返さず、子のパスのみを返す
return child.name + (string.IsNullOrEmpty(childPath) ? "" : "/" + childPath);
}
pathSegments.Add(child.name);
child = child.parent;
}
return null;

if (child == null && root != null) return null;

pathSegments.Reverse();
return String.Join("/", pathSegments);
}
#endif
}
Expand Down

0 comments on commit 691c2cf

Please sign in to comment.