UV problem on procedural mesh generation

Hi,

I am creating a procedural mesh based on a curve and

123

and this introduce a problem of uv getting zig-zagged when the size of the mesh shrink throughout the curve from one to the other end point.

unnamed

Here is the code for uv number lay outting.

Vertices.Add(R);
Vertices.Add(L);
UVs.Add(new Vector2(0f, (float)id / count));
UVs.Add(new Vector2(1f, (float)id / count));
start = Vertices.Count - 4;
         Triangles.Add(start + 0);
                Triangles.Add(start + 2);
                Triangles.Add(start + 1);
                Triangles.Add(start + 1);
                Triangles.Add(start + 2);
                Triangles.Add(start + 3);

mesh.vertices = Vertices.ToArray();
        //mesh.normals = normales;
        mesh.uv = UVs.ToArray();
        mesh.triangles = Triangles.ToArray();

Let me know if anybody had a similar problem and resolved it!

Thanks in advance.
Jae

The UV coordinates are linearly interpolated across the triangle. All modern hardware usually implements perspective correct mapping which should take the depth of the actual vertex position into account. This of course only works if the actual positions actually have the same distance and just look smaller due to perspective. If you actually let the geometry to shrink at one end it’s not possible for the hardware to calculate the proper corrected coordinates. See this image from wikipedia

The affine mapping does not have or does not know the depth of the two further away points, So the interpolation happens per triangle in screen space. Since the lower left triangle has a longer physical edge the texture appears larger and is interpolated linearly. The upper right triangle has a smaller edge.

If you want to provide geometry that is somehow distorted you may need to provide uv coordinates with 4 components (“strq” or “stpq”) instead of two components (“uv” or “st”). The 4 component UV coordinate basically allows you to specify a scale factor for each UV direction for each vertex. This allows you to specify a shrinking factor which is taken into account during interpolation. Keep in mind that all vertex attributes are linearly interpolated across the triangle during rasterization.

See this SO post for an example or this one. Unity now supports 4 component tex-coords