- Home /
Strange mesh texture created by code
I'm learning how to mesh by code in Unity, but the texture is weird as in the image, does anyone know why this is? I created the UV Map as it is in the code, searched Google and Unity documentation a lot, and saw that I should use the array vertices to map the UV, but I think I did something wrong
My code:
public Vector3[] verts = {
new Vector3(-0,5, 0,5, 0,0), new Vector3(0,5, 0,5, 0,0),
new Vector3(0,5, -0,5, 0,0), new Vector3(-0,5, -0,5, 0,0),
new Vector3(-0,2, 0,3, 0,0), new Vector3(0,2, 0,3, 0,0),
new Vector3(0,2, -0,1, 0,0), new Vector3(-0,2, -0,1, 0,0),
new Vector3(-0,2, 0,3, 0,0), new Vector3(0,2, 0,3, 0,0),
new Vector3(0,2, -0,1, 0,0), new Vector3(-0,2, -0,1, 0,0),
new Vector3(-0,2, 0,3, 0,1), new Vector3(0,2, 0,3, 0,1),
new Vector3(0,2, -0,1, 0,1), new Vector3(-0,2, -0,1, 0,1),
new Vector3(-0,2, 0,3, 0,1), new Vector3(0,2, 0,3, 0,1),
new Vector3(0,2, -0,1, 0,1), new Vector3(-0,2, -0,1, 0,1),
new Vector3(-0,5, 0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
new Vector3(0,5, 0,5, 0,1), new Vector3(0,5, -0,5, 0,1)
};
public int[] tris = {
0,1,4,1,5,4,1,2,5,2,6,5,2,3,6,3,7,6,3,0,7,0,4,7,8,9,12,9,13,12,9,10,13,10,14,13,10,11,14,11,15,14,11,8,15,8,12,15,20,0,3,20,3,21,0,20,46,0,46,1,1,46,47,1,47,2,3,2,42,2,47,45,46,20,17,20,16,17,20,19,16,20,32,15,31,14,15,31,47,14,47,46,14,46,17,14
};
MeshFilter mF = gameObject.GetComponent<MeshFilter> ();
Mesh msh = new Mesh ();
msh.vertices = verts;
msh.triangles = tris;
Vector2[] uvs = new Vector2[verts.Length];
for (int i = 0; i < uvs.Length; i++){ //uv map
uvs[i] = new Vector2(verts[i].x, verts[i].y);
}
msh.uv = uvs;
msh.RecalculateNormals ();
mF.mesh = msh;
Answer by DiegoSLTS · Dec 25, 2019 at 04:01 PM
I just tested your code and after fixing some compiler errors it works OK with all the shaders I tried. Maybe you have a problem somewhere else, are you using a custom shader?
It only looks weird in the sides since you'te always using x and y for all vertices.
Apart from that, you have a problem in your vertices so your code is not compiling. Things like "new Vector3(-0,5, 0,5, 0,0)" won't compile, you need to write "new Vector3(-0.5f, 0.5f, 0.0f)". Note the "." instead of "," for decimals and the "f" at the end. Vector3 receives float so you must use the "f", otherwise they're doubles and C# won't cast that automatically.
Also, the second half of your vertices are (-0,5, -0,5, 0,1), is that right?
Oh, and one more note, usually you want UV coordinates to be in the range [(0,0),(1,1)], and you're using x and y directly which go from -0.5 and 0.5. You can use those or even greater than 1, but note that it won't work if you set the texture's "Wrap mode" to clamp.
Your answer
![](https://koobas.hobune.stream/wayback/20220612220510im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Creating a mesh programatically for a tile-based 2D game 1 Answer
Normalizing mesh UVS? 1 Answer
Blender model GI/Bake illumination lightmaps completely wrong. 1 Answer
How do I use the output of Unwrapping.GeneratePerTriangleUV? 4 Answers
Making Different vertices,triangle,uv maps in the same mesh 1 Answer