Mapping 3D cloth models to kinect joints using C# in Unity

50 Views Asked by At
using UnityEngine;
using Windows.Kinect;

public class OverlayGarment : MonoBehaviour
{
    // Reference to the cloth model
    public GameObject clothModel;

     // Reference to the Kinect body source manager
    public BodySourceManager bodySourceManager;

    // Define an array of Transform objects called clothBones
    public Transform[] clothBones; // Assign cloth bones in the Inspector

    // Define the mapping sensitivity
    public float positionAdjustmentFactor = 1.0f;
    void Start()
    {
        Debug.Log("Hello, Unity!");
    }

    void Update()
    {

        // Unassigned
        if (bodySourceManager == null || clothModel == null)
        {
            // Mapping not performed
            return;
        }

        // Retrieve body data from bodySourceManager
        Body[] bodyData = bodySourceManager.GetData();

      
        // Check if bodyData array is empty
        
        if (bodyData == null || bodyData.Length == 0)
        {
            // Mapping not performed
            Debug.LogWarning("Empty.");
            return;
        } 

        // 1st entry of bodyData array
        if (bodyData[0].IsTracked) // Assuming you are tracking one person
        {
            Debug.Log("Tracked."); 


            // Create array to store joint position
            Vector3[] jointPositions = new Vector3[clothBones.Length];
           

            // Call MapClothBoneToJoint for each cloth bone to determine the corresponding Kinect joint
            for (int i = 0; i < clothBones.Length; i++)
            {
                Windows.Kinect.JointType jointType = MapClothBoneToJoint(i);
                
                // Use jointType to get jointPosition
                jointPositions[i] = GetJointPosition(bodyData[0].Joints[jointType]) * positionAdjustmentFactor;
                Debug.Log("Joint position of (" + i + ") is: " +jointPositions[i]);
            }

             // Update the cloth model's bone positions
            for (int i = 0; i < clothBones.Length; i++)
            {
                Debug.Log("(" + i + ") Before: " +clothBones[i].localPosition);
                clothBones[i].localPosition = jointPositions[i];
                Debug.Log("(" + i + ") After: " +clothBones[i].localPosition);
            } 

           

        }


    }


     // Convert CameraSpacePoint to Vector3 manually
    public Vector3 GetJointPosition(Windows.Kinect.Joint joint)
    {
        return new Vector3(joint.Position.X, joint.Position.Y, joint.Position.Z);
    }


    // Define a mapping between cloth bones and Kinect joint types
    private Windows.Kinect.JointType MapClothBoneToJoint(int boneIndex)
    {
    
    // Check the bone index and map it accordingly
    switch (boneIndex)
    {
        // Map the first cloth bone to the right shoulder
        case 0:
            return Windows.Kinect.JointType.ShoulderRight; 

        case 1:
            return Windows.Kinect.JointType.ElbowRight;
       
       case 2:
            return Windows.Kinect.JointType.WristRight;

        case 3:
            return Windows.Kinect.JointType.ShoulderLeft;
        
        case 4:
            return Windows.Kinect.JointType.ElbowLeft;

        case 5:
            return Windows.Kinect.JointType.WristLeft;

        case 6:
            return Windows.Kinect.JointType.SpineMid;

        case 7:
            return Windows.Kinect.JointType.SpineBase;

    

        default:
            // If the bone index is not in your mapping, return a default value or handle it accordingly
            return Windows.Kinect.JointType.SpineShoulder; // Default mapping to the base of the spine
    }
    }
}

I'm currently working on a simple AR project which overlay 3D cloth models onto user body by using Kinect v2 and Unity. I wish to map the cloth bones of 3D cloth models to kinect joints position of user body (not avatar). But why using the script given above, the position of the cloth model (after) had already updated to the position of joints detected by kinect, but the position of cloth model does not move at all. I'm very new to C# and Unity, deeply appreciative for the help and support provided.

0

There are 0 best solutions below