Skip to content

Commit

Permalink
Merge pull request #2371 from ousttrue/feature/springbone_add_joints
Browse files Browse the repository at this point in the history
root の子孫に再帰的に joint を設定するボタン
  • Loading branch information
ousttrue authored Jul 19, 2024
2 parents 35f9007 + deff52b commit 4c79c6a
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
119 changes: 119 additions & 0 deletions Assets/VRM10/Editor/Components/SpringBone/VRM10SpringBoneDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace UniVRM10
{
using static Vrm10InstanceSpringBone;

[CustomPropertyDrawer(typeof(Spring))]
public class VRM10SpringBoneDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUIUtility.singleLineHeight * 3
+ EditorGUI.GetPropertyHeight(property.FindPropertyRelative(nameof(Spring.ColliderGroups)))
+ EditorGUI.GetPropertyHeight(property.FindPropertyRelative(nameof(Spring.Joints)))
;
}

static float DrawProp(Rect rect, SerializedProperty prop)
{
rect.height = EditorGUI.GetPropertyHeight(prop);
EditorGUI.PropertyField(rect, prop);
return rect.height;
}

struct GuiEnable : IDisposable
{
bool Backup;
public static GuiEnable Enter(bool enable)
{
var value = new GuiEnable
{
Backup = GUI.enabled,
};
GUI.enabled = enable;
return value;
}

public void Dispose()
{
GUI.enabled = Backup;
}
}

static IEnumerable<VRM10SpringBoneJoint> MakeJointsRecursive(VRM10SpringBoneJoint parent)
{
if (parent.transform.childCount > 0)
{
var child = parent.transform.GetChild(0);
var joint = child.GetComponent<VRM10SpringBoneJoint>();
if (joint == null)
{
joint = child.gameObject.AddComponent<VRM10SpringBoneJoint>();
}
if (joint != null)
{
// set params
joint.m_dragForce = parent.m_dragForce;
joint.m_gravityDir = parent.m_gravityDir;
joint.m_gravityPower = parent.m_gravityPower;
joint.m_jointRadius = parent.m_jointRadius;
joint.m_stiffnessForce = parent.m_stiffnessForce;

yield return joint;
foreach (var x in MakeJointsRecursive(joint))
{
yield return x;
}
}
}
}

public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
var x = rect.x;
var y = rect.y;
var w = rect.width;
var h = EditorGUIUtility.singleLineHeight;
y += DrawProp(new Rect(x, y, w, h), property.FindPropertyRelative(nameof(Spring.Name)));
y += DrawProp(new Rect(x, y, w, h), property.FindPropertyRelative(nameof(Spring.ColliderGroups)));

var joints = property.FindPropertyRelative(nameof(Spring.Joints));
y += DrawProp(new Rect(x, y, w, h), joints);

var enable = joints.arraySize > 0 && joints.GetArrayElementAtIndex(0).objectReferenceValue != null;
using (GuiEnable.Enter(enable))
{
if (GUI.Button(new Rect(x, y, w, h), "create joints to children"))
{
if (EditorUtility.DisplayDialog("auto joints",
"先頭の joint の子孫をリストに追加します。\n既存のリストは上書きされます。",
"ok",
"cancel"))
{
var root = (VRM10SpringBoneJoint)joints.GetArrayElementAtIndex(0).objectReferenceValue;
joints.ClearArray();
int i = 0;
// 0
joints.InsertArrayElementAtIndex(i);
joints.GetArrayElementAtIndex(i).objectReferenceValue = root;
++i;
// 1...
foreach (var joint in MakeJointsRecursive(root))
{
joints.InsertArrayElementAtIndex(i);
joints.GetArrayElementAtIndex(i).objectReferenceValue = joint;
++i;
}
}
}
}
y += EditorGUIUtility.singleLineHeight;

y += DrawProp(new Rect(x, y, w, h), property.FindPropertyRelative(nameof(Spring.Center)));
}
}
}

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

0 comments on commit 4c79c6a

Please sign in to comment.