|
1 | | -// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved. |
2 | 2 |
|
3 | 3 | using System; |
4 | 4 | using System.Collections.Generic; |
@@ -77,7 +77,7 @@ public static void AlignWrists(MSDKUtilityEditorConfig source, MSDKUtilityEditor |
77 | 77 | PerformArmScaling(source, target); |
78 | 78 |
|
79 | 79 | // 2. Match wrists. |
80 | | - JointMappingUtility.PerformWristMatching(source, target); |
| 80 | + PerformWristMatching(source, target); |
81 | 81 |
|
82 | 82 | // 3. Align fingers based on wrist to middle. |
83 | 83 | foreach (var wristType in new[] { KnownJointType.LeftWrist, KnownJointType.RightWrist }) |
@@ -197,6 +197,130 @@ public static void TranslateHips(MSDKUtilityEditorConfig source, MSDKUtilityEdit |
197 | 197 | } |
198 | 198 | } |
199 | 199 |
|
| 200 | + /// <summary> |
| 201 | + /// Aligns finger bones between source and target skeletons. |
| 202 | + /// </summary> |
| 203 | + /// <param name="win">The editor window.</param> |
| 204 | + /// <param name="startBoneIds">The array of starting bone IDs for fingers.</param> |
| 205 | + /// <param name="endBoneIds">The array of ending bone IDs for fingers.</param> |
| 206 | + /// <param name="wristType">The wrist joint type.</param> |
| 207 | + public static void PerformFingerMatching(MSDKUtilityEditorWindow win, |
| 208 | + FullBodyTrackingBoneId[] startBoneIds, |
| 209 | + FullBodyTrackingBoneId[] endBoneIds, |
| 210 | + KnownJointType wristType) |
| 211 | + { |
| 212 | + var fingerNames = new[] |
| 213 | + { |
| 214 | + new[] { "thumb" }, |
| 215 | + new[] { "index" }, |
| 216 | + new[] { "middle" }, |
| 217 | + new[] { "ring" }, |
| 218 | + new[] { "pinky" } |
| 219 | + }; |
| 220 | + var startNameExtensions = new[] |
| 221 | + { |
| 222 | + "metacarpal", "proximal", "1", "2" |
| 223 | + }; |
| 224 | + var endNameExtensions = new[] |
| 225 | + { |
| 226 | + "distal", "3", "4" |
| 227 | + }; |
| 228 | + |
| 229 | + var startBones = new List<Transform>(); |
| 230 | + var endBones = new List<Transform>(); |
| 231 | + var wrist = GetTargetTransformFromKnownJoint(win.TargetInfo, wristType); |
| 232 | + var wristBones = wrist.GetAllChildren(); |
| 233 | + foreach (var fingerNameSet in fingerNames) |
| 234 | + { |
| 235 | + foreach (var fingerName in fingerNameSet) |
| 236 | + { |
| 237 | + var startBonesToCheck = startNameExtensions.Select(e => fingerName + e); |
| 238 | + var endBonesToCheck = endNameExtensions.Select(e => fingerName + e); |
| 239 | + var startBone = |
| 240 | + MSDKUtilityHelper.FindClosestMatches(wristBones.ToArray(), startBonesToCheck.ToArray())[0] |
| 241 | + .Item1; |
| 242 | + var endBone = |
| 243 | + MSDKUtilityHelper.FindClosestMatches(wristBones.ToArray(), endBonesToCheck.ToArray())[0] |
| 244 | + .Item1; |
| 245 | + startBones.Add(startBone); |
| 246 | + endBones.Add(endBone); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + for (var i = 0; i < startBoneIds.Length; i++) |
| 251 | + { |
| 252 | + var startPos = win.SourceInfo.ReferencePose[(int)startBoneIds[i]].Position; |
| 253 | + var endPos = win.SourceInfo.ReferencePose[(int)endBoneIds[i]].Position; |
| 254 | + var startBone = startBones[i]; |
| 255 | + var endBone = endBones[i]; |
| 256 | + Undo.RecordObject(startBone, "Match Fingers"); |
| 257 | + startBone.position = startPos; |
| 258 | + |
| 259 | + var targetDirection = endPos - startPos; |
| 260 | + var targetLength = targetDirection.magnitude; |
| 261 | + targetDirection.Normalize(); |
| 262 | + var currentDirection = endBone.position - startBone.position; |
| 263 | + var currentLength = currentDirection.magnitude; |
| 264 | + currentDirection.Normalize(); |
| 265 | + |
| 266 | + var scaleFactor = targetLength / currentLength; |
| 267 | + var rotation = Quaternion.FromToRotation(currentDirection, targetDirection); |
| 268 | + startBone.rotation = rotation * startBone.rotation; |
| 269 | + UpdateTPoseData(win.TargetInfo); |
| 270 | + ApplyScalingToJointIndexAndChildren(win.TargetInfo, |
| 271 | + Array.IndexOf(win.TargetInfo.JointNames, startBone.name), scaleFactor); |
| 272 | + UpdateTPoseData(win.TargetInfo); |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + /// <summary> |
| 277 | + /// Aligns wrist joints between source and target skeletons. |
| 278 | + /// </summary> |
| 279 | + /// <param name="source">The source skeleton configuration.</param> |
| 280 | + /// <param name="target">The target skeleton configuration.</param> |
| 281 | + public static void PerformWristMatching(MSDKUtilityEditorConfig source, MSDKUtilityEditorConfig target) |
| 282 | + { |
| 283 | + // Rotate to match left/right hand. |
| 284 | + var sourceRightHand = source.ReferencePose[(int)FullBodyTrackingBoneId.RightHandWrist]; |
| 285 | + var sourceLeftHand = source.ReferencePose[(int)FullBodyTrackingBoneId.LeftHandWrist]; |
| 286 | + var leftArmIndex = GetKnownJointIndex(target, KnownJointType.LeftUpperArm); |
| 287 | + var rightArmIndex = GetKnownJointIndex(target, KnownJointType.RightUpperArm); |
| 288 | + |
| 289 | + var leftUpperArm = target.SkeletonJoints[leftArmIndex]; |
| 290 | + var leftHand = |
| 291 | + target.SkeletonJoints[GetKnownJointIndex(target, KnownJointType.LeftWrist)]; |
| 292 | + leftUpperArm.rotation = |
| 293 | + GetBestRotation(leftUpperArm, leftHand, sourceLeftHand.Position); |
| 294 | + |
| 295 | + var rightUpperArm = target.SkeletonJoints[rightArmIndex]; |
| 296 | + var rightHand = |
| 297 | + target.SkeletonJoints[GetKnownJointIndex(target, KnownJointType.RightWrist)]; |
| 298 | + rightUpperArm.rotation = |
| 299 | + GetBestRotation(rightUpperArm, rightHand, sourceRightHand.Position); |
| 300 | + |
| 301 | + UpdateTPoseData(target); |
| 302 | + } |
| 303 | + |
| 304 | + /// <summary> |
| 305 | + /// Performs automatic alignment between source and target skeletons. |
| 306 | + /// </summary> |
| 307 | + /// <param name="win">The editor window.</param> |
| 308 | + public static void AutoAlignment(MSDKUtilityEditorWindow win) |
| 309 | + { |
| 310 | + win.TargetInfo.SkeletonTPoseType = win.Step switch |
| 311 | + { |
| 312 | + MSDKUtilityEditorConfig.EditorStep.MinTPose => SkeletonTPoseType.MinTPose, |
| 313 | + MSDKUtilityEditorConfig.EditorStep.MaxTPose => SkeletonTPoseType.MaxTPose, |
| 314 | + _ => win.TargetInfo.SkeletonTPoseType |
| 315 | + }; |
| 316 | + |
| 317 | + win.ModifyConfig(false, true); |
| 318 | + if (win.FileReader.IsPlaying) |
| 319 | + { |
| 320 | + win.OpenPlaybackFile(win.CurrentPreviewPose); |
| 321 | + } |
| 322 | + } |
| 323 | + |
200 | 324 | /// <summary> |
201 | 325 | /// Sets the target skeleton to a T-pose configuration. |
202 | 326 | /// </summary> |
@@ -264,7 +388,6 @@ public static void UpdateTPoseData(MSDKUtilityEditorConfig target) |
264 | 388 | } |
265 | 389 | } |
266 | 390 |
|
267 | | - |
268 | 391 | /// <summary> |
269 | 392 | /// Scales the arms of the target skeleton to match the source skeleton. |
270 | 393 | /// </summary> |
|
0 commit comments