Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Graphics.DrawMesh - Mesh transform is rounded and drawn with offset

Discussion in 'Scripting' started by Bahmann, Sep 10, 2020.

  1. Bahmann

    Bahmann

    Joined:
    Mar 5, 2019
    Posts:
    15
    Hi,

    I'm drawing a hover mesh using the Graphics.DrawMesh function as soon a object hits a box collider. The drawing works well, but it seems that the function rounds the variables for transfom and rotation. This causes a slight offset of the hover mesh to its original position.
    Is there a way to force the Graphics.DrawMesh to use all decimals (e.g. 5) to draw the mesh with precision?

    Code (CSharp):
    1.         protected virtual void DrawHoveredInteractables()
    2.         {
    3.             if (interactableHoverMeshMaterial == null)
    4.                 return;
    5.             float hoveredScale = Mathf.Max(0.0f, m_InteractableHoverScale);
    6.             for (int i = 0; i < m_HoverTargets.Count; i++)
    7.             {
    8.                 var hoverTarget = m_HoverTargets[i] as XRGrabInteractable;
    9.                 if (hoverTarget == null || hoverTarget == selectTarget)
    10.                     continue;
    11.  
    12.                 MeshFilter[] interactableMeshFilters = null;
    13.                 if (m_MeshFilterCache.TryGetValue(hoverTarget, out interactableMeshFilters))
    14.                 {
    15.                     if (interactableMeshFilters != null && interactableMeshFilters.Length > 0)
    16.                     {
    17.                         foreach (var meshFilter in interactableMeshFilters)
    18.                         {
    19.                             if (meshFilter != null && (Camera.main.cullingMask & (1 << meshFilter.gameObject.layer)) != 0)
    20.                             {
    21.  
    22.                                 int iSubMeshCount = meshFilter.mesh.subMeshCount;
    23.                                 for (int j = 0; j < iSubMeshCount; ++j)
    24.                                 {
    25.                                     Graphics.DrawMesh(meshFilter.sharedMesh, attachTransform.position, attachTransform.rotation, interactableHoverMeshMaterial, gameObject.layer, null, j, null, true, true);
    26.                                 }
    27.  
    28.                             }
    29.                         }
    30.                     }
    31.                 }
    32.             }
    33.         }
    upload_2020-9-10_8-10-57.png
     
  2. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,134
    You could try:

    Graphics.DrawMesh(meshFilter.sharedMesh, attachTransform.localToWorldMatrix, interactableHoverMeshMaterial, gameObject.layer, null, j, null, true, true);

    Position and rotation are derived from the world matrix anyway, so perhaps that's sensitive to inaccuracy.
     
  3. Bahmann

    Bahmann

    Joined:
    Mar 5, 2019
    Posts:
    15

    Thanks for your suggestion, unfortunately this doesn't change anything.