-
Notifications
You must be signed in to change notification settings - Fork 0
/
Actor.cs
465 lines (376 loc) · 22.2 KB
/
Actor.cs
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
using Rokoko.Core;
using Rokoko.Helper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Rokoko.Inputs
{
public class Actor : MonoBehaviour
{
[System.Serializable]
public enum BoneMappingEnum
{
Animator,
Custom
}
[System.Serializable]
public enum RotationSpace
{
Offset,
World,
Self
}
[HideInInspector] public string profileName = "DemoProfile";
[HideInInspector] public BoneMappingEnum boneMapping;
[HideInInspector] public Animator animator;
[HideInInspector] public HumanBoneMapping customBoneMapping;
[Header("Convert Space")]
[Tooltip("Convert Studio data to Unity position space")]
public Space positionSpace = Space.Self;
[Tooltip("Convert Studio data to Unity rotation space")]
public RotationSpace rotationSpace = RotationSpace.Offset;
[Space(10)]
[Tooltip("Calculate Model's height comparing to Actor's and position the Hips accordingly.\nGreat tool to align with the floor")]
public bool adjustHipHeightBasedOnStudioActor = true;
[HideInInspector] public Face face = null;
[Header("Log extra info")]
public bool debug = false;
protected Dictionary<HumanBodyBones, Transform> animatorHumanBones = new Dictionary<HumanBodyBones, Transform>();
private Dictionary<HumanBodyBones, Quaternion> offsets = new Dictionary<HumanBodyBones, Quaternion>();
private float hipHeight = 0;
private float LeftHandCoordinates_x = 0;
private float LeftHandCoordinates_y = 0;
private float LeftHandCoordinates_z = 0;
private float RightHandCoordinates_x = 0;
private float RightHandCoordinates_y = 0;
private float RightHandCoordinates_z = 0;
// defining an array of the tree hierarchy for each hand
private HumanBodyBones[] RightTree = new HumanBodyBones[16]; // Crea un array vacío de 15 elementos
private HumanBodyBones[] LeftTree = new HumanBodyBones[16]; // Crea un array vacío de 15 elementos
public const int size = 16;
#region Initialize
protected virtual void Awake()
{
if (!animator.isHuman)
{
Debug.LogError("Model is not marked as Humanoid. Please go in model inspector, under Rig tab and select AnimationType as Humanoid.");
return;
}
InitializeBodyBones();
// Get the Hip height independent of parent transformations
hipHeight = animatorHumanBones[HumanBodyBones.Hips].parent.InverseTransformVector(animatorHumanBones[HumanBodyBones.Hips].localPosition).y;
hipHeight = Mathf.Abs(hipHeight);
// Calculate the Righthand and LeftHand coordinates
RightHandCoordinates_x = animatorHumanBones[HumanBodyBones.LeftHand].parent.InverseTransformVector(animatorHumanBones[HumanBodyBones.LeftHand].localPosition).x;
// RightHand heigth
RightHandCoordinates_y = animatorHumanBones[HumanBodyBones.LeftHand].parent.InverseTransformVector(animatorHumanBones[HumanBodyBones.LeftHand].localPosition).y;
RightHandCoordinates_z = animatorHumanBones[HumanBodyBones.LeftHand].parent.InverseTransformVector(animatorHumanBones[HumanBodyBones.LeftHand].localPosition).z;
LeftHandCoordinates_x = animatorHumanBones[HumanBodyBones.RightHand].parent.InverseTransformVector(animatorHumanBones[HumanBodyBones.RightHand].localPosition).x;
// LeftHand heigth
LeftHandCoordinates_y = animatorHumanBones[HumanBodyBones.RightHand].parent.InverseTransformVector(animatorHumanBones[HumanBodyBones.RightHand].localPosition).y;
LeftHandCoordinates_z = animatorHumanBones[HumanBodyBones.RightHand].parent.InverseTransformVector(animatorHumanBones[HumanBodyBones.RightHand].localPosition).z;
Debug.Log(RightHandCoordinates_x);
Debug.Log(RightHandCoordinates_y);
Debug.Log(RightHandCoordinates_z);
}
/// <summary>
/// Register Actor override in StudioManager.
/// </summary>
private void Start()
{
if (!animator.isHuman) return;
if (!string.IsNullOrEmpty(profileName))
StudioManager.AddActorOverride(this);
}
/// <summary>
/// Cache the bone transforms from Animator.
/// </summary>
protected void InitializeBodyBones()
{
if (animator == null || !animator.isHuman) return;
foreach (HumanBodyBones bone in RokokoHelper.HumanBodyBonesArray)
{
if (bone == HumanBodyBones.LastBone) break;
animatorHumanBones.Add(bone, animator.GetBoneTransform(bone));
}
// RightTree
RightTree[0] = HumanBodyBones.RightHand;
RightTree[1] = HumanBodyBones.RightThumbDistal;
RightTree[2] = HumanBodyBones.RightThumbIntermediate;
RightTree[3] = HumanBodyBones.RightThumbProximal;
RightTree[4] = HumanBodyBones.RightIndexDistal;
RightTree[5] = HumanBodyBones.RightIndexIntermediate;
RightTree[6] = HumanBodyBones.RightIndexProximal;
RightTree[7] = HumanBodyBones.RightMiddleDistal;
RightTree[8] = HumanBodyBones.RightMiddleIntermediate;
RightTree[9] = HumanBodyBones.RightMiddleProximal;
RightTree[10] = HumanBodyBones.RightLittleDistal;
RightTree[11] = HumanBodyBones.RightLittleIntermediate;
RightTree[12] = HumanBodyBones.RightLittleProximal;
RightTree[13] = HumanBodyBones.RightRingDistal;
RightTree[14] = HumanBodyBones.RightRingIntermediate;
RightTree[15] = HumanBodyBones.RightRingProximal;
// LeftTree
LeftTree[0] = HumanBodyBones.LeftHand;
LeftTree[1] = HumanBodyBones.LeftThumbDistal;
LeftTree[2] = HumanBodyBones.LeftThumbIntermediate;
LeftTree[3] = HumanBodyBones.LeftThumbProximal;
LeftTree[4] = HumanBodyBones.LeftIndexDistal;
LeftTree[5] = HumanBodyBones.LeftIndexIntermediate;
LeftTree[6] = HumanBodyBones.LeftIndexProximal;
LeftTree[7] = HumanBodyBones.LeftMiddleDistal;
LeftTree[8] = HumanBodyBones.LeftMiddleIntermediate;
LeftTree[9] = HumanBodyBones.LeftMiddleProximal;
LeftTree[10] = HumanBodyBones.LeftLittleDistal;
LeftTree[11] = HumanBodyBones.LeftLittleIntermediate;
LeftTree[12] = HumanBodyBones.LeftLittleProximal;
LeftTree[13] = HumanBodyBones.LeftRingDistal;
LeftTree[14] = HumanBodyBones.LeftRingIntermediate;
LeftTree[15] = HumanBodyBones.LeftRingProximal;
// Calculate offsets based on Smartsuit T pose
offsets = GetRotationOffsets(animatorHumanBones);
}
#endregion
#region Public Methods
/// <summary>
/// Update Skeleton and Face data based on ActorFrame.
/// </summary>
public virtual void UpdateActor(ActorFrame actorFrame)
{
if (animator == null || !animator.isHuman) return;
profileName = actorFrame.name;
bool updateBody = actorFrame.meta.hasBody || actorFrame.meta.hasGloves;
// Update skeleton from data
if (updateBody)
UpdateSkeleton(actorFrame);
// Update face from data
if (actorFrame.meta.hasFace)
face?.UpdateFace(actorFrame.face);
}
/// <summary>
/// Create Idle/Default Actor.
/// </summary>
public virtual void CreateIdle(string actorName)
{
this.profileName = actorName;
}
#endregion
#region Internal Logic
/// <summary>
/// Update Humanoid Skeleton based on BodyData.
/// </summary>
// protected void UpdateSkeleton(ActorFrame actorFrame)
// {
// foreach (HumanBodyBones bone in RokokoHelper.HumanBodyBonesArray)
// {
// if (bone == HumanBodyBones.LastBone) break;
// // Obtain the info about rotation and position of the actual bone in actorFrame.
// ActorJointFrame? boneFrame = actorFrame.body.GetBoneFrame(bone); // obtains the object ActorJointFrame corresponding to the actual bone from actorFrame.body, Using the method GetBoneFrame(bone). ActorJointframe has the data of rotation and position of the bone. when we call actorFrame.bodyGetBoneFrame(bone), we access the frame of the actor that represents the actual state of the bone in terms of rotation and position in the 3D space.
// if (boneFrame != null)
// {
// bool shouldUpdatePosition = bone == HumanBodyBones.Hips;
// Quaternion worldRotation = boneFrame.Value.rotation.ToQuaternion();
// Vector3 worldPosition = boneFrame.Value.position.ToVector3();
// // Offset Hip bone
// if (shouldUpdatePosition && adjustHipHeightBasedOnStudioActor)
// worldPosition = new Vector3(worldPosition.x, worldPosition.y - (actorFrame.dimensions.hipHeight - hipHeight), worldPosition.z);
// UpdateBone(bone, worldPosition, worldRotation, shouldUpdatePosition, positionSpace, rotationSpace);
// }
// }
// }
// / <summary>
// / Update Humanoid Skeleton based on BodyData.
// / </summary>
protected void UpdateSkeleton(ActorFrame actorFrame)
{
foreach (HumanBodyBones bone in RokokoHelper.HumanBodyBonesArray)
{
if (bone == HumanBodyBones.LastBone) break;
// Obtain the info about rotation and position of the actual bone in actorFrame.
ActorJointFrame? boneFrame = actorFrame.body.GetBoneFrame(bone); // obtains the object ActorJointFrame corresponding to the actual bone from actorFrame.body, Using the method GetBoneFrame(bone). ActorJointframe has the data of rotation and position of the bone. when we call actorFrame.bodyGetBoneFrame(bone), we access the frame of the actor that represents the actual state of the bone in terms of rotation and position in the 3D space.
if (boneFrame != null)
{
for (int i = 0; i < size; i++)
{
if (RightTree[i] == bone)
{
bool shouldUpdatePosition = bone == HumanBodyBones.RightHand;
Quaternion worldRotation = boneFrame.Value.rotation.ToQuaternion();
Vector3 worldPosition = boneFrame.Value.position.ToVector3();
// referenceTransform = animator.GetBoneTransform(HumanBodyBones.RightHand);
// Offset RightHand bone
if (shouldUpdatePosition && adjustHipHeightBasedOnStudioActor)
worldPosition = new Vector3(worldPosition.x, worldPosition.y - RightHandCoordinates_y, worldPosition.z);
UpdateBone(bone, worldPosition, worldRotation, shouldUpdatePosition, positionSpace, rotationSpace);
}
else if (LeftTree[i] == bone)
{
bool shouldUpdatePosition = bone == HumanBodyBones.LeftHand;
Quaternion worldRotation = boneFrame.Value.rotation.ToQuaternion();
Vector3 worldPosition = boneFrame.Value.position.ToVector3();
// referenceTransform = animator.GetBoneTransform(HumanBodyBones.RightHand);
// Offset RightHand bone
if (shouldUpdatePosition && adjustHipHeightBasedOnStudioActor)
worldPosition = new Vector3(worldPosition.x, worldPosition.y - LeftHandCoordinates_y, worldPosition.z);
UpdateBone(bone, worldPosition, worldRotation, shouldUpdatePosition, positionSpace, rotationSpace);
}
}
}
}
}
/// <summary>
/// Update Human bone.
/// </summary>
protected void UpdateBone(HumanBodyBones bone, Vector3 worldPosition, Quaternion worldRotation, bool updatePosition, Space positionSpace, RotationSpace rotationSpace)
{
// Find Humanoid bone
Transform boneTransform;
if (boneMapping == BoneMappingEnum.Animator)
boneTransform = animatorHumanBones[bone];
else
boneTransform = customBoneMapping.customBodyBones[(int)bone];
// Check if bone is valid
if (boneTransform == null)
{
if (debug)
Debug.LogWarning($"Couldn't find Transform for bone:{bone} in {boneMapping}Mapping component", this.transform);
return;
}
// Update position
if (updatePosition)
{
if (positionSpace == Space.World || transform.parent == null)
{
boneTransform.position = worldPosition;
}
else
{
boneTransform.localPosition = boneTransform.parent.InverseTransformVector(worldPosition);
}
}
// Update Rotation
if (transform.parent != null)
{
boneTransform.rotation = this.transform.parent.rotation * worldRotation;
Debug.Log("parent rotation");
Debug.Log(transform.parent);
Debug.Log("for bone");
Debug.Log(bone);
}
else
{
boneTransform.rotation = worldRotation;
Debug.Log("World transform");
Debug.Log("for bone");
Debug.Log(bone);
}
}
// if (rotationSpace == RotationSpace.World)
// {
// boneTransform.rotation = worldRotation;
// }
// else if (rotationSpace == RotationSpace.Self)
// {
// if (transform.parent != null)
// {
// boneTransform.rotation = this.transform.parent.rotation * worldRotation;
// Debug.Log("parent rotation");
// Debug.Log(transform.parent);
// Debug.Log("for bone");
// Debug.Log(bone);
// }
// else
// {
// boneTransform.rotation = worldRotation;
// }
// }
// else
// {
// boneTransform.rotation = this.transform.rotation * worldRotation * offsets[bone];
// }
// }
#endregion
/// <summary>
/// Get the rotational difference between 2 humanoid T poses.
/// </summary>
private static Dictionary<HumanBodyBones, Quaternion> GetRotationOffsets(Dictionary<HumanBodyBones, Transform> humanoidBones)
{
Dictionary<HumanBodyBones, Quaternion> offsets = new Dictionary<HumanBodyBones, Quaternion>();
foreach (HumanBodyBones bone in RokokoHelper.HumanBodyBonesArray)
{
Quaternion rotation = Quaternion.identity;
if (humanoidBones[bone] != null)
{
// Subtract Root orientation from bone
Quaternion boneTransform = Quaternion.Inverse(humanoidBones[HumanBodyBones.Hips].rotation) * humanoidBones[bone].rotation;
// Subtract from SmartSuit T Pose
rotation = Quaternion.Inverse(SmartsuitTPose[bone]) * boneTransform;
}
offsets.Add(bone, rotation);
}
return offsets;
}
/// <summary>
/// Get Smartsuit T pose data
/// </summary>
private static Dictionary<HumanBodyBones, Quaternion> SmartsuitTPose = new Dictionary<HumanBodyBones, Quaternion>() {
{HumanBodyBones.Hips, new Quaternion(0.000f, 0.000f, 0.000f, 1.000f)},
{HumanBodyBones.LeftUpperLeg, new Quaternion(0.000f, 0.707f, 0.000f, 0.707f)},
{HumanBodyBones.RightUpperLeg, new Quaternion(0.000f, -0.707f, 0.000f, 0.707f)},
{HumanBodyBones.LeftLowerLeg, new Quaternion(0.000f, 0.707f, 0.000f, 0.707f)},
{HumanBodyBones.RightLowerLeg, new Quaternion(0.000f, -0.707f, 0.000f, 0.707f)},
{HumanBodyBones.LeftFoot, new Quaternion(0.000f, 0.707f, -0.707f, 0.000f)},
{HumanBodyBones.RightFoot, new Quaternion(0.000f, -0.707f, 0.707f, 0.000f)},
{HumanBodyBones.Spine, new Quaternion(0.000f, 0.000f, 1.000f, 0.000f)},
{HumanBodyBones.Chest, new Quaternion(0.000f, 0.000f, 1.000f, 0.000f)},
{HumanBodyBones.Neck, new Quaternion(0.000f, 0.000f, 1.000f, 0.000f)},
{HumanBodyBones.Head, new Quaternion(0.000f, 0.000f, 1.000f, 0.000f)},
{HumanBodyBones.LeftShoulder, new Quaternion(0.000f, 0.000f, 0.707f, -0.707f)},
{HumanBodyBones.RightShoulder, new Quaternion(0.000f, 0.000f, 0.707f, 0.707f)},
{HumanBodyBones.LeftUpperArm, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.RightUpperArm, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.LeftLowerArm, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.RightLowerArm, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.LeftHand, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.RightHand, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.LeftToes, new Quaternion(0.000f, 0.707f, -0.707f, 0.000f)},
{HumanBodyBones.RightToes, new Quaternion(0.000f, -0.707f, 0.707f, 0.000f)},
{HumanBodyBones.LeftEye, new Quaternion(0.000f, 0.000f, 0.000f, 0.000f)},
{HumanBodyBones.RightEye, new Quaternion(0.000f, 0.000f, 0.000f, 0.000f)},
{HumanBodyBones.Jaw, new Quaternion(0.000f, 0.000f, 0.000f, 0.000f)},
{HumanBodyBones.LeftThumbProximal, new Quaternion(-0.561f, -0.701f, 0.430f, -0.092f)},
{HumanBodyBones.LeftThumbIntermediate, new Quaternion(-0.653f, -0.653f, 0.271f, -0.271f)},
{HumanBodyBones.LeftThumbDistal, new Quaternion(-0.653f, -0.653f, 0.271f, -0.271f)},
{HumanBodyBones.LeftIndexProximal, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.LeftIndexIntermediate, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.LeftIndexDistal, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.LeftMiddleProximal, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.LeftMiddleIntermediate, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.LeftMiddleDistal, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.LeftRingProximal, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.LeftRingIntermediate, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.LeftRingDistal, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.LeftLittleProximal, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.LeftLittleIntermediate, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.LeftLittleDistal, new Quaternion(-0.500f, -0.500f, 0.500f, -0.500f)},
{HumanBodyBones.RightThumbProximal, new Quaternion(0.561f, -0.701f, 0.430f, 0.092f)},
{HumanBodyBones.RightThumbIntermediate, new Quaternion(0.653f, -0.653f, 0.271f, 0.271f)},
{HumanBodyBones.RightThumbDistal, new Quaternion(0.653f, -0.653f, 0.271f, 0.271f)},
{HumanBodyBones.RightIndexProximal, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.RightIndexIntermediate, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.RightIndexDistal, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.RightMiddleProximal, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.RightMiddleIntermediate, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.RightMiddleDistal, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.RightRingProximal, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.RightRingIntermediate, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.RightRingDistal, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.RightLittleProximal, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.RightLittleIntermediate, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.RightLittleDistal, new Quaternion(0.500f, -0.500f, 0.500f, 0.500f)},
{HumanBodyBones.UpperChest, new Quaternion(0.000f, 0.000f, 1.000f, 0.000f)}
};
}
}