Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* implement SpringBone validation #474 #510

Merged
merged 2 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Assets/VRM/UniVRM/Editor/Format/VRMExporterWizard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ void OnWizardUpdate()

m_validations.Clear();
m_validations.AddRange(Validate());
m_validations.AddRange(VRMSpringBoneValidator.Validate(ExportRoot));
var hasError = m_validations.Any(x => !x.CanExport);
m_IsValid = !hasError && !MetaHasError;

Expand Down
70 changes: 70 additions & 0 deletions Assets/VRM/UniVRM/Editor/SpringBone/VRMSpringBoneValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace VRM
{
static class VRMSpringBoneValidator
{
// https://github.com/vrm-c/UniVRM/issues/474
public static IEnumerable<Validation> Validate(GameObject root)
{
if (root == null)
{
yield break;
}

Dictionary<Transform, List<VRMSpringBone>> rootMap = new Dictionary<Transform, List<VRMSpringBone>>();

foreach (var sb in root.GetComponentsInChildren<VRMSpringBone>())
{
foreach (var springRoot in sb.RootBones)
{
if (!rootMap.TryGetValue(springRoot, out List<VRMSpringBone> list))
{
list = new List<VRMSpringBone>();
rootMap.Add(springRoot, list);
}
list.Add(sb);
}
}

foreach (var kv in rootMap)
{
if (kv.Value.Count > 1)
{
// * GameObjectが複数回ルートとして指定されてる(SpringBoneが同じでも別でも)
var list = string.Join(", ", kv.Value.Select(x => string.IsNullOrEmpty(x.m_comment) ? x.name : x.m_comment));
yield return Validation.Warning($"{kv.Key} found multiple. {list}");
}

var rootInRootMap = new Dictionary<Transform, List<Transform>>();
foreach (var child in kv.Key.GetComponentsInChildren<Transform>())
{
// * Rootから子をだどって、別のRootが現れる
if (child == kv.Key)
{
continue;
}

if (!rootMap.ContainsKey(child))
{
continue;
}

if (!rootInRootMap.TryGetValue(kv.Key, out List<Transform> rootInRoot))
{
rootInRoot = new List<Transform>();
rootInRootMap.Add(kv.Key, rootInRoot);
}
rootInRoot.Add(child);
}
foreach (var rootList in rootInRootMap)
{
var list = string.Join(", ", rootList.Value.Select(x => x.name));
yield return Validation.Warning($"{rootList.Key} hierarchy contains other root: {list}");
}
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/VRM/UniVRM/Editor/SpringBone/VRMSpringBoneValidator.cs.meta

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