Skip to content

Instantly share code, notes, and snippets.

@inoook
Created December 28, 2021 09:11
Show Gist options
  • Save inoook/5c43297ec5b3a5d16bde44b94af60a2c to your computer and use it in GitHub Desktop.
Save inoook/5c43297ec5b3a5d16bde44b94af60a2c to your computer and use it in GitHub Desktop.
ViveTrackerManagement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
public class ViveTrackerManagement : MonoBehaviour {
// Use this for initialization
void Start () {
}
void OnEnable()
{
SteamVR_Events.Initializing.Listen(OnInitializing);
SteamVR_Events.Calibrating.Listen(OnCalibrating);
SteamVR_Events.OutOfRange.Listen(OnOutOfRange);
SteamVR_Events.DeviceConnected.Listen(OnDeviceConnected);
SteamVR_Events.NewPoses.Listen(OnNewPoses);
}
void OnDisable()
{
SteamVR_Events.Initializing.Remove(OnInitializing);
SteamVR_Events.Calibrating.Remove(OnCalibrating);
SteamVR_Events.OutOfRange.Remove(OnOutOfRange);
SteamVR_Events.DeviceConnected.Remove(OnDeviceConnected);
SteamVR_Events.NewPoses.Remove(OnNewPoses);
}
#region Event callbacks
private void OnInitializing(bool initializing)
{
Debug.Log("initializing: "+ initializing);
}
private void OnCalibrating(bool calibrating)
{
Debug.Log("calibrating: "+ calibrating);
}
private void OnOutOfRange(bool outOfRange)
{
Debug.Log("outOfRange: "+ outOfRange);
}
private void OnDeviceConnected(int i, bool connected)
{
Debug.Log("connected: "+ i+" : "+connected);
}
TrackedDevicePose_t[] devices;
private void OnNewPoses(TrackedDevicePose_t[] poses)
{
devices = poses;
}
#endregion
// Update is called once per frame
void Update () {
}
[SerializeField]
Rect drawRect = new Rect(10,10,200,200);
[SerializeField]
float width0 = 200;
[SerializeField]
float width1 = 200;
[SerializeField]
float width2 = 200;
void OnGUI()
{
GUILayout.BeginArea (drawRect);
GUILayout.BeginVertical ("box");
var system = OpenVR.System;
if (system == null) {
GUILayout.Label ("Error");
} else {
for (int i = 0; i < devices.Length; i++) {
TrackedDevicePose_t dev = devices [i];
string deviceName = "unknown";
if (dev.bDeviceIsConnected) {
deviceName = GetModelNameById (system, i); // name
}
if (dev.eTrackingResult != 0) {
SteamVR_Utils.RigidTransform pose = new SteamVR_Utils.RigidTransform (dev.mDeviceToAbsoluteTracking);
float per = GetBatteryPercentage (system, i);
string batt = "none";
if (per > 0) {
batt = (per * 100).ToString ("0") + "%";
}
// VRControllerState_t controllerState;
// system.GetControllerState ((uint)i, ref controllerState, 0);
ETrackedControllerRole role = system.GetControllerRoleForTrackedDeviceIndex ((uint)i);
uint index = system.GetTrackedDeviceIndexForControllerRole (role);
string activityLv = GetActivityLevel (system, i);
// GUILayout.Label (i + " / connect: " + dev.bDeviceIsConnected + " / Tracking: " + dev.eTrackingResult + " / Pose IsValid: " + dev.bPoseIsValid + " / Activity: " + activityLv + " / " + deviceName + " / " + batt + " / role: "+role);
// GUILayout.Label (i + " / Tracking: " + dev.eTrackingResult + " / Pose IsValid: " + dev.bPoseIsValid + " / Activity: " + activityLv + " / " + deviceName + " / " + batt + " / role: "+role);
GUILayout.BeginHorizontal ();
GUILayout.Label (i + " | Tracking: " + dev.eTrackingResult + " / Pose IsValid: " + dev.bPoseIsValid + " | " + deviceName, GUILayout.Width(width0));
GUILayout.Label (" | Activity: " + activityLv, GUILayout.Width(width1));
GUILayout.Label (" | " + batt + " | role: "+role, GUILayout.Width(width2));
GUILayout.EndHorizontal ();
}
}
}
GUILayout.EndVertical ();
GUILayout.EndArea ();
}
string GetModelNameById(CVRSystem system, int i)
{
string s = "";
var error = ETrackedPropertyError.TrackedProp_Success;
var capacity = system.GetStringTrackedDeviceProperty ((uint)i, ETrackedDeviceProperty.Prop_RenderModelName_String, null, 0, ref error);
if (capacity <= 1) {
return "error";
}
var buffer = new System.Text.StringBuilder ((int)capacity);
system.GetStringTrackedDeviceProperty ((uint)i, ETrackedDeviceProperty.Prop_RenderModelName_String, buffer, capacity, ref error);
s = buffer.ToString ();
return s;
}
string GetActivityLevel(CVRSystem system, int i){
EDeviceActivityLevel activeLevel = system.GetTrackedDeviceActivityLevel ((uint)i);
return activeLevel.ToString ().Substring (23).ToString ();
}
float GetBatteryPercentage(CVRSystem system, int i)
{
ETrackedPropertyError error = ETrackedPropertyError.TrackedProp_Success;
float capacity = system.GetFloatTrackedDeviceProperty((uint)i, ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float, ref error);
if (error != ETrackedPropertyError.TrackedProp_Success) {
return -1;
}
return capacity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment