Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Procedural UV problem?

Discussion in 'General Graphics' started by Kobus, Apr 24, 2018.

  1. Kobus

    Kobus

    Joined:
    Mar 27, 2015
    Posts:
    48
    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:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. [RequireComponent(typeof(MeshFilter)), RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshCollider))]
    4. public class CubeMesh_Script : MonoBehaviour {
    5.      [SerializeField]
    6.      private Material myMaterial = null;
    7.      private MeshRenderer meshRenderer = null;
    8.      private MeshFilter meshFilter = null;
    9.      private Mesh mesh = null;
    10.      private MeshCollider meshCollider = null;
    11.        
    12.      // Use this for initialization
    13.      void Start () {
    14.          InitMesh();
    15.          CreateMesh();
    16.      }
    17.        
    18.      private void InitMesh()
    19.      {
    20.          meshRenderer = gameObject.GetComponent<MeshRenderer>();
    21.          meshFilter = gameObject.GetComponent<MeshFilter>();
    22.          mesh = meshFilter.mesh;
    23.          mesh.Clear();
    24.          meshCollider = gameObject.GetComponent<MeshCollider>();
    25.      }
    26.    
    27.      private void CreateMesh()
    28.      {
    29.          List<Vector3> vertsList = DoVertices();
    30.          mesh.SetVertices(vertsList);
    31.          mesh.SetTriangles(DoTriangles(vertsList), 0);
    32.          mesh.SetUVs(0, DoUVs(vertsList));
    33.          meshRenderer.material = myMaterial;
    34.      }
    35.    
    36.      private List<Vector3> DoVertices()
    37.      {
    38.          List<Vector3> vertsList = new List<Vector3>();
    39.          // side 1
    40.          vertsList.Add(new Vector3(-1f, 0f, -1f));
    41.          vertsList.Add(new Vector3(1f, 0f, -1f));
    42.          vertsList.Add(new Vector3(-1f, 2f, -1f));
    43.          vertsList.Add(new Vector3(1f, 2f, -1f));
    44.          // side 2
    45.          vertsList.Add(new Vector3(1f, 0f, -1f));
    46.          vertsList.Add(new Vector3(1f, 0f, 1f));
    47.          vertsList.Add(new Vector3(1f, 2f, -1f));
    48.          vertsList.Add(new Vector3(1f, 2f, 1f));
    49.          // side 3
    50.          vertsList.Add(new Vector3(1f, 0f, 1f));
    51.          vertsList.Add(new Vector3(-1f, 0f, 1f));
    52.          vertsList.Add(new Vector3(1f, 2f, 1f));
    53.          vertsList.Add(new Vector3(-1f, 2f, 1f));
    54.          // side 4
    55.          vertsList.Add(new Vector3(-1f, 0f, 1f));
    56.          vertsList.Add(new Vector3(-1f, 0f, -1f));
    57.          vertsList.Add(new Vector3(-1f, 2f, 1f));
    58.          vertsList.Add(new Vector3(-1f, 2f, -1f));
    59.          // top
    60.          vertsList.Add(new Vector3(-1f, 2f, -1f));
    61.          vertsList.Add(new Vector3(1f, 2f, -1f));
    62.          vertsList.Add(new Vector3(-1f, 2f, 1f));
    63.          vertsList.Add(new Vector3(1f, 2f, 1f));
    64.          // bottom
    65.          vertsList.Add(new Vector3(-1f, 0f, 1f));
    66.          vertsList.Add(new Vector3(1f, 0f, 1f));
    67.          vertsList.Add(new Vector3(-1f, 0f, -1f));
    68.          vertsList.Add(new Vector3(1f, 0f, -1f));
    69.          return vertsList;
    70.      }
    71.    
    72.      private List<int> DoTriangles(List<Vector3> vertsListP)
    73.      {
    74.          List<int> trisList = new List<int>();
    75.          for (int index = 0; index < vertsListP.Count; index += 4)
    76.          {
    77.              trisList.Add(index);
    78.              trisList.Add(index + 2);
    79.              trisList.Add(index + 3);
    80.              trisList.Add(index);
    81.              trisList.Add(index + 3);
    82.              trisList.Add(index + 1);
    83.          }
    84.          return trisList;
    85.      }
    86.    
    87.      private List<Vector2> DoUVs(List<Vector3> vertsListP)
    88.      {
    89.          List<Vector2> uvsList = new List<Vector2>();
    90.          for (int index = 0; index < vertsListP.Count; index++)
    91.          {
    92.              float uvX = vertsListP[index].x;
    93.              float uvY = vertsListP[index].y;
    94.              uvsList.Add(new Vector2(uvX, uvY));
    95.          }
    96.          return uvsList;
    97.      }
    98. }
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,854
    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.
     
  3. Kobus

    Kobus

    Joined:
    Mar 27, 2015
    Posts:
    48
    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.
     
  4. Remy_Unity

    Remy_Unity

    Unity Technologies

    Joined:
    Oct 3, 2017
    Posts:
    639
    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 ?
     
    bgolus likes this.
  5. Kobus

    Kobus

    Joined:
    Mar 27, 2015
    Posts:
    48
    Hi Remy_Unity. I shall read up on tri-planar shaders.
     
  6. Remy_Unity

    Remy_Unity

    Unity Technologies

    Joined:
    Oct 3, 2017
    Posts:
    639
  7. Kobus

    Kobus

    Joined:
    Mar 27, 2015
    Posts:
    48