Skip to content

Commit

Permalink
Merge pull request #2506 from ousttrue/fix/duplicate_rootbone
Browse files Browse the repository at this point in the history
[0.x][springbone] rootBone 重複対策
  • Loading branch information
ousttrue authored Nov 19, 2024
2 parents 264a6bc + bf9c438 commit fbe9342
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
37 changes: 32 additions & 5 deletions Assets/VRM/Editor/SpringBone/VRMSpringBoneEditor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using UniGLTF;
using UnityEditor;
using UnityEngine;

Expand All @@ -9,6 +12,7 @@ class VRMSpringBoneEditor : Editor
{
private VRMSpringBone m_target;

private SerializedProperty m_script;
private SerializedProperty m_commentProp;
private SerializedProperty m_gizmoColorProp;
private SerializedProperty m_stiffnessForceProp;
Expand All @@ -29,6 +33,7 @@ void OnEnable()
}
m_target = (VRMSpringBone)target;

m_script = serializedObject.FindProperty("m_Script");
m_commentProp = serializedObject.FindProperty(nameof(VRMSpringBone.m_comment));
m_gizmoColorProp = serializedObject.FindProperty("m_gizmoColor");
m_stiffnessForceProp = serializedObject.FindProperty(nameof(VRMSpringBone.m_stiffnessForce));
Expand All @@ -42,32 +47,54 @@ void OnEnable()
m_updateTypeProp = serializedObject.FindProperty(nameof(VRMSpringBone.m_updateType));
}

public void OnSceneGUI()
{
foreach (var valiation in m_target.Validations)
{
var t = (Transform)valiation.Context.Context;
if (t != null)
{
Handles.Label(t.position, "duplicate rootBone !");
}
}
}

public override void OnInspectorGUI()
{
serializedObject.Update();

// header
using (new EditorGUI.DisabledScope(true))
{
EditorGUILayout.PropertyField(m_script);
}
EditorGUILayout.PropertyField(m_commentProp);
EditorGUILayout.PropertyField(m_gizmoColorProp);

EditorGUILayout.Space();
foreach (var validation in m_target.Validations)
{
validation.DrawGUI();
}

// settings
EditorGUILayout.LabelField("Settings", EditorStyles.boldLabel);

LimitBreakSlider(m_stiffnessForceProp, 0.0f, 4.0f, 0.0f, Mathf.Infinity);
LimitBreakSlider(m_gravityPowerProp, 0.0f, 2.0f, 0.0f, Mathf.Infinity);
EditorGUILayout.PropertyField(m_gravityDirProp);
EditorGUILayout.PropertyField(m_dragForceProp);
EditorGUILayout.PropertyField(m_centerProp);
EditorGUILayout.PropertyField(m_rootBonesProp);

EditorGUILayout.Space();

// collision
EditorGUILayout.LabelField("Collision", EditorStyles.boldLabel);

LimitBreakSlider(m_hitRadiusProp, 0.0f, 0.5f, 0.0f, Mathf.Infinity);
EditorGUILayout.PropertyField(m_colliderGroupsProp);
EditorGUILayout.PropertyField(m_updateTypeProp);
EditorGUILayout.Space();

// runtime
EditorGUILayout.LabelField("Runtime", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(m_updateTypeProp);

serializedObject.ApplyModifiedProperties();
}
Expand Down
41 changes: 40 additions & 1 deletion Assets/VRM/Runtime/SpringBone/VRMSpringBone.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using UniGLTF;
using UnityEngine;

namespace VRM
Expand Down Expand Up @@ -47,15 +49,52 @@ public enum SpringBoneUpdateType
}
[SerializeField] public SpringBoneUpdateType m_updateType = SpringBoneUpdateType.LateUpdate;

List<Transform> m_rootBonesNonNullUnique = new();
List<Transform> RootBonesNonNullUnique
{
get
{
m_rootBonesNonNullUnique.Clear();
m_rootBonesNonNullUnique.AddRange(RootBones.Where(x => x != null).Distinct());
return m_rootBonesNonNullUnique;
}
}
SpringBone.SpringBoneSystem m_system = new();

Dictionary<Transform, int> m_rootCount = new();
List<Validation> m_validations = new();
public List<Validation> Validations => m_validations;
public void OnValidate()
{
Validations.Clear();
m_rootCount.Clear();
foreach (var root in RootBones)
{
if (m_rootCount.TryGetValue(root, out var count))
{
m_rootCount[root] = count + 1;
}
else
{
m_rootCount.Add(root, 1);
}
}
foreach (var (k, v) in m_rootCount)
{
if (v > 1)
{
Validations.Add(Validation.Error($"Duplicate rootBone: {k} => {v}", ValidationContext.Create(k)));
}
}
}

void Awake()
{
Setup();
}

SpringBone.SceneInfo Scene => new(
rootBones: RootBones,
rootBones: RootBonesNonNullUnique,
center: m_center,
colliderGroups: ColliderGroups,
externalForce: ExternalForce);
Expand Down

0 comments on commit fbe9342

Please sign in to comment.