Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected override void OnUpdate()
- {
- // We want to iterate over all unique MeshInstanceRenderer shared component data,
- // that are attached to any entities in the world
- EntityManager.GetAllUniqueSharedComponentDatas(m_CacheduniqueRendererTypes);
- for (int i = 0; i != m_CacheduniqueRendererTypes.Count; i++)
- {
- // For each unique MeshInstanceRenderer data, we want to get all entities with a TransformMatrix
- // SharedComponentData gurantees that all those entities are packed togehter in a chunk with linear memory layout.
- // As a result the copy of the matrices out is internally done via memcpy.
- var renderer = m_CacheduniqueRendererTypes[i];
- m_InstanceRendererGroup.SetFilter(renderer);
- var transforms = m_InstanceRendererGroup.GetComponentDataArray<TransformMatrix>();
- // Graphics.DrawMeshInstanced has a set of limitations that are not optimal for working with ECS.
- // Specifically:
- // * No way to push the matrices from a job
- // * no NativeArray API, currently uses Matrix4x4[]
- // As a result this code is not yet jobified.
- // We are planning to adjust this API to make it more efficient for this use case.
- // 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.
- // So we just have a for loop here, representing each Graphics.DrawMeshInstanced batch
- int beginIndex = 0;
- while (beginIndex < transforms.Length)
- {
- int length = math.min(m_MatricesArray.Length, transforms.Length - beginIndex);
- CopyMatrices(transforms, beginIndex, length, m_MatricesArray);
- Graphics.DrawMeshInstanced(renderer.mesh, 0, renderer.material, m_MatricesArray, length, null, renderer.castShadows, renderer.receiveShadows);
- beginIndex += length;
- }
- }
- m_CacheduniqueRendererTypes.Clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement