Search Unity

unity5.6 DrawMeshInstanced(),how to set the different color for any instanced?

Discussion in 'Shaders' started by dreamerflyer, Oct 12, 2017.

  1. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    According api: DrawMeshInstanced(Mesh mesh, int submeshIndex, Material material, List<Matrix4x4> matrices, MaterialPropertyBlock properties);how to set the MaterialPropertyBlock for per instanced?I think Maybe should be List<MaterialPropertyBlock> ?
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    You assign only 1 MaterialPropertyBlock. if you want a per instance property you need to set this up in the shader and add the property as an array on the materialpropertyblock. 1 per instance.
     
  3. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    colorG.png noG.png
    When use: Transform grassObj = GameObject.Instantiate(grassPrefab).transform;and change the color:
    Code (CSharp):
    1.  MaterialPropertyBlock pb = new MaterialPropertyBlock();
    2.             pb.SetColor("_Color", grassData.color);
    3.             grass.GetComponent<MeshRenderer>().SetPropertyBlock(pb);
    ,but can not batching,what's wrong? View attachment 250312
     
    Last edited: Oct 12, 2017
  4. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    OK,batching is working,but ,still have waring,maybe ignore it? work.png