summaryrefslogtreecommitdiff
path: root/Runtime/Animation/AvatarBuilder.h
blob: 03eb8ae5161befda9d227859aa9b3720b17b3f6b (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#ifndef AVATARBUILDER_H
#define AVATARBUILDER_H

#include "Runtime/BaseClasses/NamedObject.h"

#include "Runtime/Math/Vector3.h"
#include "Runtime/Math/Quaternion.h"
#include "Runtime/mecanim/types.h"

#include "Runtime/Animation/MecanimUtility.h"

#include "Runtime/Serialize/SerializeTraits.h"

#include <vector>

namespace Unity { class GameObject; }
class Avatar;
class Transform;

namespace mecanim
{
	namespace skeleton
	{
		struct SkeletonPose;
		struct Skeleton;
	}

	namespace animation
	{
		struct AvatarConstant;
	}
}

struct SkeletonBone
{
	DEFINE_GET_TYPESTRING(SkeletonBone)

	SkeletonBone()
		:m_Position(Vector3f::zero), 
		m_Rotation(Quaternionf::identity()),
		m_Scale(Vector3f::one),
		m_TransformModified(false)
	{
	}

	UnityStr	m_Name;
	Vector3f	m_Position;
	Quaternionf m_Rotation;
	Vector3f	m_Scale;
	bool		m_TransformModified;

	template<class TransferFunction>
	inline void Transfer (TransferFunction& transfer)
	{
		TRANSFER(m_Name);		
		TRANSFER(m_Position);
		TRANSFER(m_Rotation);
		TRANSFER(m_Scale);
		TRANSFER(m_TransformModified);
		transfer.Align();
	}
};

struct SkeletonBoneLimit
{
	DEFINE_GET_TYPESTRING(SkeletonBoneLimit)

	SkeletonBoneLimit()
		:m_Min(Vector3f::zero), 
		m_Max(Vector3f::zero),
		m_Value(Vector3f::zero),
		m_Length(0.f),
		m_Modified(false)
	{
	}

	Vector3f	m_Min;
	Vector3f	m_Max;
	Vector3f	m_Value;
	float		m_Length;
	bool		m_Modified;
	
	template<class TransferFunction>
	inline void Transfer (TransferFunction& transfer)
	{
		TRANSFER(m_Min);		
		TRANSFER(m_Max);
		TRANSFER(m_Value);
		TRANSFER(m_Length);
		TRANSFER(m_Modified);
		transfer.Align();
	}
};


struct HumanBone
{
	DEFINE_GET_TYPESTRING(HumanBone)

	HumanBone();
	HumanBone(std::string const& humanName);

	UnityStr			m_BoneName;
	UnityStr			m_HumanName;
	SkeletonBoneLimit   m_Limit;
	//Vector3f			m_ColliderPosition;
	//Quaternionf			m_ColliderRotation;
	//Vector3f			m_ColliderScale;
	
	template<class TransferFunction>
	inline void Transfer (TransferFunction& transfer)
	{
		TRANSFER(m_BoneName);
		TRANSFER(m_HumanName);
		TRANSFER(m_Limit);

		//TRANSFER(m_ColliderPosition);
		//TRANSFER(m_ColliderRotation);
		//TRANSFER(m_ColliderScale);
	}
};

class FindHumanBone
{
protected:
	UnityStr  mName;
public:
	FindHumanBone(UnityStr const& name):mName(name){}
	bool operator()(HumanBone const& bone){ return mName == bone.m_HumanName;}
};

class FindBoneName
{
protected:
	UnityStr  mName;
public:
	FindBoneName(UnityStr const& name):mName(name){}
	bool operator()(HumanBone const& bone){ return mName == bone.m_BoneName;}
};

typedef std::vector<HumanBone> HumanBoneList;
typedef std::vector<SkeletonBone> SkeletonBoneList;

struct HumanDescription
{
	HumanBoneList m_Human;
	SkeletonBoneList m_Skeleton;

	float	m_ArmTwist;
	float	m_ForeArmTwist;
	float	m_UpperLegTwist;
	float	m_LegTwist;

	float	m_ArmStretch;
	float	m_LegStretch;

	float	m_FeetSpacing;

	UnityStr	m_RootMotionBoneName;

	void Reset()
	{
		m_Human.clear();
		m_Skeleton.clear();

		m_ArmTwist = 0.5f;
		m_ForeArmTwist = 0.5f;
		m_UpperLegTwist = 0.5f;
		m_LegTwist = 0.5f;
		m_ArmStretch = 0.05f;
		m_LegStretch = 0.05f;

		m_FeetSpacing = 0.0f;

		m_RootMotionBoneName = "";
	}

	DEFINE_GET_TYPESTRING(HumanDescription)
	
	template<class TransferFunction>
	inline void Transfer (TransferFunction& transfer)
	{
		TRANSFER(m_Human);
		TRANSFER(m_Skeleton);

		TRANSFER(m_ArmTwist);
		TRANSFER(m_ForeArmTwist);
		TRANSFER(m_UpperLegTwist);
		TRANSFER(m_LegTwist);
		TRANSFER(m_ArmStretch);
		TRANSFER(m_LegStretch);
		TRANSFER(m_FeetSpacing);
		TRANSFER(m_RootMotionBoneName);
	}
};

// The type of Avatar to create
enum AvatarType
{
	kGeneric  = 2,
	kHumanoid = 3
};

class AvatarBuilder
{
public:
	struct Options
	{
		Options():avatarType(kGeneric), readTransform(false), useMask(false){}

		AvatarType avatarType;
		bool readTransform;
		bool useMask;
	};

	struct NamedTransform
	{
		UnityStr    name;
		UnityStr    path;
		Transform*  transform;
	};

	typedef std::vector<NamedTransform>	NamedTransforms;

	// readTransform is mainly used by importer. It does read the Default pose from the first frame pose found in the file.
	// useMask is mainly used for API call, when suer want to select only a sub part of a hierarchy.
	static std::string	BuildAvatar(Avatar& avatar, const Unity::GameObject& go, bool doOptimizeTransformHierarchy, const HumanDescription& humanDescription, Options options = Options() );	

	static bool			IsValidHuman(HumanDescription const& humanDescription, NamedTransforms const& namedTransform, std::string& error);
	static bool			IsValidHumanDescription(HumanDescription const& humanDescription, std::string& error);

	static std::string	GenerateAvatarMap(Unity::GameObject const& go, NamedTransforms& namedTransform, const HumanDescription& humanDescription, bool doOptimizeTransformHierarchy, AvatarType avatarType, bool useMask = false);

	static void			GetAllChildren(Transform& node, NamedTransforms& transforms, std::vector<UnityStr> const& mask = std::vector<UnityStr>() );
	static void			GetAllParent(Transform& node, NamedTransforms& transforms, std::vector<UnityStr> const& mask = std::vector<UnityStr>(), bool includeSelf = false );

	static void			ReadFromLocalTransformToSkeletonPose(mecanim::skeleton::SkeletonPose* pose, NamedTransforms const& namedTransform);
	
	static bool			TPoseMatch(mecanim::animation::AvatarConstant const& avatar, NamedTransforms const& namedTransform, std::string& warning);
protected:		

	static void GetAllChildren (Transform& node, UnityStr& path, NamedTransforms& transforms, std::vector<UnityStr> const& mask);
	static void	GetAllParent(Transform& root, Transform& node, NamedTransforms& transforms, std::vector<UnityStr> const& mask = std::vector<UnityStr>(), bool includeSelf = false );

	static Transform* GetTransform(int id, HumanDescription const& humanDescription, NamedTransforms const& namedTransform, std::vector<string> const& boneName);	

	
	static Transform*	GetHipsNode(const HumanDescription& humanDescription, NamedTransforms const& transforms);
	static Transform*	GetRootMotionNode(const HumanDescription& humanDescription, NamedTransforms const& transforms);
	static bool			RemoveAllNoneHumanLeaf(NamedTransforms& namedTransform, HumanDescription const& humanDescription);
};




#endif