Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for GazeTarget getting overwritten by non-gaze pointers #11148

Merged
merged 2 commits into from
Oct 28, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions Assets/MRTK/Services/InputSystem/FocusProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ private class PointerHitResult
public int rayStepIndex = -1;
public float rayDistance;

public bool isSet;

public void Clear()
{
raycastHit = default(MixedRealityRaycastHit);
Expand All @@ -205,7 +207,9 @@ public void Clear()
ray = default(RayStep);
rayStepIndex = -1;
rayDistance = 0.0f;
}

isSet = false;
}

/// <summary>
/// Set hit focus information from a closest-colliders-to pointer check.
Expand All @@ -222,7 +226,9 @@ public void Set(GameObject hitObject, Vector3 hitPointOnObject, Vector4 hitNorma
this.ray = ray;
this.rayStepIndex = rayStepIndex;
this.rayDistance = rayDistance;
}

isSet = true;
}

/// <summary>
/// Set hit focus information from a physics raycast.
Expand All @@ -239,7 +245,9 @@ public void Set(MixedRealityRaycastHit hit, RayStep ray, int rayStepIndex, float
this.ray = ray;
this.rayStepIndex = rayStepIndex;
this.rayDistance = rayDistance;
}

isSet = true;
}

/// <summary>
/// Set hit information from a canvas raycast.
Expand All @@ -262,7 +270,22 @@ public void Set(RaycastResult result, Vector3 hitPointOnObject, Vector4 hitNorma
this.ray = ray;
this.rayStepIndex = rayStepIndex;
this.rayDistance = rayDistance;
}

isSet = true;
}

public void CopyFrom(PointerHitResult other)
{
raycastHit = other.raycastHit;
graphicsRaycastResult = other.graphicsRaycastResult;
hitObject = other.hitObject;
hitPointOnObject = other.hitPointOnObject;
hitNormalOnObject = other.hitNormalOnObject;
ray = other.ray;
rayStepIndex = other.rayStepIndex;
rayDistance = other.rayDistance;
isSet = other.isSet;
}
}

[Serializable]
Expand Down Expand Up @@ -565,7 +588,7 @@ public override void Update()
/// to do a gaze raycast even if gaze isn't used for focus.
/// </summary>
private PointerEventData gazeProviderPointingData;
private PointerHitResult gazeHitResult;
private PointerHitResult gazeHitResult = new PointerHitResult();

/// <summary>
/// Updates the gaze raycast provider even in scenarios where gaze isn't used for focus
Expand All @@ -579,7 +602,7 @@ private void UpdateGazeProvider()
bool gazeProviderEnabled = gazeProvider.IsNotNull() && gazeProvider.Enabled;
// The gaze hit result may be populated from the UpdatePointers call. If it has not, then perform
// another raycast if it's not populated
if (gazeProviderEnabled && gazeHitResult == null)
if (gazeProviderEnabled && !gazeHitResult.isSet)
{
IMixedRealityPointer gazePointer = gazeProvider?.GazePointer;
// Check that the gazePointer isn't null and that it has been properly registered as a pointer.
Expand All @@ -601,22 +624,22 @@ private void UpdateGazeProvider()
}

// set gaze hit according to distance and prioritization layer mask
gazeHitResult = GetPrioritizedHitResult(hitResult3d, hitResultUi, prioritizedLayerMasks);
}
gazeHitResult.CopyFrom(GetPrioritizedHitResult(hitResult3d, hitResultUi, prioritizedLayerMasks));
}
else
{
return;
}
}

if (gazeProvider.IsNotNull() && gazeHitResult.IsNotNull())
if (gazeProvider.IsNotNull() && gazeHitResult.isSet)
{
gazeProvider.UpdateGazeInfoFromHit(gazeHitResult.raycastHit);
gazeProvider.UpdateGazeInfoFromHit(gazeHitResult.raycastHit);
}

// Zero out value after every use to ensure the hit result is updated every frame.
gazeHitResult = null;
}
gazeHitResult.Clear();
}
}

#endregion IMixedRealityService Implementation
Expand Down Expand Up @@ -1098,8 +1121,8 @@ private void UpdatePointer(PointerData pointerData)
IMixedRealityPointer gazePointer = InputSystem?.GazeProvider.GazePointer;
if (gazePointer.IsNotNull() && pointerData.Pointer.PointerId == gazePointer.PointerId)
{
gazeHitResult = hit;
}
gazeHitResult.CopyFrom(hit);
}
keveleigh marked this conversation as resolved.
Show resolved Hide resolved

// Set the pointer's result last
pointerData.Pointer.Result = pointerData;
Expand Down