• 联系我们   Contact
    您的位置:首页 > 新闻动态 > 技术文章

    WiseGlove数据手套驱动unity3D游戏角色右手模型关节

    2017/2/20      点击:

    目前unity3D游戏引擎已经广泛的用于游戏开发,而且unity3d在国内发展非常迅速,已经成为了主流的游戏开发引擎之一。随着越来越多的开发人员开始使用unity3D,网络上unity3D的中文学习资料也逐渐丰富。为了方便客户使用wiseglove数据手套,我们专门组织编写了在Unity3D环境下调用wiseglove数据手套SDK开发包,用数据手套的实时数据来驱动unity3d中的角色右手模型的demo程序。

    Unity3D的新版动画系统Mecanim已经对人类类型的角色支援设计了一套特殊的工作流程。用户将3dsmax或者maya中导入的人形角色导入unity3d后,需要为角色创建Avatar,本质上就是分析导入资源的骨骼结构,并对其进行标识,从而转化成Mecanim可以识别的骨骼结构,或者说转化成通用的骨骼结构,这也是为什么在资源准备时在骨骼的创建及命名要遵循一定的规范的原因,这样方便mecanim对骨骼的识别。

    在导入的资源都具有通用的骨骼结构时,就可以实现动画的共用。

    在这里我们用wiseGlove数据手套驱动右手模型时也使用了unity标准的avatar映射的人手关节模型,这样方便我们对不同的角色的右手模型进行驱动。

    下面是用于驱动人手模型的代码,需要将这段代码挂载在场景中的角色身上:

     

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;


    public class RightHand : MonoBehaviour {

        Animator animator;

        Transform rightThumbProximal; //This is the right thumb 1st phalange.

        Transform rightThumbIntermediate; // This is the right thumb 2nd phalange.

        Transform rightThumbDistal;    //This is the right thumb 3rd phalange.

        Transform rightIndexProximal; // This is the right index 1st phalange.

        Transform rightIndexIntermediate; // This is the right index 2nd phalange.

        Transform rightIndexDistal; // This is the right index 3rd phalange.

        Transform rightMiddleProximal; // This is the right middle 1st phalange.

        Transform rightMiddleIntermediate;// This is the right middle 2nd phalange.

        Transform rightMiddleDistal;// This is the right middle 3rd phalange.

        Transform rightRingProximal;// This is the right ring 1st phalange.

        Transform rightRingIntermediate;// This is the right ring 2nd phalange.

        Transform rightRingDistal;// This is the right ring 3rd phalange.

        Transform rightLittleProximal;// This is the right little 1st phalange.

        Transform rightLittleIntermediate;// This is the right little 2nd phalange.

        Transform rightLittleDistal;// This is the right little 3rd phalange.


        //将从数据手套获取到的各个手指关节的Rotation赋值给下面对应的Quaternion类型的公用变量,

        //就可以实现手指关节的运动

        public Quaternion R_Thumb_P_rotation; //R-right,T-Thumb,P-Proximal

        public Quaternion R_Thumb_I_rotation;

        public Quaternion R_Thumb_D_roatation;

        public Quaternion R_Index_P_rotation; //R-right,I-Index,P-Proximal

        public Quaternion R_Index_I_rotation;

        public Quaternion R_Index_D_roatation;

        public Quaternion R_Middle_P_rotation; //R-right,M-Middle,P-Proximal

        public Quaternion R_Middle_I_rotation;

        public Quaternion R_Middle_D_roatation;

        public Quaternion R_Ring_P_rotation; //R-right,R-Ring,P-Proximal

        public Quaternion R_Ring_I_rotation;

        public Quaternion R_Ring_D_roatation;

        public Quaternion R_Little_P_rotation; //R-right,L-Little,P-Proximal

        public Quaternion R_Little_I_rotation;

        public Quaternion R_Little_D_roatation;


        // Use this for initialization

        void Start () {

            //获取角色的Animator组件

            animator = transform.GetComponent();

            //通过Animator组件获取右手手指的各个关节

            rightThumbProximal = animator.GetBoneTransform(HumanBodyBones.RightThumbProximal); 

            rightThumbIntermediate = animator.GetBoneTransform(HumanBodyBones.RightThumbIntermediate);

            rightThumbDistal = animator.GetBoneTransform(HumanBodyBones.RightThumbDistal);

            rightIndexProximal = animator.GetBoneTransform(HumanBodyBones.RightIndexProximal);

            rightIndexIntermediate = animator.GetBoneTransform(HumanBodyBones.RightIndexIntermediate);

            rightIndexDistal = animator.GetBoneTransform(HumanBodyBones.RightIndexDistal);

            rightMiddleProximal = animator.GetBoneTransform(HumanBodyBones.RightMiddleProximal);

            rightMiddleIntermediate = animator.GetBoneTransform(HumanBodyBones.RightMiddleIntermediate);

            rightMiddleDistal = animator.GetBoneTransform(HumanBodyBones.RightMiddleDistal);

            rightRingProximal = animator.GetBoneTransform(HumanBodyBones.RightRingProximal);

            rightRingIntermediate = animator.GetBoneTransform(HumanBodyBones.RightRingIntermediate);

            rightRingDistal = animator.GetBoneTransform(HumanBodyBones.RightRingDistal);

            rightLittleProximal = animator.GetBoneTransform(HumanBodyBones.RightLittleProximal);

            rightLittleIntermediate = animator.GetBoneTransform(HumanBodyBones.RightLittleIntermediate);

            rightLittleDistal = animator.GetBoneTransform(HumanBodyBones.RightLittleDistal);

        }


        // Update is called once per frame

        void Update () {

            //将从数据手套获取到的旋转量赋值给相应的手指关节的localRotaion就可以了

            rightThumbProximal.localRotation= R_Thumb_P_rotation;

            rightThumbIntermediate.localRotation = R_Thumb_I_rotation;

            rightThumbDistal.localRotation = R_Thumb_D_roatation;

            rightIndexProximal.localRotation = R_Index_P_rotation;

            rightIndexIntermediate.localRotation = R_Index_I_rotation;

            rightIndexDistal.localRotation = R_Index_D_roatation;

            rightMiddleProximal.localRotation = R_Middle_P_rotation;

            rightMiddleIntermediate.localRotation = R_Middle_I_rotation;

            rightMiddleDistal.localRotation = R_Middle_D_roatation;

            rightRingProximal.localRotation = R_Ring_P_rotation;

            rightRingIntermediate.localRotation = R_Ring_I_rotation;

            rightRingDistal.localRotation = R_Ring_D_roatation;

            rightLittleProximal.localRotation = R_Little_P_rotation;

            rightLittleIntermediate.localRotation = R_Little_I_rotation;

            rightLittleDistal.localRotation = R_Little_D_roatation;


        }

    }

         Unity3d Avatar右手各关节的位置以及名称如下图:


         配置好角色并挂载好代码后,运行场景,效果如下图所示:WiseGlove数据手套可以非常完美的驱动角色的右手各个手指的关节。