Search Unity

Failing to set instanced property using BatchRendererGroup.GetBatchVectorArray

Discussion in 'General Graphics' started by Baggers_, Sep 16, 2020.

  1. Baggers_

    Baggers_

    Joined:
    Sep 10, 2017
    Posts:
    98
    Hi. I've been able to set the transform of objects and handle batching of objects using the BatchRendererGroup but so far I've failed to be able to set instance properties without using MaterialPropertyBlocks. For this question, I need to use GetBatchVectorArray.

    The material being used does have gpu-instancing enabled.

    It's probably something very simple I'm missing but any help you can provide would be awesome

    The code below is available as a project here: https://github.com/cbaggers/BRG_Test

    This is my code (which is successfully drawing the cube and rotating it):
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using Unity.Jobs;
    4.  
    5. public class BatchRendererGroupTest : MonoBehaviour
    6. {
    7.     // Allow us to set the mesh and material from the inspector
    8.     public GameObject ObjectWeGetTheMeshFrom;
    9.     public Material _material;
    10.  
    11.     // The batch.
    12.     BatchRendererGroup _brg;
    13.     int _batch;
    14.  
    15.     void OnEnable()
    16.     {
    17.         _brg = new BatchRendererGroup(CullingCallback);
    18.  
    19.         var mesh = ObjectWeGetTheMeshFrom.GetComponent<MeshFilter>().sharedMesh;
    20.  
    21.         _batch = _brg.AddBatch(mesh, 0, _material, 0, castShadows: ShadowCastingMode.On,
    22.                    receiveShadows: true, invertCulling: false,
    23.                    bounds: new Bounds(Vector3.one * 10, Vector3.one * 20),
    24.                    instanceCount: 1, customProps: null,
    25.                    associatedSceneObject: null);
    26.     }
    27.  
    28.     void OnDisable()
    29.     {
    30.         _brg.Dispose();
    31.     }
    32.  
    33.     void Update()
    34.     {
    35.         // Spin the cube. Proves that the batch is reading the matrices just fine
    36.         var transforms = _brg.GetBatchMatrices(_batch);
    37.         transforms[0] = Matrix4x4.Rotate(Quaternion.Euler(Mathf.Sin(Time.time) * 180, Mathf.Sin(Time.time) * 180, Mathf.Cos(Time.time) * 180));
    38.  
    39.         // I thought this would set the color in the shader but it isn't working for me
    40.         var instVals = _brg.GetBatchVectorArray(_batch, "_Color");
    41.         instVals[0] = new Vector4(0, 1, 0, 1);
    42.     }
    43.  
    44.     // You can safely ignore this. It is just a pass-throught that performs no culling
    45.     JobHandle CullingCallback(BatchRendererGroup rendererGroup, BatchCullingContext cullingContext)
    46.     {
    47.         // dummy culling
    48.         for (var batchIndex = 0; batchIndex < cullingContext.batchVisibility.Length; ++batchIndex)
    49.         {
    50.             var batchVisibility = cullingContext.batchVisibility[batchIndex];
    51.  
    52.             for (var i = 0; i < batchVisibility.instancesCount; ++i)
    53.             {
    54.                 cullingContext.visibleIndices[batchVisibility.offset + i] = i;
    55.             }
    56.  
    57.             batchVisibility.visibleCount = batchVisibility.instancesCount;
    58.             cullingContext.batchVisibility[batchIndex] = batchVisibility;
    59.         }
    60.         return default;
    61.     }
    62. }
    63.  
    And here is the shader:

    Code (CSharp):
    1. Shader "BRG_Test"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color 0", Color) = (0, 0, 1, 0) // Default color blue, we are trying to set this green from the script
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Tags{ "RenderType" = "Opaque"  "Queue" = "Geometry+0" }
    11.         Cull Back
    12.         CGPROGRAM
    13.         #pragma target 5.0
    14.         #pragma multi_compile_instancing
    15.         #pragma surface surf Standard keepalpha addshadow fullforwardshadows
    16.         struct Input
    17.         {
    18.             half filler;
    19.         };
    20.  
    21.         UNITY_INSTANCING_BUFFER_START(BRG_Test)
    22.             UNITY_DEFINE_INSTANCED_PROP(float4, _Color)
    23. #define _Color_arr BRG_Test
    24.         UNITY_INSTANCING_BUFFER_END(BRG_Test)
    25.  
    26.         void surf( Input i , inout SurfaceOutputStandard o )
    27.         {
    28.             float4 _Color_Instance = UNITY_ACCESS_INSTANCED_PROP(_Color_arr, _Color);
    29.             o.Albedo = _Color_Instance.rgb;
    30.             o.Alpha = 1;
    31.         }
    32.  
    33.         ENDCG
    34.     }
    35.     Fallback "Diffuse"
    36. }
     
    Opeth001 likes this.
  2. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    did you end up with a solution ?
     
  3. Baggers_

    Baggers_

    Joined:
    Sep 10, 2017
    Posts:
    98
    @Opeth001 unfortunately the problem was that GetBatchVectorArray and co don't work with the built in renderer. You have to be using SRP. I wish that had been documented somewhere at the time.

    edit: I've removed the 'help wanted' so I dont waste anyone else's time. Hope you have better luck with your issues :)
     
    Opeth001 likes this.
  4. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Btw I'm using SRP and it's not working too.
    Have a nice day ;)