- Home /
Creating Mesh from meshes with different materials and UVs / Создание Mesh'а из разных мешей с разными UV и материалами
English: I disassemble the old game, and there are text files of models that I now want to load into the game. The format of the models is quite simple:
(Material1) (shader name, material index, texture index, additional parameters)
(Material2) ...
(Vertex1) (simply Vector3)
(Vertex2) ...
(Polygon1) (material index, Vector3 vertices (to describe the polygon), Vector2 [3] UV coordinates for this material and texture)
(Polygon2)
(Polygon3) ...
So that's it. Each polygon is built on these vertices, BUT uses its material and UV relative to this material (by "material index").
How can this be implemented in Unity? Break everything down into subMeshes? But how then for each submesh to define different materials + textures?
Accordingly, everything is done only through scripts in C #.
Русский: Суть такова: Разбираю старую игрушку, и там есть текстовые файлы моделек, которые я сейчас хочу загружать в игру. Формат моделек довольно прост:
(материал1)(название шейдера, индекс материала, индекс текстуры, дополнительные параметры)
(материал2) ...
(вершина1)(просто Vector3)
(вершина2) ...
(полигон1) (индекс материала, Vector3 вершин(для описания полигона),Vector2[3] UV координат для этого материала и текстуры)
(полигон2)
(полигон3) ...
Так вот. Каждый полигон строится на этих вершинах, НО использует свой материал и UV относительно этого материала. Как такое возможно реализовать в Unity? Разбивать всё на subMesh'ы? Но как тогда для каждого субмеша определить разные материалы+текстуры? Соответственно всё делается только через скрипты на C#.
Can you, please, post the question in english? That way is going to be much easier to help you
Answer by Bunny83 · Jul 06, 2017 at 10:23 AM
Yes, the solution is called "submeshes". A single Mesh can be split into several submeshes. Each submesh will have it's own material. The submeshes share the same vertices but have seperate index lists. Each element in the "materials" array of the MeshRenderer corresponds to the same submesh index. So your MeshRenderer needs as many materials as there are submeshes.
So you would collect all "polygons" / triangles and sort them into seperate lists based on the material used. Set the subMeshCount to the number of materials used. Finally you can use SetTriangles to pass in the different submesh- triangle lists
So, if I have, for example, 3 materials (with 3 different textures), I need:
$$anonymous$$eshFilter.mesh.sub$$anonymous$$eshCount = 3;
$$anonymous$$eshRenderer.materialsCount = 3;
Insert 3 materials with different textures into $$anonymous$$eshRenderer.materials;
After this, i need to insert vertices into mesh.vertices and polygons, in order of materials, into mesh.poligons. Next, use mesh.SetTriangles for each submesh (3 in this example). Am I right?
BUT, what to do with UVs ??? How they must be stored for each sub$$anonymous$$esh?
Trouble with UVs. Unity can't have UVs more than vertices. But I need this to achive goal :( Any ideas to have UV for each triangle, not vertex?
It's actually a bit difficult to understand the text format the way you described it. However if the UV coordinates are defined per polygon you have to duplicate the vertices when they use different UVs. UV coordinate are simply "vertex features". So if two triangles use the same vertex but different UVs, you have to create two seperate vertices with the same position but different UV coordinates.
Also note that Unity does not support polygons. Ins$$anonymous$$d of SetTriangles you can use $$anonymous$$esh.SetIndices which however only support those topologies: Triangles, Quads, Lines, LineStrip, Points. Unless you know that all the primitives are Quads you have to use triangles. If your text format specifies actual polygons (with 4 or more points) you have to manually split the polygon into triangles. This splitting process isn't that trivial. A common solution is the Triangulator script.
It would help of you could post any reference of hint what format you actually talk about. It seems to be similar to Wavefront OBJ. What old game is it?
$$anonymous$$eep in $$anonymous$$d that any content from other games is generally copyright protected. You are free to do whatever you want on your PC but keep in $$anonymous$$d that in almost all cases you are not allowed to redistribute any asset from another game without having a license.
Your answer
![](https://koobas.hobune.stream/wayback/20220612125636im_/https://answers.unity.com/themes/thub/images/avi.jpg)