mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-10-02 07:42:25 -05:00
Merge pull request #1602 from ousttrue/vrm10/add_joint_chain_menu
[ContextMenu("AddJointsToAllChild0")]
This commit is contained in:
@@ -8,6 +8,7 @@ using UnityEditor;
|
||||
namespace UniVRM10
|
||||
{
|
||||
[Serializable]
|
||||
[DisallowMultipleComponent]
|
||||
public class VRM10SpringBoneJoint : MonoBehaviour
|
||||
{
|
||||
[SerializeField, Range(0, 4), Header("Settings")]
|
||||
@@ -22,10 +23,88 @@ namespace UniVRM10
|
||||
[SerializeField, Range(0, 1)]
|
||||
public float m_dragForce = 0.4f;
|
||||
|
||||
[SerializeField]
|
||||
public bool m_exclude;
|
||||
|
||||
[SerializeField, Range(0, 0.5f), Header("Collision")]
|
||||
public float m_jointRadius = 0.02f;
|
||||
|
||||
void AddJointRecursive(Transform t, VRM10SpringBoneJoint src)
|
||||
{
|
||||
var joint = t.gameObject.GetComponent<VRM10SpringBoneJoint>();
|
||||
if (joint == null)
|
||||
{
|
||||
joint = t.gameObject.AddComponent<VRM10SpringBoneJoint>();
|
||||
Debug.Log($"{joint} added");
|
||||
}
|
||||
|
||||
// copy settings
|
||||
joint.m_stiffnessForce = src.m_stiffnessForce;
|
||||
joint.m_gravityPower = src.m_gravityPower;
|
||||
joint.m_gravityDir = src.m_gravityDir;
|
||||
joint.m_dragForce = src.m_dragForce;
|
||||
joint.m_jointRadius = src.m_jointRadius;
|
||||
|
||||
if (t.childCount > 0)
|
||||
{
|
||||
// only first child
|
||||
AddJointRecursive(t.GetChild(0), src);
|
||||
}
|
||||
}
|
||||
|
||||
void GetJoints(Transform t, List<VRM10SpringBoneJoint> joints)
|
||||
{
|
||||
var joint = t.GetComponent<VRM10SpringBoneJoint>();
|
||||
if (joint != null)
|
||||
{
|
||||
joints.Add(joint);
|
||||
}
|
||||
|
||||
if (t.childCount > 0)
|
||||
{
|
||||
GetJoints(t.GetChild(0), joints);
|
||||
}
|
||||
}
|
||||
|
||||
[ContextMenu("Add joints")]
|
||||
private void AddJointsToChild0()
|
||||
{
|
||||
var root = GetComponentInParent<Vrm10Instance>();
|
||||
if (root == null)
|
||||
{
|
||||
Debug.LogWarning("not Vrm10Instance");
|
||||
return;
|
||||
}
|
||||
|
||||
if (transform.childCount == 0)
|
||||
{
|
||||
Debug.LogWarning("no children");
|
||||
return;
|
||||
}
|
||||
|
||||
AddJointRecursive(transform.GetChild(0), this);
|
||||
|
||||
// updater root
|
||||
foreach (var spring in root.SpringBone.Springs)
|
||||
{
|
||||
for (int i = 0; i < spring.Joints.Count; ++i)
|
||||
{
|
||||
if (spring.Joints[i] == this)
|
||||
{
|
||||
// found
|
||||
while (spring.Joints.Count - 1 > i)
|
||||
{
|
||||
// remove after this joint
|
||||
spring.Joints.RemoveAt(spring.Joints.Count - 1);
|
||||
}
|
||||
// get descendants joints
|
||||
var joints = new List<VRM10SpringBoneJoint>();
|
||||
GetJoints(transform.GetChild(0), joints);
|
||||
// add jonits to after this
|
||||
spring.Joints.AddRange(joints);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Debug.LogWarning($"{this} is found in {root}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user