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;
     }
}

Hi!
Why do you want to convert the 3D vertex position to a 2D UV coordinate?
I’d suggest settings the UV values explicitly as you did with vertex positions.

Hi aleksandrk. I can do it explicitly for this example (the cube), but I need to do more complex shapes as well and for that setting it explicitly will not be feasible. For the complex shapes the vertex positions are calculated procedurally depending on values of variables.

Automatic UV generation is not an easy topic. On complex shapes, a lot of questions will rise : where to place the UV seams to minimize the deformation ? How to pack the UV islands ? Once this is resolved, you then may need to add vertices to create those UV seams …
I don’t say it’s not possible, but going in detail on how to do this on an arbitrary shape is a bit out of my knowledge. Maybe an option could be to simply don’t generate UVs, and use a tri-planar mapping shader to apply textures on your object ?

1 Like

Hi Remy_Unity. I shall read up on tri-planar shaders.

There is a small example at this page of the doc : Unity - Manual: Custom shader fundamentals
And a user write this tutorial with surface shader code : Triplanar Mapping – Martin Palko

Thank you Remy_Unity