• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by seiti_. · May 16, 2014 at 11:08 PM · gameobjectmeshtilemapsubmesh

How do you use submeshes correctly?

Hi! I want to generate a tile map in unity and came up with two implementations:

  1. using a gameobject for each tile

  2. 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),    
     };
Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · May 16, 2014 at 11:12 PM 0
Share

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.

avatar image seiti_. · May 16, 2014 at 11:41 PM 0
Share

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.

avatar image robertbu · May 17, 2014 at 12:36 AM 1
Share

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).

avatar image seiti_. · May 17, 2014 at 08:23 AM 0
Share

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.

avatar image Cherno · May 17, 2014 at 01:03 PM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HEATH3N · Oct 27, 2020 at 06:14 AM 0
Share

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?

avatar image Orami · Oct 27, 2020 at 06:28 AM 0
Share

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.

avatar image
0

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Orami · Oct 24, 2020 at 06:11 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObjects vs. Submeshes 2 Answers

Import mesh with submeshes from OBJ? 2 Answers

Turning off renderer in other game object (C#) 2 Answers

How to realize Unity Editor Scene Filter View Effect in my game. 0 Answers

Submesh creation from script 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges