Exemplo n.º 1
0
    // Return false if the BVH file and the FBX model do not match
    public bool LoadBVHFile(string filepath)
    {
        m_idJointMap.Clear();
        m_bvhLoaded = false;
        // m_canMove = false;
        m_bvhNotMatch = true;
        if (FKIKPlugin.LoadBVH(m_id, filepath))
        {
            // Reconstrcut idJoint Map. If files do not match, return false
            if (!ConstructIdJointMap())
            {
                m_canMove     = false;
                m_bvhNotMatch = false;
                return(false);
            }

            m_bvhLoaded = true;
            // m_canMove = true;
            m_t               = 0;
            m_maxt            = FKIKPlugin.GetDuration(m_id);
            m_keySize         = FKIKPlugin.GetKeySize(m_id);
            m_lastRootOffsetY = m_root.transform.position.y;
            m_lastLeftFootY   = m_leftFoot.transform.position.y;
            m_lastRightFootY  = m_rightFoot.transform.position.y;

            // Reset end joint ids
            ConstructIKChains();
            return(true);
        }
        return(false);
    }
Exemplo n.º 2
0
 // Set id of end joints and construct the IK chains
 void ConstructIKChains()
 {
     foreach (KeyValuePair <int, GameObject> pair in m_idJointMap)
     {
         if (pair.Value == m_root)
         {
             //Debug.Log("Root:" + pair.Key.ToString());
             FKIKPlugin.SetRootID(m_id, pair.Key);
         }
         else if (pair.Value == m_leftFoot)
         {
             //Debug.Log("Left Foot:" + pair.Key.ToString());
             FKIKPlugin.SetLeftFootID(m_id, pair.Key);
         }
         else if (pair.Value == m_rightFoot)
         {
             //Debug.Log("Right Foot:" + pair.Key.ToString());
             FKIKPlugin.SetRightFootID(m_id, pair.Key);
         }
         else if (pair.Value == m_leftHand)
         {
             //Debug.Log("Left Hand:" + pair.Key.ToString());
             FKIKPlugin.SetLeftHandID(m_id, pair.Key);
         }
         else if (pair.Value == m_rightHand)
         {
             //Debug.Log("Right Hand:" + pair.Key.ToString());
             FKIKPlugin.SetRightHandID(m_id, pair.Key);
         }
     }
     FKIKPlugin.CreateLimbIKChains(m_id);
 }
Exemplo n.º 3
0
    // Construct a map between joints and indices
    bool ConstructIdJointMap()
    {
        m_idJointMap.Clear();
        foreach (GameObject joint in m_joints)
        {
            int id = -1;
            // End Joint: can only be found by its parent's name
            if (joint.transform.childCount == 0)
            {
                id = FKIKPlugin.GetJointIdByParentName(m_id, joint.transform.parent.name);
            }
            else
            {
                id = FKIKPlugin.GetJointIdByName(m_id, joint.transform.name);
            }

            // Not found
            if (id == -1)
            {
                m_idJointMap.Clear();
                Debug.Log("Joint Not Found:" + joint.name);
                return(false);
            }
            m_idJointMap.Add(id, joint);
        }
        return(true);
    }
Exemplo n.º 4
0
 void UpdateGuideByTarget(bool rotateToTarget)
 {
     float[] newPos  = new float[3];
     float[] newQuat = new float[4];
     FKIKPlugin.UpdateGuideJointByTarget(m_id, FKIKPlugin.UnityToBVHTranslation(m_guideTarget.transform.position),
                                         newPos, newQuat);
     // Update guide
     gameObject.transform.position = FKIKPlugin.BVHToUnityTranslation(newPos);
     gameObject.transform.rotation = FKIKPlugin.BVHToUnityQuaternion(newQuat);
 }
Exemplo n.º 5
0
 public void SetTimeByProgressBar(float id)
 {
     if (id >= m_keySize)
     {
         Debug.LogError("Key id out of range");
         return;
     }
     m_t     = FKIKPlugin.GetKeyTime(m_id, (int)id);
     m_keyID = (int)id;
 }
Exemplo n.º 6
0
 void LinkJoints()
 {
     foreach (GameObject joint in m_joints)
     {
         FKIKPlugin.JointData data;
         data.id = FindIdByJoint(joint);
         data.localTranslation = FKIKPlugin.UnityToBVHTranslation(joint.transform.localPosition);
         data.localRotation    = FKIKPlugin.UnityToBVHQuaternion(joint.transform.localRotation);
         int parentID = FindIdByJoint(joint.transform.parent.gameObject);
         FKIKPlugin.SetJointData(m_id, data, parentID);
     }
     FKIKPlugin.UpdateSkeleton(m_id);
 }
Exemplo n.º 7
0
    void TraverseJoints(Transform transform)
    {
        m_joints.Add(transform.gameObject);

        // Initialize joint in plugin
        int id = FKIKPlugin.CreateJoint(m_id, transform.gameObject.name, transform.gameObject == m_root);

        m_idJointMap.Add(id, transform.gameObject);

        // Travser childeren joints
        foreach (Transform child in transform)
        {
            TraverseJoints(child);
        }
    }
Exemplo n.º 8
0
    // Set joints' localRotation and localPosition with JointData
    private void ApplyJointDataArray()
    {
        for (int i = 0; i < m_joints.Count; ++i)
        {
            FKIKPlugin.JointData data  = m_jointDataArray[i];
            GameObject           joint = m_idJointMap[data.id];
            joint.transform.localPosition = FKIKPlugin.BVHToUnityTranslation(data.localTranslation);
            joint.transform.localRotation = FKIKPlugin.BVHToUnityQuaternion(data.localRotation);
        }

        if (m_fixedRoot)
        {
            // Set root local position x and z to zero
            m_root.transform.localPosition = new Vector3(0, m_root.transform.localPosition.y, 0);
        }
    }
Exemplo n.º 9
0
    // Start is called before the first frame update
    void Start()
    {
        m_id           = FKIKPlugin.CreateActor();
        m_jointPainter = this.GetComponent <JointPainter>();

        // Initialize the plugin and virtual joints
        TraverseJoints(m_root.transform); // Add joints to the m_joints and m_idJointMap
        LinkJoints();                     // Set the parent child relationship
        m_jointPainter.InitVirtualJoints(this.name + "joints", m_joints);
        ConstructJointDataArray();        // Initialize the joint data array
        ConstructIKChains();              // Construct IK chains for hands and feet
        GameObject[] endJoints = new GameObject[] { m_root, m_leftHand, m_rightHand, m_leftFoot, m_rightFoot };
        m_jointPainter.InitializeTargetJoints(endJoints);

        if (m_defaultBVH)
        {
            string projectPath = Application.dataPath;
            string filePath    = AssetDatabase.GetAssetPath(m_defaultBVH);
            string filename    = Path.Combine(projectPath, "../", filePath);
            Debug.Log(filename);
            LoadBVHFile(filename);
            StartMove();
        }
    }
Exemplo n.º 10
0
    private void UpdateJointTransform(float t)
    {
        // FK
        if (m_bvhLoaded)
        {
            FKIKPlugin.UpdateBVHSkeleton(m_id, t);
            FKIKPlugin.GetJointData(m_id, m_jointDataArray, m_joints.Count);
        }
        else
        {
            FKIKPlugin.UpdateSkeleton(m_id);
            FKIKPlugin.GetJointData(m_id, m_jointDataArray, m_joints.Count);
        }
        ApplyJointDataArray();

        // Check foot-ground contact
        CheckFootGroundContact();

        // IK
        if (m_enableFootIK)
        {
            FootIK();
        }
    }
Exemplo n.º 11
0
    public void SolveIK(GameObject targetJoint, Vector3 pos)
    {
        int jointID = FindIdByJoint(targetJoint.GetComponent <VirtualJointController>().GetBindedJoint());

        FKIKPlugin.SolveLimbIK(m_id, jointID, FKIKPlugin.UnityToBVHTranslation(pos));
    }
Exemplo n.º 12
0
    public void EditKeyRotation(GameObject virtualJoint)
    {
        int jointID = FindIdByJoint(virtualJoint.GetComponent <VirtualJointController>().GetBindedJoint());

        FKIKPlugin.SetJointRotation(m_id, jointID, FKIKPlugin.UnityToBVHQuaternion(virtualJoint.transform.localRotation));
    }
Exemplo n.º 13
0
    private void FootIK()
    {
        // Get foot position in world space
        Vector3 leftFootPos  = m_leftFoot.transform.position;
        Vector3 rightFootPos = m_rightFoot.transform.position;

        /// C++ Implementation
        RaycastFootHeight(leftFootPos, out float leftHeight, out Vector3 leftNormal);
        RaycastFootHeight(rightFootPos, out float rightHeight, out Vector3 rightNormal);
        FKIKPlugin.SolveFootIK(m_id, leftHeight, rightHeight, m_leftFootGroundContact, m_rightFootGroundContact,
                               FKIKPlugin.UnityToBVHTranslation(leftNormal), FKIKPlugin.UnityToBVHTranslation(rightNormal));

        FKIKPlugin.GetJointData(m_id, m_jointDataArray, m_joints.Count);
        ApplyJointDataArray();

        /// Unity Implementation

        //if (RaycastFootHeight(leftFootPos, out float leftHeight, out Vector3 leftNormal))
        //{
        //    leftFootPos.y = Mathf.Lerp(leftHeight + leftFootPos.y, m_lastLeftFootY, m_footSpeedY);
        //}
        //if (RaycastFootHeight(rightFootPos, out float rightHeight, out Vector3 rightNormal))
        //{
        //    rightFootPos.y = Mathf.Lerp(rightHeight + rightFootPos.y, m_lastRightFootY, m_footSpeedY);
        //}

        //m_lastLeftFootY = leftFootPos.y;
        //m_lastRightFootY = rightFootPos.y;
        //// Transform to guide space
        //leftFootPos = gameObject.transform.InverseTransformPoint(leftFootPos); // World space to guide space
        //rightFootPos = gameObject.transform.InverseTransformPoint(rightFootPos); // World space to guide space

        //// Move root height
        //float rootOffsetY = leftFootPos.y < rightFootPos.y ? leftHeight : rightHeight;
        //rootOffsetY = Mathf.Lerp(m_lastRootOffsetY, rootOffsetY, m_rootSpeedY);
        //m_lastRootOffsetY = rootOffsetY;
        //Vector3 rootPos = m_root.transform.position + new Vector3(0, rootOffsetY, 0);
        //rootPos = gameObject.transform.InverseTransformPoint(rootPos);

        //FKIKPlugin.SetRootJointTranslation(m_id, FKIKPlugin.UnityToBVHTranslation(rootPos));
        //FKIKPlugin.UpdateSkeleton(m_id);
        //FKIKPlugin.SolveLimbIK(m_id, FindIdByJoint(m_leftFoot), FKIKPlugin.UnityToBVHTranslation(leftFootPos));
        //FKIKPlugin.SolveLimbIK(m_id, FindIdByJoint(m_rightFoot), FKIKPlugin.UnityToBVHTranslation(rightFootPos));
        //FKIKPlugin.GetJointData(m_id, m_jointDataArray, m_joints.Count);

        //ApplyJointDataArray();

        //// Foot rotation
        //if (m_leftFootGroundContact)
        //{
        //    Vector3 leftForward = Vector3.Cross(m_leftFoot.transform.right, leftNormal); // Calculate new forward vector
        //    m_leftFoot.transform.up = leftNormal;
        //    m_leftFoot.transform.forward = leftForward;
        //}
        //if (m_rightFootGroundContact)
        //{
        //    Vector3 rightForward = Vector3.Cross(m_rightFoot.transform.right, rightNormal);
        //    m_rightFoot.transform.up = rightNormal;
        //    m_rightFoot.transform.forward = rightForward;
        //}
    }