diff --git a/Assets/VRM10/Editor/Components/SpringBone/VRM10SpringSelectorWindow.cs b/Assets/VRM10/Editor/Components/SpringBone/VRM10SpringSelectorWindow.cs new file mode 100644 index 000000000..747493bca --- /dev/null +++ b/Assets/VRM10/Editor/Components/SpringBone/VRM10SpringSelectorWindow.cs @@ -0,0 +1,108 @@ +using System; +using UnityEditor; +using UnityEngine; + +namespace UniVRM10 +{ + /// + /// ヒエラルキーから VRM10SpringBone と VRM10ColliderGroup を集めてリスト表示する + /// + public class VRM10SpringSelectorWindow : EditorWindow + { + const string MENU_KEY = VRMVersion.MENU + "/SpringBone"; + + [MenuItem(MENU_KEY, false, 0)] + private static void ExportFromMenu() + { + var window = (VRM10SpringSelectorWindow)GetWindow(typeof(VRM10SpringSelectorWindow)); + window.titleContent = new GUIContent("SpringBone selector"); + window.Show(); + } + + void OnEnable() + { + // Debug.Log("OnEnable"); + Undo.willFlushUndoRecord += Repaint; + Selection.selectionChanged += Repaint; + } + + void OnDisable() + { + // Debug.Log("OnDisable"); + Selection.selectionChanged -= Repaint; + Undo.willFlushUndoRecord -= Repaint; + } + + Transform m_root; + Transform Root + { + get => m_root; + set + { + if (m_root == value) + { + return; + } + if (value != null && !value.gameObject.scene.IsValid()) + { + // skip prefab + return; + } + m_root = value; + if (m_root != null) + { + m_springs = m_root.GetComponentsInChildren(); + m_colliderGroups = m_root.GetComponentsInChildren(); + } + } + } + + static bool s_foldSprings = true; + public VRM10SpringBone[] m_springs; + + static bool s_foldColliders = true; + public VRM10SpringBoneColliderGroup[] m_colliderGroups; + + void Reload() + { + var backup = Root; + Root = null; + Root = backup; + } + + private void OnGUI() + { + Root = (Transform)EditorGUILayout.ObjectField("vrm1 root", m_root, typeof(Transform), true); + + if (m_springs != null && m_springs.Length > 0 && m_springs[0] == null) + { + Reload(); + } + + GUI.enabled = false; + s_foldSprings = EditorGUILayout.Foldout(s_foldSprings, "springs"); + if (s_foldSprings) + { + if (m_springs != null) + { + foreach (var s in m_springs) + { + EditorGUILayout.ObjectField(s, typeof(VRM10SpringBone), true); + } + } + } + + s_foldColliders = EditorGUILayout.Foldout(s_foldColliders, "colliders"); + if (s_foldColliders) + { + if (m_colliderGroups != null) + { + foreach (var c in m_colliderGroups) + { + EditorGUILayout.ObjectField(c, typeof(VRM10SpringBoneColliderGroup), true); + } + } + } + } + } +} diff --git a/Assets/VRM10/Editor/Components/SpringBone/VRM10SpringSelectorWindow.cs.meta b/Assets/VRM10/Editor/Components/SpringBone/VRM10SpringSelectorWindow.cs.meta new file mode 100644 index 000000000..9bbae3601 --- /dev/null +++ b/Assets/VRM10/Editor/Components/SpringBone/VRM10SpringSelectorWindow.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a38b6d598e4f6c9458cd0a819302b438 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBone.cs b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBone.cs index f834791c5..f87ea8048 100644 --- a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBone.cs +++ b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBone.cs @@ -19,6 +19,9 @@ namespace UniVRM10 [Serializable] public class VRM10SpringBone : MonoBehaviour { + [SerializeField] + Color m_gizmoColor = Color.yellow; + [SerializeField] public string Comment; @@ -28,9 +31,6 @@ namespace UniVRM10 [SerializeField] public List ColliderGroups = new List(); - [SerializeField] - public Transform m_center; - [ContextMenu("Reset bones")] public void ResetJoints() { @@ -43,9 +43,12 @@ namespace UniVRM10 } } + Transform m_center; + List m_colliderList = new List(); - public void Process() + public void Process(Transform center) { + m_center = center; if (Joints == null) { return; @@ -93,20 +96,20 @@ namespace UniVRM10 VRM10SpringJoint lastJoint = Joints.FirstOrDefault(x => x != null); foreach (var joint in Joints.Where(x => x != null).Skip(1)) { - lastJoint.Update(m_center, Time.deltaTime, m_colliderList, joint); + lastJoint.Update(center, Time.deltaTime, m_colliderList, joint); lastJoint = joint; } - lastJoint.Update(m_center, Time.deltaTime, m_colliderList, null); + lastJoint.Update(center, Time.deltaTime, m_colliderList, null); } } - public void DrawGizmo(Color color) + public void OnDrawGizmosSelected() { foreach (var joint in Joints) { if (joint != null) { - joint.DrawGizmo(m_center, color); + joint.DrawGizmo(m_center, m_gizmoColor); } } } diff --git a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneColliderGroup.cs b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneColliderGroup.cs index 149f59bde..221f896d5 100644 --- a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneColliderGroup.cs +++ b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneColliderGroup.cs @@ -10,6 +10,9 @@ namespace UniVRM10 [AddComponentMenu("VRM10/VRM10SpringBoneColliderGroup")] public class VRM10SpringBoneColliderGroup : MonoBehaviour { + [SerializeField] + Color m_gizmoColor = Color.cyan; + [SerializeField] public List Colliders = new List(); @@ -61,9 +64,9 @@ namespace UniVRM10 } } - public void DrawGizmos(Color color) + public void OnDrawGizmosSelected() { - Gizmos.color = color; + Gizmos.color = m_gizmoColor; Matrix4x4 mat = transform.localToWorldMatrix; Gizmos.matrix = mat * Matrix4x4.Scale(new Vector3( 1.0f / transform.lossyScale.x, diff --git a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringJoint.cs b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringJoint.cs index 88214a493..d37cc2c49 100644 --- a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringJoint.cs +++ b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringJoint.cs @@ -1,6 +1,9 @@ using System; using System.Collections.Generic; using UnityEngine; +#if UNITY_EDITOR +using UnityEditor; +#endif namespace UniVRM10 { @@ -43,7 +46,11 @@ namespace UniVRM10 } else { - // TODO +#if UNITY_EDITOR + // Gizmos.matrix = Transform.localToWorldMatrix; + Gizmos.color = color; + Gizmos.DrawSphere(Transform.position, m_jointRadius); +#endif } } diff --git a/Assets/VRM10/Runtime/Components/VRM10Controller.cs b/Assets/VRM10/Runtime/Components/VRM10Controller.cs index 631a89290..bc4be32e0 100644 --- a/Assets/VRM10/Runtime/Components/VRM10Controller.cs +++ b/Assets/VRM10/Runtime/Components/VRM10Controller.cs @@ -27,6 +27,9 @@ namespace UniVRM10 [SerializeField, Header("UpdateSetting")] public UpdateTypes UpdateType = UpdateTypes.LateUpdate; + + [SerializeField, Header("SpringBone")] + public Transform SpringBoneCenter; } [SerializeField] @@ -128,7 +131,7 @@ namespace UniVRM10 } foreach (var spring in m_springs) { - spring.Process(); + spring.Process(Controller.SpringBoneCenter); } //