Search Unity

  1. Unity 2019.4 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  3. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

MaterialPropertyBlock SetVectorArray WebGL and Chrome Error?

Discussion in 'Shaders' started by gkadamopoulos, Jun 30, 2020 at 12:26 PM.

  1. gkadamopoulos


    Apr 18, 2019
    Hello all,
    I am working on a big project that primarily targets WebGL as a platform
    I use DrawMeshInstanced to draw thousands of objects and by using MaterialProprtyBlock I am able to change their attributes (color, ST, custom variables used in shader)

    I am using batches to draw as many objects in the same draw call as possible.
    Everything was working fine and as expected both in Firefox and Chrome, windows and MacOS.
    suddenly after a chrome update around April May all the colors and the values I pass using setVectorArray are meshed up but only on Chrome and on Windows. Both Firefox on MacOS and Windows work as expected, Chrome on MacOS also renders everything ok as well as the editor

    this is a screenshot of the editor

    and this is a screenshot of chrome Windows
    also the meshed up chrome values seem to change when rotating the camera and changing the view.

    the code is the one below

    Code (CSharp):
    1. int total = width * depth;
    2.          int batches = (int)Mathf.Ceil(total / BATCH_MAX_FLOAT);
    3.          for (int i = 0; i < batches; ++i)
    4.          {
    5.              int batchCount = Mathf.Min(BATCH_MAX, total - (BATCH_MAX * i));
    6.              int start = Mathf.Max(0, (i - 1) * BATCH_MAX);
    7.              Matrix4x4[] batchedMatrices = GetBatchedMatrices(start, batchCount);
    8.              Vector4[] batchedColorArray = GetBatchedArray(start, batchCount);
    9.              propertyBlock.SetVectorArray("_Color", batchedColorArray);
    10.              Graphics.DrawMeshInstanced(mMeshFilter.sharedMesh, 0, meshMaterial, batchedMatrices, batchCount, propertyBlock);
    11.          }
  2. gkadamopoulos


    Apr 18, 2019
    I found my problem and I state it here for anyone ever running to something like this in the future.

    the problem was the way i declared my multiple instanced properties in shader.
    I never found an example where more than one properties were instanced, thus it didnt occur to me that i set them up wrong.

    Code (CSharp):
    1. UNITY_INSTANCING_BUFFER_START(InstanceProperties)
    2.     UNITY_DEFINE_INSTANCED_PROP(int, _highlighted)
    3.     #define _highlightArr InstanceProperties
    4.     UNITY_DEFINE_INSTANCED_PROP(float4, _Color)
    5.     #define _Color_arr InstanceProperties
    7.     #define _ST_arr InstanceProperties
    8. UNITY_INSTANCING_BUFFER_END(InstanceProperties)
    this is how I had them set up. Everything was working fine to all builds except chrome windows upfter an update that I suspect it changed something in way webGL reads data.

    I changed it to this and everything worked fine
    Code (CSharp):
    2. UNITY_INSTANCING_BUFFER_START(InstanceProperties)
    3.     UNITY_DEFINE_INSTANCED_PROP(int, _highlighted)
    4.     #define _highlightArr InstanceProperties
    5. UNITY_INSTANCING_BUFFER_END(InstanceProperties)
    7. UNITY_INSTANCING_BUFFER_START(InstanceProperties1)
    8.     UNITY_DEFINE_INSTANCED_PROP(float4, _Color)
    9.     #define _Color_arr InstanceProperties1
    10. UNITY_INSTANCING_BUFFER_END(InstanceProperties1)
    12. UNITY_INSTANCING_BUFFER_START(InstanceProperties2)
    13.     UNITY_DEFINE_INSTANCED_PROP(float4, _ST)
    14.     #define _ST_arr InstanceProperties2
    15. UNITY_INSTANCING_BUFFER_END(InstanceProperties2)