Merging Submeshes with multiple materials

Hey,

I am having trouble figuring out a way to do this. The issue is i have a shader that outlines the current looked at object, but it doesn’t like multiple submeshes as it only applies to the first mesh. I can merge the meshes together but then it looses reference to the materials. Is there a way to merge the submeshes but also keep the reference to their materials individual materials, or do I have to look into altering the shader to affect each submesh?

Also this has to be done at runtime.

Any help would be greatly appertained :slight_smile:

Cheers

The way Unity’s renderers are set up, submeshes map 1-to-1 to separate materials.

Since a material is simply an instance of a shader with (possibly) some settings, why don’t you just put enough materials using your shader into the materials slots (remember they correspond to submeshes) of your renderer? You can put the same material into multiple slots.

NOW… if instead you are talking about meshes that are on other child GameObjects besides the current one, then those are not submeshes but rather just “other meshes on other (child) objects.” In that case, put a material using your shader on those objects too.

It is 1 renderer with multiple sub meshes. For example, I have 1 renderer with 9 different materials in the material list. When i merge the sub meshes, I get 1 mesh but only the first material is applied to the new mesh. See attached image.

3916006--333862--Renderer.jpg

Yes, that is precisely what I mean by this statement:

“The way Unity’s renderers are set up, submeshes map 1-to-1 to separate materials.”

Any given submesh maps to precisely one material, regardless of how many you put in the .materials slots.

I get what you’re saying (I hope :(). The problem is with my outline custom script / shader. What it does is creates 2 different materials for the object, one for masking and another for colour (so there will be 11 materials in total). If i leave the submeshes as they are (9 of them) the colors are all correct on the ball, but the outline is only applied to the first submesh. If i merge the submeshes into one, the color of the ball is incorrect, but the outline is correct.

Also if i find a way to apply the outline materials to each sub meshes, the problem is instead of outline the outside of the ball, it’ll outline the individual sub meshes instead of just the outside of the ball.

How about this workaround: you have 9 different submeshes and materials in your source ball. Leave those alone.

Make a fresh mesh that is all of those 9 meshes combined, and put that brand-new mesh on a brand-new GameObject with MeshFilter and MeshRenderer.

Now turn your little outliner script loose on that object and when you’re all done you’ll have three submeshes and three materials on the NEW object:

  1. the glob of all meshes (which looks all wrong material-wise)
  2. your masking mesh
  3. your colour mesh

Reach into that first glob Submesh and set its triangle count to zero (ie., an empty array of int[ ]) and it will disappear.

And… finally, parent both this new object and the original ball to a common shared object, or else parent the new object to the first ball, either way.

I have thought about this solution, but i am worried about performance. The amount of meshes and their complexity in a scene is unknown, and is being run on android phones / tablet (S8 / Tab S3 minimum). But obviously the scenes need to meet a performance standard to run smoothly.

I don’t really have the knowledge to say how much it’ll effect performance on the devices, but my initial reaction was to try and avoid the duplicating if possible.

I was wondering if anyone had a solution to this problem.

I cant add anything to the material question itself, but if it is only different textures you need on one mesh, then using a texture atlas can help. A texture atlas would be one big texture containing all textures. You can then access each texture with the right coordinate offset in the texture atlas. This is how voxel based games often manage to have a single mesh per chunk, but still use different textures. You’d still only use one material in unity tho, which means you couldnt have different values for metallic or smoothnes if that’s needed.

Tyvm this helps :slight_smile:

I am curious to know how did you resolve it?
Did adding material to submeshes work?

My impression is that sketchup merges collada files with 2000 submeshes into single groups which are fast to render in Unit3d. So, perhaps try export to SKP, then use a plugin RBZ from sketchucation like “explode recursively” that’s the best one, see also mesh optimizer for skp. then use the SKP as the basis for the game mesh… i.e. save a sketchup file, into unity3d directly, then you have an integrated editor for the mesh within unity. it’s ok here.