Skip to content

Commit 9d73ebb

Browse files
committed
Add VirtualMotionTracker support.
1 parent 0364c4b commit 9d73ebb

57 files changed

Lines changed: 10501 additions & 97 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Assets/ExternalPlugins/VirtualMotionTracker.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/VMTClient.cs renamed to Assets/ExternalPlugins/VirtualMotionTracker/VMTClient.cs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@
33
using System.Collections.Generic;
44
using UnityEngine;
55
using uOSC;
6+
using Valve.VR;
67

7-
public class VMTClient : MonoBehaviour {
8+
public class VMTClient : MonoBehaviour
9+
{
810
private int vitrualTrackerNo = 50; //トラッカー番号(他と被らなさそうな適当な番号)
911
public ControlWPFWindow window;
1012

1113
public bool sendEnable = false;
1214
uOscClient client = null;
1315

14-
void Start () {
16+
void Start()
17+
{
1518
client = GetComponent<uOscClient>();
16-
}
19+
}
1720

18-
public int GetNo() {
21+
public int GetNo()
22+
{
1923
return vitrualTrackerNo;
2024
}
21-
public void SetNo(int no) {
25+
public void SetNo(int no)
26+
{
2227
if (sendEnable == true)
2328
{
2429
disable();
@@ -33,15 +38,18 @@ public bool GetEnable()
3338
}
3439
public void SetEnable(bool en)
3540
{
36-
if(sendEnable == true && en == false)
41+
if (sendEnable == true && en == false)
3742
{
3843
disable();
3944
}
4045

4146
sendEnable = en;
4247
}
4348

44-
void Update () {
49+
private float lastRoomMatrixSendTime = 0;
50+
51+
void Update()
52+
{
4553
if (client == null || sendEnable == false)
4654
{
4755
return;
@@ -52,15 +60,27 @@ void Update () {
5260
if (target != null)
5361
{
5462
//enable=1
55-
client.Send("/VMT/Room/Unity", (int)vitrualTrackerNo, (int)(enable?1:0), (float)0f,
56-
(float)target.position.x,
57-
(float)target.position.y,
58-
(float)target.position.z,
59-
(float)target.rotation.x,
60-
(float)target.rotation.y,
61-
(float)target.rotation.z,
62-
(float)target.rotation.w
63+
client.Send("/VMT/Room/Unity", (int)vitrualTrackerNo, (int)(enable ? 1 : 0), (float)0f,
64+
(float)target.localPosition.x,
65+
(float)target.localPosition.y,
66+
(float)target.localPosition.z,
67+
(float)target.localRotation.x,
68+
(float)target.localRotation.y,
69+
(float)target.localRotation.z,
70+
(float)target.localRotation.w
6371
);
72+
73+
if (lastRoomMatrixSendTime + 5f < Time.realtimeSinceStartup)
74+
{
75+
lastRoomMatrixSendTime = Time.realtimeSinceStartup;
76+
77+
HmdMatrix34_t m = new HmdMatrix34_t();
78+
OpenVR.ChaperoneSetup.GetWorkingStandingZeroPoseToRawTrackingPose(ref m);
79+
client.Send("/VMT/SetRoomMatrix/Temporary",
80+
m.m0, m.m1, m.m2, m.m3,
81+
m.m4, m.m5, m.m6, m.m7,
82+
m.m8, m.m9, m.m10, m.m11);
83+
}
6484
}
6585
}
6686

Assets/VMTClient.cs.meta renamed to Assets/ExternalPlugins/VirtualMotionTracker/VMTClient.cs.meta

File renamed without changes.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+

2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Runtime.InteropServices;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using UnityEngine;
9+
using Valve.VR;
10+
11+
public class VMTServer : MonoBehaviour
12+
{
13+
14+
private static string driverVersion = null;
15+
private static string installPath = null;
16+
17+
private void Start()
18+
{
19+
var server = GetComponent<uOSC.uOscServer>();
20+
server.onDataReceived.AddListener(OnDataReceived);
21+
}
22+
23+
24+
void OnDataReceived(uOSC.Message message)
25+
{
26+
//有効なとき以外処理しない
27+
if (this.isActiveAndEnabled)
28+
{
29+
//仮想コントローラー V2.3
30+
if (message.address == "/VMT/Out/Alive"
31+
&& (message.values[0] is string)
32+
)
33+
{
34+
driverVersion = (string)message.values[0];
35+
if (message.values.Length > 1 && message.values[1] is string)
36+
{
37+
installPath = (string)message.values[1];
38+
}
39+
}
40+
}
41+
}
42+
43+
public static async Task<string> InstallVMT()
44+
{
45+
if (string.IsNullOrEmpty(driverVersion) == false)
46+
{
47+
return "Please uninstall VMT before install.\nインストールを続ける前に、VMTをアンインストールしてください";
48+
}
49+
50+
try
51+
{
52+
string driverPath_rel = @"C:\VirtualMotionTracker\vmt";
53+
string driverPath = System.IO.Path.GetFullPath(driverPath_rel);
54+
var runtimePath = OpenVR.RuntimePath();
55+
56+
System.Diagnostics.Process process = new System.Diagnostics.Process();
57+
process.StartInfo.WorkingDirectory = runtimePath + @"\bin\win64";
58+
process.StartInfo.FileName = runtimePath + @"\bin\win64\vrpathreg.exe";
59+
process.StartInfo.Arguments = "adddriver \"" + driverPath + "\"";
60+
process.StartInfo.UseShellExecute = false;
61+
await Task.Run(() =>
62+
{
63+
process.Start();
64+
process.WaitForExit();
65+
});
66+
}
67+
catch (Exception ex)
68+
{
69+
return "Error:" + ex.Message + "\n" + ex.StackTrace;
70+
}
71+
return null;
72+
}
73+
74+
public static async Task<string> UninstallVMT()
75+
{
76+
string driverPath_rel = @"C:\VirtualMotionTracker\vmt";
77+
string driverPath = System.IO.Path.GetFullPath(driverPath_rel);
78+
79+
if (string.IsNullOrEmpty(installPath))
80+
{
81+
//インストールパスが受信できていない場合少し待つ
82+
await Task.Delay(2000);
83+
}
84+
85+
if (string.IsNullOrEmpty(installPath) == false)
86+
{
87+
//場所がわかっている
88+
driverPath = installPath;
89+
}
90+
91+
try
92+
{
93+
var runtimePath = OpenVR.RuntimePath();
94+
System.Diagnostics.Process process = new System.Diagnostics.Process();
95+
process.StartInfo.WorkingDirectory = runtimePath + @"\bin\win64";
96+
process.StartInfo.FileName = runtimePath + @"\bin\win64\vrpathreg.exe";
97+
process.StartInfo.Arguments = "removedriver \"" + driverPath + "\"";
98+
process.StartInfo.UseShellExecute = false;
99+
await Task.Run(() =>
100+
{
101+
process.Start();
102+
process.WaitForExit();
103+
});
104+
}
105+
catch (Exception ex)
106+
{
107+
return "Error:" + ex.Message + "\n" + ex.StackTrace;
108+
}
109+
return null;
110+
}
111+
112+
}

Assets/ExternalPlugins/VirtualMotionTracker/VMTServer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)