Search Unity

Setting the material/ color of mesh made with Gizmos.DrawMesh or Graphics.DrawMeshNow

Discussion in 'Graphics for ECS' started by MadboyJames, Oct 4, 2019.

  1. MadboyJames

    MadboyJames

    Joined:
    Oct 28, 2017
    Posts:
    262
    I recently opened this thread to talk about how to "paint" datatiles on a gripmap. Essentially I decided on making myself a tool that can do that. Sadly, I do not know how to set the color or material of my tiles, which are simple meshes. Anyone know how I can change the color of a mesh generated in script?
    Below is my code for generating the mesh tile map graphically.

    Code (CSharp):
    1. Material mat;
    2.                 int length = tileMap.Count;
    3.                 for (int i = 0; i < length; i++)
    4.                 {
    5.                     float3 pos = new float3(i % (mapLength * QuadrantSystem.tileToMapQuadMultiplier), bottomLeftCorner.y, math.floor(i / (mapWidth * QuadrantSystem.tileToMapQuadMultiplier)));
    6.                     mat = tileMap[GetDictionaryKey(pos)].material;
    7.                     pos.x += .5f;
    8.                     pos.z += .5f;
    9.  
    10.                         Graphics.DrawMeshNow(tileMesh, pos, Quaternion.Euler(90, 0, 0));
    11.  
    12.                 }
    any help is appreciated.
     
  2. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    You can either use:
    1) DrawMeshInstanced - requires the use of a material with GPU instanced activated with material property blocks and it's limited to 1023 batches per call.

    2) DrawMeshInstancedIndirect - requires the use of compute buffers with custom shaders and there is no limit on the batches.
     
    MadboyJames likes this.
  3. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    MadboyJames likes this.
  4. MadboyJames

    MadboyJames

    Joined:
    Oct 28, 2017
    Posts:
    262
    Okay, thank you! And sorry for the late response. I've implemented a DrawMeshInstanced solution, but it derenders everytime I mouse over the object in the scene view. Additionally, it does not appear to be rendering the different materials per tile. My code is essentially the same as before.
    Code (CSharp):
    1. if (tileMap != null)
    2.             {
    3.                 Material mat;
    4.                 Matrix4x4 tileTrans = new Matrix4x4();
    5.                 Matrix4x4[] TileTransArray = new Matrix4x4[1];
    6.                 int length = tileMap.Count;
    7.                 for (int i = 0; i < length; i++)
    8.                 {
    9.                     float3 pos = new float3(i % (mapLength * QuadrantSystem.tileToMapQuadMultiplier), bottomLeftCorner.y, math.floor(i / (mapWidth * QuadrantSystem.tileToMapQuadMultiplier)));
    10.                     mat = tileMap[GetDictionaryKey(pos)].material;
    11.                     pos.x += .5f;
    12.                     pos.z += .5f;
    13.  
    14.                     tileTrans.SetTRS(pos, Quaternion.Euler(90, 0, 0), new float3(1, 1, 1));
    15.                     TileTransArray[0] = tileTrans;
    16.                    
    17.                     Graphics.DrawMeshInstanced(tileMesh, 0, mat, TileTransArray);
    18.                 }
    19.             }
    @sngdan I do not know anything about Unity hybrid renderer, is it a package? a method? how would I go about using it?
     
  5. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    Yes, it’s a package. Just look at the official ecs examples for usage.

    I guess for your use case it’s not the best approach, but you can optimize this later.
     
    MadboyJames likes this.
  6. MadboyJames

    MadboyJames

    Joined:
    Oct 28, 2017
    Posts:
    262
    Got it. Thank you! I have a question about it though. The meshes seem to be "flashing" or simply unrendered when I have my mouse over them. I'm using this in the editor scene view, using the OnDrawGizmos method. Is there another method I should be using? I can see what I have painted now when my mouse leaves the scene window (And it paints correctly, yay!), but I don't know why it's struggling to render. I have an idea (but I don't know how to check for a fix for it). Idea: It is updating the gizmos so rapidly when my mouse is over the meshes that the system does not complete rendering them before it is called to render them again.

    Any ideas?