- Home /
Blured texture of custom mesh
Hi all!
When I create a mesh with the MeshFilter, The triangles look fine, but the texture is blured, doesn't really look like it should. I inspected the created mesh, the correct material is assigned. I tried just to use the original prefab's texture, and even assign a new texture to the MeshRenderer (from code and from Editor in runtime as well), but the results are the same.
One weird thing is, when I debug the code and inspect the mesh object, its uv array is empty, even after I assigned a valid array to it.
public void drawMesh(Vector3[] vertices
, int[] indices
, Vector2[] uvs
, Texture2D tex
, GameObject obj)
{
Mesh mesh = new Mesh();
obj.GetComponent<MeshFilter>().mesh = mesh;
mesh.Clear();
obj.GetComponent<MeshRenderer>().material.mainTexture=tex;
mesh.vertices = vertices;
mesh.triangles = indices;
mesh.uv=uvs;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
}
These are the indices and UVs:
int[] indices = new int[]{
0, 1, 2
, 2, 1, 3};
Vector2 uv0 = new Vector2(0,0);
Vector2 uv1 = new Vector2(1,0);
Vector2 uv2 = new Vector2(0,1);
Vector2 uv3 = new Vector2(1,1);
Vector2[] uvs = new Vector2[]{
uv2, uv3, uv0
, uv0, uv3, uv1
};
A picture about this:
http://kepfeltoltes.hu/120507/asphalt_incorrect_texture_www.kepfeltoltes.hu_.png
Any help appreciated!
You should assign the triangles after the uvs. First setup the vertex data, then the triangles. However without a screenshot or the actual vertex / uv data we can just guess what's wrong.
Thanks! Unfortunately, putting the trianlges after the uvs didn't solve the problem. I added a link to a picture.
An empty uv array would probably cause exactly what you're seeing. If likely guesses all the UV coords are (0,0), which duplicates the lower-left pixel on everything (I usually try a texture with many colors to see if that's the case.)
I'm guessing the UV array is bad somehow (wrong length?...) and ins$$anonymous$$d of an error, it just gets ignored.
I think you are right about the uvs being ignored, because after assigning my values, the uv of the mesh is still an empty array. I updated the question with the uvs. Actually, I don't know what's the problem with them. Thanks!
Answer by Owen-Reynolds · May 08, 2012 at 01:13 AM
It looks like you have 4 verts (you assign tris using verts 0-3,) but have 6 uv coordinates. Should only have 4 (one per vert.)
Yep, i guess that's the problem. Texture coords are per vertex, not per triangle index. If you need splitted uvs or normals (which you don't need in this case) you would have to duplicate the shared vertices.
The vertex data consists of those arrays: vertices, normals, tangents, uv, uv2, colors and boneWeights. The element count of all those arrays (as long as they are used) have to match the element count of the vertices array which have to be assigned first.
The triangles array should be assigned last. It just builds up the actual faces / triangles out of the vertices.
If you want to change any of the above properties for a single face, you need to duplicate the whole vertex.
For example the default box mesh usually have 8 vertices (one at each corner). But since you need a different vertex normal for each face to get correct lighting they have to use 24 vertices (4 vertices for each face)
Hey! Thanks a lot! In the meamtime I figured it out that it is the problem, but thank you though!
Your answer
![](https://koobas.hobune.stream/wayback/20220613071212im_/https://answers.unity.com/themes/thub/images/avi.jpg)