Search Unity

[2018.2.0f2] Graphics.DrawMeshInstanced doesn't actually instance a mesh?

Discussion in 'General Graphics' started by LukasAtharis, Jul 21, 2018.

  1. LukasAtharis

    LukasAtharis

    Joined:
    Mar 20, 2015
    Posts:
    5
    Hi,

    I'm trying to use Graphics.DrawMeshInstanced but watching the result with the frame debugger, it seems that it doesn't actually instance anything. Just to be sure I made a new project, a new material with "Instancing" and then called it and it just shows each mesh ... Am I missing something?

    (This is the only thing being rendered. My scene setup has boxes but I just disable their mesh renderer on Start).
    Code (CSharp):
    1. Graphics.DrawMeshInstanced(entry.Key.Mesh, entry.Key.Part, entry.Key.Material, instanceDesc.Matrices, instanceDesc.Matrices.Length, null, UnityEngine.Rendering.ShadowCastingMode.On, true);
    upload_2018-7-21_1-57-11.png

    For reference, I attached my min repro project.
     

    Attached Files:

    LennartJohansen likes this.
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    Hi.

    Had a look at your code. You call DrawMeshInstanced for each of the cubes. instanceDesc.Matrices.Length is always 1. In order to instance together meshes they have to be in the same matrix list/array. Only 1 call.
     
    richardkettlewell likes this.
  3. LukasAtharis

    LukasAtharis

    Joined:
    Mar 20, 2015
    Posts:
    5
    /facepalm

    I guess it's the price of me going back to Unity after a long while ... :)
    Indeed it was added multiple times because I had different mesh instances. I forgot the "sharedMesh" rule and if I replace all mesh.mesh to mesh.sharedMesh, it works as expected.

    Thanks!
     
    LennartJohansen likes this.