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

AvatarBuilder.BuildHumanAvatar には隠れたボーン名 HumanTrait.BoneName が必用 #1800

Merged
merged 4 commits into from
Sep 15, 2022
Merged
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
59 changes: 24 additions & 35 deletions Assets/UniGLTF/Runtime/UniHumanoid/AvatarDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace UniHumanoid
{
// TODO: BoneLimit.cs に分ける(v0.104以降)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff が分かりにくくなるので、あとで

[Serializable]
public struct BoneLimit
{
Expand All @@ -19,26 +20,34 @@ public struct BoneLimit
public Vector3 max;
public Vector3 center;
public float axisLength;
private static string[] cashedHumanTraitBoneName = null;
private static readonly Dictionary<HumanBodyBones, string> cachedHumanBodyBonesToBoneNameMap =
new Dictionary<HumanBodyBones, string>();

static BoneLimit()
{
// 呼び出し毎にGCが発生するのでキャッシュする
string[] boneNames = HumanTrait.BoneName;
cashedHumanTraitBoneName = new string[boneNames.Length];
for (var i = 0; i < boneNames.Length; i++)
{
cashedHumanTraitBoneName[i] = boneNames[i].Replace(" ", "");
}
}

// HumanTrait.BoneName は HumanBodyBones.ToString とほぼ一対一に対応するが、
// 指のボーンについては " " の有無という微妙な違いがある。
// このスペースは AvatarBuilder.BuildHumanAvatar において必用であり、
// HumanBodyBones.ToString と区別する必要がある。
//
// また、下記についてGCが発生するのでキャッシュします。
// * HumanTrait.BoneName
// * traitName.Replace
// * Enum.Parse
//
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HumanTrait.BoneName が問題の根源

private static readonly Dictionary<HumanBodyBones, string> cachedHumanBodyBonesToBoneTraitNameMap =
HumanTrait.BoneName.ToDictionary(
traitName => (HumanBodyBones)Enum.Parse(typeof(HumanBodyBones), traitName.Replace(" ", "")),
traitName => traitName);

// 逆引き
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum.Parse がGC出ると書いた手前、逆引きもキャッシュすることにした

private static readonly Dictionary<string, HumanBodyBones> cachedBoneTraitNameToHumanBodyBonesMap =
HumanTrait.BoneName.ToDictionary(
traitName => traitName,
traitName => (HumanBodyBones)Enum.Parse(typeof(HumanBodyBones), traitName.Replace(" ", "")));

public static BoneLimit From(HumanBone bone)
{
return new BoneLimit
{
humanBone = (HumanBodyBones) Enum.Parse(typeof(HumanBodyBones), bone.humanName.Replace(" ", ""), true),
humanBone = cachedBoneTraitNameToHumanBodyBonesMap[bone.humanName],
boneName = bone.boneName,
useDefaultValues = bone.limit.useDefaultValues,
min = bone.limit.min,
Expand All @@ -48,32 +57,12 @@ public static BoneLimit From(HumanBone bone)
};
}

public static String ToHumanBoneName(HumanBodyBones b)
{
if (cachedHumanBodyBonesToBoneNameMap.TryGetValue(b, out string result))
{
return result;
}

var bs = b.ToString();
foreach (var x in cashedHumanTraitBoneName)
{
if (x == bs)
{
cachedHumanBodyBonesToBoneNameMap[b] = x;
return x;
}
}

throw new KeyNotFoundException();
}

public HumanBone ToHumanBone()
{
return new HumanBone
{
boneName = boneName,
humanName = ToHumanBoneName(humanBone),
humanName = cachedHumanBodyBonesToBoneTraitNameMap[humanBone],
limit = new HumanLimit
{
useDefaultValues = useDefaultValues,
Expand Down