Is there a limit to the number of instanced meshes?

I am trying to spawn 65536 cubes using a UInstancedStaticMeshComponent, but the editor crashes the moment I try to spawn a simple 256x256 grid of cubes.

It works fine with 64x64, so I know the code works, and I assume there is a limit to the number of instances?

I get no warning, no errors. It just crashes.

for (size_t PixelY = 0; PixelY < TextureHeight; PixelY++)
	{
		for (size_t PixelX = 0; PixelX < TextureWidth; PixelX++)
		{
			if (PixelX >= 0 && PixelX < TextureWidth && PixelY >= 0 && PixelY < TextureHeight)
			{
			
				FTransform CurrentTrans = FTransform(FRotator(0, 0, 0), FVector(XOffset, YOffset, (PixelColor.R / 256.0f)*100.0f), FVector(1, 1, 1));

				InstancedStaticComponent->AddInstance(CurrentTrans);

			}
		}
	}

Above you can see the code that adds the instances.

It crashes at line 1223 of Vector.h, aka the second line in the code under here:

FORCEINLINE FVector::FVector(float InX, float InY, float InZ)
	: X(InX), Y(InY), Z(InZ)
{
	DiagnosticCheckNaN();
}

Making the floats smaller did not help. Is the engine unable to handle that many vectors and that is why it crashes?
The exception states: UE4Editor.exe: 0xC0000005: Access violation reading location 0x0000000000000002.

Thank you for your help.

After a lot of testing and digging I found out that I could send more than 100 000 transforms and instance them.

The problem came from the casting of the pointer to the raw data when the texture was larger than 256x256. To fix it I had to turn of sRGB and MipMapping, and set the Compression Method to VectorDisplacementMap. Some textures only needed the MiMapping to be disabled.

I still have no idea what the max limit on instances is, but 100 000 was enough for my use.

HTH somebody

1 Like