Mesh triangles - Y U SO SLOW?! ()

Hey guys!
I need to store 2048 meshes into an array of Meshes so that they can be later changed,but just by the vertices.They are all pretty much 64x64 verts planes,so all 2048 of them have the same array for the triangles but different vertices.So,instead of creating a new mesh and assigning it the triangles,i thought to assign it only once in the start function,but it is VERY slow and freezes my game for like 10 seconds…Is there a way of doing this faster,or i have to deal with it?

Where’s a simplified version of my code :

var Meshez : Mesh[] = new Mesh[2048];
var Tr : int[] = new int[23814];
var Vs : Vector3[] = new Vector3[4096];

function Start(){

print("Starting : Second " + System.DateTime.Now.Second + " Milisecond " + System.DateTime.Now.Millisecond);

for(var x : int;x < 2048;x++){
var G : Mesh = new Mesh();
G.vertices = Vs;//This seems to be waaay faster than triangles...Why?!
G.triangles = Tr; //Argh!
Meshez[x] = G;
}

print("Finish! : Second " + System.DateTime.Now.Second + " Milisecond " + System.DateTime.Now.Millisecond);


}

I did tried do break it down into 2 for loops and add yields between them,but still,it would either take my player like 1 minute to start his game,or his game will run at 3 fps for like 10 seconds…

Please help me! :frowning:
Thanks!

So, when these meshes are displayed, are they smoothed? Or are they jagged?

Heey BigMisterB!For now they are not even rendered.They are just stored in that Array,so they don’t eat up any VRAM or anything.Of course,at the very end i will want them smoothed,but of course,not all of them will be rendered at the same time.Probably max 512 at a time.But does it really matter?

If it’s so slow, don’t store meshes, store arrays of vertices and one array of triangles, and create meshes on the fly when you really need them.

But it is gonna be verrry slow,since i would need to modify like 64 meshes / frame.I tested it,and works fine,but only if changing verts.If i have to change triangles and generate a new array too,it will simply kill it :[

Assigning to Triangles or Vertices is a copy operation. This is because it has to be sent to the GPU, and the property itself flags changes to be sent. This means you can’t assign once, and change the original because A) It’s not a direct reference and B) Even if it was, Unity would require some sort of flag to send the new model data to the GPU.
So TR is an array of integers sized 23814. You copy this array 2048 times. Integers are 4 bytes. 23814 * 2048 * 4 = 195084288, or ~186 megs. That is a lot of triangle data to be sending to the GPU/Copying on the CPU. Vertices is likely faster, because it’s 10x less data.

What on earth are you doing that requires that amount of meshes?

Note that loading is going to be the least of your issues if you really plan to have 512 meshes being rendered on screen, because that is 512 draw calls…

Added: If you need to change these per frame as well, you need to come up with a new strategy. You are not going to get away with changing 186 megs of triangle indices every frame.

You’ll need to come up with a different design for the effect you want, as the current strategy isn’t going to run on anything.

Can you explain the effect you want? Maybe someone here could give an alternative that’s not so resource intensive.

I am working on a highly detailed terain generator.And yes,i gave 512 as a max.But powerful computers would render 1024 meshes of 4096 verts with no probs.

@Ntero,well my problem is that is being generated in 10 secs,not in 1…And technically it shouldn’t be 10 times less data,coz they are Vector3 arrays and each vector3 contains 3 floats.And in Java it says that a float takes 4 bytes aswell,so that would be only twice as less data…And still,they are being assigned almost in no-time

Added for NTero : But i mentioned i don’t have to change the triangles,only the verts…And i tested it,and works fine.It does work,but i just don’t want the players to take the lag spike at the begining when asigning the tris.

Your right, doing the math the Verts are about 96megs of data.

It’s very likely it won’t transfer the vertices to the GPU is the triangle count is 0. Hence when you set triangles you have enough data to continue and it all gets sent.

The important part is that both of these are massive amounts of data. You are sending a very large amount of data onto the GPU in one swoop, and it’s going to stall. There are some tricks you can use, but the ones I know of are more low level than Unity allows, and I get the suspicion you can reduce the amount of data much easier than access those operations.

Edit:Java has no relation to Unity’s Javascript. And while it is true that in .NET/Mono a float is 4 bytes, the Java references have no language specific information relevant to Unity.

Aw,cool!And which would those tricks be? :smile:

Well,maybe,but i am sure it doesn’t take less than 4 bytes…Maybe more,but i don’t think less than an integer

Terrains often heavily rely on Level of Detail effects to help minimize the amount of vertices/indices at any given time.

This is due to the very same issue you are hitting, and why Terrains used to be dis-allowed in mobile. When done using a standard implementation, without any Level of Detail management, they are bloated and ineffecient.

I do use LODs based on quadtrees…Well,i guess if i don’t want that lag spike i might need to either take down the caching amount or let it cache them over time…Thanks for your input!