- Home /
,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[i];
var originalPos = OriginalCubePositions[i];
cubePos.y = originalPos.y + Mathf.Sin((Time.time * Mathf.PI + cubePos.x) + Mathf.Cos(Time.time * Mathf.PI + cubePos.z) / CycleTime) * Strength;
CubePositions[i] = 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);
}
}
}
Answer by Dobbydoo · Aug 02, 2020 at 04:10 PM
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.
why use compute shaders to then take the result back on the cpu to give it back to the gpu? What stops you from just having a shader that combines the computation and the drawing part?
I could probably do that as well yes. I’m gonna try to make a shader implementation as well after I’m done with the ECS version of flocking boids, but I don’t have any shader experience, so I don’t really know. This was just meant as a performance comparison between doing the same operation on the cpu and gpu.
Your answer
Follow this Question
Related Questions
Drawcalls or Batched, which is more important to performace? 1 Answer
Is there any way I can statically batch procedurally generated objects? 0 Answers
Adding ONE shadow caster and ONE shadow receiver to my scene DOUBLES my draw calls. What? 0 Answers
Why can't I batch some draw calls? 0 Answers
Dynamic Batching and Instantiating 2 Answers