wiseglove數據手套驅動unity3D遊戲角色右手模型關節
目前unity3D遊戲引擎已經廣泛的用於遊戲(xì)開(kāi)發,而且unity3d在國內(nèi)發展比較迅速(sù),已(yǐ)經成(chéng)為(wéi)了主流的(de)遊(yóu)戲開發引擎(qíng)之一(yī)。隨著越來越多的開發人員開始使(shǐ)用unity3D,網絡上unity3D的中文學習資(zī)料也逐漸豐富。為了方便客戶使用wiseglove數據手套,我們專(zhuān)門組織編寫了在Unity3D環境下調用wiseglove數據手(shǒu)套SDK開發包,用數據手套的實時數據來驅動unity3d中的角色右手模型的demo程序。
Unity3D的新版動畫係統Mecanim已經對人類類型的角色支援設計了一套(tào)殊的工作流程。用戶將(jiāng)3dsmax或者maya中導(dǎo)入的人形角色導(dǎo)入unity3d後,需要為角色創建Avatar,本質上就(jiù)是分析導入資(zī)源(yuán)的骨骼結構,並對其進行標識,從而轉化成Mecanim可以(yǐ)識別的骨骼結構,或者說轉化(huà)成(chéng)通用的骨骼結構,這也是為什麽在資源準備時在骨骼的創建及命名要遵循一定的規範的(de)原因,這(zhè)樣方便mecanim對骨(gǔ)骼的識別(bié)。
在導入的(de)資源都具(jù)有通用的骨骼結構(gòu)時,就(jiù)可以實現(xiàn)動畫的共(gòng)用(yòng)。
在這裏我們用wiseGlove數(shù)據手套驅動右手模型時也使用了unity標準的(de)avatar映射的人手關節模型,這樣(yàng)方便我們對不(bú)同的角色(sè)的(de)右手模型進行驅動。
下麵(miàn)是用於驅動人手模型(xíng)的代碼,需要將這段代碼掛載在場(chǎng)景中的角色身上:
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.
//將從數據(jù)手套獲取到的各個手指(zhǐ)關節的Rotation賦值給下麵對應(yīng)的(de)Quaternion類型(xíng)的公用變量,
//就可以實現手指關節的運動
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組(zǔ)件
animator = transform.GetComponent();
//通過Animator組件獲取右手手指的各個關(guān)節(jiē)
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 () {
//將從數(shù)據手套獲取到的旋轉量賦值(zhí)給相應的(de)手指關節的localRotaion就(jiù)可以了
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;
}
}
- 上(shàng)一篇:虛擬(nǐ)現實(shí)開發(fā)引擎Unity3D與unreal比較 2017/3/10
- 下一篇:WiseGlove可(kě)穿戴戰術智能數據手套 2017/2/19