Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Setting per-entity color using an instanced mesh?

Discussion in 'Data Oriented Technology Stack' started by schsam, Apr 9, 2018.

  1. schsam

    schsam

    Joined:
    Jan 15, 2017
    Posts:
    2
    Loving the new ECS / job system so far.

    I am currently working on a simple molecular dynamics simulation using the ECS (https://github.com/sschoenholz/unity_ecs_md). I am rendering lots of spheres using a single MeshInstanceRenderer. Is it currently possible to color the spheres individually using a component? I guess, more generally, is there an easy way to currently send information from a component to the fragment shader? If it is not currently easy to do, is it possible to feature request that it be easy to do later? I feel like it's a really useful way to debug things.
     
  2. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    173
  3. schsam

    schsam

    Joined:
    Jan 15, 2017
    Posts:
    2
    Ah thanks! :)
     
  4. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    173
    As a temporary solution, you COULD create a new system based on the MeshInstancedRendererSystem, adding the property block parameter, but I am unsure of performance at the moment. Changing the null to a property block devised by the components. You, however can not store a MaterialPropertyBlock on a IComponentData so some cleverness will be required :p
    Code (CSharp):
    1.  Graphics.DrawMeshInstanced(cacheduniqueRendererType.mesh, 0, cacheduniqueRendererType.material, this.m_MatricesArray, num, (MaterialPropertyBlock) null, cacheduniqueRendererType.castShadows, cacheduniqueRendererType.receiveShadows);
    **Edit: I actually missed the fact that the loop is a batch render, so a per-entity colorization with that change wouldn't actually be as feasible, but it is a start.
     
    Last edited: Apr 9, 2018
  5. StephanK

    StephanK

    Joined:
    Jul 10, 2012
    Posts:
    46
    You can store the the MaterialPropertyBlock on a ISharedComponent and have different ISharedComponents for different colors. Basically replaicng null in that call with a MaterialPropertyBlock and adding that MaterialPropertyBlock to your MeshInstanceRenderer component should already do the trick for you.
     
  6. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    173
    The problem with just replacing that null with a prop block is that most of the performance gain is from batching the render together with cached info. You will have to limit the batch to a size of 1 before you will truly see a "per-entity color" if I understand it correctly
     
  7. jack_leng

    jack_leng

    Joined:
    Jan 3, 2017
    Posts:
    3
    i have a question,can u help me? i want to render something,but I have more than one material and only one mesh.how should i use the MeshInstanceRenderer
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,569
    2 meshes, or merge materials.