- Home /
How do you use submeshes correctly?
Hi! I want to generate a tile map in unity and came up with two implementations:
using a gameobject for each tile
using MeshFilter, MeshRenderer: Create a submeshes for each tile
I expected that the version using a mesh would be faster than the one using gameobjects, but this was not the case - so I am wondering if I did it correctly (I was reading a tutorial where a big texture was generated by setting pixels and applied to the whole mesh - the author mentioned that using individual vertices for each tile would be more efficient and that it would be easy to apply different textures to each tile - I thought he meant submeshes):
I am generating my tiles using two loops:
Vector3[] vertices = new Vector3[6*hexagonCount];
Vector2[] uv = new Vector2[6*hexagonCount];
List<List<int>> triangles = new List<List<int>>(hexagonCount);
for(int y=0; y<tileNumberY; ++y){
for(int x=0; x<tileNumberX; ++x){
generateHexMesh(ref vertices, ref uv, ref triangles, index, posX, posY, posZ);
}
}
These loops fill my vertices, uv and triangles list/arrays. After that I do the following:
Material[] materials = new Material[hexagonCount];
this.mFilter.mesh.vertices = vertices; //add our vertices to the mesh
this.mFilter.mesh.uv = uv; //add out UV coordinates to the mesh
this.mFilter.renderer.materials = materials;
for(int i=0; i<hexagonCount; ++i){
int[] temp = triangles[i].ToArray();
this.mFilter.mesh.SetTriangles(temp, i);
this.mRenderer.materials[i] = material;
}
this.mFilter.mesh.RecalculateBounds(); //recalcualte the dimensions of our mesh
this.mFilter.mesh.RecalculateNormals();
My code generates the desired Hexagons, but the framerate drops to 5 FPS when running it for 100x100 tiles, while the gameobject version runs at 300 FPS. Did I do something wrong or are submeshed not meant to be used in such ways? How are submeshes used correctly to be able to assign different textures to those tiles?
EDIT: Updated code according to robertbu's suggestions
this.mFilter.mesh.vertices = vertices; //add our vertices to the mesh
this.mFilter.mesh.uv = uv; //add out UV coordinates to the mesh
for(int i=0; i<hexagonCount; ++i){
this.mFilter.mesh.SetTriangles(triangles[i].ToArray(), i);
}
this.mRenderer.material = material;
this.mFilter.mesh.RecalculateBounds(); //recalcualte the dimensions of our mesh
this.mFilter.mesh.RecalculateNormals();
UVS used for each hexagon:
// generate uv
Vector2[] uv = new Vector2[]{
new Vector2(0,0.25f),
new Vector2(0,0.75f),
new Vector2(0.5f,1),
new Vector2(1,0.75f),
new Vector2(1,0.25f),
new Vector2(0.5f,0),
};
Do you need a new material for each hexagon? You can change the texture of individual hexigons just by changing the uvs for that hex. You can change the color with vertex colors and a shader that supports vertex colors. If you don't need an individual material for each hex, then you will see a dramatic speed improvement.
Yes I would like to use one texture, but where/how would I have to set this texture, so that it gets applied to all submeshes?
edit: so that this texture is displayed repeatedly: I thought by having the same uvs for each hexagon would be sufficient, but then only the first hexagon gets displayed.
Don't create a materials array. Don't assign anything to Renderer.materials. Assign a single material to Renderer.material (no 's' on the end).
thank you for your answers. Well I did what you wrote, but it does not seem to work. I added the uvs, wich are used for each hexagon, to my question.
As a result of your suggestions only one Hexagon gets rendered. I updated my question with the source code for you.
I would suggest having only as many materials, and thus submeshes, as you really need. that means that all tiles that have the same material can share a submesh so you put all the triangles from those tiles together for one submesh, and so on.
Answer by Eric5h5 · May 17, 2014 at 03:42 PM
The point of using submeshes is basically convenience, for example an animated character that contains several materials (skin, clothes, hair, eyes). The actual performance of submeshes is more or less the same as separate objects, so there's generally no point using submeshes for tiles since you're adding complexity for no gain. The two approaches to a tile system that make sense are 1) create meshes (typically without submeshes) which use a texture atlas and manipulate UVs to display the correct texture on each tile, or 2) create a pooling system where the tiles are separate objects, but only the visible tiles are actually created so as not to overwhelm Unity with umpteen thousand objects.
And if you want to use a shader instead of texture? Would you then need to create submeshes so that you could assign multiple materials?
Oddly enough I had houses that have 400 or so objects that they are built from if I render individual pieces the editor can not display the house to the quest2 but if I sub mesh combine all the materials into separate sub meshes I can render the houses with no problems. So you could do this and use shaders on some parts and not on others in theory.
Answer by a436t4ataf · Dec 12, 2019 at 09:43 AM
As of 2019/2020 ... submeshes are still not fully implemented within Unity.
For instance, mesh.RecalculateBounds() was never updated to support them, so submeshes get broken lighting, reflection, etc (they are treated as if they are the size + shape of the total of all meshes).
Simply splitting your submeshes to separate objects instantly fixes a bunch of lighting problems.
TL;DR: as @Eric5h5 put it "basically convenience", and in practice you should avoid them as much as possible. Too many partially-implemented parts (and given how many years it's gone untouched, this isn't going to be fixed).
Isn't this how we would write a mesh combiner though? I have lots of parts for building buildings in my game they are generic, but I want to combine them into one mesh for each material so there are less draw calls. This way there are only a few draw calls per building instead of upwards of 100 if it is reasonably sized.
Answer by Blenderik · Feb 24 at 09:50 AM
for(int i=0; i<hexagonCount; ++i){
this.mFilter.mesh.SetTriangles(triangles[i].ToArray(), i);
}
This should be the culprit. You are creating hexagonCount instances of your mesh. Not a good idea. Also remove the .ToArray(), it makes it much slower. Use this:
Mesh mesh = new Mesh(); // fill it with your verts and normals
for(int i=0; i<hexagonCount; ++i){
mesh.SetTriangles(triangles[i], i);
}
this.mFilter.sharedMesh = mesh;
This should speed it up a lot, but I would still consider this bad practice. If you want to use several materials, you should group your submeshes and unite those that need the same material, however the good practice would be to create a texture atlas, adjust the UVs of each hexagon so it takes its information from the correct position of the atlas. This way you can render all your hexes with 1 drawcall and use only 1 submesh. Also don't use .material, it creates a new material everytime you do that. use .sharedMaterials instead
Your answer
Follow this Question
Related Questions
GameObjects vs. Submeshes 2 Answers
Dynamic mesh positioning with Raycast intersection... 0 Answers
How can I save a gameobject's mesh? 1 Answer
Mesh slicing or shattering 1 Answer
CombineInstance[] submesh index 1 Answer