- Home /
Procedural UV problem?
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;
}
}
Answer by Harinezumi · Apr 24, 2018 at 11:04 AM
UV coordinates should fall in the range [0, 1] (inclusive). As some of your vertices have negative and greater than 1 coordinates, DoUVs()
will also assign negative UVs.
Fortunately, the way you set up the vertices of the cube makes it really easy to set up your UVs (that is, this isn't general, but will work for your current setup):
private List<Vector2> DoUVs(List<Vector3> vertsListP) {
List<Vector2> uvsList = new List<Vector2>();
for (int index = 0; index < vertsListP.Count; index++) {
int uvIndex = index % 4;
switch (uvIndex) {
case 0: { uvsList.Add(new Vector2(0, 0)); break; }
case 1: { uvsList.Add(new Vector2(1, 0)); break; }
case 2: { uvsList.Add(new Vector2(0, 1)); break; }
case 3: { uvsList.Add(new Vector2(1, 1)); break; }
}
}
return uvsList;
}
Your answer
Follow this Question
Related Questions
How to get uvCordinat of raycast hit without mesh collider 1 Answer
UV V coordinate Not showing on Texture. 1 Answer
Texture on custom mesh splits at edge. 1 Answer
need guide in uv texture 0 Answers
Vertex Colors not smooth enough? 0 Answers