Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

How to pass different colors to instances when using BatchRendererGroup?

Discussion in '2019.3 Beta' started by HeronHuang, Oct 20, 2019.

  1. HeronHuang

    HeronHuang

    Joined:
    Nov 30, 2012
    Posts:
    27
    Hi guys,
    I just downloaded 2019.3.b7,I found unity had extended the APIs of BatchRendererGroup. Such as GetBatchVectorArray(batchid,paramName) .....I guessed it means to fetch a array that should be passed to the shader's param.I filled the returned array with different colors on every instance. But nothing happend. What's wrong?

    Here's my codes:
    Code (CSharp):
    1. var    _batchIndex = _batchRendererGroup.AddBatch(
    2.             mesh,
    3.             0,
    4.             material,
    5.             0,
    6.             ShadowCastingMode.Off,
    7.             false,
    8.             false,
    9.             new Bounds(Vector3.zero, Vector3.one*2000  ),
    10.             InstanceCount,
    11.             property,
    12.             gameObject);
    13.  
    14.   var colors = _batchRendererGroup.GetBatchVectorArray(_batchIndex, "_Color");
    15.   for (int i = 0; i < InstanceCount; i++)
    16.     {
    17.             colors[i] = new float4( i/ (float)InstanceCount  ,
    18.                                     i/ (float)InstanceCount  ,
    19.                                     i/ (float)InstanceCount  , 1.0f);    
    20.   }
    [Wrong result:]
    upload_2019-10-20_12-55-25.png
     
    Last edited: Oct 21, 2019
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,619
    Does this code even compile? I guess it should the emit an error like:
    PS: If you post code, please use code tags to make it easier to read.
     
  3. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    The property you are setting must be set up for instancing in the shader. Shader Graph support should come with 7.2 and DOTS will use this in 0.2.

    There are implicit conversions between float4 and Vector4.
     
  4. HeronHuang

    HeronHuang

    Joined:
    Nov 30, 2012
    Posts:
    27
    Thanks for reply.
     
  5. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,619
    The original code assigned a float4 to a NativeArray, rather than to a NativeArray index. That's why I was skeptical if the code even compiles. I guess the forum just screwed up the code due to the missing code-tags in the original post, but he/she edited the post in the meantime and the syntax looks fine to me now.