AvatarDescription.cs
8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using System;
using System.Linq;
using System.Collections.Generic;
namespace UniHumanoid
{
[Serializable]
public struct BoneLimit
{
public HumanBodyBones humanBone;
public string boneName;
public bool useDefaultValues;
public Vector3 min;
public Vector3 max;
public Vector3 center;
public float axisLength;
public static BoneLimit From(HumanBone bone)
{
return new BoneLimit
{
humanBone = (HumanBodyBones)Enum.Parse(typeof(HumanBodyBones), bone.humanName.Replace(" ", ""), true),
boneName = bone.boneName,
useDefaultValues = bone.limit.useDefaultValues,
min = bone.limit.min,
max = bone.limit.max,
center = bone.limit.center,
axisLength = bone.limit.axisLength,
};
}
public static String ToHumanBoneName(HumanBodyBones b)
{
foreach (var x in HumanTrait.BoneName)
{
if (x.Replace(" ", "") == b.ToString())
{
return x;
}
}
throw new KeyNotFoundException();
}
public HumanBone ToHumanBone()
{
return new HumanBone
{
boneName = boneName,
humanName = ToHumanBoneName(humanBone),
limit = new HumanLimit
{
useDefaultValues = useDefaultValues,
axisLength = axisLength,
center = center,
max = max,
min = min
},
};
}
}
[Serializable]
public class AvatarDescription : ScriptableObject
{
public float armStretch = 0.05f;
public float legStretch = 0.05f;
public float upperArmTwist = 0.5f;
public float lowerArmTwist = 0.5f;
public float upperLegTwist = 0.5f;
public float lowerLegTwist = 0.5f;
public float feetSpacing = 0;
public bool hasTranslationDoF;
public BoneLimit[] human;
public HumanDescription ToHumanDescription(Transform root)
{
return new HumanDescription
{
skeleton = root.Traverse().Select(x => x.ToSkeletonBone()).ToArray(),
human = human.Select(x => x.ToHumanBone()).ToArray(),
armStretch = armStretch,
legStretch = legStretch,
upperArmTwist = upperArmTwist,
lowerArmTwist = lowerArmTwist,
upperLegTwist = upperLegTwist,
lowerLegTwist = lowerLegTwist,
feetSpacing = feetSpacing,
hasTranslationDoF = hasTranslationDoF,
};
}
public Avatar CreateAvatar(Transform root)
{
return AvatarBuilder.BuildHumanAvatar(root.gameObject, ToHumanDescription(root));
}
public Avatar CreateAvatarAndSetup(Transform root)
{
var avatar = CreateAvatar(root);
avatar.name = name;
var animator = root.GetComponent<Animator>();
if (animator != null)
{
var positionMap = root.Traverse().ToDictionary(x => x, x => x.position);
animator.avatar = avatar;
foreach (var x in root.Traverse())
{
x.position = positionMap[x];
}
}
var transfer = root.GetComponent<HumanPoseTransfer>();
if (transfer != null)
{
transfer.Avatar = avatar;
}
return avatar;
}
#if UNITY_EDITOR
public static AvatarDescription CreateFrom(Avatar avatar)
{
var description = default(HumanDescription);
if (!GetHumanDescription(avatar, ref description))
{
return null;
}
return CreateFrom(description);
}
#endif
public static AvatarDescription CreateFrom(HumanDescription description)
{
var avatarDescription = ScriptableObject.CreateInstance<AvatarDescription>();
avatarDescription.name = "AvatarDescription";
avatarDescription.armStretch = description.armStretch;
avatarDescription.legStretch = description.legStretch;
avatarDescription.feetSpacing = description.feetSpacing;
avatarDescription.hasTranslationDoF = description.hasTranslationDoF;
avatarDescription.lowerArmTwist = description.lowerArmTwist;
avatarDescription.lowerLegTwist = description.lowerLegTwist;
avatarDescription.upperArmTwist = description.upperArmTwist;
avatarDescription.upperLegTwist = description.upperLegTwist;
avatarDescription.human = description.human.Select(BoneLimit.From).ToArray();
return avatarDescription;
}
public static AvatarDescription Create(AvatarDescription src=null)
{
var avatarDescription = ScriptableObject.CreateInstance<AvatarDescription>();
avatarDescription.name = "AvatarDescription";
if (src != null)
{
avatarDescription.armStretch = src.armStretch;
avatarDescription.legStretch = src.legStretch;
avatarDescription.feetSpacing = src.feetSpacing;
avatarDescription.upperArmTwist = src.upperArmTwist;
avatarDescription.lowerArmTwist = src.lowerArmTwist;
avatarDescription.upperLegTwist = src.upperLegTwist;
avatarDescription.lowerLegTwist = src.lowerLegTwist;
}
else
{
avatarDescription.armStretch = 0.05f;
avatarDescription.legStretch = 0.05f;
avatarDescription.feetSpacing = 0.0f;
avatarDescription.lowerArmTwist = 0.5f;
avatarDescription.upperArmTwist = 0.5f;
avatarDescription.upperLegTwist = 0.5f;
avatarDescription.lowerLegTwist = 0.5f;
}
return avatarDescription;
}
public static AvatarDescription Create(Transform[] boneTransforms, Skeleton skeleton)
{
return Create(skeleton.Bones.Select(
x => new KeyValuePair<HumanBodyBones, Transform>(x.Key, boneTransforms[x.Value])));
}
public static AvatarDescription Create(IEnumerable<KeyValuePair<HumanBodyBones, Transform>> skeleton)
{
var description = Create();
description.SetHumanBones(skeleton);
return description;
}
public void SetHumanBones(IEnumerable<KeyValuePair<HumanBodyBones, Transform>> skeleton)
{
human = skeleton.Select(x =>
{
return new BoneLimit
{
humanBone = x.Key,
boneName = x.Value.name,
useDefaultValues = true,
};
}).ToArray();
}
#if UNITY_EDITOR
/// <summary>
/// * https://answers.unity.com/questions/612177/how-can-i-access-human-avatar-bone-and-muscle-valu.html
/// </summary>
/// <param name="target"></param>
/// <param name="des"></param>
/// <returns></returns>
public static bool GetHumanDescription(UnityEngine.Object target, ref HumanDescription des)
{
if (target != null)
{
var importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(target));
if (importer != null)
{
Debug.Log("AssetImporter Type: " + importer.GetType());
ModelImporter modelImporter = importer as ModelImporter;
if (modelImporter != null)
{
des = modelImporter.humanDescription;
Debug.Log("## Cool stuff data by ModelImporter ##");
return true;
}
else
{
Debug.LogWarning("## Please Select Imported Model in Project View not prefab or other things ##");
}
}
}
return false;
}
#endif
}
}