Search Unity

ProBuilder [Probuilder API]Modify a mesh example

Discussion in 'World Building' started by SgtLame, Nov 27, 2019.

  1. SgtLame

    SgtLame

    Joined:
    Nov 26, 2015
    Posts:
    68
    Hi everyone,

    I would like to use the Probuilder API to build meshes through some Unity editor scripts.
    I was able to reproduce the "Modify a mesh" example from https://docs.unity3d.com/Packages/com.unity.probuilder@4.0/manual/api.html, but with two problems:
    First, the line:
    Code (CSharp):
    1. vertices[i] += Vector3.one;
    generates a "Operator '+=' cannot be applied to operands of type 'Vertex' and 'Vector3'" error, which looks logical. What I tried instead is:
    Code (CSharp):
    1. vertices[i].position += Vector3.one;
    and it looks like it's working, but here's the 2nd problem:
    Clipboard02.jpg

    It looks like faces are moved, but not the object. Does it have something to do with the fact that the Rebuild() method doesn't exist, despite what's written in the example?

    My next question is related to the "Subdivide objet" function in the Probuiler editor window: How do you call it from code?

    Last question: Through code, how can one slightly move (I'm talking about a few inches in a mesh with meters long edges) the vertices of a mesh (keeping the mesh "gathered" of course, meaning vertices with the same position remain at an identical position).
     
  2. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    180
  3. SgtLame

    SgtLame

    Joined:
    Nov 26, 2015
    Posts:
    68
    Thanks a lot, I'll try that! :)
     
  4. SgtLame

    SgtLame

    Joined:
    Nov 26, 2015
    Posts:
    68
    Okay, so a couple more questions:
    Replicating the way the "create a mesh" API example works, the script I wrote creates a mesh that is basically a serie of vertical faces:
    upload_2019-11-29_18-2-56.png

    But as you can see, the texture on the face on the right is stretched. I also noticed that if I use the "triangulate" function, everything is back in order:

    upload_2019-11-29_18-6-13.png

    What is the problem? And how can I solve it (using the triangulate function or not) through script?

    Here is the code:
    Code (CSharp):
    1.  
    2. List<Vector3> vertices = new List<Vector3> ();
    3. List<Face> faces = new List<Face>();
    4.  
    5. // tempMarkers is an array of vector3 coordinates, representing a "base line" from which the faces will be created
    6. for (int i = 0; i < tempMarkers.Count; i++)
    7. {
    8.     vertices.Add(tempMarkers[i]);
    9.     vertices.Add(new Vector3(tempMarkers[i].x, tempMarkers[i].y + heightParameter, tempMarkers[i].z));
    10. }
    11.  
    12. for (int i = 0; i < points.Count-3; i = i+2)
    13. {
    14.     faces.Add(new Face(new int[] { i, i+2, i+1, i+2, i+3, i+1 }));
    15. }
    16.  
    17. pb = ProBuilderMesh.Create(vertices, faces);
    18. pb.Refresh();
    19. EditorMeshUtility.Optimize(pb, true);
    20.  
     
  5. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    180
    It looks to me like the faces you're creating are not square quads. If you open the ProBuilder Editor window you can visualize what the those quads look like.
     
  6. SgtLame

    SgtLame

    Joined:
    Nov 26, 2015
    Posts:
    68
    I am not sure I get it, but here is a screen capture:

    upload_2019-11-30_12-53-24.png

    What do you think?
     

    Attached Files:

  7. SgtLame

    SgtLame

    Joined:
    Nov 26, 2015
    Posts:
    68
    Not sure this is what you meant, but I added this piece of code in order to check if faces are quads:

    Code (CSharp):
    1. foreach(Face f in pb.faces)
    2. {
    3.     Debug.Log(f.IsQuad());
    4. }
    Result is always True.