Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Resolved Graphics.RenderMeshInstanced not showing in Scene View (SOLVED)

Discussion in 'General Graphics' started by TylerT_DreamToaster, Nov 19, 2023.

  1. TylerT_DreamToaster

    TylerT_DreamToaster

    Joined:
    Jul 19, 2021
    Posts:
    8
    I was using Graphics.RenderMeshInstanced but could not get it to show in the scene view. I figured out a solution and wanted to share it with the community for anyone else running into in the future.

    What I tried first and didn't work:
    This resulted in flickering visuals in the scene view.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.Rendering;
    4.  
    5. [ExecuteInEditMode]
    6. public class RenderMeshInstancedTest : MonoBehaviour
    7. {
    8.     public Mesh Mesh;
    9.     public Material Material;
    10.  
    11.     //the below code works for game-view and flickers in scene view
    12.     void Update()
    13.     {
    14.         //Render game view
    15.         RenderParams renderParams = new RenderParams(this.Material) { camera = Camera.main, layer = this.gameObject.layer };
    16.         Matrix4x4[] instanceData = new Matrix4x4[] { transform.localToWorldMatrix };
    17.  
    18.         Graphics.RenderMeshInstanced(renderParams, Mesh, 0, instanceData);
    19.  
    20.  
    21.         //render scene view
    22.         var cams = SceneView.GetAllSceneCameras();
    23.         var renderParamsSceneView = new RenderParams(this.Material) { layer = this.gameObject.layer };
    24.         foreach (var cam in cams)
    25.         {
    26.             renderParamsSceneView.camera = cam;
    27.             Graphics.RenderMeshInstanced(renderParamsSceneView, Mesh, 0, instanceData);
    28.         }
    29.     }
    30.  
    31. }

    What worked:

    The below code works! BUT, not in 2021. The issue is documented here but only fixed in 2022+.

    There's a lot of ways that feel right, but did not work. It took a decent amount of digging to find out that this works. Ideally an example like this should be added to the Graphics.RenderMeshInstanced documentation.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.Rendering;
    4.  
    5. [ExecuteInEditMode]
    6. public class RenderMeshInstancedTest : MonoBehaviour
    7. {
    8.     public Mesh Mesh;
    9.     public Material Material;
    10.  
    11.     private void Awake()
    12.     {
    13.         RenderPipelineManager.beginCameraRendering += HandleRenderStart;
    14.  
    15.         if (Application.isPlaying == false)
    16.             SceneView.beforeSceneGui += OnSceneGUI;
    17.     }
    18.  
    19.     private void OnDestroy()
    20.     {
    21.         RenderPipelineManager.beginCameraRendering -= HandleRenderStart;
    22.  
    23.         if (Application.isPlaying == false)
    24.             SceneView.beforeSceneGui -= OnSceneGUI;
    25.     }
    26.  
    27.     private void OnSceneGUI(SceneView sceneView)
    28.     {
    29.         RenderMeshInstanced(sceneView.camera);
    30.     }
    31.  
    32.     private void HandleRenderStart(ScriptableRenderContext context, Camera cam)
    33.     {
    34.         RenderMeshInstanced(cam);
    35.     }
    36.  
    37.     void RenderMeshInstanced(Camera cam)
    38.     {
    39.         Matrix4x4[] instanceData = new Matrix4x4[] { transform.localToWorldMatrix };
    40.         var renderParamsSceneView = new RenderParams(this.Material) { layer = this.gameObject.layer };
    41.         renderParamsSceneView.camera = cam;
    42.         Graphics.RenderMeshInstanced(renderParamsSceneView, Mesh, 0, instanceData);
    43.     }
    44. }
     
    Dimonasdf and nanddo like this.