Search Unity

BatchRendererGroup.GetBatchVectorArray API does not work

Discussion in 'Graphics for ECS' started by Opeth001, Apr 10, 2021.

  1. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Hello Everyone,

    im trying to set the _BaseColor Property of a shader using the GetBatchVectorArray function but nothing Happens.

    im using the URP SimpleLit shader.

    Thanks in Advance!

    code Example:
    Code (CSharp):
    1.  
    2. using Unity.Burst;
    3. using Unity.Collections;
    4. using Unity.Jobs;
    5. using Unity.Mathematics;
    6. using UnityEngine;
    7. using UnityEngine.Rendering;
    8.  
    9. public class TestBatchGroup : MonoBehaviour
    10. {
    11.     public bool UpdateTransforms = true;
    12.     public bool UpdateColors = true;
    13.  
    14.     public GameObject Prefab;
    15.     private Mesh mesh;
    16.     private Material material;
    17.  
    18.  
    19.     private BatchRendererGroup _batchRendererGroup;
    20.  
    21.     private JobHandle _jobDependency;
    22.  
    23.     private const int Split = 10;
    24.  
    25.     private const int InstanceCount = Split * Split * Split;
    26.  
    27.     private int _batchIndex;
    28.  
    29.     private int _BaseColorShaderIndex;
    30.  
    31.     private void Awake()
    32.     {
    33.         material = Prefab.GetComponent<MeshRenderer>().sharedMaterial;
    34.         mesh = Prefab.GetComponent<MeshFilter>().sharedMesh;
    35.  
    36.  
    37.         _BaseColorShaderIndex = material.shader.FindPropertyIndex("_BaseColor");
    38.  
    39.     }
    40.  
    41.  
    42.     private void OnEnable()
    43.     {
    44.         _batchRendererGroup = new BatchRendererGroup(CullingCallback);
    45.         _batchIndex = _batchRendererGroup.AddBatch(
    46.             mesh,
    47.             0,
    48.             material,
    49.             0,
    50.             ShadowCastingMode.Off,
    51.             false,
    52.             false,
    53.             new Bounds(Vector3.zero, Vector3.one * float.MaxValue),
    54.             InstanceCount,
    55.             null,
    56.             gameObject);
    57.  
    58.  
    59.  
    60.     }
    61.  
    62.  
    63.     [BurstCompatible]
    64.     struct FillRandomColors : IJob
    65.     {
    66.         public NativeArray<Vector4> Colors;
    67.  
    68.         public void Execute()
    69.         {
    70.             for (int i = 0; i < Colors.Length; i++)
    71.             {
    72.                 var random = Unity.Mathematics.Random.CreateFromIndex((uint)i);
    73.                 Colors[i] = random.NextFloat4();
    74.             }
    75.         }
    76.     }
    77.  
    78.  
    79.     private void OnDisable()
    80.     {
    81.         _batchRendererGroup.Dispose();
    82.     }
    83.  
    84.     private void Update()
    85.     {
    86.         _jobDependency.Complete();
    87.  
    88.  
    89.         if (UpdateTransforms)
    90.         {
    91.             _jobDependency = new UpdateMatrixJob
    92.             {
    93.                 Matrices = _batchRendererGroup.GetBatchMatrices(_batchIndex),
    94.                 Time = Time.time
    95.             }.Schedule(InstanceCount, 128);
    96.         }
    97.  
    98.         if (UpdateColors)
    99.         {
    100.             var batchColorsHandle = _batchRendererGroup.GetBatchVectorArray(_batchIndex, _BaseColorShaderIndex);
    101.  
    102.             var fillRandomColors = new FillRandomColors
    103.             {
    104.                 Colors = batchColorsHandle,
    105.             };
    106.  
    107.             _jobDependency = JobHandle.CombineDependencies(_jobDependency, fillRandomColors.Schedule());
    108.  
    109.         }
    110.  
    111.     }
    112.  
    113.     private JobHandle CullingCallback(BatchRendererGroup rendererGroup, BatchCullingContext cullingContext)
    114.     {
    115.         var inputDependency = _jobDependency;
    116.         for (var i = 0; i < cullingContext.batchVisibility.Length; ++i)
    117.         {
    118.             var job = new CullingJob
    119.             {
    120.                 CullingContext = cullingContext,
    121.                 Matrices = rendererGroup.GetBatchMatrices(i),
    122.                 BatchIndex = i
    123.             }.Schedule(inputDependency);
    124.          
    125.             _jobDependency = JobHandle.CombineDependencies(job, _jobDependency);
    126.         }
    127.  
    128.         return _jobDependency;
    129.     }
    130.  
    131.     // culling job
    132.     [BurstCompatible]
    133.     private struct CullingJob : IJob
    134.     {
    135.         public BatchCullingContext CullingContext;
    136.         public NativeArray<Matrix4x4> Matrices;
    137.         public int BatchIndex;
    138.  
    139.         public void Execute()
    140.         {
    141.             var batchVisibility = CullingContext.batchVisibility[BatchIndex];
    142.  
    143.             var visibleCount = 0;
    144.             for (var i = 0; i < batchVisibility.instancesCount; ++i)
    145.             {
    146.                 if (!Contains(CullingContext.cullingPlanes, Matrices[i])) continue;
    147.                 CullingContext.visibleIndices[visibleCount] = batchVisibility.offset + i;
    148.                 ++visibleCount;
    149.             }
    150.  
    151.             batchVisibility.visibleCount = visibleCount;
    152.             CullingContext.batchVisibility[BatchIndex] = batchVisibility;
    153.         }
    154.      
    155.         private bool Contains(NativeArray<Plane> planes, Matrix4x4 matrix)
    156.         {
    157.             for (var i = 0; i < planes.Length; ++i)
    158.             {
    159.                 if (!planes[i].GetSide(matrix.MultiplyPoint(Vector3.zero)))
    160.                 {
    161.                     return false;
    162.                 }
    163.             }
    164.  
    165.             return true;
    166.         }
    167.     }
    168.  
    169.     [BurstCompile]
    170.     private struct UpdateMatrixJob : IJobParallelFor
    171.     {
    172.         public NativeArray<Matrix4x4> Matrices;
    173.         public float Time;
    174.  
    175.         public void Execute(int index)
    176.         {
    177.             var id = new Vector3(index / Split / Split, index / Split % Split, index % Split);
    178.             Matrices[index] = Matrix4x4.TRS(id * 5,
    179.                 quaternion.EulerXYZ(id + Vector3.one * Time),
    180.                 Vector3.one);
    181.         }
    182.     }
    183. }
     
    Last edited: Apr 10, 2021