首先,感恩大神的分享,没有前人之路,后来者想要乘凉就难了。
借鉴大神的地址:http://www.gad.qq.com/article/detail/27127
一、准备
1.设备
HTC Vive追踪器(Tracker X3,Controller X2):一套HTC Vive设备有两个手柄和一个追踪器,再订两个追踪器即可
2.插件
①Unity的 Assets Store里面插件,搜索“Final IK”就能找到(直接买太贵,还好大神无私)
②当然,还需要在 Assets Store里下载SteamVR Plugin这个插件(接触过VR的应该都知道)
二、思路
1.IK,也就是逆向运动学,这个插件用来保持关节弯曲的正确
2.怎么区分追踪器?
①HTC Vive能识别15个设备(定位器+追踪器),根据Valve.VR.ETrackedDeviceClass的实例判断设备类型,然后用SteamVR_TrackedObject.isValid判断设备是否可用。若可用,则获取其index。
②先根据Y轴方向上值的大小判断脚1、脚2,腰,手1,手2;然后以头盔为参照,若 脚1 - 脚2 的向量方向为头盔右方,则脚1为右脚,手同理。
3.怎么让模型动作同步?
不能直接把设备设为四个关节的运动目标。因为脚上和腰部的追踪器不可能与模型关节点重合,而且,旋转的角度也有一个差值。
新建五个物体,识别的时候让这五个物体的rotation为关节点的rotation,再将其设为追踪器的子节点。
三、实现
贴出识别追踪器的代码:
/*
* CheckTrackers/InitPositions的子对象为 手腕、脚踝、腰,将其拖到模型的相应位置上
* 使用协程CheckTrackers()开始检测追踪器,orderedList存储得出的部位index
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[DisallowMultipleComponent]
public class GetTrackers : MonoBehaviour
{
public Dictionary<int, Transform> trackerTrans = new Dictionary<int, Transform>();//存储追踪器index和transform
public List<int> orderedList = new List<int>();//存储追踪器的index
public Transform[] originBodyTrans = new Transform[5];//四肢+腰(跟随的目标)
public Transform bodyRightTrans;//得到头盔右向坐标,以区分左右追踪器
[SerializeField] private GameObject trackers;//父节点,获取追踪器
//顺序为: 右脚、左脚、腰、右手、左手
private SteamVR_TrackedObject[] trackerArray;
private Dictionary<int, Transform> devicesIndexArray = new Dictionary<int, Transform>();//存储识别的追踪器
public static GetTrackers instance;
// Use this for initialization
void Awake()
{
instance = this;
trackerArray = trackers.GetComponentsInChildren<SteamVR_TrackedObject>();
for (int i = 0; i < trackerArray.Length; i++)
{
trackerTrans[(int)(trackerArray[i].index)] = trackerArray[i].transform;//获取15个子节点的属性
}
}
/// <summary>
/// 开始检测追踪器
/// </summary>
/// <returns></returns>
public IEnumerator CheckTrackers()
{
while (true)
{
yield return new WaitForFixedUpdate();
for (int i = (int)SteamVR_TrackedObject.EIndex.Device1; i < (int)SteamVR_TrackedObject.EIndex.Device15; i++)
{
Valve.VR.ETrackedDeviceClass deviceClass = Valve.VR.OpenVR.System.GetTrackedDeviceClass((uint)i);
if (deviceClass == Valve.VR.ETrackedDeviceClass.Controller ||
deviceClass == Valve.VR.ETrackedDeviceClass.GenericTracker)//手柄或追踪器
{
for (int m = 0; m < trackerArray.Length; m++)
{
if ((int)trackerArray[m].index == i && trackerArray[m].isValid)
{
if (!devicesIndexArray.ContainsKey(i))
{
devicesIndexArray.Add(i, trackerTrans[i]);//添加到字典
orderedList.Add(i);//将key的顺序添加到数组
Debug.Log(deviceClass.ToString() + i + ": " + trackerTrans[i].position.y);
}
if (devicesIndexArray.Count == 5)//若有五个追踪器
{
yield return StartCoroutine(BindingTrickersToModel());//以位置区分追踪器
yield break;//结束循环
}
}
}
}
}
}
}
/// <summary>
/// 确定追踪器对应的关节
/// </summary>
/// <param name="devicesIndexArray">存储追踪器的index</param>
/// <returns></returns>
IEnumerator BindingTrickersToModel()
{
yield return null;
//根据Y,先区分手脚腰
//1、2脚 3腰 4、5手
for (int i = 0; i < orderedList.Count - 1; i++)
{
for (int j = orderedList.Count - 1; j > i; j--)
{
float y1 = devicesIndexArray[orderedList[i]].position.y;
float y2 = devicesIndexArray[orderedList[j]].position.y;
if (y1 > y2)
{
var temp = orderedList[i];
orderedList[i] = orderedList[j];
orderedList[j] = temp;
}
}
}
//水平方向向量
Vector3 footVector = devicesIndexArray[orderedList[0]].position - devicesIndexArray[orderedList[1]].position;
footVector.y = 0;
Vector3 handVector = devicesIndexArray[orderedList[3]].position - devicesIndexArray[orderedList[4]].position;
handVector.y = 0;
if (Vector3.Angle(new Vector3(bodyRightTrans.right.x, 0, bodyRightTrans.right.z), footVector) > 90)
{
//0 右 1 左
Debug.Log("0<-->1");
var temp = orderedList[0];
orderedList[0] = orderedList[1];
orderedList[1] = temp;
}
if (Vector3.Angle(new Vector3(bodyRightTrans.right.x, 0, bodyRightTrans.right.z), handVector) > 90)
{
//3 右 4 左
Debug.Log("3<-->4");
var temp = orderedList[3];
orderedList[3] = orderedList[4];
orderedList[4] = temp;
}
Debug.LogFormat("fr:{0},fl:{1},t:{2},hr:{3},hl:{4}",
orderedList[0], orderedList[1], orderedList[2], orderedList[3], orderedList[4]);
devicesIndexArray.Clear();//清空字典
}
}