Search Unity

Unity mesh uv setup

Discussion in 'Scripting' started by MikeyJY, Jun 22, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I created an triangle mesh with 3 vertices and 1 triangle with 3 indices. But when I apply an material the MainTexture isn't showing on the triangle. I think it is about the uvs. That's how I generate the mesh:

    Code (CSharp):
    1. void Start()
    2.     {
    3.         mesh = new Mesh();
    4.         mesh = GetComponent<MeshFilter>().mesh;
    5.         //lastxSize = xSize;
    6.         //lastm = m;
    7.         CreateMesh();
    8.         RenderMesh();
    9.         //AssetDatabase.CreateAsset(mesh, "Assets/Cloth.asset");
    10.         //AssetDatabase.SaveAssets();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     private void Update()
    15.     {
    16.        
    17.  
    18.        
    19.         //RenderMesh();
    20.         //if(lastxSize != xSize)
    21.         //{
    22.             //CreateMesh();
    23.             //lastm = removeX;
    24.             //lastxSize = xSize;
    25.         //}
    26.         if (save == true)
    27.         {
    28.             RenderMesh();
    29.             save = false;
    30.             AssetDatabase.CreateAsset(mesh, "Assets/sails.asset");
    31.             AssetDatabase.SaveAssets();
    32.            
    33.         }
    34.  
    35.     }
    36.    
    37.  
    38.     void CreateMesh()
    39.     {
    40.  
    41.         //vertices = new Vector3[(xSize + 1) * (zSize + 1)];
    42.         vertices = new Vector3[3];
    43.         for (int i = 0, z = 0; z <= zSize; z++)
    44.         {
    45.  
    46.  
    47.             for (int x = 0; x <= xSize; x++)
    48.             {
    49.                 if (i < 3)
    50.                 {
    51.                     vertices[i] = new Vector3(x, 0, z);
    52.                     i++;
    53.                 }
    54.  
    55.  
    56.  
    57.  
    58.             }
    59.         }
    60.         triangles = new int[xSize * zSize * 3];//*only 3 triangle indicies
    61.  
    62.         int vert = 0;
    63.         int tris = 0;
    64.         for (int z = 0; z < zSize; z++)
    65.         {
    66.             for (int x = 0; x < xSize; x++)
    67.             {
    68.  
    69.                
    70.                
    71.                     triangles[tris + 0] = vert + 0;
    72.                     triangles[tris + 1] = vert + xSize + 1;
    73.                     triangles[tris + 2] = vert + 1;
    74.                     //triangles[tris + 3] = vert + 1;
    75.                     //triangles[tris + 4] = vert + xSize + 1;
    76.                     //triangles[tris + 5] = vert + xSize + 2;
    77.  
    78.                     vert++;
    79.                     //tris += 3;
    80.                
    81.             }
    82.             vert++;
    83.         }
    84.  
    85.        
    86.     }
    87.  
    88.     void RenderMesh()
    89.     {
    90.        
    91.        
    92.      
    93.        
    94.         mesh.Clear();
    95.  
    96.      
    97.        
    98.  
    99.         mesh.vertices = vertices;
    100.  
    101.         mesh.triangles = triangles;
    102.        
    103.        
    104.         mesh.RecalculateNormals();
    105.         GetComponent<MeshCollider>().sharedMesh = mesh;
    106.        
    107.        
    108.  
    109.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I think it's something in your logic, your loops or something. This works fine for me, I get a single pair of triangles, one facing each direction:

    Code (csharp):
    1.     void Start()
    2.     {
    3.         var mesh = new Mesh();
    4.  
    5.         mesh.vertices = new Vector3[]
    6.         {
    7.             new Vector3( 0, 0, 0),
    8.             new Vector3( 0, 1, 0),
    9.             new Vector3( 1, 0, 0),
    10.         };
    11.  
    12.         mesh.uv = new Vector2[]
    13.         {
    14.             new Vector2( 0, 0),
    15.             new Vector2( 0, 1),
    16.             new Vector2( 1, 0),
    17.         };
    18.  
    19.         mesh.triangles = new int[]
    20.         {
    21.             0,1,2,
    22.             0,2,1,  // doublesided
    23.         };
    24.  
    25.         AssetDatabase.CreateAsset(mesh, "Assets/Fnord.asset");
    26.         AssetDatabase.SaveAssets();
    27.     }
    I don't even recalculate bounds or normals or anything (you probably should).
     
    Last edited: Jun 22, 2020
  3. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Yes, but I didn't work with uvs in unity especially c# only in modeling softwares like blender where all is visualized
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I'm sorry, I misunderstood your question. I thought you weren't seeing ANYTHING working... well, UVs should save (I see them in my file even though I never set them), but as for what texture is on a mesh, that has nothing to do with the mesh. That is entirely the renderer.

    Here's what goes in:

    Assets:

    Mesh
    Textures
    Material <- can have colors and texture references

    Components

    MeshFilter <- has a Mesh
    MeshRenderer <- has a Material

    EDIT: I added UV adding to the script above; works fine; put a textured material on a MeshRenderer and see.
     
  5. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Yep, the mesh generation worked for my the first time I have some troubles with applying some texturing:

    Isn't this related with uv mapping?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Actually no, pink usually means "no material" or else in some cases "no texture." Most often no material.
     
  7. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I don't mean the pink, I meant the fact that after applying the material the texture wasn't on the surface
     
  8. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    This texture should be on the triangle: upload_2020-6-23_0-38-57.png

    But it is a solid color.

    I'm not sure but I think it takes the color of 1 pixel from the texture and that's why the color is changing after changing the offset. Is there in unity an "unwrap" action like blender?
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
  10. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    It worked. And thank you for help and for procedural mesh geometry urls
     
    Kurt-Dekker likes this.