Search Unity

Graphics.DrawMesh flickering in Editor

Discussion in 'General Graphics' started by MvNimwegen, Jun 15, 2022.

Thread Status:
Not open for further replies.
  1. MvNimwegen

    MvNimwegen

    Joined:
    Dec 19, 2019
    Posts:
    56
    Hi,

    I am trying to draw a simple quad mesh with a Material in both the Scene View and the GameView using
    Graphics.DrawMesh
    . However, the result ends up flickering in the Scene View. I have no clue as to why.

    Unity_dk43UXu62H.gif

    This is the code I use for this
    Code (CSharp):
    1.     public void OnDrawGizmos()
    2.     {
    3.         //Do not draw if the required assets are not set
    4.         if (Mesh == null || DrawingMaterial == null)
    5.         {
    6.             return;
    7.         }
    8.  
    9.         foreach(Camera cam in SceneView.GetAllSceneCameras())
    10.         {
    11.             Graphics.DrawMesh(Mesh, transform.position, transform.rotation, DrawingMaterial, 0, cam);
    12.         }
    13.     }
    I want to draw for each Camera individually because I want them to face each Camera at all times. For now, they simply all share the same transform.

    Does anybody know what I am missing here?

    Thanks!
     
  2. MvNimwegen

    MvNimwegen

    Joined:
    Dec 19, 2019
    Posts:
    56
  3. GurhanH

    GurhanH

    Joined:
    Apr 24, 2017
    Posts:
    539
    OnDrawGizmos is not reliable to make camera based draw calls. Try this instead:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. public class DrawTest : MonoBehaviour
    5. {
    6.     public Mesh mesh;
    7.     public Material material;
    8.  
    9.     private void OnEnable()
    10.     {
    11.         if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset == null) // built-in
    12.             Camera.onPreCull+= DrawMesh;
    13.         else // SRP
    14.             UnityEngine.Rendering.RenderPipelineManager.beginCameraRendering += DrawMesh;
    15.     }
    16.  
    17.     private void OnDisable()
    18.     {
    19.         if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset == null) // built-in
    20.             Camera.onPreCull-= DrawMesh;
    21.         else // SRP
    22.             UnityEngine.Rendering.RenderPipelineManager.beginCameraRendering -= DrawMesh;
    23.     }
    24.  
    25.     private void DrawMesh(UnityEngine.Rendering.ScriptableRenderContext context, Camera camera)
    26.     {
    27.         DrawMesh(camera);
    28.     }
    29.  
    30.     public void DrawMesh(Camera cam)
    31.     {
    32.         if (mesh == null || material == null)
    33.             return;
    34.         Graphics.DrawMesh(mesh, transform.position, Quaternion.LookRotation(transform.position - cam.transform.position), material, 0, cam);
    35.     }
    36. }
    37.  
     
    Last edited: Jun 28, 2022
    Mnemotic and MvNimwegen like this.
  4. MvNimwegen

    MvNimwegen

    Joined:
    Dec 19, 2019
    Posts:
    56
    Unfortunately, this does not seem to draw anything for me.
    The code does seem to run, but I do not see anything being drawn on my screen.
    Im running v2020.2.2f1

    Unity_nHomFjpnhJ.png
     
    Last edited: Jun 28, 2022
  5. GurhanH

    GurhanH

    Joined:
    Apr 24, 2017
    Posts:
    539
    Change onPreRender to onPreCull and see if that does the trick. I also edited the code in the previous post.
     
    MvNimwegen likes this.
  6. MvNimwegen

    MvNimwegen

    Joined:
    Dec 19, 2019
    Posts:
    56
    Thats seems to do it! Thanks a bunch! :D
     
Thread Status:
Not open for further replies.