diff --git a/Assets/VRM10/Editor/Components/SpringBone/SpringBoneEditor.cs b/Assets/VRM10/Editor/Components/SpringBone/SpringBoneEditor.cs index 36faf4958..a06fe694f 100644 --- a/Assets/VRM10/Editor/Components/SpringBone/SpringBoneEditor.cs +++ b/Assets/VRM10/Editor/Components/SpringBone/SpringBoneEditor.cs @@ -48,7 +48,10 @@ namespace UniVRM10 public static void Draw3D(Vrm10Instance target, SerializedObject so) { var tree = GetTree(target, so); - tree.Draw3D(); + if (tree != null && target != null) + { + tree.Draw3D(target.SpringBone); + } } } } diff --git a/Assets/VRM10/Editor/Components/SpringBone/SpringBoneTreeView.cs b/Assets/VRM10/Editor/Components/SpringBone/SpringBoneTreeView.cs index a9788ed8c..00072502f 100644 --- a/Assets/VRM10/Editor/Components/SpringBone/SpringBoneTreeView.cs +++ b/Assets/VRM10/Editor/Components/SpringBone/SpringBoneTreeView.cs @@ -37,24 +37,30 @@ namespace UniVRM10 // load _map = new Dictionary(); - for (var i = 0; i < target.SpringBone.ColliderGroups.Count; ++i) + if (target?.SpringBone?.ColliderGroups != null) { - var colliderGroup = target.SpringBone.ColliderGroups[i]; - var name = colliderGroup.GUIName(i); - var id = _nextNodeID++; - var item = new TreeViewItem(id, 2, name); - _map.Add(id, colliderGroup); - _colliderGroups.AddChild(item); + for (var i = 0; i < target.SpringBone.ColliderGroups.Count; ++i) + { + var colliderGroup = target.SpringBone.ColliderGroups[i]; + var name = colliderGroup.GUIName(i); + var id = _nextNodeID++; + var item = new TreeViewItem(id, 2, name); + _map.Add(id, colliderGroup); + _colliderGroups.AddChild(item); + } } - for (var i = 0; i < target.SpringBone.Springs.Count; ++i) + if (target?.SpringBone?.Springs != null) { - var spring = target.SpringBone.Springs[i]; - var name = spring.GUIName(i); - var id = _nextNodeID++; - var item = new TreeViewItem(id, 2, name); - _map.Add(id, spring); - _springs.AddChild(item); + for (var i = 0; i < target.SpringBone.Springs.Count; ++i) + { + var spring = target.SpringBone.Springs[i]; + var name = spring.GUIName(i); + var id = _nextNodeID++; + var item = new TreeViewItem(id, 2, name); + _map.Add(id, spring); + _springs.AddChild(item); + } } } @@ -107,16 +113,92 @@ namespace UniVRM10 } } - public void Draw3D() + Color s_selectedColor = new Color(0.5f, 0, 0, 0.5f); + Color s_Color = new Color(0.6f, 0.6f, 0.6f, 0.5f); + + public void Draw3D(Vrm10InstanceSpringBone springBone) { if (_selected is SelectedColliderGroupGUI colliderGroup) { - colliderGroup.Draw3D(); + // colliderGroup.Draw3D(); } else if (_selected is SelectedSpringGUI spring) { // TODO } + + foreach (var group in springBone.ColliderGroups) + { + foreach (var collider in group.Colliders) + { + var color = collider.gameObject == Selection.activeObject ? s_selectedColor : s_Color; + if (DrawCollider(collider, color)) + { + Selection.activeObject = collider.gameObject; + } + } + } + foreach (var spring in springBone.Springs) + { + for (int i = 1; i < spring.Joints.Count; ++i) + { + var head = spring.Joints[i - 1]; + var tail = spring.Joints[i]; + var color = tail.gameObject == Selection.activeObject ? s_selectedColor : s_Color; + if (DrawJoint(head, tail, color)) + { + Selection.activeObject = tail.gameObject; + } + } + } + } + + static bool IsLeftDown() + { + return Event.current.type == EventType.MouseDown && Event.current.button == 0; + } + + bool DrawCollider(VRM10SpringBoneCollider collider, Color color) + { + if (Event.current.type == EventType.Repaint || Event.current.type == EventType.Layout) + { + Handles.matrix = collider.transform.localToWorldMatrix; + Handles.color = color; + switch (collider.ColliderType) + { + case VRM10SpringBoneColliderTypes.Sphere: + // Handles.color = Color.magenta; + Handles.SphereHandleCap(collider.gameObject.GetInstanceID(), collider.Offset, Quaternion.identity, collider.Radius * 2, Event.current.type); + break; + + case VRM10SpringBoneColliderTypes.Capsule: + // Handles.color = Color.cyan; + Handles.SphereHandleCap(collider.gameObject.GetInstanceID(), collider.Offset, Quaternion.identity, collider.Radius * 2, Event.current.type); + Handles.SphereHandleCap(collider.gameObject.GetInstanceID(), collider.Tail, Quaternion.identity, collider.Radius * 2, Event.current.type); + Handles.DrawLine(collider.Offset, collider.Tail); + break; + } + Handles.matrix = Matrix4x4.identity; + } + else if (IsLeftDown()) + { + return HandleUtility.nearestControl == collider.gameObject.GetInstanceID(); + } + return false; + } + + bool DrawJoint(VRM10SpringBoneJoint head, VRM10SpringBoneJoint tail, Color color) + { + if (Event.current.type == EventType.Repaint || Event.current.type == EventType.Layout) + { + Handles.color = color; + Handles.SphereHandleCap(tail.gameObject.GetInstanceID(), tail.transform.position, Quaternion.identity, tail.m_jointRadius * 2, Event.current.type); + } + else if (IsLeftDown()) + { + return HandleUtility.nearestControl == tail.gameObject.GetInstanceID(); + } + return false; } } } diff --git a/Assets/VRM10/Editor/Components/VRM10Window.cs b/Assets/VRM10/Editor/Components/VRM10Window.cs index 476a50ce6..f7aca7c2b 100644 --- a/Assets/VRM10/Editor/Components/VRM10Window.cs +++ b/Assets/VRM10/Editor/Components/VRM10Window.cs @@ -59,7 +59,7 @@ namespace UniVRM10 } m_root = id; m_so = value != null ? new SerializedObject(value) : null; - m_constraints = null; + // m_constraints = null; if (Root != null) { @@ -71,28 +71,28 @@ namespace UniVRM10 Transform m_head; - public IVrm10Constraint[] m_constraints; + // public IVrm10Constraint[] m_constraints; ScrollView m_scrollView = new ScrollView(); - enum VRMSceneUI - { - Constraints, - SpringBone, - } - static VRMSceneUI s_ui = default; - static string[] s_selection; - static string[] Selection - { - get - { - if (s_selection == null) - { - s_selection = Enum.GetNames(typeof(VRMSceneUI)); - } - return s_selection; - } - } + // enum VRMSceneUI + // { + // Constraints, + // SpringBone, + // } + // static VRMSceneUI s_ui = default; + // static string[] s_selection; + // static string[] Selection + // { + // get + // { + // if (s_selection == null) + // { + // s_selection = Enum.GetNames(typeof(VRMSceneUI)); + // } + // return s_selection; + // } + // } /// /// public entry point @@ -100,20 +100,22 @@ namespace UniVRM10 /// void OnSceneGUI(SceneView sceneView) { - switch (s_ui) - { - case VRMSceneUI.Constraints: - Tools.hidden = false; - break; + // switch (s_ui) + // { + // case VRMSceneUI.Constraints: + // Tools.hidden = false; + // break; - case VRMSceneUI.SpringBone: - Tools.hidden = true; - SpringBoneEditor.Draw3D(Root, m_so); - break; + // case VRMSceneUI.SpringBone: + // Tools.hidden = true; + // SpringBoneEditor.Draw3D(Root, m_so); + // break; - default: - throw new NotImplementedException(); - } + // default: + // throw new NotImplementedException(); + // } + Tools.hidden = true; + SpringBoneEditor.Draw3D(Root, m_so); } // @@ -137,12 +139,12 @@ namespace UniVRM10 return; } - var ui = (VRMSceneUI)GUILayout.SelectionGrid((int)s_ui, Selection, 3); - if (s_ui != ui) - { - s_ui = ui; - SceneView.RepaintAll(); - } + // var ui = (VRMSceneUI)GUILayout.SelectionGrid((int)s_ui, Selection, 3); + // if (s_ui != ui) + // { + // s_ui = ui; + // SceneView.RepaintAll(); + // } if (m_so == null) { @@ -154,43 +156,44 @@ namespace UniVRM10 } m_so.Update(); - switch (s_ui) - { - case VRMSceneUI.Constraints: - m_scrollView.Draw(this.position.y, DrawConstraints, Repaint); - break; + // switch (s_ui) + // { + // case VRMSceneUI.Constraints: + // m_scrollView.Draw(this.position.y, DrawConstraints, Repaint); + // break; - case VRMSceneUI.SpringBone: - SpringBoneEditor.Draw2D(Root, m_so); - break; + // case VRMSceneUI.SpringBone: + // SpringBoneEditor.Draw2D(Root, m_so); + // break; - default: - throw new NotImplementedException(); - } + // default: + // throw new NotImplementedException(); + // } + SpringBoneEditor.Draw2D(Root, m_so); m_so.ApplyModifiedProperties(); } - void DrawConstraints() - { - if (Root != null) - { - if (m_constraints == null) - { - m_constraints = Root.GetComponentsInChildren(); - } - } + // void DrawConstraints() + // { + // if (Root != null) + // { + // if (m_constraints == null) + // { + // m_constraints = Root.GetComponentsInChildren(); + // } + // } - using (new EditorGUI.DisabledScope(true)) - { - if (m_constraints != null) - { - foreach (var c in m_constraints) - { - EditorGUILayout.ObjectField(c.ConstraintTarget, typeof(MonoBehaviour), true); - } - } - } - } + // using (new EditorGUI.DisabledScope(true)) + // { + // if (m_constraints != null) + // { + // foreach (var c in m_constraints) + // { + // EditorGUILayout.ObjectField(c.ConstraintTarget, typeof(MonoBehaviour), true); + // } + // } + // } + // } } }