,Drawing 100 000+ cubes

I’ve started a project to try to learn compute shaders. I decided to do a very basic test where I have a field of cubes moving in waves.

So I started with making this normally. However, I’ve found that my biggest performance hit is in the rendering, which makes it difficult to compare the speed of a compute shader.

I’m trying to draw 100 000 cubes (default Unity cube). I have enabled Gpu instancing on the material. I’m actually batching them manually to not clutter the scene with GameObjects.
I’m only getting around 20FPS using this with only the draw logic (right now it’s not even calculating matrices).

Does anyone know anything I can do to speed this up more? I’m going to simulate boids later, so constructing an optimized cube mesh won’t really help me here.

Here is my code:

public class OscillatorPool : MonoBehaviour {
    public float CycleTime = 1f;
    public float Strength = 1f;

    public GameObject SpawnObject;
    MeshFilter MeshFilter;
    Renderer Renderer;

    List<Vector3> CubePositions;
    List<Vector3> OriginalCubePositions;
    List<Matrix4x4> CubeMatrices;

    public int Amount = 100;

    void Start()
    {
        MeshFilter = SpawnObject.GetComponent<MeshFilter>();
        Renderer = SpawnObject.GetComponent<Renderer>();

        CubeMatrices = new List<Matrix4x4>();
        CubePositions = new List<Vector3>();
        OriginalCubePositions = new List<Vector3>();

        var srqtAmount = (int)Mathf.Sqrt(Amount);

        for (int x = 0; x < srqtAmount; x++)
        {
            for (int z = 0; z < srqtAmount; z++)
            {
                var posX = x * SpawnObject.transform.localScale.x;
                var posZ = z * SpawnObject.transform.localScale.z;

                var cubePos = new Vector3(posX, transform.position.y, posZ);
                CubePositions.Add(cubePos);
                OriginalCubePositions.Add(cubePos);
                CubeMatrices.Add(Matrix4x4.TRS(
                                cubePos,
                                Renderer.transform.rotation,
                                SpawnObject.transform.localScale));
            }
        }
    }

    private void Update()
    {
        /*for (int i = 0; i < CubePositions.Count; i++)
        {
            var cubePos = CubePositions*;*

var originalPos = OriginalCubePositions*;*

cubePos.y = originalPos.y + Mathf.Sin((Time.time * Mathf.PI + cubePos.x) + Mathf.Cos(Time.time * Mathf.PI + cubePos.z) / CycleTime) * Strength;

CubePositions = cubePos;
}*/

Draw();
}

private void Draw()
{
int drawAmount = 1023;

for (int i = 0; i < CubePositions.Count; i += drawAmount)
{
var batch = CubeMatrices
.Skip(i)
.Take(drawAmount)
.ToArray();

Graphics.DrawMeshInstanced(MeshFilter.sharedMesh,
0,
Renderer.sharedMaterial,
batch,
drawAmount <= batch.Length ? drawAmount : batch.Length);
}
}
}

I solved this by using Unity ECS. It seems Unity with gameobjects is just too slow for this task, and according to the profiler a lot of the performance issues were on the cpu, so I tried converting everything to use the ECS system. This gave me an incredible performance boost, and I was able to draw around 1 million cubes at 60fps.