Search Unity

Question XR Socket Interactor has hover mesh offset due to rounding of float variables

Discussion in 'AR/VR (XR) Discussion' started by Bahmann, Sep 9, 2020.

  1. Bahmann

    Bahmann

    Joined:
    Mar 5, 2019
    Posts:
    15
    I have vaious objects with Grab Interactable Components. Because it is essential, that the objects and thier sockets are identical due to the right assembly of the objects I face the problem that the hover mesh from the socket interactor has a slight offset (see attached pictures). I took an intensive look into the XR Socket Interactor Script and the problem seems to be caused by some rounding of the floats happening somewhere in the code. As I'm not familiar with Matrix4x4 i don't know how to solve this.

    Until Vector3 finalPosition is generated anything is fine. So the rounding must happen somewhere afterwards.

    Code (CSharp):
    1.  Matrix4x4 GetInteractableAttachMatrix(XRGrabInteractable interactable, Vector3 scale)
    2.         {
    3.            
    4.             var interactableLocalPosition = Vector3.zero;
    5.             var interactableLocalRotation = Quaternion.identity;
    6.             var interactableScale = interactable.transform.lossyScale;
    7.             if (interactable.attachTransform != null)
    8.             {
    9.                 interactableLocalPosition = interactable.attachTransform.localPosition;
    10.                 interactableLocalRotation = interactable.attachTransform.localRotation;
    11.                 interactableScale = interactable.attachTransform.lossyScale;
    12.             }
    13.  
    14.             Vector3 finalPosition = attachTransform.position;
    15.             Debug.Log("Final Position is:" + finalPosition.x + finalPosition.y + finalPosition.z);
    16.             Quaternion finalRotation = attachTransform.rotation * interactableLocalRotation;
    17.             return Matrix4x4.TRS(finalPosition, finalRotation, Vector3.Scale(scale, interactableScale));
    18.          
    19.         }
    20.         protected virtual void DrawHoveredInteractables()
    21.         {
    22.             if (interactableHoverMeshMaterial == null)
    23.                 return;
    24.             float hoveredScale = Mathf.Max(0.0f, m_InteractableHoverScale);
    25.             for (int i = 0; i < m_HoverTargets.Count; i++)
    26.             {
    27.                 var hoverTarget = m_HoverTargets[i] as XRGrabInteractable;
    28.                 if (hoverTarget == null || hoverTarget == selectTarget)
    29.                     continue;
    30.  
    31.                 MeshFilter[] interactableMeshFilters = null;
    32.                 if (m_MeshFilterCache.TryGetValue(hoverTarget, out interactableMeshFilters))
    33.                 {
    34.                     if (interactableMeshFilters != null && interactableMeshFilters.Length > 0)
    35.                     {
    36.                         foreach (var meshFilter in interactableMeshFilters)
    37.                         {
    38.                             if (meshFilter != null && (Camera.main.cullingMask & (1 << meshFilter.gameObject.layer)) != 0)
    39.                             {
    40.  
    41.                                 int iSubMeshCount = meshFilter.mesh.subMeshCount;
    42.                                 for (int j = 0; j < iSubMeshCount; ++j)
    43.                                 {
    44.                                     Graphics.DrawMesh(meshFilter.sharedMesh, GetInteractableAttachMatrix(hoverTarget, meshFilter.transform.lossyScale * hoveredScale), interactableHoverMeshMaterial, gameObject.layer, null, j, null, true, true);
    45.  
    46.                                 }
    47.  
    48.                             }
    49.                         }
    50.                     }
    51.                 }
    52.             }
    53.         }
    upload_2020-9-9_10-13-21.png
    upload_2020-9-9_10-13-44.png
     
    markasuter likes this.
  2. Shanti_Baba

    Shanti_Baba

    Joined:
    Nov 13, 2018
    Posts:
    12
    Happening to me as well....
    did you manage a fix?