Advertisement
Rengaw

MeshInstanceRendererSystem

Apr 27th, 2018
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. protected override void OnUpdate()
  2. {
  3.     // We want to iterate over all unique MeshInstanceRenderer shared component data,
  4.     // that are attached to any entities in the world
  5.     EntityManager.GetAllUniqueSharedComponentDatas(m_CacheduniqueRendererTypes);
  6.  
  7.     for (int i = 0; i != m_CacheduniqueRendererTypes.Count; i++)
  8.     {
  9.         // For each unique MeshInstanceRenderer data, we want to get all entities with a TransformMatrix
  10.         // SharedComponentData gurantees that all those entities are packed togehter in a chunk with linear memory layout.
  11.         // As a result the copy of the matrices out is internally done via memcpy.
  12.         var renderer = m_CacheduniqueRendererTypes[i];
  13.         m_InstanceRendererGroup.SetFilter(renderer);
  14.         var transforms = m_InstanceRendererGroup.GetComponentDataArray<TransformMatrix>();
  15.  
  16.         // Graphics.DrawMeshInstanced has a set of limitations that are not optimal for working with ECS.
  17.         // Specifically:
  18.         // * No way to push the matrices from a job
  19.         // * no NativeArray API, currently uses Matrix4x4[]
  20.         // As a result this code is not yet jobified.
  21.         // We are planning to adjust this API to make it more efficient for this use case.
  22.  
  23.         // For now, we have to copy our data into Matrix4x4[] with a specific upper limit of how many instances we can render in one batch.
  24.         // So we just have a for loop here, representing each Graphics.DrawMeshInstanced batch
  25.         int beginIndex = 0;
  26.         while (beginIndex < transforms.Length)
  27.         {
  28.             int length = math.min(m_MatricesArray.Length, transforms.Length - beginIndex);
  29.             CopyMatrices(transforms, beginIndex, length, m_MatricesArray);
  30.             Graphics.DrawMeshInstanced(renderer.mesh, 0, renderer.material, m_MatricesArray, length, null, renderer.castShadows, renderer.receiveShadows);
  31.  
  32.             beginIndex += length;
  33.         }
  34.     }
  35.  
  36.     m_CacheduniqueRendererTypes.Clear();
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement