- Home /
Mesh texture not properly rendered
Hello, I am creating random shapes by code and trying to texture them.. I ended up with this :
You can see there that my shape is divided into several triangles forming a hexagon..
It is created by taking a mesh from a cube, its uv, normals and so on, and then I substitute the vertices, triangles ,etc
I did that (taking it from a cube) because otherwise nothing was shown on the surface...but still something is not quite right, as you can see there are some triangles in my mesh that do not have a texture...
Can anyone help me find out why these triangles in particular do not get textured?
Thanks!
This is my source code :
public class Tetrahedron : MonoBehaviour { List vertices = new List(); List triangles = new List(); List uv = new List(); List normals = new List(); List colors = new List();
public bool sharedVertices = false;
public void Build(Center c){
GameObject cube = GameObject.Find("Cube");
Mesh mesh = cube.GetComponent<MeshFilter>().mesh;
MeshFilter meshFilter = GetComponent<MeshFilter>();
var mesh2 = meshFilter.sharedMesh;
meshFilter.sharedMesh = mesh;
MeshRenderer renderer = (MeshRenderer) meshFilter.renderer;
//var sha = Shader.Find ("Diffuse");
//Material mat = new Material( sha);
var mat = Resources.Load("materials/Terrain",typeof(Material)) as Material;
renderer.material = mat;
mesh.colors = colors.ToArray();
foreach (Triangle t in c.Polygon.tris) {
t.vertices.Select(b => b.Position).ToList().ForEach(x => vertices.Add(x));
}
mesh.triangles = new int[]{};
mesh.vertices = new Vector3[]{};
mesh.vertices = vertices.ToArray();
//mat.color = c.Biome.Color;
vertices.ForEach(b => colors.Add(c.Biome.Color));
System.Random rand = new System.Random();
triangles = new List<int>();
int i = vertices.Count -1;
foreach( var v in vertices)
{
triangles.Add(i);
i--;
}
mesh.triangles = triangles.ToArray();
uv = new List<Vector2>();
vertices.ForEach(b => uv.Add( new Vector2(0f,0f)));
// basically just assigns a corner of the texture to each vertex
mesh.uv = uv.ToArray();//new Vector2[vertices.Count];// uv.ToArray();
vertices.ForEach(b => normals.Add( c.Normal));
mesh.normals = normals.ToArray();
//mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Your answer
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
uv sub meshes in cube c# 0 Answers
Distribute terrain in zones 3 Answers
Procedural Uniform UVs On a Plane 2 Answers
How do I texture a mesh runtime? 2 Answers