Search Unity

How to get MaterialProperyBlock for MeshRenderer submesh

Discussion in 'General Graphics' started by Miguel-Ferreira, May 26, 2016.

  1. Miguel-Ferreira

    Miguel-Ferreira

    Joined:
    May 8, 2015
    Posts:
    90
    Hi guys, started working on 3d now, having problems with something that ought to be very simple. I have a mesh renderer with multiple materials, I wanted to GetPropertyBlock of a specific material that the mesh renderer uses. I only seem to be able to get one of them through Renderer.GetPropertyBlock

    There ought to be a method that allowed me to get a MaterialPropertyBlock for a submesh index but I can't seem to find anything in the docs about it.

    Am I missing something obvious?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    What you're missing is that there really is only one MaterialPropertyBlock per renderer component and there isn't one per sub mesh.

    If you want unique properties per material and you have multiple materials on a single renderer you'll need to use Renderer.materials[index].Set____() functions which will create a unique material instance.
     
  3. Miguel-Ferreira

    Miguel-Ferreira

    Joined:
    May 8, 2015
    Posts:
    90
    Hi,

    Thank you, that was what I thought, but I still find the current solution unintuitive since you can ask a mesh renderer for a material property block, but if you have multiple materials you don't really know which one you are going to get.

    Having to access the materials array will create additional draw calls which I really didn't want, but if that's the only option I guess it will have to do.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    The single material property block is applied to all the materials of that renderer, and unless you're using the 5.4 beta with instanced materials it too will increase the draw calls count.
     
  5. Miguel-Ferreira

    Miguel-Ferreira

    Joined:
    May 8, 2015
    Posts:
    90
    Thank you for the explanation. Just regarding that last point, I'm also noticing that. Applying a MaterialPropertyBlock with a different color to a Mesh Renderer (while keeping the material) increases the draw call count. Why is that? Isn't the whole point of material property block to prevent the increase of draw calls?
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    That's what I used to think it did too, as it was what others thought it did, but Unity doesn't really support instancing which would allow for that (at least not before 5.4 where they're started to add more complete support). At best it appears to be minor CPU side performance improvement and memory usage / garbage collection reduction.

    There's stuff that's been in shader lab from Unity 3 for calling out material properties you expect to come from a material property block which feels like early work to get ready for instancing, and some old documentation on the terrain system that made things confusing by making it sound like it was able to do instancing. I now think it may have been just a hack they were using for setting vertex colors at some point.