Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Reasons why DrawMeshInstanced might not be visible?

Discussion in 'General Graphics' started by rrh, Oct 19, 2017.

  1. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    If I run this nothing is visible:
    Code (csharp):
    1. for (int i = 0; i < matrices.Length; i++)
    2.         {
    3.             var mat = Matrix4x4.identity;
    4.             mat.SetTRS(transform.position+Random.insideUnitSphere*100f, Quaternion.identity, Vector3.one*200f);
    5.             matrices[i] = mat;
    6.         }
    7.        
    8.         Graphics.DrawMeshInstanced(meshToRender, 0, material, matrices);
    9.  
    If I run this the meshes are visible, but obviously without the performance benefit:
    Code (csharp):
    1.  
    2. for (int i = 0; i < matrices.Length; i++)
    3.         {
    4.             var mat = Matrix4x4.identity;
    5.             mat.SetTRS(transform.position+Random.insideUnitSphere*100f, Quaternion.identity, Vector3.one*200f);
    6.             matrices[i] = mat;
    7.             Graphics.DrawMesh(meshToRender, matrices[i], material, 0);          
    8.         }
    9.  
    What are the possible reasons that DrawMeshInstanced wouldn't be working for me? I'm not doing anything with the shader yet, but I thought that was just for if I wanted to pass specific material properties to each instance.
     
  2. sylon

    sylon

    Joined:
    Mar 5, 2017
    Posts:
    246
    Does the shader you are using with your material support instancing?
     
  3. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    I tried it with the Standard shader which supposedly supports instancing but the checkbox listed here never shows up: https://docs.unity3d.com/Manual/GPUInstancing.html

    I also used the example InstancedColorSurfaceShader but again the checkbox never shows up.
     
  4. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    what version of unity?

    EDIT: sounds like either incorrect version of unity, or perhaps using batching and instancing together? only one can be used not both

    EDIT 2: looks like you did not read all of that manual page:

    Batching priority
    When batching, Unity prioritizes Static batching over instancing. If you mark one of your GameObjects for static batching, and Unity successfully batches it, Unity disables instancing on that GameObject, even if its Renderer uses an instancing Shader. When this happens, the Inspector window displays a warning message suggesting that you disable Static Batching. To do this, open the Player Settings (Edit > Project Settings > Player), open Other Settings, and under the Rendering section, untick the Static Batching checkbox.

    Unity prioritizes instancing over dynamic batching. If Unity can instance a Mesh, it disables dynamic batching for that Mesh.
     
  5. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    I was using version 5.5.4 and I had read instancing was supported since 5.4 but updating to 2017 does make the checkbox appear, so that could have been it.

    Now that I got it to work with the standard shader, now my goal is to make my own shader that works with instancing.