unity使用opentrack进行头部捕捉
一、介绍
1、opentrack
Opentrack是一款致力于追踪用户头部运动并将信息传递给游戏和飞行模拟软件的应用程序。
2、Freetrack
FreeTrack是一款类似于Track-IR™的免费软件(FreeTrack与大多数使用该技术的游戏兼容),可以在游戏和模拟器中模拟头部运动,以增加乐趣,真实感和沉浸感。
它使得在视频游戏或模拟器中控制你的视角成为可能,只需轻微的头部运动。还有什么比把脸转向后视镜看后视镜更自然的呢?还有什么比低头检查塞斯纳飞机前灯开关更简单的呢?
沉浸感确实得到了提升,你在赛车游戏中对其他车辆有了更好的认识,你在飞行模拟器中执行了更漂亮的方法……
二、使用
1、安装opentrack与Freetrack
opentrack下载地址:https://github.com/opentrack/opentrack/releases
Freetrack下载地址:https://www.free-track.net/english/freetrack/telechargement.php
都正常安装,一溜下一步就行。
2、准备DLL文件
去freetrack安装目录(C:\Program Files (x86)\Abbequerque Inc\FaceTrackNoIR_v200)找FreeTrackClient.dll和FreeTrackClient64.dll两个文件,复制到unity工作目录中找个地方放
3、代码
FreeTrackClientDll.cs
using System;
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class FreeTrackClientDll : MonoBehaviour
{
[StructLayout(LayoutKind.Sequential)]
public struct FreeTrackData
{
public int dataid;
public int camwidth, camheight;
public Single Yaw, Pitch, Roll, X, Y, Z;
public Single RawYaw, RawPitch, RawRoll;
public Single RawX, RawY, RawZ;
public Single x1, y1, x2, y2, x3, y3, x4, y4;
}
[DllImport("FreeTrackClient64")] // 如果需要32位就用FreeTrackClient.dll
public static extern bool FTGetData(ref FreeTrackData data);
[DllImport("FreeTrackClient64")]
public static extern string FTGetDllVersion();
[DllImport("FreeTrackClient64")]
public static extern void FTReportID(Int32 name);
[DllImport("FreeTrackClient64")]
public static extern string FTProvider();
FreeTrackData FreeTrackDataRef;
public float Yaw = 0F;
public float Pitch = 0F;
public float Roll = 0F;
public float X = 0F;
public float Y = 0F;
public float Z = 0F;
public float RawYaw = 0F;
public float RawPitch = 0F;
public float RawRoll = 0F;
public float RawX = 0F;
public float RawY = 0F;
public float RawZ = 0F;
public float x1 = 0F;
public float y1 = 0F;
public float x2 = 0F;
public float y2 = 0F;
public float x3 = 0F;
public float y3 = 0F;
public float x4 = 0F;
public float y4 = 0F;
void Start()
{
FreeTrackDataRef = new FreeTrackClientDll.FreeTrackData();
}
void Update()
{
if (!FreeTrackClientDll.FTGetData(ref FreeTrackDataRef))
{
Debug.Log("FTGetData returned false. FreeTrack likely not working.");
return;
}
FreeTrackClientDll.FTGetData(ref FreeTrackDataRef);
Yaw = FreeTrackDataRef.Yaw;
Pitch = FreeTrackDataRef.Pitch;
Roll = FreeTrackDataRef.Roll;
X = FreeTrackDataRef.X;
Y = FreeTrackDataRef.Y;
Z = FreeTrackDataRef.Z;
RawYaw = FreeTrackDataRef.RawYaw;
RawPitch = FreeTrackDataRef.RawPitch;
RawRoll = FreeTrackDataRef.RawRoll;
RawX = FreeTrackDataRef.RawX;
RawY = FreeTrackDataRef.RawY;
RawZ = FreeTrackDataRef.RawZ;
x1 = FreeTrackDataRef.x1;
y1 = FreeTrackDataRef.y1;
x2 = FreeTrackDataRef.x2;
y2 = FreeTrackDataRef.y2;
x3 = FreeTrackDataRef.x3;
y3 = FreeTrackDataRef.y3;
x4 = FreeTrackDataRef.x4;
y4 = FreeTrackDataRef.y4;
}
}
TranslateToCube.cs
using UnityEngine;
using System.Collections;
public class TranslateToCube : MonoBehaviour
{
public FreeTrackClientDll FTinput;
public Transform Target;
public float PositionSpeed = 0.001f;
Vector3 startPos = Vector3.zero;
public float RotationSpeed = 60F;
void Start()
{
FTinput = this.GetComponent<FreeTrackClientDll>();
startPos = Target.position;
}
void Update()
{
if (Target)
{
Target.position = startPos + new Vector3(-FTinput.X * PositionSpeed, FTinput.Y * PositionSpeed, -FTinput.Z * PositionSpeed);
Target.rotation = Quaternion.Euler(-FTinput.Pitch * RotationSpeed, -FTinput.Yaw * RotationSpeed, FTinput.Roll * RotationSpeed);
}
else return;
}
}
4、使用
将FreeTrackClientDll.cs和TranslateToCube.cs挂到需要控制的物体上
然后正确选择和填充cs和TranslateToCube的公共属性
F Tinput选择挂载了FreeTrackClientDll.cs的物体(按上文挂载是自身)
Target选择需要控制的物体 (按上文挂载是自身)
PositionSpeed是位置敏感度
RotationSpeed是旋转敏感度
5、运行
参考:
https://github.com/opentrack/opentrack
https://www.free-track.net/