- Home /
Multiple materials for multiple submeshes
Hi,
I have been using the fact that MeshRenderer's materials is an array for two things :
1) Render a mesh with multiple submeshes, each with its own material 2) Render a mesh with one submesh several times, each time with a different material (for example, adding a rimlight effect to the model regardless of what the main shader is)
However, I don't know how to combine the two. I have a model with two submeshes, and I want to render one of them twice - once with each material. That means a total of three materials. I tried putting the three materials in different orders, but could not get this effect.
Is this possible?
If you put 3 materials on a mesh with sub$$anonymous$$esh.Count = 1, mesh (or submesh) will be rendered three times, and if you put 3 materials on a mesh with sub$$anonymous$$esh.Count = 2, the third material will be ignored, right?
Answer by Max Kaufmann · Dec 16, 2010 at 07:24 PM
Occam's Razor: Split the submeshes into multiple meshes.
Aside: I don't know how it works in Unity, but in the last game engine we used "submesh" was used to describe draw calls that were not interleaved with material/state switches, which required them to all have the same material.
Answer by Jessy · Dec 17, 2010 at 06:33 PM
Unity does not currently have the capability to do what you want, without a little bit of extra hassle and stored data. You need to rework the mesh so that it has three submeshes. If your modeling tool doesn't permit assigning the same triangle set to two materials, you can make an Editor script to copy the triangle indeces from one submesh to a new one.
If you put 3 materials on a mesh with sub$$anonymous$$esh.Count = 1, mesh (or submesh) will be rendered three times, and if you put 3 materials on a mesh with sub$$anonymous$$esh.Count = 2, the third material will be ignored, right?
Answer by Whatever560 · Jan 17, 2020 at 06:31 PM
@tomekkie2 Actually it is clarified in unity documentation https://docs.unity3d.com/Manual/class-MeshRenderer.html#Materials
"If a Mesh contains more Materials than sub-Meshes, Unity renders the last sub-Mesh with each of the remaining Materials, one on top of the next. This allows you to set up multi-pass rendering on that sub-Mesh. However, this can impact the performance at run time. Fully opaque Materials overwrite the previous layers, which causes a decrease in performance with no advantage."
I'll have to retest myself as it seems that the behaviour is a bit random in the case of multiple submeshes.