private static void ControllerMovementDirection(ILContext il) { ILCursor c = new ILCursor(il); c.GotoNext(x => x.MatchStloc(5)); c.EmitDelegate <Func <Transform, Transform> >((headTransform) => { if (!ModConfig.ControllerMovementDirection.Value) { return(headTransform); } if (MotionControls.HandsReady) { // If controller movement tracking is enabled, replace the base camera transform with the controller transform. return(MotionControls.GetHandByDominance(false).muzzle.transform); } else { // See below for handling of the case where hands are not ready. return(headTransform); } }); c.GotoNext(x => x.MatchLdloca(6)); c.GotoNext(x => x.MatchLdloca(6)); c.Emit(OpCodes.Ldloc_S, (byte)6); c.EmitDelegate <Func <Vector2, Vector2> >((vector) => { if (!ModConfig.ControllerMovementDirection.Value || MotionControls.HandsReady) { return(vector); } // Special case only for if hands are not ready; in this case, we can't assume a hand transform exists, so we fall back to old logic. // Note: this will only provide y-axis rotation (yaw); z-axis rotation (pitch) will still be controlled by the head. Quaternion controllerRotation = InputTracking.GetLocalRotation(XRNode.LeftHand); Quaternion headRotation = Camera.main.transform.localRotation; float angleDifference = headRotation.eulerAngles.y - controllerRotation.eulerAngles.y; return(Quaternion.Euler(new Vector3(0, 0, angleDifference)) * vector); }); c.Emit(OpCodes.Stloc_S, (byte)6); c.GotoNext(x => x.MatchCallvirt <Transform>("get_right")); c.Index += 1; c.EmitDelegate <Func <Vector3, Vector3> >((vector) => { if (!ModConfig.ControllerMovementDirection.Value || !MotionControls.HandsReady) { return(vector); } // In flight mode, clamp the controller's left-right vector to the xz plane (normal to Vector3.up) so that only pitch and yaw are affected, not roll. return(Vector3.ProjectOnPlane(vector, Vector3.up).normalized *vector.magnitude); }); }
private static Ray GetVRCrosshairRaycastRay(On.RoR2.CameraModes.CameraModePlayerBasic.orig_GetCrosshairRaycastRay orig, RoR2.CameraModes.CameraModePlayerBasic self, ref RoR2.CameraModes.CameraModeBase.CameraModeContext context, Vector2 crosshairOffset, Vector3 raycastStartPlanePoint, ref CameraState cameraState) { if (MotionControls.HandsReady) { return(MotionControls.GetHandByDominance(true).aimRay); } Camera sceneCam = context.cameraInfo.sceneCam; if (!sceneCam) { return(default(Ray)); } float fov = sceneCam.fieldOfView; float num = fov * sceneCam.aspect; Quaternion quaternion = Quaternion.Euler(crosshairOffset.y * fov, crosshairOffset.x * num, 0f); quaternion = sceneCam.transform.rotation * quaternion; return(new Ray(Vector3.ProjectOnPlane(sceneCam.transform.position - raycastStartPlanePoint, sceneCam.transform.rotation * Vector3.forward) + raycastStartPlanePoint, quaternion * Vector3.forward)); }
private static void EditPointerPosition(On.RoR2.UI.MPInput.orig_Update orig, MPInput self) { if (!cachedUICam || !Utils.isUsingUI || Utils.localInputPlayer == null || Utils.localInputPlayer.controllers.GetLastActiveController() == null || Utils.localInputPlayer.controllers.GetLastActiveController().type == Rewired.ControllerType.Joystick) { if (cursorInstance && cursorInstance.activeSelf) { cursorInstance.SetActive(false); } orig(self); return; } if (!cursorInstance && cursorPrefab) { cursorInstance = GameObject.Instantiate(cursorPrefab); } self.internalScreenPositionDelta = Vector2.zero; self._scrollDelta = new Vector2(0f, self.player.GetAxis(26)); if (self.GetMouseButtonDown(0)) { clickTime = Time.realtimeSinceStartup; clickPosition = pointerHitPosition; } else if (self.GetMouseButtonUp(0)) { clickTime = 0; } Camera uiCam = cachedUICam; Ray ray; if (MotionControls.HandsReady) { ray = MotionControls.GetHandByDominance(true).uiRay; } else { ray = new Ray(uiCam.transform.position, uiCam.transform.forward); } RaycastHit hit; if (Physics.Raycast(ray, out hit, 100, LayerIndex.ui.mask)) { pointerHitPosition = hit.point; if (cursorInstance) { if (!cursorInstance.activeSelf) { cursorInstance.SetActive(true); } cursorInstance.transform.position = pointerHitPosition; cursorInstance.transform.rotation = Quaternion.LookRotation(hit.normal, Vector3.up); } lastHitCanvas = hit.collider.GetComponent <Canvas>(); } else if (cursorInstance.activeSelf) { cursorInstance.SetActive(false); } Vector3 mousePosition = uiCam.WorldToScreenPoint(pointerHitPosition); if (Time.realtimeSinceStartup - clickTime > 0.25f || (pointerHitPosition - clickPosition).magnitude > 1f) { self.internalMousePosition = mousePosition; } }