Hi I am trying out procedural mesh generation. I am managing the vertices and triangles, but am struggling with the UVs. I started by just creating a procedural cube. I thought this is a good shape to start with because the different sides all have differing values for x, y, z. In my code I am looping through all the vertices to create the UV list, but at the moment I am only using the x- and y-coordinates of the vertices to generate the UVs. However the UVs only work correctly for some of the sides (the sides in the x,y plane). I am trying to come up with an algorithm that can generate the UV coordinate correctly for whichever vertex. Can someone point me in the right direction to help me convert the Vector3 vertex coordinate to the Vector2 UV coordinate?
My code currently is as follows:
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter)), RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshCollider))]
public class CubeMesh_Script : MonoBehaviour {
[SerializeField]
private Material myMaterial = null;
private MeshRenderer meshRenderer = null;
private MeshFilter meshFilter = null;
private Mesh mesh = null;
private MeshCollider meshCollider = null;
// Use this for initialization
void Start () {
InitMesh();
CreateMesh();
}
private void InitMesh()
{
meshRenderer = gameObject.GetComponent<MeshRenderer>();
meshFilter = gameObject.GetComponent<MeshFilter>();
mesh = meshFilter.mesh;
mesh.Clear();
meshCollider = gameObject.GetComponent<MeshCollider>();
}
private void CreateMesh()
{
List<Vector3> vertsList = DoVertices();
mesh.SetVertices(vertsList);
mesh.SetTriangles(DoTriangles(vertsList), 0);
mesh.SetUVs(0, DoUVs(vertsList));
meshRenderer.material = myMaterial;
}
private List<Vector3> DoVertices()
{
List<Vector3> vertsList = new List<Vector3>();
// side 1
vertsList.Add(new Vector3(-1f, 0f, -1f));
vertsList.Add(new Vector3(1f, 0f, -1f));
vertsList.Add(new Vector3(-1f, 2f, -1f));
vertsList.Add(new Vector3(1f, 2f, -1f));
// side 2
vertsList.Add(new Vector3(1f, 0f, -1f));
vertsList.Add(new Vector3(1f, 0f, 1f));
vertsList.Add(new Vector3(1f, 2f, -1f));
vertsList.Add(new Vector3(1f, 2f, 1f));
// side 3
vertsList.Add(new Vector3(1f, 0f, 1f));
vertsList.Add(new Vector3(-1f, 0f, 1f));
vertsList.Add(new Vector3(1f, 2f, 1f));
vertsList.Add(new Vector3(-1f, 2f, 1f));
// side 4
vertsList.Add(new Vector3(-1f, 0f, 1f));
vertsList.Add(new Vector3(-1f, 0f, -1f));
vertsList.Add(new Vector3(-1f, 2f, 1f));
vertsList.Add(new Vector3(-1f, 2f, -1f));
// top
vertsList.Add(new Vector3(-1f, 2f, -1f));
vertsList.Add(new Vector3(1f, 2f, -1f));
vertsList.Add(new Vector3(-1f, 2f, 1f));
vertsList.Add(new Vector3(1f, 2f, 1f));
// bottom
vertsList.Add(new Vector3(-1f, 0f, 1f));
vertsList.Add(new Vector3(1f, 0f, 1f));
vertsList.Add(new Vector3(-1f, 0f, -1f));
vertsList.Add(new Vector3(1f, 0f, -1f));
return vertsList;
}
private List<int> DoTriangles(List<Vector3> vertsListP)
{
List<int> trisList = new List<int>();
for (int index = 0; index < vertsListP.Count; index += 4)
{
trisList.Add(index);
trisList.Add(index + 2);
trisList.Add(index + 3);
trisList.Add(index);
trisList.Add(index + 3);
trisList.Add(index + 1);
}
return trisList;
}
private List<Vector2> DoUVs(List<Vector3> vertsListP)
{
List<Vector2> uvsList = new List<Vector2>();
for (int index = 0; index < vertsListP.Count; index++)
{
float uvX = vertsListP[index].x;
float uvY = vertsListP[index].y;
uvsList.Add(new Vector2(uvX, uvY));
}
return uvsList;
}
}