Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Custom ProBuilder tooling] how do I update faces to match a moved edge?

Discussion in 'World Building' started by megan_l_fox, Mar 1, 2019.

  1. megan_l_fox

    megan_l_fox

    Joined:
    Jul 10, 2010
    Posts:
    35
    What this does is take a pair of edges, connect them, and shift the resultant edge inward opposite the edge normal by a set amount. Think of it as a tool for caving a face inward by a set amount. It ALMOST works, but...

    Code (CSharp):
    1. public override ActionResult DoAction()
    2. {
    3.     float archDepth = EditorPrefs.GetFloat("pb_Arch_depth", .1f);
    4.     int archSubDivs = EditorPrefs.GetInt("pb_Arch_subDivs", 1);
    5.  
    6.     foreach (ProBuilderMesh mesh in MeshSelection.top)
    7.     {
    8.         Undo.RecordObject(mesh, "Arched mesh");
    9.  
    10.         SimpleTuple<Face[], Edge[]> connectResults = mesh.Connect(mesh.selectedEdges);
    11.         Vertex[] vertices = mesh.GetVertices();
    12.         foreach(Edge createdEdge in connectResults.item2)
    13.         {
    14.             Vertex edgeStart = vertices[createdEdge.a];
    15.             Vertex edgeEnd = vertices[createdEdge.b];
    16.             edgeStart.position -= edgeStart.normal * archDepth;
    17.             edgeEnd.position -= edgeEnd.normal * archDepth;
    18.  
    19.             System.Collections.Generic.List<int> vertIndex = new System.Collections.Generic.List<int>();
    20.             vertIndex.Add(createdEdge.a);
    21.             System.Collections.Generic.List<int> vertSet = mesh.GetCoincidentVertices(vertIndex);
    22.  
    23.             foreach (int index in vertSet)
    24.             {
    25.                 vertices[index].position = edgeStart.position;
    26.             }
    27.  
    28.             vertIndex.Clear();
    29.             vertIndex.Add(createdEdge.b);
    30.             vertSet = mesh.GetCoincidentVertices(vertIndex);
    31.  
    32.             foreach (int index in vertSet)
    33.             {
    34.                 vertices[index].position = edgeEnd.position;
    35.             }
    36.         }
    37.                
    38.         mesh.SetVertices(vertices);
    39.                
    40.         mesh.Refresh();
    41.         mesh.RefreshUV(mesh.faces);
    42.         //mesh.Optimize();
    43.         PrefabUtility.RecordPrefabInstancePropertyModifications(mesh);
    44.  
    45.         break;
    46.     }
    47.  
    48.     // Refresh the Editor wireframe and working caches.
    49.     ProBuilderEditor.Refresh();
    50.  
    51.     return new ActionResult(ActionResult.Status.Success, "Arched Edges");
    52. }
    ... it doesn't update the connected faces, causing the following:


    If I then manually move that created edge even a tiny bit, it fixes immediately:


    I just need to know what function ProBuilder is calling to force an update of faces, after an edge moves. I'm guessing there's one that that calls?
     
  2. megan_l_fox

    megan_l_fox

    Joined:
    Jul 10, 2010
    Posts:
    35
    AH HAH! Found it myself.

    The answer is: ToMesh(). I needed to call mesh.ToMesh() before the Refresh(). THERE we go.
     
    kaarrrllll likes this.