/// <summary> /// Updates the devices. /// </summary> public override void UpdateDevices() { // Get indexes of left and right controllers _leftControllerIndex = -1; _rightControllerIndex = -1; int lhIndex = (int)_vrSystem.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand); int rhIndex = (int)_vrSystem.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand); _controllers.Clear(); _trackingReferences.Clear(); // Update all tracked devices for (uint i = 0; i < OVR.k_unMaxTrackedDeviceCount; i++) { TrackedDevicePose_t pose = _devicePoses[i]; // We are interested in valid and connected devices only if (pose.bDeviceIsConnected && pose.bPoseIsValid) { ToVRPose(pose, out Vector3 posePosition, out Quaternion poseOrientation); ETrackedDeviceClass c = _vrSystem.GetTrackedDeviceClass(i); // Update controller if (c == ETrackedDeviceClass.Controller) { VRControllerState_t state_t = default(VRControllerState_t); if (_vrSystem.GetControllerState(i, ref state_t, (uint)System.Runtime.InteropServices.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, posePosition, poseOrientation); _controllers.Add(state); } } // Update generic reference (base station etc...) else if (c == ETrackedDeviceClass.TrackingReference) { VRTrackingReference reference = new VRTrackingReference(); reference.Update(posePosition, poseOrientation); _trackingReferences.Add(reference); } } } }
//NOTE: Late update used bc. OpenVR is updated during Update() and this way we ensure, that we have latest data and we're not frame behind private void LateUpdate() { if (VRSystem.Instance == null || !VRSystem.Instance.IsConnected) { return; } VREye[] eyes = VRSystem.Instance.EyesProperties; VRPose pose = VRSystem.Instance.TrackerCameraPose; UpdateCamera(LeftEyeCamera, 0); UpdateCamera(RightEyeCamera, 1); var transform = TrackerAnchor.LocalTransform; transform.Translation = pose.Position; transform.Orientation = pose.Orientation; TrackerAnchor.LocalTransform = transform; foreach (VRController controller in vrControllers) { VRControllerRole role = controller.Role; int cIndex; if (role == VRControllerRole.LeftHand) { cIndex = VRSystem.Instance.LeftControllerIndex; } else if (role == VRControllerRole.RightHand) { cIndex = VRSystem.Instance.RightControllerIndex; } else { cIndex = controller.ControllerIndex; } VRControllerState[] states = VRSystem.Instance.Controllers; VRControllerState newState; if (cIndex >= 0 && states != null && cIndex < states.Length) { newState = states[cIndex]; } else { newState = new VRControllerState(); } controller.UpdateState(newState); } VRPose centerPose = eyes[2].Pose; transform = CenterEyeAnchor.LocalTransform; transform.Translation = centerPose.Position; transform.Orientation = centerPose.Orientation; CenterEyeAnchor.LocalTransform = transform; }
internal void UpdateState(VRControllerState newState) { State = newState; if (State.IsConnected) { var transform = LocalTransform; transform.Translation = State.Pose.Position; transform.Orientation = State.Pose.Orientation; LocalTransform = transform; } }