using System.Collections.Generic; using jeanf.validationTools; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.InputSystem; using UnityEngine.XR.Interaction.Toolkit; using UnityEngine.XR.Interaction.Toolkit.Interactors; namespace jeanf.universalplayer { /// /// Desktop counterpart of the VR grab preview: the reticle tints while the player /// aims at something usable, and flashes when they interact. /// /// EVERY source must answer about the AIM POINT — where the cursor actually is — /// not about the camera. Those are the same ray only while the cursor is LOCKED /// (the reticle is pinned to screen centre, so centre IS the cursor). In /// free-cursor mode (tablet/menu) the cursor roams and the two diverge, so a /// camera-forward source reports whatever is parked at screen centre regardless of /// where the player is pointing — which left the cursor stuck on its hover colour /// for as long as anything usable sat at centre. Hence the split below. /// /// Hover sources, any of which tints: /// 1. XRI interactor hover (grabbables, chairs, switches) — LOCKED ONLY: it comes /// from the gaze ray, which TrackedPoseSchemeGate pins camera-forward, so it /// cannot be aimed at a free cursor. /// 2. the gaze ray's UI raycast (world-space UI elements) — LOCKED ONLY, same reason. /// 3. the DesktopWorldUiInteractor's hover (world-space UI): already casts through /// the aim point in both modes, so it is the free-cursor UI source. /// 4. a plain physics raycast against — the same /// pattern project click handlers use (e.g. uvs's UIClickHandler → /// Highlight_Interactionable), cast through the aim point rather than /// camera-forward so it follows a free cursor. Set the SAME LayerMask on the /// Player variant as the project's click handler uses; layers are /// project-specific so the packaged default is empty (source disabled). /// Colours come from the CursorStateController's CursorPaletteSO (hover / click / /// resting) — one palette for the reticle, the pointing ray and the interaction ray. /// M&K and gamepad modes only. /// public class ReticleHoverFeedback : MonoBehaviour { private const string LogPrefix = "[UniversalPlayer]"; [Header("Physics hover (project click-handler parity)")] [Tooltip("Layers holding the project's raycast interactables (uvs: the layers UIClickHandler raycasts). Empty = this hover source is off.")] [SerializeField] private LayerMask physicsHoverMask; [SerializeField] private float physicsHoverDistance = 50f; [Tooltip("How far the reticle looks for XRI interactables/seats/pickables (a gaze interactor only hovers interactables that opt into allowGazeInteraction, so the reticle raycasts for them instead).")] [SerializeField] private float interactableHoverDistance = 3.5f; private readonly HashSet hovered = new HashSet(); private readonly List subscribedInteractors = new List(); private XRRayInteractor gazeRay; private Camera playerCamera; private InputAction interactAction; private InputAction takeAction; private bool interactHeld; private CursorStateController cursorColors; // the palette's carrier (resting/hover/click) private DesktopWorldUiInteractor worldUiInteractor; // world-canvas hover (tracks the reticle even off-center in tablet mode) private bool currentlyTinted; private bool currentlyFlashing; private bool missingCursorWarned; private void OnEnable() { playerCamera = Camera.main; cursorColors = GetComponentInParent() ?? FindAnyObjectByType(); worldUiInteractor = GetComponentInParent() ?? FindAnyObjectByType(); foreach (var interactor in GetComponentsInChildren(true)) { interactor.hoverEntered.AddListener(OnHoverEntered); interactor.hoverExited.AddListener(OnHoverExited); subscribedInteractors.Add(interactor); // The gaze rig (the one TrackedPoseSchemeGate keeps camera-aligned on // desktop) is the ray that hits UI in M&K/gamepad mode. if (gazeRay == null && interactor is XRRayInteractor ray && ray.GetComponentInParent() != null) { gazeRay = ray; } } if (gazeRay == null) { Debug.LogWarning($"{LogPrefix} ReticleHoverFeedback on '{name}': no gaze XRRayInteractor found (the ray " + "under a TrackedPoseSchemeGate) — UI hover cannot tint the reticle in M&K/gamepad, only interactable " + "hover will. Is the Gaze Interactor piece present on this Player (variant)?", this); } var playerInput = GetComponentInParent() ?? GetComponentInChildren(true); if (playerInput != null && playerInput.actions != null) { interactAction = playerInput.actions.FindAction("FPS/Interact", throwIfNotFound: false); takeAction = playerInput.actions.FindAction("FPS/TakeObject", throwIfNotFound: false); if (interactAction != null) { interactAction.performed += OnInteractPerformed; interactAction.canceled += OnInteractCanceled; } if (takeAction != null) { takeAction.performed += OnInteractPerformed; takeAction.canceled += OnInteractCanceled; } } } private void OnDisable() { foreach (var interactor in subscribedInteractors) { if (interactor == null) continue; interactor.hoverEntered.RemoveListener(OnHoverEntered); interactor.hoverExited.RemoveListener(OnHoverExited); } subscribedInteractors.Clear(); hovered.Clear(); if (interactAction != null) { interactAction.performed -= OnInteractPerformed; interactAction.canceled -= OnInteractCanceled; } if (takeAction != null) { takeAction.performed -= OnInteractPerformed; takeAction.canceled -= OnInteractCanceled; } interactHeld = false; // Never leave the reticle stranded pressed (shrunk) if we are torn down or // the scheme switches while the button is still down. if (cursorColors != null) cursorColors.SetClickHeld(false); Apply(false, false); } private void OnHoverEntered(HoverEnterEventArgs args) => hovered.Add(args.interactableObject); private void OnHoverExited(HoverExitEventArgs args) => hovered.Remove(args.interactableObject); // Telemetry for the F8 overlay. Four independent sources can tint the reticle, // and if ANY of them is stuck on it never returns to its resting colour — so the // overlay reports each one separately instead of just the final verdict. internal bool DebugTinted => currentlyTinted; internal bool DebugInteractHeld => interactHeld; internal int DebugXriHoverCount => hovered.Count; internal bool DebugGazeRayUiHover => CursorLocked && gazeRay != null && gazeRay.TryGetCurrentUIRaycastResult(out var h) && IsRealUi(h); /// What the gaze ray's UI raycast returned — and whether it was a real UI element or a blocker sentinel. internal string DebugGazeRayUiDescription() { if (gazeRay == null) return ""; if (!CursorLocked) return "cursor FREE — gaze sources stood down (they only see screen centre)"; if (!gazeRay.TryGetCurrentUIRaycastResult(out var hit) || hit.gameObject == null) return "no UI hit"; var raycaster = hit.module != null ? hit.module.GetType().Name : "?"; return IsRealUi(hit) ? $"'{hit.gameObject.name}' via {raycaster} at {hit.distance:0.##}m -> TINTS" : $"'{hit.gameObject.name}' via {raycaster} — BLOCKER sentinel (the raycaster's own object, not UI) -> ignored"; } internal bool DebugWorldUiHover => worldUiInteractor != null && (worldUiInteractor.HasUiHover || worldUiInteractor.HasAimClickTarget); /// Which half of the world-UI source answered — they mean different /// things (owned click vs. any clickable at the aim point). internal string DebugWorldUiDescription() { if (worldUiInteractor == null) return ""; var owned = worldUiInteractor.HasUiHover ? $"owned '{worldUiInteractor.CurrentHoverTarget.name}'" : "owned "; var clickable = worldUiInteractor.HasAimClickTarget ? $"clickable-at-aim '{worldUiInteractor.AimClickTarget.name}' -> TINTS" : "clickable-at-aim "; return $"{owned}; {clickable}"; } internal bool DebugPhysicsHover => PhysicsHover(); internal LayerMask DebugPhysicsMask => physicsHoverMask; internal float DebugPhysicsDistance => physicsHoverDistance; internal float DebugInteractableDistance => interactableHoverDistance; /// What the two physics hover rays actually hit right now, and why it counts as a hover. internal string DebugPhysicsHitDescription() { if (playerCamera == null) playerCamera = Camera.main; if (playerCamera == null) return ""; var ray = AimRay(); var text = CursorLocked ? "aim: screen centre (cursor locked)\n " : $"aim: cursor at {AimScreenPoint()} (cursor free)\n "; if (physicsHoverMask.value != 0 && Physics.Raycast(ray, out var masked, physicsHoverDistance, physicsHoverMask)) { var held = IsInOurOwnHand(masked.collider) ? " (IN HAND -> ignored)" : " -> TINTS"; text += $"maskRay hit '{masked.collider.name}' layer '{LayerMask.LayerToName(masked.collider.gameObject.layer)}' " + $"at {masked.distance:0.##}m{held}"; } else text += physicsHoverMask.value == 0 ? "maskRay: " : "maskRay: no hit"; if (Physics.Raycast(ray, out var hit, interactableHoverDistance)) { var what = IsInOurOwnHand(hit.collider) ? "IN HAND -> ignored" : hit.collider.GetComponentInParent() != null ? "IXRInteractable -> TINTS" : hit.collider.GetComponentInParent() != null ? "Seat -> TINTS" : hit.collider.GetComponentInParent() != null ? "PickableObject -> TINTS" : hit.collider.GetComponentInParent() != null ? "IReticleHoverable -> TINTS" : "nothing usable on it"; text += $"\n usableRay hit '{hit.collider.name}' at {hit.distance:0.##}m: {what}"; } else text += "\n usableRay: no hit"; return text; } private void OnInteractPerformed(InputAction.CallbackContext _) { // Only turn gold when the press lands on something the reticle marks as // usable; it stays gold for as long as the press is held (drags included). if (currentlyTinted) { interactHeld = true; if (cursorColors != null) cursorColors.SetClickHeld(true); // and shrink, for as long as it is held } } private void OnInteractCanceled(InputAction.CallbackContext _) { interactHeld = false; if (cursorColors != null) cursorColors.SetClickHeld(false); // released: grow back } private void Update() { var scheme = BroadcastControlsStatus.controlScheme; var desktop = scheme == BroadcastControlsStatus.ControlScheme.KeyboardMouse || scheme == BroadcastControlsStatus.ControlScheme.Gamepad; // The gaze ray is pinned camera-forward (TrackedPoseSchemeGate zeroes its // local pose), so both of ITS sources answer about screen centre. That is the // aim point only while the cursor is locked. Consulting them with a free // cursor pinned the reticle to whatever sat at centre and it could never // return to resting — so they stand down and the aim-point sources below // (world UI + the physics ray) own free-cursor hover. var aimed = desktop && CursorLocked; var uiHovered = aimed && gazeRay != null && gazeRay.TryGetCurrentUIRaycastResult(out var gazeUiHit) && IsRealUi(gazeUiHit); // The world-canvas interactor casts through the aim point in BOTH modes, so // it follows the reticle even when the cursor is free. Two questions, not one: // HasUiHover is what it OWNS the click for, HasAimClickTarget is what a click // would land on whoever delivers it. The reticle wants the second — with a // free cursor the first is null for every canvas the input module owns, which // is what left the tablet with no hover source at all. uiHovered |= desktop && worldUiInteractor != null && (worldUiInteractor.HasUiHover || worldUiInteractor.HasAimClickTarget); var physicsHovered = desktop && PhysicsHover(); var xriHovered = aimed && hovered.Count > 0; Apply(desktop && (xriHovered || uiHovered || physicsHovered), desktop && interactHeld); } /// /// Is the cursor pinned to screen centre? While it is, centre and the cursor are /// the same point and every camera-forward source is valid; once it frees up /// (tablet/menu) they are not. Read directly off Cursor.lockState — the same /// signal DesktopWorldUiInteractor resolves its screen point from, so the two /// components can never disagree about where the player is pointing. /// private static bool CursorLocked => Cursor.lockState == CursorLockMode.Locked; /// Where the reticle actually points: screen centre while locked, the /// (stick-warped) cursor position while free. private static Vector2 AimScreenPoint() { if (CursorLocked || Mouse.current == null) return new Vector2(Screen.width * 0.5f, Screen.height * 0.5f); return Mouse.current.position.ReadValue(); } /// The aim ray. Through the cursor, NOT the camera's nose — in tablet /// mode those point at different things. private Ray AimRay() => playerCamera.ScreenPointToRay(AimScreenPoint()); private bool PhysicsHover() { if (playerCamera == null) { playerCamera = Camera.main; if (playerCamera == null) return false; } var ray = AimRay(); // Project layers: any hit on the mask counts (uvs Highlight_Interactionable pattern). if (physicsHoverMask.value != 0 && Physics.Raycast(ray, out var masked, physicsHoverDistance, physicsHoverMask) && !IsInOurOwnHand(masked.collider)) return true; // XRI interactables and package usables: the gaze interactor only hovers // interactables that opt into allowGazeInteraction (off by default), so the // reticle finds them by raycast — same way TakeObject/SitController do. if (Physics.Raycast(ray, out var hit, interactableHoverDistance)) { // The thing we are HOLDING is docked right in front of the camera, so it // sits permanently under the reticle. Without this the reticle stays lit // for as long as you carry anything and can never return to its resting // colour — you are not "hovering" what is already in your hand. if (IsInOurOwnHand(hit.collider)) return false; if (hit.collider.GetComponentInParent() != null) return true; if (hit.collider.GetComponentInParent() != null) return true; if (hit.collider.GetComponentInParent() != null) return true; if (hit.collider.GetComponentInParent() != null) return true; // tooltips & friends } return false; } // Held items are parented to the camera (or to a dock beneath it), so "is it under // the camera?" is exactly "is it in our hand?" — and it stays true for hand docks // and the camera dock alike. private bool IsInOurOwnHand(Component hit) => playerCamera != null && hit != null && hit.transform.IsChildOf(playerCamera.transform); /// /// Is this UI raycast result actually UI? /// /// "The ray produced a UI hit" is NOT the same question. UI Toolkit's /// WorldDocumentRaycaster appends a result for ANY collider its world picker hits, /// even when there is no document there — and sets that result's gameObject to /// ITSELF. Unity's own comment says why: such hits "should block UI but not hide /// the PhysicsRaycaster results", i.e. they are occlusion sentinels, not UI. /// /// Taking them at face value meant a plain wall, floor or building counted as /// hovering UI, so the reticle sat on its hover colour almost everywhere. /// private static bool IsRealUi(RaycastResult hit) { if (hit.gameObject == null) return false; // The blocker sentinel: the result points at the raycaster's own GameObject. if (hit.module != null && hit.gameObject == hit.module.gameObject) return false; return true; } private void Apply(bool tinted, bool flashing) { // The cursor manager's invalid-request flash owns the reticle color while // active — don't fight it. if (cursorColors != null && cursorColors.IsFlashingInvalid) { currentlyTinted = false; currentlyFlashing = false; return; } // NO "unchanged since last frame" early-out. CursorStateController ALSO writes // this color (state changes, the invalid flash), so a cached flag goes stale // behind our back and the reticle then stays tinted forever — the resting // color is only re-applied on a CHANGE that never comes. Re-asserting it every // frame costs one color write and makes this the single authority. if (cursorColors == null) { if (!missingCursorWarned && tinted) { missingCursorWarned = true; Debug.LogWarning($"{LogPrefix} ReticleHoverFeedback on '{name}': no CursorStateController — " + "the reticle cannot signal hover anywhere.", this); } return; } // Hover / click / resting all come from the CursorStateController's palette. // Resting is its CURRENT colour, so the reticle returns to the right base after // a tablet toggle. The controller forwards the result to the UI Toolkit HUD. var resolved = flashing ? cursorColors.ClickColor : tinted ? cursorColors.HoverColor : cursorColors.RestingColor; cursorColors.SetResolvedColor(resolved); currentlyTinted = tinted; currentlyFlashing = flashing; } } }