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

RecalculateNormals, adding new vertices

Discussion in 'Scripting' started by game4tress, Jun 5, 2015.

  1. game4tress

    game4tress

    Joined:
    May 13, 2010
    Posts:
    46
    I'm adding new vertices to a plane, in order to deform the plane, but when I add the vertices the plane stays the same, it's not deformed. Does anyone ever bumped into this situation? How can I solve it?
    My thanks in advanced.

    I've uploaded the PrtScr, that shows the news vertices being created but without deforming the plane.
    vertices.png

    This is my code to add vertices

    Code (csharp):
    1.  
    2. public void AddSingleVertice()
    3.     {
    4.         mainObject=GameObject.Find("Plane");
    5.        
    6.         Mesh mesh = new Mesh ();
    7.        
    8.         mesh.Clear ();
    9.        
    10.         mesh = mainObject.GetComponent<MeshFilter> ().mesh;
    11.  
    12.         Script_Global.p_vertices.Clear();      
    13.  
    14.         Vector3[] vertices=mesh.vertices ;
    15.         Vector3[] vertices2=new Vector3[vertices.Length+2];
    16.         Vector3 V3;
    17.  
    18.         float val=-2.111f;
    19.         float vx=-0.222f;
    20.  
    21.         vertices.CopyTo(vertices2,0);
    22.        
    23.         vertices2[vertices.Length]=new Vector3(vertices[0].x+vx,vertices[0].y+1,vertices[0].z);
    24.         vertices2[vertices.Length+1]=new Vector3(vertices[0].x+1,vertices[0].y+2,vertices[0].z);
    25.        
    26.  
    27.         Debug.Log("Vertice1: " + vertices.Length.ToString());
    28.         Debug.Log("Vertice2: " + vertices2.Length.ToString());
    29.  
    30.         foreach (var item in vertices2) {
    31.             Debug.Log("Vertice X value: " + item.x.ToString() + " Vertice Y value: " + item.y.ToString() + " Vertice Z value: " + item.z.ToString());
    32.            
    33.             V3=new Vector3(item.x,item.y,item.z);          
    34.            
    35.            
    36.             Script_Global.p_vertices.Add(V3);
    37.         }
    38.  
    39.         mesh.vertices=vertices2;
    40.         mesh.RecalculateNormals();
    41.     }
    42.  
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Vertices themselves are just points. You need to connect them and create triangles or quads.

    Detail: You don't have to create a new mesh when you get the one from a mesh filter anyway.
     
    game4tress likes this.
  3. game4tress

    game4tress

    Joined:
    May 13, 2010
    Posts:
    46
    Is there any automatic way to do that? a function that receives the values and connects them?
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    No, at least not in Unity. When you add vertices in Unity, you have to create the corresponding triangles along with it. I am also not aware of something in the asset store that would help you with that.
     
    game4tress likes this.
  5. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
    You have to write a script that connects all them points/vertices and form triangles. Like Dantus said, 'Vertices themselves are just points.'
    You should create your own gameobject with a custom mesh instead of using the plane, that way you should have more control over the ammount and position of vertices that you want, but brace yourself, it may get complicated
     
    game4tress likes this.