/// <summary> /// Updates the devices. /// </summary> private void UpdateDevices() { // Cache HMD CVRSystem hmd = Hmd; // Get indexes of left and right controllers LeftControllerIndex = -1; RightControllerIndex = -1; int lhIndex = (int)hmd.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand); int rhIndex = (int)hmd.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand); controllers.Clear(); // Update all tracked devices for (uint i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++) { TrackedDevicePose_t pose = renderPoses[i]; // We are interested in valid and connected devices only if (pose.bDeviceIsConnected && pose.bPoseIsValid) { pose.ToVRPose(out poses[i]); ETrackedDeviceClass c = hmd.GetTrackedDeviceClass(i); // Update controller if (c == ETrackedDeviceClass.Controller) { VRControllerState_t state_t = default(VRControllerState_t); if (hmd.GetControllerState(i, ref state_t, (uint)Marshal.SizeOf(state_t))) { VRControllerRole role; if (i == lhIndex) { role = VRControllerRole.LeftHand; LeftControllerIndex = controllers.Count; } else if (i == rhIndex) { role = VRControllerRole.RightHand; RightControllerIndex = controllers.Count; } else { role = VRControllerRole.Undefined; } VRControllerState state = new VRControllerState(); state.Update(role, ref state_t, ref poses[i]); controllers.Add(state); } } // Update generic reference (base station etc...) else if (c == ETrackedDeviceClass.TrackingReference) { VRTrackingReference reference = new VRTrackingReference(); reference.Update(poses[i]); trackingReferences.Add(reference); } } } // Convert to array trackingReferencesArray = trackingReferences.ToArray(); controllersArray = controllers.ToArray(); }