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

[VRM1] SpringBone center をモデルレベルに移動など #880

Merged
merged 4 commits into from
Apr 16, 2021
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
108 changes: 108 additions & 0 deletions Assets/VRM10/Editor/Components/SpringBone/VRM10SpringSelectorWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using UnityEditor;
using UnityEngine;

namespace UniVRM10
{
/// <summary>
/// ヒエラルキーから VRM10SpringBone と VRM10ColliderGroup を集めてリスト表示する
/// </summary>
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<VRM10SpringBone>();
m_colliderGroups = m_root.GetComponentsInChildren<VRM10SpringBoneColliderGroup>();
}
}
}

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);
}
}
}
}
}
}

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

19 changes: 11 additions & 8 deletions Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace UniVRM10
[Serializable]
public class VRM10SpringBone : MonoBehaviour
{
[SerializeField]
Color m_gizmoColor = Color.yellow;

[SerializeField]
public string Comment;

Expand All @@ -28,9 +31,6 @@ public class VRM10SpringBone : MonoBehaviour
[SerializeField]
public List<VRM10SpringBoneColliderGroup> ColliderGroups = new List<VRM10SpringBoneColliderGroup>();

[SerializeField]
public Transform m_center;

[ContextMenu("Reset bones")]
public void ResetJoints()
{
Expand All @@ -43,9 +43,12 @@ public void ResetJoints()
}
}

Transform m_center;

List<SpringBoneLogic.InternalCollider> m_colliderList = new List<SpringBoneLogic.InternalCollider>();
public void Process()
public void Process(Transform center)
{
m_center = center;
if (Joints == null)
{
return;
Expand Down Expand Up @@ -93,20 +96,20 @@ public void Process()
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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace UniVRM10
[AddComponentMenu("VRM10/VRM10SpringBoneColliderGroup")]
public class VRM10SpringBoneColliderGroup : MonoBehaviour
{
[SerializeField]
Color m_gizmoColor = Color.cyan;

[SerializeField]
public List<VRM10SpringBoneCollider> Colliders = new List<VRM10SpringBoneCollider>();

Expand Down Expand Up @@ -61,9 +64,9 @@ private static void DrawWireCircle(Vector3 centerPos, Vector3 xAxis, Vector3 yAx
}
}

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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace UniVRM10
{
Expand Down Expand Up @@ -43,7 +46,11 @@ public void DrawGizmo(Transform center, Color color)
}
else
{
// TODO
#if UNITY_EDITOR
// Gizmos.matrix = Transform.localToWorldMatrix;
Gizmos.color = color;
Gizmos.DrawSphere(Transform.position, m_jointRadius);
#endif
}
}

Expand Down
5 changes: 4 additions & 1 deletion Assets/VRM10/Runtime/Components/VRM10Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public enum UpdateTypes

[SerializeField, Header("UpdateSetting")]
public UpdateTypes UpdateType = UpdateTypes.LateUpdate;

[SerializeField, Header("SpringBone")]
public Transform SpringBoneCenter;
}

[SerializeField]
Expand Down Expand Up @@ -128,7 +131,7 @@ public void Process()
}
foreach (var spring in m_springs)
{
spring.Process();
spring.Process(Controller.SpringBoneCenter);
}

//
Expand Down