// use -1 for absolute tracking space public static int GetDeviceIndex(DeviceRelation relation, TrackedDeviceClass deviceClass = TrackedDeviceClass.Controller, int relativeTo = (int)OpenVR.k_unTrackedDeviceIndex_Hmd) { var invXform = ((uint)relativeTo < OpenVR.k_unTrackedDeviceIndex_Hmd) ? Input(relativeTo).transform.GetInverse() : SteamVR_Utils.RigidTransform.identity; var vr = SteamVR.instance; var result = -1; var best = -float.MaxValue; var dir = (relation == DeviceRelation.Leftmost) ? Vector3.left : Vector3.right; for (int i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++) { if (i == relativeTo || vr.hmd.GetTrackedDeviceClass((uint)i) != deviceClass) continue; var device = Input(i); if (!device.connected) continue; if (relation == DeviceRelation.First) return i; var score = Vector3.Dot(dir, invXform * device.transform.pos); if (score > best) { result = i; best = score; } } return result; }
// use -1 for absolute tracking space public static int GetDeviceIndex(DeviceRelation relation, TrackedDeviceClass deviceClass = TrackedDeviceClass.Controller, int relativeTo = (int)OpenVR.k_unTrackedDeviceIndex_Hmd) { var invXform = ((uint)relativeTo < OpenVR.k_unMaxTrackedDeviceCount) ? Input(relativeTo).transform.GetInverse() : SteamVR_Utils.RigidTransform.identity; var vr = SteamVR.instance; var result = -1; var best = -float.MaxValue; for (int i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++) { if (i == relativeTo || vr.hmd.GetTrackedDeviceClass((uint)i) != deviceClass) continue; var device = Input(i); if (!device.connected) continue; if (relation == DeviceRelation.First) return i; float score; var pos = invXform * device.transform.pos; if (relation == DeviceRelation.FarthestRight) { score = pos.x; } else if (relation == DeviceRelation.FarthestLeft) { score = -pos.x; } else { var dir = new Vector3(pos.x, 0.0f, pos.z).normalized; var dot = Vector3.Dot(dir, Vector3.forward); var cross = Vector3.Cross(dir, Vector3.forward); if (relation == DeviceRelation.Leftmost) { score = (cross.y > 0.0f) ? 2.0f - dot : dot; } else { score = (cross.y < 0.0f) ? 2.0f - dot : dot; } } if (score > best) { result = i; best = score; } } return result; }
public static int GetDeviceIndex(DeviceRelation relation, TrackedDeviceClass deviceClass = TrackedDeviceClass.Controller, int relativeTo = (int)OpenVR.k_unTrackedDeviceIndex_Hmd) // use -1 for absolute tracking space { var invXform = ((uint)relativeTo < OpenVR.k_unTrackedDeviceIndex_Hmd) ? Input(relativeTo).transform.GetInverse() : SteamVR_Utils.RigidTransform.identity; var vr = SteamVR.instance; var result = -1; var best = -float.MaxValue; var dir = (relation == DeviceRelation.Leftmost) ? Vector3.left : Vector3.right; for (int i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++) { if (i == relativeTo || vr.hmd.GetTrackedDeviceClass((uint)i) != deviceClass) { continue; } var device = Input(i); if (!device.connected) { continue; } if (relation == DeviceRelation.First) { return(i); } var score = Vector3.Dot(dir, invXform * device.transform.pos); if (score > best) { result = i; best = score; } } return(result); }
internal static extern uint VR_IVRSystem_GetSortedTrackedDeviceIndicesOfClass(IntPtr instancePtr, TrackedDeviceClass eTrackedDeviceClass, [In, Out] uint[] punTrackedDeviceIndexArray, uint unTrackedDeviceIndexArrayCount, uint unRelativeToTrackedDeviceIndex);
public abstract uint GetSortedTrackedDeviceIndicesOfClass(TrackedDeviceClass eTrackedDeviceClass,uint [] punTrackedDeviceIndexArray,uint unRelativeToTrackedDeviceIndex);
public override uint GetSortedTrackedDeviceIndicesOfClass(TrackedDeviceClass eTrackedDeviceClass,uint [] punTrackedDeviceIndexArray,uint unRelativeToTrackedDeviceIndex) { CheckIfUsable(); uint result = VRNativeEntrypoints.VR_IVRSystem_GetSortedTrackedDeviceIndicesOfClass(m_pVRSystem,eTrackedDeviceClass,punTrackedDeviceIndexArray,(uint) punTrackedDeviceIndexArray.Length,unRelativeToTrackedDeviceIndex); return result; }
public static int GetDeviceIndex(DeviceRelation relation, TrackedDeviceClass deviceClass = TrackedDeviceClass.Controller, int relativeTo = (int)OpenVR.k_unTrackedDeviceIndex_Hmd) // use -1 for absolute tracking space { var invXform = ((uint)relativeTo < OpenVR.k_unMaxTrackedDeviceCount) ? Input(relativeTo).transform.GetInverse() : SteamVR_Utils.RigidTransform.identity; var vr = SteamVR.instance; var result = -1; var best = -float.MaxValue; for (int i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++) { if (i == relativeTo || vr.hmd.GetTrackedDeviceClass((uint)i) != deviceClass) { continue; } var device = Input(i); if (!device.connected) { continue; } if (relation == DeviceRelation.First) { return(i); } float score; var pos = invXform * device.transform.pos; if (relation == DeviceRelation.FarthestRight) { score = pos.x; } else if (relation == DeviceRelation.FarthestLeft) { score = -pos.x; } else { var dir = new Vector3(pos.x, 0.0f, pos.z).normalized; var dot = Vector3.Dot(dir, Vector3.forward); var cross = Vector3.Cross(dir, Vector3.forward); if (relation == DeviceRelation.Leftmost) { score = (cross.y > 0.0f) ? 2.0f - dot : dot; } else { score = (cross.y < 0.0f) ? 2.0f - dot : dot; } } if (score > best) { result = i; best = score; } } return(result); }