Skip to content

Commit

Permalink
capsule の頭側も drag できるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue committed Nov 29, 2024
1 parent 1087bfe commit 6cee7b3
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ class VRM10SpringBoneColliderEditor : Editor
SerializedProperty _colliderType;
SerializedProperty _offset;
SerializedProperty _tail;
Tool _last = Tool.None;
bool _openUtility = false;

static VRM10SpringBoneCollider s_selected;
enum HandleType
{
Offset,
Tail,
Normal,
}
static (VRM10SpringBoneCollider Collider, HandleType HandleType)? s_selected;

private void OnEnable()
{
Expand Down Expand Up @@ -66,11 +74,39 @@ public override void OnInspectorGUI()

EditorGUILayout.BeginHorizontal();
{
if (GUILayout.Button("Drag handle"))
if (GUILayout.Button("Drag offset"))
{
s_selected = (_target, HandleType.Offset);
}
if (
_target.ColliderType == VRM10SpringBoneColliderTypes.Capsule
|| _target.ColliderType == VRM10SpringBoneColliderTypes.CapsuleInside)
{
s_selected = _target;
if (GUILayout.Button("Drag tail"))
{
s_selected = (_target, HandleType.Tail);
}
}
if (_target.ColliderType == VRM10SpringBoneColliderTypes.Plane)
{
if (GUILayout.Button("Drag normal"))
{
s_selected = (_target, HandleType.Normal);
}
}
using (new EditorGUI.DisabledScope(s_selected == default))
{
if (GUILayout.Button("Drag end"))
{
s_selected = default;
}
}
}
EditorGUILayout.EndHorizontal();

_openUtility = EditorGUILayout.Foldout(_openUtility, "utility");
if (_openUtility)
{
if (_target.transform.childCount > 0)
{
if (GUILayout.Button("Fit head-tail capsule"))
Expand All @@ -83,7 +119,6 @@ public override void OnInspectorGUI()
}
}
}
EditorGUILayout.EndHorizontal();

if (serializedObject.ApplyModifiedProperties())
{
Expand All @@ -104,39 +139,79 @@ public void OnSceneGUI()

bool updated = false;

if (s_selected == _target)
if (s_selected.HasValue && s_selected.Value.Collider == _target)
{
var c = _target;
Handles.matrix = c.transform.localToWorldMatrix;
if (_target.ColliderType == VRM10SpringBoneColliderTypes.Capsule
|| _target.ColliderType == VRM10SpringBoneColliderTypes.CapsuleInside)
if (Tools.current != Tool.None)
{
EditorGUI.BeginChangeCheck();
var newTargetPosition = Handles.PositionHandle(c.Tail, Quaternion.identity);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(c, "collider");
c.Tail = newTargetPosition;
updated = true;
}
_last = Tools.current;
}
else
Tools.current = Tool.None;

Handles.matrix = _target.transform.localToWorldMatrix;
switch (s_selected.Value.HandleType)
{
EditorGUI.BeginChangeCheck();
var newTargetPosition = Handles.PositionHandle(c.Offset, Quaternion.identity);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(c, "collider");
c.Offset = newTargetPosition;
updated = true;
}
case HandleType.Offset:
{
Handles.Label(_target.Offset, "Collider offset");
EditorGUI.BeginChangeCheck();
var newTargetPosition = Handles.PositionHandle(_target.Offset, Quaternion.identity);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(_target, "collider");
_target.Offset = newTargetPosition;
updated = true;
}
}
break;

case HandleType.Tail:
{
Handles.Label(_target.Tail, "Capsule tail");
EditorGUI.BeginChangeCheck();
var newTargetPosition = Handles.PositionHandle(_target.Tail, Quaternion.identity);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(_target, "collider");
_target.Tail = newTargetPosition;
updated = true;
}
}
break;

case HandleType.Normal:
{
Handles.Label(_target.Offset, "Plane normal");
EditorGUI.BeginChangeCheck();
var r = Quaternion.FromToRotation(Vector3.up, _target.Normal);
var newRotation = Handles.RotationHandle(r, _target.Offset);
Handles.DrawLine(_target.Offset, _target.Offset + _target.Normal);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(_target, "collider");
_target.Normal = newRotation * Vector3.up;
updated = true;
}
}
break;
}
}
else
{
if (_last != Tool.None)
{
Tools.current = _last;
_last = Tool.None;
}
}

if (Application.isPlaying && updated)
if (updated)
{
// 反映!
_vrm.Runtime.SpringBone.ReconstructSpringBone();
_target.OnValidate();
if (Application.isPlaying && updated)
{
// 反映!
_vrm.Runtime.SpringBone.ReconstructSpringBone();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class VRM10SpringBoneCollider : MonoBehaviour

public bool IsSelected => GetInstanceID() == SelectedGuid;

public void OnValidate()
{
Normal = Normal.normalized;
}

void OnDrawGizmosSelected()
{
DrawGizmos();
Expand Down

0 comments on commit 6cee7b3

Please sign in to comment.