Multiple materials for multiple submeshes


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?

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.

Actually it is clarified in unity documentation

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

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.