Tris and UVS mapping for mesh generation.

hi, I’ve been generating meshes via Mesh class. Regular planes are simple, since there geometer is a big loop with a single cut. but for more complex planes such as this im unsure how one would map the triangles or UVs.

reading this doc(Unity - Manual: Using the Mesh Class) tells me that the vertices follow a “clock-wise” rule, which is easy to implement on a 4 vert plane. but a 4 vert with an inset? there
can anyone explain how one would work out which vert is which? assuming the lower left most is 0?

here is the mesh im trying to generate:
10892-d4.png

trial and error seems to be getting me nowhere as hopelessly deformed meshes pop up in front of my screen. why is this overlooked in the docs, it has regular one 4 vert plane and nothing else…All I know is that the triangles need to be a 30 sized array, with number ranging from 0-7.

the vertices themselves are there, after trial an error of seen the deformed geometry wap itself.

I don’t understand how the UV’s work either, with regular planes the vector values are 1 or 0. are they based on ratios between vertices? I don’t even know where to start with these guys.

if it helps here is a snippet of plane-gen code

 mesh.vertices = new Vector3[] {
            Manager.GUI.ScreenToWorldPoint(new Vector3(0f,0f,0f)),
             Manager.GUI.ScreenToWorldPoint(new Vector3(padding,padding,0f)),
            Manager.GUI.ScreenToWorldPoint(new Vector3(width,padding,0f)),
             Manager.GUI.ScreenToWorldPoint(new Vector3(width + padding,0f,0f)),
            Manager.GUI.ScreenToWorldPoint(new Vector3(width, height, 0f)),
            Manager.GUI.ScreenToWorldPoint(new Vector3(width + padding, height + padding, 0f)),
            Manager.GUI.ScreenToWorldPoint(new Vector3 (padding, height,0f)),
            Manager.GUI.ScreenToWorldPoint(new Vector3 (0f, height + padding,0f)),

        };

        mesh.triangles = new int[] { 
         0, 5, 6, 
         0, 6, 2, 
         0, 4, 1, 
         0 ,4, 3,
         0 ,3, 4,
         7, 5, 2, 
         3, 4, 5,
         5, 3, 2,
         6, 0, 2,
         3, 4, 1
};
        mesh.uv = new Vector2[] {
            new Vector2(0,0), new Vector2(1,0),
            new Vector2(1,1), new Vector2(0,1),
            new Vector2(0,0), new Vector2(1,0),
            new Vector2(1,1), new Vector2(1,1)


        };


        mesh.RecalculateNormals();

I just punched random numbers for the triangles and UVs as you can tell.
thanks in advance

(Your padding isn’t actual padding; it should be multiplied by two when finding the far right/bottom sides because there are two pads + widht/height to get to those; that may be throwing you off)

This is all standard 3D stuff, not unity-specific; that’s why it’s not detailed in the manual (I assume)

I see this pattern in your vertices:

7  5
 64
 12 
0  3

You need to build all the triangles, and they all need to be wound the same way - CW triangles would be:

7,5,6,
6,5,4,
4,5,3,
2,4,3,
0,2,3,
0,1,2,
0,7,6,
0,6,1,

1,6,4,
1,4,2

UV is a ratio of what part of the image should show at the vertex. If you want the whole image to cover that set of triangles, then uv for the four corners is:

0,0,
.
.
1,0,
.
1,1,
.
0,1

The UV for the inner points is just some maths; point 6 would be (p/(p2+w), (p+w)/(p2+h))