Search Unity

Question Graphics.DrawMesh unstable in Scene GUI enviroment

Discussion in 'General Graphics' started by MvanNimwegen, Nov 17, 2022.

  1. MvanNimwegen

    MvanNimwegen

    Joined:
    Nov 17, 2022
    Posts:
    4
    Hi!

    I am trying to draw meshes using
    Graphics.DrawMesh
    . This is partially successful.
    I am using it to draw meshes in the Scene view, not in Game view (necessarilly). However, I run into a problem where the meshes are not always being drawn.
    As you can see below, the meshes sometimes are not drawn, untill you move your mouse back in the Scene View to trigger a
    Repaint
    .
    Unity_IJLPOrBPgr.gif
    PreviewMeshA is the source GameObject, DrawMesh has the script that draws the mesh.

    This happens when I call the method from a Scene GUI environment (
    OnDrawGizmos
    ,
    OnSceneGUI
    ,
    DrawGizmo
    attributed method). This behaviour does not exist when I call the method from an Update method, and decorate the class with an ExecuteAlways. But I do not want to be dependant on a Monobehaviour, so Update is not available for me.

    This is the code I currently use.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class DrawMeshMultipleMaterials : MonoBehaviour
    4. {
    5.     public GameObject ObjectToDraw;
    6.  
    7.     public void DrawGhost()
    8.     {
    9.         if (ObjectToDraw == null)
    10.         {
    11.             return;
    12.         }
    13.  
    14.         MeshFilter[] meshFilters = ObjectToDraw.GetComponentsInChildren<MeshFilter>();
    15.  
    16.         foreach (MeshFilter meshFilter in meshFilters)
    17.         {
    18.             MeshRenderer renderer = meshFilter.GetComponent<MeshRenderer>();
    19.             if (renderer == null)
    20.             {
    21.                 continue;
    22.             }
    23.  
    24.             Material[] materials = renderer.sharedMaterials;
    25.  
    26.             for (int i = 0; i < meshFilter.sharedMesh.subMeshCount; i++)
    27.             {
    28.                 Graphics.DrawMesh(meshFilter.sharedMesh, Vector3.zero, Quaternion.identity, materials[i], 0, null, i);
    29.             }
    30.         }
    31.     }
    32.  
    33.     public void OnDrawGizmos()
    34.     {
    35.         DrawGhost();
    36.     }
    37. }
    I want to be able to call this method from a utility class, and use it from any editor script to draw meshes in the Scene View. But before I am able to do that, I need this drawing to be consistent.

    Does anybody have an idea how I could achieve this?

    Thanks in advance! :)
     
  2. MvanNimwegen

    MvanNimwegen

    Joined:
    Nov 17, 2022
    Posts:
    4
  3. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    727
    Try using [ExecuteAlways] on your script and have your Graphics.DrawMesh() in Update() or LateUpdate(). If you're using URP or HDRP, try registering to their render update loop instead.
     
  4. MvanNimwegen

    MvanNimwegen

    Joined:
    Nov 17, 2022
    Posts:
    4
    That is exactly what I do not want to do. I did mention this in my post.