How do uv work when several material are used on the the same mesh

I’m trying to make a texture atlas.
So the atlas is fine and I have a new material which uses it.
But I can’t figure how I can change the UV texture coords when I have several materials used on the same mesh.

Example:
I have Mat1 and Mat2 assigned to Mesh1.
Mat1 uses texture1 and Mat2 uses texture2.
Then I create Mat3 which uses texture3 which is is a combined texture of texture1 and texture2.
So now I want to update my mesh’s UV before assigning the new material but how can I do as I don’t know which uv are for texture1 or texture2.
Maybe there is something I haven’t understood…

Thanks for any help.

You can have several (4 IIRC) UV’s per vertex. Your material just need to use a shader which utilizes the correct one. But I don’t really understand what you try to do. Is texture 3 created during runtime by script? You can also blend it directly in the shader. A shader can have several input textures and use a different UV for each. The question i how you get the UV’s in the model in the first place. Maybe you should elaborate a bit more on what you try to achieve (example).

You have to edit your mesh. If you open it in a modelling programm then you should have submeshes for each of the two materials. Usually materials will be assigned and you can see which submesh belogns to which material. Otherwise just try it in unity and swap the atlas if it doesn’t fit.
You have to look for a modelling programm and look into UV editing tutorials if this is new to you.

Hi, thanks for your answer. I know that I can edit the object with Blender or another 3D modeler but that is not what I’m trying to do as I want to make a editor tool designed to optimize draw calls by reducing draw state changes.
The idea is to merge complex models having lot of useless textures and meshes with the same material properties into an optimized single mesh having a single material and a single texture.

At this step I have succeeded :

  • making a single merged mesh with all original materials (UV coords unchanged)
  • making a texture from all other textures (with associated coordinates factors and offsets using MaxRectBinPack algorithm)
  • combined material and combined texture creation in the assets directory

But, I’m unable to remap my combined material/texture to my combined mesh as I don’t understand which uv coords goes with which material/texture (it only works if I have a single material by mesh in the original model).

So I would like to understand how I can know which UV coords goes with which material.

For example if I take this small example, how can I remap
ORIGINAL:
MeshFilter (UV and vertices)
MeshRenderer
Material 1 (Shader standard) → Texture 1 (256x256)
Material 2 (Shader standard) → Texture 2 (256x256)

If Texture 3 is 512x512, it can contain Texture1 and Texture2. Texture2 has a x offset of 256 pixels (0.5f)

EXPECTED:
MeshFilter (updated UVs)
MeshRender
Material 3 (Shader standard) → Texture 1 + Texture 2

Alright now. I think GetTriangles() can help you.
If you call GetTriangles(0) you should get all indices for material 1 which you can use to get the uvs.

Thanks for hint. I’m going to check that.

Hi It works with GetIndices().
Thanks for help.